/////////////////////////////////////////////////////// // // bounce.cpp // // Jeff Ondich 11/5/99 // Based on Mike Tie's example g2 program. // Last modified 11/5/99 // // Try these: // // 1. Make the ball bounce side to side. // 2. Make the color of the ball change // gradually from blue to red as the // ball moves up, and back from red to // blue as it moves down. // 3. Make the ball move diagonally, // bouncing off walls, floor, and // ceiling when appropriate. // /////////////////////////////////////////////////////// #include #include #include const int windowHeight = 500; const int windowWidth = 500; const int ballRadius = 50; int main(void) { // Open a window on screen. Keep its device ID int window = g2_open_X11( windowWidth, windowHeight ); // Let's define some colors now int black_ink = g2_ink(window,0,0,0); int white_ink = g2_ink(window,1,1,1); // Get ready to draw. g2_set_background( window, white_ink ); // Bounce the ball. int y = ballRadius; bool goingUp = true; while( 1 == 1 ) { // Draw the ball g2_pen( window, black_ink ); g2_filled_circle( window, windowWidth / 2, y, ballRadius ); // Wait a little while for( int i=0; i < 1000000; i++ ) { } // Erase the ball g2_pen( window, white_ink ); g2_filled_circle( window, windowWidth / 2, y, ballRadius ); // Change the y-coordinate of the ball's center. if( goingUp ) { y++; if( y + ballRadius > windowHeight ) { y--; goingUp = false; } } else { y--; if( y - ballRadius < 0 ) { y--; goingUp = true; } } } g2_close(window); return( 0 ); }