Screen Saver

Random Moving Screen Saver
My version is available at the bottom of the page!

Screen shot of Screen Saver program
The program on the right randomly draws lines on the screen; the trick to this program is to make the lines continue on the same pattern for a few jumps before choosing a new one.

To draw a line in MS Basic we use: Shapes.AddLine(X1, Y1, X2, Y2)
We are going to use the 'Shapes' function instead of the 'GraphicsWindow' one because we can hide the lines afterwards to create the effect that the lines are disappearing.

To get a random number we use: Math.GetRandomNumber(x)    
x is the maximum number to choose from, this function will get a random integer number between 0 and x.

For the program we will need 11 standard variables and 1 array. An array is a variable which can store multiple values under a single name with different indexes. In MS Basic we use a standard variable name followed by square brackets [ ] the index will be in the brackets, you can use numbers or number variables. For example: line[counter].

Now for the programming:
Start by setting the four location variables to the center of the window; I called my variables prevX1, prevX2, prevY1 and prevY2. Give them any sensible name you want. We also need to set the 'counter' variable to 0. We will use this variable to keep track of what loop number we are on so we can use it as the index in the array.
We also need a while loop, so you will also need to set a 'continue' variable to "true". 
It should look something like this:

It should look something like this:

counter = 0
prevX1 = 200
prevY1 = 250
prevX2 = 300
prevy2 = 250
Contin = "true"
While Contin = "true"
EndWhile


Next we need to make sure the counter increments on each loop. To do this simply input counter = counter + 1   inside the while loop.

We also need to get four random variables. The numbers must between -15 and 15; to do this we must get a random number between 0 and 30 and -15 from it.

So set your four random variables like this:
randomX1 = (Math.GetRandomNumber(30) - 15)
randomY1 = (Math.GetRandomNumber(30) - 15)
randomX2 = (Math.GetRandomNumber(30) - 15)
randomY2 = (Math.GetRandomNumber(30) - 15)

Now we must put a For loop inside our While loop, we can use this to repeat each pattern a few times.
Simply input:  For i = 1 To 10 
This will continue each pattern 10 times, because it counts to 10.
Next will we add the random values to the X or Y values to calculate the next position:
    prevX1 = prevX1 + randomX1
    prevY1 = prevY1 + randomY1
    prevX2 = prevX2 + randomX2
    prevy2 = prevy2 + randomY2

This will continue to move the next location the same jump 10 times before it gets new random values.
Now that we have the calculation values we need to draw the lines; we are going to store each line under the 'line' variable and use the 'counter' variable as the index to the array:
    line[counter] = Shapes.AddLine(prevX1,prevY1,prevX2,prevy2)

Simply use a slight pause to slow down the program:
    Program.Delay(125)

...And increment the counter:
    counter = counter + 1

To finish the For section we need to hide the 100th line(you can choose whatever number you like) before it, then stick an 'Endfor'
    Shapes.HideShape(line[counter-100])
    EndFor

It should look something like this:

It should look something like this:

counter = 0
prevX1 = 200
prevY1 = 250
prevX2 = 300
prevy2 = 250
Contin = "true"
While Contin = "true"
  counter = counter +1
  randomX1 = (Math.GetRandomNumber(30) - 15)
  randomY1 = (Math.GetRandomNumber(30) - 15)
  randomX2 = (Math.GetRandomNumber(30) - 15)
  randomY2 = (Math.GetRandomNumber(30) - 15)
  For i = 1 To 10
    prevX1 = prevX1 + randomX1
    prevY1 = prevY1 + randomY1
    prevX2 = prevX2 + randomX2
    prevy2 = prevy2 + randomY2
    line[counter] = Shapes.AddLine(prevX1,prevY1,prevX2,prevy2)
    Program.Delay(125)
    counter = counter + 1
    Shapes.HideShape(line[counter-100])
  EndFor
EndWhile


Now go ahead and run your program; one thing you will notice after a while is that the lines start getting drawn off the page.
To solve this we need to add some validation rules to check that the next location is on the screen.
This can be done using IF statements:
"If prevX1 > 499 or < 1 then set prevX1 to 200."
Now try this is MS Basic syntax and you should have your finished program, if you get stuck my solution is at the bottom.













Random Moving Screen Saver

  counter = 0
  prevX1 = 200
  prevY1 = 250
  prevX2 = 300
  prevy2 = 250
  Contin = "true"
  While Contin = "true"
    counter = counter +1
    randomX1 = (Math.GetRandomNumber(30) - 15)
    randomY1 = (Math.GetRandomNumber(30) - 15)
    randomX2 = (Math.GetRandomNumber(30) - 15)
    randomY2 = (Math.GetRandomNumber(30) - 15)
    For i = 1 To 10
      prevX1 = prevX1 + randomX1
      prevY1 = prevY1 + randomY1
      prevX2 = prevX2 + randomX2
      prevy2 = prevy2 + randomY2
      line[counter] = Shapes.AddLine(prevX1,prevY1,prevX2,prevy2)
      Program.Delay(125)
      counter = counter + 1
      Shapes.HideShape(line[counter-100])
    EndFor
    If prevX1 > 490 Or prevX2 > 490 Or prevY1 > 490 Or prevy2 > 490 or prevX1 < 10 Or prevX2 < 10 Or prevY1 < 10 Or prevy2 < 10 Then
      prevX1 = 200
      prevY1 = 250
      prevX2 = 300
      prevy2 = 250
    EndIf  

  EndWhile

ċ
Screen Saver.zip
(150k)
Jack Joynson,
4 Mar 2013, 12:39