X-Git-Url: https://git.armaanb.net/?a=blobdiff_plain;f=pong.c;h=a61bc0d68bc5986ced25f223cf274c9e275d97e4;hb=1cfbdcdeeb9260b901cb8e78ed656354d7415550;hp=717b1ae919752eb237e2bf451d34a8b1d646fed6;hpb=65ba1edac573cbc321bb4a22068becf1f1bd9b67;p=pong.git diff --git a/pong.c b/pong.c index 717b1ae..a61bc0d 100644 --- a/pong.c +++ b/pong.c @@ -4,6 +4,7 @@ #include #include #include +#include int die(int code) @@ -20,18 +21,29 @@ randslope(void) return (rand() % (high - low + 1)) + low; } +double +randspeed(void) +{ + const int low = 1; + const int high = 2; + return ((rand() % (high - low + 1)) + low) / 100.0; +} + +int +randsign(void) +{ + return pow(-1, rand()); +} + int main(void) { srand((unsigned int)time(NULL)); - int x, y, aoff = 0, boff = 0; - int max_y, max_x; - int max_y_new = 0, max_x_new = 0; - int pad_h = 5; + int max_y, max_x, max_y_new, max_x_new; int a_score = 0, b_score = 0; int slope_x = randslope(); - int index_dir = 1; - int direction = 1; - double speed = 0.02; + int index_dir = randsign(); + int direction = randsign(); + double speed = randspeed(); struct timeval start, stop; // Initialize curses @@ -42,8 +54,11 @@ main(void) { getmaxyx(stdscr, max_y, max_x); --max_x; --max_y; - int index = slope_x*max_y/2; - x = max_x/2; + int y = max_y/2; + int x = max_x/2; + int pad_h = 0.2 * max_y; + int aoff = max_y / 2 - pad_h; + int boff = max_y / 2 - pad_h; gettimeofday(&start, NULL); while(1) { @@ -63,13 +78,24 @@ main(void) { if (key == 'q') { die(0); } else if (key == 'k') { - boff += 2; + if (boff < max_y - pad_h) boff += 2; } else if (key == 'i') { - boff -= 2; + if (boff > 0) boff -= 2; } else if (key == 's') { - aoff += 2; + if (aoff < max_y - pad_h) aoff += 2; } else if (key == 'w') { - aoff -= 2; + if (aoff > 0) aoff -= 2; + } else if (key == 27) { + while(1) { + key = getch(); + mvprintw(0, 0, "PAUSED"); + if (key == 27) { + clear(); + break; + } else if (key == 'q') { + die(0); + } + } } // Clear screen @@ -103,36 +129,53 @@ main(void) { if ((double)(stop.tv_usec - start.tv_usec) / 1000000 + (double)(stop.tv_sec - start.tv_sec) > speed) { gettimeofday(&start, NULL); - index += index_dir; mvprintw(y, x, " "); x += direction; - y = index / slope_x; + if (x % slope_x == 0) y += index_dir; mvprintw(y, x, "o"); } if ((x >= max_x - 1 && y >= boff && y <= boff + pad_h) || (x <= 1 && y >= aoff && y <= aoff + pad_h)) { // Ball hits paddle + + /* Find the distance that the ball hits from the center of the padel and + accordingly adjust ball path */ + int poff = (x <= 1) ? aoff : boff; + double fromc = fabs(0.5 * (poff + 0.5 * pad_h - y)); + slope_x *= (slope_x < 20) ? 1 : (int) ((double) slope_x / fromc); + slope_x += 1; + direction *= -1; x += direction; speed *= 0.9; } else if (x >= max_x - 1 || x <= 1) { // Ball goes out of bounds - if (x <=1) { + if (x <= 1) { b_score += 1; + mvprintw(0, 0, "PLAYER B SCORES"); } else { a_score += 1; + mvprintw(0, 0, "PLAYER A SCORES"); } + refresh(); + + // Reset to default values x = max_x/2; - index = slope_x*max_y/2; + y = max_y / 2; slope_x = randslope(); - speed = 0.02; + index_dir = randsign(); + speed = randspeed(); + aoff = max_y / 2 - pad_h; + boff = max_y / 2 - pad_h; + sleep(1); + clear(); } else if (y == 0 || y == max_y) { // Ball hits top or bottom wall index_dir *= -1; mvprintw(y, x, " "); - y = max_y - 1; + y = (y == max_y) ? max_y - 1 : 1; } } }