]> git.armaanb.net Git - pong.git/commitdiff
Reflect ball depending on where its hit
authorArmaan Bhojwani <me@armaanb.net>
Sun, 16 May 2021 00:23:21 +0000 (20:23 -0400)
committerArmaan Bhojwani <me@armaanb.net>
Sun, 16 May 2021 00:23:36 +0000 (20:23 -0400)
pong.c

diff --git a/pong.c b/pong.c
index 1363ae59531c37b8c90f9b1fe24ff9c4a373ab08..4bda2a7f21bbeae2bfcc795b6409a2986044b149 100644 (file)
--- a/pong.c
+++ b/pong.c
@@ -21,6 +21,14 @@ 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)
 {
@@ -30,15 +38,15 @@ randsign(void)
 int
 main(void) {
        srand((unsigned int)time(NULL));
-       int x, y, aoff = 0, boff = 0;
+       int 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 = randslope();
        int index_dir = randsign();
-       int direction = 1;
-       double speed = 0.02;
+       int direction = randsign();
+       double speed = randspeed();
        struct timeval start, stop;
 
        // Initialize curses
@@ -49,8 +57,8 @@ 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;
 
        gettimeofday(&start, NULL);
        while(1) {
@@ -110,16 +118,23 @@ 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;
@@ -131,16 +146,15 @@ main(void) {
                                a_score += 1;
                        }
                        x = max_x/2;
-                       index = slope_x*max_y/2;
                        slope_x = randslope();
                        index_dir = randsign();
-                       speed = 0.02;
+                       speed = randspeed();
                        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;
+                       y = (y == max_y) ? max_y - 1 : 1;
                }
        }
 }