Simple Motion [1/10]
Simple Motion
The clearScr function paints the entire screen in the given color.
In other words, the clearScr function 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 function 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.
The clearScr function 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 all the clearScr function calls into fillScr function calls to observe the difference.
This program draws a row of green discs, as it was demonstrated in the previous chapter.
The text after symbols << is called a single-line comment. Comments are ignored by the computer.
This program draws one animation frame in each iteration of the for loop.
When the clearScr function is called without specifying any colors, it clears the screen to the background color, which is black by default.
This program draws a moving disc leaving a trail.
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.
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.
After running and analyzing this program, try changing the arguments 200 and 300 in disc function calls to some other values in the range from 0 to 700.
Set the second argument of the blue disc function call to an expression 2*i to make the blue disc move diagonally.
Set the third argument of the red disc function 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 function call.
* * *
-- The clearScr Function --
The clearScr function can be called with two optional arguments. The first argument sets the default background color, and the second argument sets the default foreground color.
Exercise:
Change the default colors by adding a new statement at the start of the source code:
clearScr(/white, /magenta);
This program animates a line. Run the program
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.
Add the statement fillScr(/black); as the first statement in the body of the loop. Run the program again.
The screen will start to scroll again, but this will not be easily noticeable.
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.
Remove the println statement from the program, and also remove all fillScr and clearScr statements. Run the program again.
This program draws a moving text on the screen.
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 >>