Simple Motion [2/10]
Simple Motion
The clearScr procedure paints the entire screen in the given color. It erases everything previously painted on the screen.
That's why the text of the first statement cannot be seen.
The clearScr procedure also resets the text output position back to the top left corner.
Bookmark your progress by using the grey sharing buttons. They create a link to the current page. The sharing buttons for Google Plus, Twitter, Facebook, Pinterest, Reddit and LinkedIn are provided.
The clearScr procedure resets the
text output position to the the top left corner.
As a consequence, the last
println statement of this program
outputs the text at the top left corner.
[Run the program]
The clearScr procedure also sets the global background color. Intially, the global background color is black.
The println statement uses the global background color, although that cannot be discerned in this example. For the text output, println uses the global foreground color.
Exercise:
Replace both clearScr procedure calls with fillScr procedure calls to see the difference between them.
This program draws a row of green discs,
as demonstrated in the previous chapter.
[Run the program]
The text after the symbols << is a single-line comment.
- You can track your progress by clicking the “Overview” button at the top left menu.
- Don't forget to take regular breaks to stay refreshed and focused.
This program draws one animation frame in each iteration
of the for loop.
[Run the program]
The clearScr procedure clears the screen to the global background color when called without a color argument.
The global background color is black, initialy.
Exercise:
Change the argument of the sleepMs procedure call so that it pauses for 500 milliseconds.
This program draws a moving disc leaving a trail.
[Run the program]
To remove the trail, the screen needs to be erased before drawing the disc at each new position.
Exercise:
Change the sleep time in the sleepMs call to value 500, in order to view each animation frame separately. In this example, the difference between frames is so small that this effect is barely visible.
This program draws a moving disc.
[Run the program]
The value of variable i specifies the distance from the left edge of the screen to the center of the blue disc.
In each iteration of the for loop, one animation frame is drawn to the screen.
Exercises:
Can you make the disc move faster, WITHOUT changing the sleepMs(30) statement?
Hint 1
What is the step size of the for loop?
Solution 1
for #i=1..860 +=5
Solution 2
disc(i*5, 300, 100, /blue);
Change the sleep time in the sleepMs procedure call to value 400, in order to view each animation frame separately.
This program animates two moving discs.
The blue disc moves twice as fast as the red one.
[Run the program]
The disc procedure has the form:
disc(x, y, radius, color)
Exercises:
Change the arguments 200 and 300 in disc procedure calls to some other values in the range from 0 to 700.
Set the second argument of the blue disc call to expression 2*i to make the blue disc move diagonally.
Set the third argument of the red disc call to expression i/2 to make the red disc grow in size.
Change the argument of the sleepMs call to value 500.
One way to make the disc move in the opposite direction is to make a slight modification to the first argument of the disc procedure call.
* * *
-- The clearScr Procedure --
The clearScr procedure can be called with two optional arguments. The first argument sets the global background color, and the second argument sets the global foreground color.
Exercises:
Change the global colors by adding a new statement at the start of the source code:
clearScr(/white, /magenta);
Change the argument of the sleepMs call to value 500 in order to view each animation frame separately.
This program animates a line.
[Run the program]
Exercises:
Remove the clearScr statement from the program, or comment it out behind symbols /// or <<.
[Run the program]The screen will be scrolled by the println statement. It creates an interesting effect.
Insert the statement fillScr(/black); as the first statement in the body of the loop.
[Run the program]The screen be scrolling nevertheless, but this will not be easily noticeable.
To stop the scrolling, a call to clearScr has to be used because it resets the text output position back to the top left corner of the screen. Add back the removed clearScr statement.
[Run the program]Remove the println statement from the program, and also remove all calls to fillScr and clearScr procedures.
[Run the program]
This demonstrational program draws a moving text on the screen.
The program uses named arguments to set the color and the coordinates of the displayed text. Named arguments of println statements are explained in the next chapter.
Exercise:
The animated text leaves a trail. Can you remove the trail?
Solution
for #i=300..0 -=5 { clearScr(); << insert this line only ... (the rest of code)
Another possibility is to use a call to fillScr(/black) procedure.
<< F2:Prev - - F4:Next >>