// 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 struct coordinate_2d { double x, y; }; //enum color {BLACK, RED, GREEN, ...}; //struct ball //{ // coordinate_2d center; // coordinate_2d radius; // color c; //}; // function prototypes void initialize_screen(); void initialize_ball(coordinate_2d &ball_loc, double &ball_direction, double &ball_velocity); void update_ball_loc(coordinate_2d &ball_loc, double &ball_direction, double ball_velocity); void update_screen(coordinate_2d new_ball_screen); int random_in_range(int lower, int upper); int main () { coordinate_2d ball_loc; // ball 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, ball_direction, ball_velocity); do // loop forever { update_ball_loc(ball_loc, ball_direction, ball_velocity); update_screen(ball_loc); 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(coordinate_2d &ball_loc, 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(coordinate_2d &ball_loc, 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(coordinate_2d new_ball_screen) { static coordinate_2d old_ball_screen = {-1., -1.}; // previous displayed position of ball // check to see if ball has moved on screen if (int(old_ball_screen.x) != int(new_ball_screen.x) || int(old_ball_screen.y) != int(new_ball_screen.y)) { // erase ball in old location setfillstyle(SOLID_FILL, TABLE_COLOR); setcolor(TABLE_COLOR); fillellipse(int(old_ball_screen.x), int(old_ball_screen.y), BALL_RADIUS, BALL_RADIUS); // draw ball in new location setfillstyle(SOLID_FILL, BALL_COLOR); setcolor(BALL_COLOR); fillellipse(int(new_ball_screen.x), int(new_ball_screen.y), BALL_RADIUS, BALL_RADIUS); // update stored ball position old_ball_screen = new_ball_screen; } } /************************************ 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)); }