]> git.armaanb.net Git - pong.git/commitdiff
Eliminate usleep and improve logic
authorArmaan Bhojwani <me@armaanb.net>
Sat, 15 May 2021 21:29:01 +0000 (17:29 -0400)
committerArmaan Bhojwani <me@armaanb.net>
Sat, 15 May 2021 21:29:01 +0000 (17:29 -0400)
pong.c

diff --git a/pong.c b/pong.c
index e6b8f38d25f872fd02da7106edc374b12e7352d7..0be9fe3f28be0925eb638ba8dba4a066025e88b7 100644 (file)
--- a/pong.c
+++ b/pong.c
@@ -2,6 +2,8 @@
 #include <stdlib.h>
 #include <stdio.h>
 #include <unistd.h>
+#include <sys/time.h>
+#include <time.h>
 
 int
 die(int code)
@@ -12,29 +14,34 @@ die(int code)
 
 int
 main(void) {
-       int x = 1, y = 0, aoff = 0, boff = 0;
-       int max_y = 0, max_x = 0;
+       int x, y, aoff = 0, boff = 0;
+       int max_y, max_x;
        int max_y_new = 0, max_x_new = 0;
-       int direction = 1;
        int pad_h = 5;
        int a_score = 0, b_score = 0;
-       int slope_x = 5;
+       int slope_x = 8;
        int slope_y = 1;
-       int index = 0;
        int index_dir = 1;
+       int direction = 1;
+       double speed = 0.015;
+       struct timeval start, stop;
 
        // Initialize curses
        initscr();
        noecho();
        curs_set(0);
        nodelay(stdscr, 1); // Don't wait for getch input
-       index = max_y/2;
+       getmaxyx(stdscr, max_y, max_x);
+       --max_x;
+       --max_y;
+       int index = (max_y/2)*(slope_x/slope_y);
        x = max_x/2;
 
+       gettimeofday(&start, NULL);
        while(1) {
                getmaxyx(stdscr, max_y_new, max_x_new);
                --max_x_new;
-               --max_y_new; 
+               --max_y_new;
                pad_h = 0.2 * max_y;
                if (max_y_new != max_y || max_x_new != max_x) {
                        clear();
@@ -42,8 +49,6 @@ main(void) {
                        max_y = max_y_new;
                }
 
-               index += index_dir;
-
                // Parse keypresses
                int key = getch();
                if (key == 'q') {
@@ -59,9 +64,11 @@ main(void) {
                }
 
                // Draw paddles
-               for (int i = 0; i < max_y; i++) {
+               for (int i = 0; i < max_y + 1; i++) {
                        mvprintw(i, 0, " ");
+                       mvprintw(i, 1, " ");
                        mvprintw(i, max_x, " ");
+                       mvprintw(i, max_x - 1, " ");
                }
                for (int i = aoff; i < aoff + pad_h; i++) {
                        mvprintw(i, 0, "|");
@@ -79,34 +86,42 @@ main(void) {
                mvprintw(0, max_x - 1, "%d", b_score);
 
                // Move ball
-               mvprintw(y, x, " ");
-
-               x += direction;
-               y = index / slope_x * slope_y;
-               mvprintw(y, x, "o");
+               gettimeofday(&stop, NULL);
+               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 = slope_y * index / slope_x;
+                       mvprintw(y, x, "o");
+               }
 
-               // Check if touching paddle
-               if (x == max_x) {
+               // Check if touching paddle or edges
+               if (x >= max_x - 1) {
                        if (y >= boff && y <= boff + pad_h) {
                                direction *= -1;
+                               x += direction;
                        } else {
                                a_score += 1;
-                               usleep(600000);
                                x = max_x/2;
-                               index = max_y/2;
+                               index = (max_y/2)*(slope_x/slope_y);
+                               speed /= 0.8;
+                               sleep(1);
                        }
-               } else if (x == 0) {
+               } else if (x <= 1) {
                        if (y >= aoff && y <= aoff + pad_h) {
                                direction *= -1;
+                               x += direction;
                        } else {
                                b_score += 1;
-                               usleep(600000);
                                x = max_x/2;
-                               index = max_y/2;
+                               index = (max_y/2)*(slope_x/slope_y);
+                               speed /= 0.8;
+                               sleep(1);
                        }
-               } else if (y < 0 || y > max_y) {
+               } else if (y == 0 || y == max_y) {
                        index_dir *= -1;
                }
-               usleep(3000000 * (1.0/((double)max_x + (double)max_y)));
        }
 }