Simple Motion [2/10]
Simple Motion
The clearScr procedure paints the entire screen in the given color.
In other words, the clearScr procedure erases everything that has been previously painted on the screen. It also resets the text output position back to the top left corner.
Bookmark your progress by using the grey sharing buttons. The sharing buttons for Google Plus, Twitter, Facebook, Pinterest, Reddit and LinkedIn are provided. The sharing buttons create a link to the current page.
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 default background color. By default, the default background color is black. The println statement uses the default background color for text output.
Exercise:
Change both clearScr procedure calls into fillScr procedure calls to observe the difference.
This program draws a row of green discs,
as it was demonstrated in the previous chapter.
[Run the program]
The text after symbols << is called a single-line comment. Comments are ignored by the computer.
- To track how far you have progressed, click on the “Overview” button at the top left menu.
- Remember to do some regular breaks from your learning journey.
This program draws one animation frame in each iteration of
the for loop.
[Run the program]
When the clearScr procedure is called without any colors as arguments, it clears the screen to the global background color. The initial global background color is black color.
This program draws a moving disc leaving a trail.
[Run the program]
To get rid of the trail, the program would need to erase the screen each time before drawing a disc at a new position.
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:
1. Can you make the disc move faster, WITHOUT changing the statement sleepMs(30) ?
Hint 1
How much is the step of the for loop?
Solution 1
for #i=1..860 +=5
Solution 2
disc(i*5, 300, 100, /blue);
2. Change the pause time in the sleepMs statement to value 400 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:
1. Try changing the arguments 200 and 300 in disc procedure calls to some other values in the range from 0 to 700.
2. Set the second argument of the blue disc procedure call to an expression 2*i to make the blue disc move diagonally.
3. Set the third argument of the red disc procedure call to an expression i/2 to make the red disc grow in size.
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.
Exercise:
Change the global colors by adding a new statement at the start of the source code:
clearScr(/white, /magenta);
This program animates a line.
[Run the program]
Exercises:
1. Remove the clearScr statement from the program
or comment it out behind symbols <<.
[Run the program]
The screen will start to scroll because of the println statement. This creates an interesting effect.
2. Add the statement
fillScr(/black);
as the first statement in the body of the loop.
[Run the program]
The screen will start to scroll again, but this will not be easily noticeable.
3. To stop the scrolling, a clearScr statement
has to be used because it resets the text output position back to the
top left corner of the screen.
Add back a clearScr statement.
[Run the program]
4. Remove the println statement from the program, and also remove all fillScr and clearScr statements. Run the program again.
This demonstrational program draws a moving text on the screen. Named arguments in 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 fillScr(/black) statement.
<< F2:Prev - - F4:Next >>