]> 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 a61bc0d68bc5986ced25f223cf274c9e275d97e4..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,6 +36,14 @@ 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));
@@ -95,6 +104,7 @@ main(void) {
                                } else if (key == 'q') {
                                        die(0);
                                }
+                               msleep();
                        }
                }
 
@@ -142,9 +152,9 @@ 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;
@@ -177,5 +187,6 @@ main(void) {
                        mvprintw(y, x, " ");
                        y = (y == max_y) ? max_y - 1 : 1;
                }
+               msleep();
        }
 }