/////////////////////////////////////////////////////////////////// // // graphics3.cpp // // Started in Pascal by Jeff Ondich on 1/25/95 // Last modified 1/19/00 // // 1. Compile it, run it, and figure out how it works. // // 2. Complain about the flickering. // // 3. To slow the motion down or speed it up, // change the value to which the delay // loop (that is, the for-loop inside the // while-loop) counts. // // 4. Try to get the ball to bounce from side to // side instead of up and down. // // 6. Can you get the colors to change gradually // from red to blue instead of abruptly? // /////////////////////////////////////////////////////////////////// #include #include #include const int windowHeight = 600; const int windowWidth = 600; const int circleRadius = 50; // Prototypes void NewCenter( int window, int& horiz, int& vert, bool& goingUp, int& ink ); int main() { ////////////////////////////////////// // Open the window. ////////////////////////////////////// int window = g2_open_X11( windowWidth, windowHeight ); int background_ink = g2_ink( window, 1, 1, 1 ); g2_set_background( window, background_ink ); cout << "Click in this window and hit Ctrl-C to stop." << endl; ////////////////////////////////////// // Do the animation. ////////////////////////////////////// int x = windowWidth / 2; int y = circleRadius; bool goingUp = true; int circle_ink = g2_ink( window, 1, 0, 0 ); // This is an intentionally infinite loop. while( true ) { // Draw the circle g2_pen( window, circle_ink ); g2_circle( window, x, y, circleRadius ); // Wait a while for( int i=0; i < 100000; i++ ) { } // Erase the circle g2_pen( window, background_ink ); g2_circle( window, x, y, circleRadius ); // Move the circle NewCenter( window, x, y, goingUp, circle_ink ); } return( 0 ); } void NewCenter( int window, int& horiz, int& vert, bool& goingUp, int& ink ) { if( goingUp ) vert = vert + 1; else vert = vert - 1; if( vert >= windowHeight - circleRadius ) { goingUp = false; ink = g2_ink( window, 0, 0, 1 ); } else if( vert <= circleRadius ) { goingUp = true; ink = g2_ink( window, 1, 0, 0 ); } }