For Loops Lab

September 1997 - Fell

Overview

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

Some Other New Things

Software Tools

In this laboratory you will use a shell that contains some new software tools:
#include "Random.h"				// random longs and doubles
#include "Delay.h"				// delay & pause tools

Activities

Start by running the application program LoopsLabSolution. 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

Scene 1: Roll a ball around the track

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);
		Delay(1);	// waits one sixtieth of a second
					// this function is in Delay.h
	}
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

Scene 2: Spots

(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

Scene 3: Random Track

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.

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 LoopsLabSolution but make it change as smoothly as possible. How smooth it can change depends on the color available on your machine, i.e. how much VRAM your machine has and how much it is set to use.


Last Updated: September 28, 1997 1:17 pm by
Harriet Fell
College of Computer Science, Northeastern University
360 Huntington Avenue #161CN,
Boston, MA 02115
Internet: fell@ccs.neu.edu
Phone: (617) 373-2198 / Fax: (617) 373-5121
The URL for this document is: http://www.ccs.neu.edu/home/fell/COM1100/PROG/ForLoopsLab.html