]> git.armaanb.net Git - pong.git/blobdiff - pong.c
Improve paddle bounce logic
[pong.git] / pong.c
diff --git a/pong.c b/pong.c
index 72b5602763c04924b9eac37652a7f24edf4ab7db..0a750506c13d8a6db893ebfebf8503874781a306 100644 (file)
--- a/pong.c
+++ b/pong.c
@@ -142,9 +142,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;
@@ -159,12 +159,16 @@ main(void) {
                                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) {