]> git.armaanb.net Git - pong.git/blobdiff - pong.c
Randomize direction of index
[pong.git] / pong.c
diff --git a/pong.c b/pong.c
index 0be9fe3f28be0925eb638ba8dba4a066025e88b7..1363ae59531c37b8c90f9b1fe24ff9c4a373ab08 100644 (file)
--- a/pong.c
+++ b/pong.c
@@ -4,6 +4,7 @@
 #include <unistd.h>
 #include <sys/time.h>
 #include <time.h>
+#include <math.h>
 
 int
 die(int code)
@@ -12,18 +13,32 @@ die(int code)
        exit(code);
 }
 
+int
+randslope(void)
+{
+       const int low = 2;
+       const int high = 10;
+       return (rand() % (high - low + 1)) + low;
+}
+
+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 a_score = 0, b_score = 0;
-       int slope_x = 8;
-       int slope_y = 1;
-       int index_dir = 1;
+       int slope_x = randslope();
+       int index_dir = randsign();
        int direction = 1;
-       double speed = 0.015;
+       double speed = 0.02;
        struct timeval start, stop;
 
        // Initialize curses
@@ -34,11 +49,12 @@ main(void) {
        getmaxyx(stdscr, max_y, max_x);
        --max_x;
        --max_y;
-       int index = (max_y/2)*(slope_x/slope_y);
+       int index = slope_x*max_y/2;
        x = max_x/2;
 
        gettimeofday(&start, NULL);
        while(1) {
+               // Handle resizes
                getmaxyx(stdscr, max_y_new, max_x_new);
                --max_x_new;
                --max_y_new;
@@ -63,13 +79,15 @@ main(void) {
                        aoff -= 2;
                }
 
-               // Draw paddles
+               // Clear screen
                for (int i = 0; i < max_y + 1; i++) {
                        mvprintw(i, 0, " ");
                        mvprintw(i, 1, " ");
                        mvprintw(i, max_x, " ");
                        mvprintw(i, max_x - 1, " ");
                }
+
+               // Draw paddles
                for (int i = aoff; i < aoff + pad_h; i++) {
                        mvprintw(i, 0, "|");
                }
@@ -78,14 +96,16 @@ main(void) {
                        mvprintw(i, max_x, "|");
                }
 
+               // Draw net
                for (int i = 0; i < max_y; i++) {
                        mvprintw(i, max_x/2, ":");
                }
 
-               mvprintw(0, 2, "%d", a_score);
-               mvprintw(0, max_x - 1, "%d", b_score);
+               // Draw scrores
+               mvprintw(0, max_x/2 - 2, "%d", a_score);
+               mvprintw(0, max_x/2 + 2, "%d", b_score);
 
-               // Move ball
+               // Move ball if enough time has elapsed
                gettimeofday(&stop, NULL);
                if ((double)(stop.tv_usec - start.tv_usec) / 1000000 +
                                (double)(stop.tv_sec - start.tv_sec) > speed) {
@@ -93,35 +113,34 @@ main(void) {
                        index += index_dir;
                        mvprintw(y, x, " ");
                        x += direction;
-                       y = slope_y * index / slope_x;
+                       y = index / slope_x;
                        mvprintw(y, x, "o");
                }
 
-               // Check if touching paddle or edges
-               if (x >= max_x - 1) {
-                       if (y >= boff && y <= boff + pad_h) {
-                               direction *= -1;
-                               x += direction;
+               if ((x >= max_x - 1 && y >= boff && y <= boff + pad_h) ||
+                               (x <= 1 && y >= aoff && y <= aoff + pad_h)) {
+                       // Ball hits paddle
+                       direction *= -1;
+                       x += direction;
+                       speed *= 0.9;
+               } else if (x >= max_x - 1 || x <= 1) {
+                       // Ball goes out of bounds
+                       if (x <=1) {
+                               b_score += 1;
                        } else {
                                a_score += 1;
-                               x = max_x/2;
-                               index = (max_y/2)*(slope_x/slope_y);
-                               speed /= 0.8;
-                               sleep(1);
-                       }
-               } else if (x <= 1) {
-                       if (y >= aoff && y <= aoff + pad_h) {
-                               direction *= -1;
-                               x += direction;
-                       } else {
-                               b_score += 1;
-                               x = max_x/2;
-                               index = (max_y/2)*(slope_x/slope_y);
-                               speed /= 0.8;
-                               sleep(1);
                        }
+                       x = max_x/2;
+                       index = slope_x*max_y/2;
+                       slope_x = randslope();
+                       index_dir = randsign();
+                       speed = 0.02;
+                       sleep(1);
                } else if (y == 0 || y == max_y) {
+                       // Ball hits top or bottom wall
                        index_dir *= -1;
+                       mvprintw(y, x, " ");
+                       y = max_y - 1;
                }
        }
 }