// Copyright 1999 // College of Computer Science // Northeastern University Boston MA 02115 // This software may be used for educational purposes as long as this copyright // notice is retained at the top of all files // Should this software be modified, the words "Modified from Original" must be // included as a comment below this notice // All publication rights are retained. This software or its documentation may // not be published in any media either in whole or in part. /////////////////////////////////////////////////////////////////////////////// // Lab: Loops Lab // Goals: Produce simple animation using for loops // Prepared by Harriet Fell, 9/28/96; revised 9/20/98; // revised for Visual C++ 10/16/99 // STUDENT NAME: // STUDENT ID: // DATE: /////////////////////////////////////////////////////////////////////////////// // The standard include files that include traditional C and C++ headers and // many other Core Tools headers ... see CHeaders.h for additional details #include "IOTools.h" #include "Graphics.h" #include "Random.h" // Enter project specific include files here as well as classes and functions // that you choose to define in the main shell rather than in separate files // ¥¥¥ FUNCTION PROTOTYPES void Credits(void); // Identify yourself as the programmer void FixedTrack(void); // Draw fixed track and roll ball in it leaving trace void Spots(void); // Put yellow spots around the track void RandomTrack(void); // Draw random track and roll ball in it leaving trace int main(int argc, char* argv[]) { // Use the following line if you choose NOT to open any graphics windows // InitializeConsole(); // Build graphics window 0 GraphicsWindow GW0(400, 400); // Move the console below graphics window 0 ConsolePlaceBelow(0); // Give the console the focus for user interaction MakeConsoleForeground(); ////////// // Enter the main program here SetRandomSeed(); // this function is defined in Random.h // it makes the random numbers different in each run Credits(); FixedTrack(); PressReturn(""); Spots(); PressReturn(""); ClearDrawing(); RandomTrack(); ////////// // The lines below make sure that the graphics windows remain open just // before the program terminates PressReturn("\nThe main program is about to terminate\n"); return 0; } void Credits(void) { // identify yourself as the programmer // ¥?¥?¥ (0) Copy the credits from another program you've written // or write a new version here. } // end Credits void FixedTrack(void){ cout << "Fixed Track" << endl; const margin = 20; // distance of from edge of outer square to edge of drawing window. const radius = 13; // radius of the ball FrameRect(20, 20, 380, 380); // The outer square FrameRect(50, 50, 350, 350); // The inner square // Roll a ball around the track int x; // x is initialized in the first loop int y = 35; // y is set to the middle height of the top part of the track // Across the top for ( x = 35; x < 365; x++){ PaintCircle(x, y, radius); Sleep(1); // waits 1/1000 second, but it seems longer } // ¥?¥?¥ (1) Move the ball down the right side // ¥?¥?¥ (2) Move the ball back across the bottom // ¥?¥?¥ (3) Move the ball up the left side // Now that you've made it all the way around, // Sound the System Alert/Bell cout << '\a' << endl; } void Spots(void){ cout << "Yellow Spots" << endl; const margin = 20; // distance of from edge of outer square to edge of drawing window. const radius = 10; // radius of the ball // ¥?¥?¥ (4) // Make the drawing color yellow // 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 // ¥?¥?¥ (7) // Set the drawing color back to black } void RandomTrack(void){ // A ball will roll around a track between two squares, leaving the trace of its path. cout << "Random Track" << endl; 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 // Draw the track // left, top, right, bottom define the outer square short left = margin; short top = margin; short right = 400 - margin; short bottom = 400 - margin; short width = 2*(radius + 2); // width of track is the diameter of ball plus 4. FrameRect(left, top, bottom, right); // The outer square // ¥?¥?¥ (8) Add the inner square to finish the track // 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 // ¥?¥?¥ (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" // ¥?¥?¥ (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. }