]> git.armaanb.net Git - pong.git/blobdiff - pong.c
Sleep during loops
[pong.git] / pong.c
diff --git a/pong.c b/pong.c
index cea4d2ad555187b3155b73e8ea910a5d80441cf3..c15860731b9cabf998e285cb39223741450781d2 100644 (file)
--- a/pong.c
+++ b/pong.c
@@ -3,6 +3,7 @@
 #include <stdio.h>
 #include <unistd.h>
 #include <sys/time.h>
+#include <sys/select.h>
 #include <time.h>
 #include <math.h>
 
@@ -35,13 +36,18 @@ randsign(void)
        return pow(-1, rand());
 }
 
+void
+msleep(void)
+{
+       struct timeval timeout;
+       timeout.tv_usec = 10000;
+       select(NULL, NULL, NULL, NULL, &timeout); // Sleep 100 ms
+}
+
 int
 main(void) {
        srand((unsigned int)time(NULL));
-       int 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 = randsign();
@@ -59,6 +65,9 @@ main(void) {
        --max_y;
        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) {
@@ -78,13 +87,25 @@ main(void) {
                if (key == 'q') {
                        die(0);
                } else if (key == 'k') {
-                       if (boff < max_y - pad_h) boff += 1;
+                       if (boff < max_y - pad_h) boff += 2;
                } else if (key == 'i') {
-                       if (boff > 0) boff -= 1;
+                       if (boff > 0) boff -= 2;
                } else if (key == 's') {
-                       if (aoff < max_y - pad_h) aoff += 1;
+                       if (aoff < max_y - pad_h) aoff += 2;
                } else if (key == 'w') {
-                       if (aoff > 0) aoff -= 1;
+                       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);
+                               }
+                               msleep();
+                       }
                }
 
                // Clear screen
@@ -131,30 +152,41 @@ main(void) {
                        /* 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;
+                       double fromc = (fabs(poff + 0.5 * pad_h - y) / 2);
+                       slope_x /= (abs(slope_x) > 25) ? 2 : fromc;
+                       slope_x = (slope_x < 2) ? 2 : slope_x;
 
                        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;
+                       y = max_y / 2;
                        slope_x = randslope();
                        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 = (y == max_y) ? max_y - 1 : 1;
                }
+               msleep();
        }
 }