For Loops Laboratory
October 1999
In this laboratory you will produce three simple animations that we call scenes. You will start with a partially completed program but most of the top-level work is left to you. First, a black ball will roll around a square track leaving a black trace along its path. In the second scene, yellow spots will appear along the black trace. In the third scene, your code will guide a ball through a square track of randomly generated width. The balls color and the trace it leaves behind will change as it moves.
Primary
Goals
• Practice for loops.
A for statement has this general form:
for (initial-statement; condition; iteration-statement)
body-statement; // This can be many statements inside { }
Often for loops are used for repetition when there is a counter that is to be incremented or decremented each time through the loop. In that case, the loop takes the form:
for (i = start_value; i < end_value; i++)
body-statement;
In this lab you will:
Start with fixed initial-statement and condition.
Use different iteration-statements.
Make initial-statements and conditions that depend on variables.
• Learn
how to create the illusion of motion and how to use a delay to slow down the
motion.
Some Other New Things
• See how to call for random numbers in your programs.
• Learn how to produce sound using the system alert/bell.
In this laboratory you will use a new software toolkit:
#include "Random.h" // random longs and doubles
and a Visual C++ function:
Sleep(number_of_clock_ticks); // to slow down the motion
Start by running the application program forloopsSol. Your final program should look pretty much like this. Note the three scenes as described in the overview.
••• Run the program again and observe how the last scene changes.
Now do the coding:
We always start with this:
¥?¥?¥ (0) Copy the credits from one of your other programs
The center of the ball is (x, y). The variable y is set to 35, half-way between the outer track and the inner track. You are given the first loop that sends the ball across the top part of the track.
for (x = 35; x < 365; x++){
PaintCircle(x, y, radius);
Sleep(1); // waits 1/1000
second, but it seems longer
}
We create the illusion of motion by drawing the ball in different positions. If the drawing happens to slowly or too quickly, the illusion is spoiled.
••• Comment out the statement Delay(1); and see what happens.
Now write the code to move the ball around the rest of the track. Change x when it is moving horizontally, Change y when it is moving vertically.
Note that after the first loop x has the right value for the second loop.
¥?¥?¥ (1) Move the ball down the right side
¥?¥?¥ (2) Move the ball back across the bottom
¥?¥?¥ (3) Move the ball up the left side
¥?¥?¥ (4) // Make the drawing color yellow
Use the statement: SetForeColor(red, green, blue);
where 0 <= red, green, blue <= 255.
When working with light, yellow is an even mix of red and green.
// Make spots around the track
¥?¥?¥ (5) // Copy the loops from FixedTrack()
// then change the iteration-statement in each loop
// to make the centers of the balls 30 pixels apart.
¥?¥?¥ (6) // Play the System Beep sound
The statement SysBeep(1); will play whatever system beep is currently set. The number 1 is leftover from the days when the system beep was a constant note and you had to tell how many ticks to hold it for. Now, the value doesn't matter but there must be a number there.
¥?¥?¥ (7) // Set the drawing color back to black
This is where you have the most work to do on your own. The margin and radius of the ball are generated at random within reasonable bounds. You must now write initial-statements and conditions that depend on these variable values.
// The margin and the radius are set for you.
short margin = RandomLong(10, 80); // distance of from edge of outer
// square to edge of drawing window.
short radius = RandomLong(10, 30); // radius of the ball
The function RandomLong(long a, long b); is defined in Random.h. Every time it is called it returns a long (integer) between a and b.
¥?¥?¥ (8) Add the inner square to finish the track
Make sure that there are width pixels between the inner and outer tracks.
// Roll a ball around the track
¥?¥?¥ (9) // Declare x and y and set y to the middle height of the top part of the track.
Express y in terms of margin and radius.
¥?¥?¥ (10) // Write 4 for loops that will roll the ball around the track leaving a black trace. Your initial-statement and condition must depend on "margin" and "radius"
Where x starts and ends depends on margin
and radius.
¥?¥?¥ (11)// In each loop, add a statement before the PaintCircle statement that will make the drawing color depend on x and y. Remember that each of the red, green, and blue values should be between 0 and 255.
x
+ y is
a simple function that depends on x and y but it
must be scaled (multiplied or divided by a constant) to give values that are
between 0 and 255.
You
don't have to use the same color scheme as in the forloopsSol but make it change as smoothly as possible. How smoothly it can change depends on the
color available on your machine, i.e. how much video ram your machine has and
how much it is set to use.
Turn in: A diskette with your source code
(forloops.cpp), and a printout of your source code.
This work is due two
days after your scheduled lab.
Please turn it in to your lab instructor.