// This version of pong bounces a ball on the screen. // We don't have a paddle yet. // There are no global variables in this version. #include #include #include #include #include #define XDIM 600 // x dimension of table #define YDIM 400 // y dimension of table #define BALL_RADIUS 20 // ball radius #define TABLE_COLOR COLOR(0,50,0) // color of table #define BALL_COLOR RED // color of ball // function prototypes void initialize_screen(); void initialize_ball(double &ball_loc_x, double &ball_loc_y, double &ball_direction, double &ball_velocity); void update_ball_loc(double &ball_loc_x, double &ball_loc_y, double &ball_direction, double ball_velocity); void update_screen(int new_ball_screen_x, int new_ball_screen_y); int random_in_range(int lower, int upper); int main () { double ball_loc_x; // ball x coordinate in screen units double ball_loc_y; // ball y coordinate in screen units double ball_direction; // ball angle in radians double ball_velocity; // ball velocity in screen units initialize_screen(); initialize_ball(ball_loc_x, ball_loc_y, ball_direction, ball_velocity); do // loop forever { update_ball_loc(ball_loc_x, ball_loc_y, ball_direction, ball_velocity); update_screen(int(ball_loc_x), int(ball_loc_y)); delay(1); // sleep for 1 millisecond } while (!kbhit()); // loop until a key is hit } /************************************ initialize_screen **********************/ void initialize_screen() { // open window initwindow(XDIM, YDIM); // draw table : filled rectangle the size of the screen setfillstyle(SOLID_FILL, TABLE_COLOR); bar(0,0,XDIM-1,YDIM-1); } /************************************ initialize_ball ************************/ void initialize_ball(double &ball_loc_x, double &ball_loc_y, double &ball_direction, double &ball_velocity) { // initialize ball location, direction, and velocity ball_loc_x = (double) random_in_range(BALL_RADIUS, XDIM - BALL_RADIUS); ball_loc_y = (double) random_in_range(BALL_RADIUS, YDIM - BALL_RADIUS); ball_direction = (double) random_in_range(0,359) * 3.14159 / 180.; ball_velocity = (double) random_in_range(5, 15); } /************************************ update_ball_loc ************************/ void update_ball_loc(double &ball_loc_x, double &ball_loc_y, double &ball_direction, double ball_velocity) { double delta_x; double delta_y; delta_x = cos(ball_direction) * ball_velocity; delta_y = sin(ball_direction) * ball_velocity; // check whether ball has hit left or right wall if ((ball_loc_x + delta_x >= XDIM - BALL_RADIUS) || (ball_loc_x + delta_x < BALL_RADIUS)) ball_direction = 3.14159 - ball_direction; else ball_loc_x += delta_x; // check whether ball has hit upper or lower wall if ((ball_loc_y + delta_y >= YDIM - BALL_RADIUS) || (ball_loc_y + delta_y < BALL_RADIUS)) ball_direction = -ball_direction; else ball_loc_y += delta_y; } /************************************ update_screen **************************/ void update_screen(int new_ball_screen_x, int new_ball_screen_y) { static int old_ball_screen_x = -1; // previous displayed x position of ball static int old_ball_screen_y = -1; // previous displayed y position of ball // check to see if ball has moved on screen if (old_ball_screen_x != new_ball_screen_x || old_ball_screen_y != new_ball_screen_y) { // erase ball in old location setfillstyle(SOLID_FILL, TABLE_COLOR); setcolor(TABLE_COLOR); fillellipse(old_ball_screen_x, old_ball_screen_y, BALL_RADIUS, BALL_RADIUS); // draw ball in new location setfillstyle(SOLID_FILL, BALL_COLOR); setcolor(BALL_COLOR); fillellipse(new_ball_screen_x, new_ball_screen_y, BALL_RADIUS, BALL_RADIUS); // update stored ball position old_ball_screen_x = new_ball_screen_x; old_ball_screen_y = new_ball_screen_y; } } /************************************ random_in_range ************************/ // Generate a random number in the integer range [lower, upper] int random_in_range(int lower, int upper) { static int first_time = 1; if (first_time) { srand(time(0)); first_time = 0; } return (lower + rand()%(upper-lower+1)); }