/////////////////////////////////////////////////////////////////// // // graphics3.cpp // // Started in Pascal by Jeff Ondich on 1/25/95 // Last modified 5/20/98 // // 1. Compile it, run it, figure out how it works and // how to stop it. Note that there's a warning when // you compile. Don't worry about it. // // 2. Complain about the flickering. Try to get the ball to // bounce from side to side instead of up and down. Can // you get the colors to change gradually from red to // blue instead of abruptly? // /////////////////////////////////////////////////////////////////// #include #include // Constants const int radius = 40; const int windowWidth = 600; const int windowHeight = 600; const int windowBottom = 100; const int windowLeft = 100; // Prototypes void StartGraphics( void ); void NewCenter( int& horiz, int& vert, bool& goingUp ); // Global variables. float red, blue, green; int main( void ) { int x, y; bool goingUp; StartGraphics(); x = windowWidth / 2; y = radius; goingUp = true; red = 0.0; blue = 1.0; green = 0.0; while( true ) { setrgbcolor( red, green, blue ); circle( x, y, radius ); flushgraphics(); setrgbcolor( 0, 0, 0 ); circle( x, y, radius ); flushgraphics(); NewCenter( x, y, goingUp ); } return( 0 ); } void NewCenter( int& horiz, int& vert, bool& goingUp ) { if( goingUp ) vert = vert + 1; else vert = vert - 1; if( vert >= windowHeight - radius ) { goingUp = false; red = 1.0; blue = 0; } else if( vert <= radius ) { goingUp = true; red = 0; blue = 1.0; } } /////////////////////////////////////////////////////////////////// // StartGraphics initializes the graphics package // and opens a black window for drawing. /////////////////////////////////////////////////////////////////// void StartGraphics( void ) { // Call initializegraphics only once during the program, // before you do any other graphics initializegraphics(); // Open a window and flood it with a black background. createwindow( windowLeft, windowBottom, windowLeft + windowWidth, windowBottom + windowHeight ); setrgbcolor( 0.0, 0.0, 0.0 ); flood(); flushgraphics(); }