altos/pong: Don't let the ball skip over the paddle pong
authorKeith Packard <keithp@keithp.com>
Fri, 3 Nov 2017 04:03:34 +0000 (21:03 -0700)
committerKeith Packard <keithp@keithp.com>
Fri, 3 Nov 2017 04:03:34 +0000 (21:03 -0700)
Check for intersect at each X step.

Signed-off-by: Keith Packard <keithp@keithp.com>
src/pong/ao_pong.c

index 79b50dd031fb7a3b5915ff49bd289f9f733c64af..2492468c2c58f65bc799235f91fae6be71a687ac 100644 (file)
@@ -49,7 +49,7 @@ static const int      player_led[2] = { AO_LED_GREEN, AO_LED_BLUE };
 static int             player_value[2];
 static int             player_score[2];
 
-#define player_filter(old, new)        (((old) * 7 + (new)) >> 3)
+#define player_filter(old, new)        (((old) + (new)) >> 1)
 
 static struct rect     player[2];
 
@@ -73,50 +73,53 @@ static int
 ao_ball_step(void)
 {
        int     p;
+       int     s;
 
        /* Move the ball */
-       ball_e += ball_dy * ball_speed;
-       ball.x += ball_step_x * ball_speed;
-       while (ball_e >= ball_dx) {
-               ball_e -= ball_dx;
-               ball.y += ball_step_y;
-
-               /* bounce off walls */
-               if (ball.y < 0 || ball.y + ball.h > AO_VGA_HEIGHT) {
-                       ball_step_y = -ball_step_y;
+       for (s = 0; s < ball_speed; s++) {
+               ball_e += ball_dy;
+               ball.x += ball_step_x;
+               while (ball_e >= ball_dx) {
+                       ball_e -= ball_dx;
                        ball.y += ball_step_y;
+
+                       /* bounce off walls */
+                       if (ball.y < 0 || ball.y + ball.h > AO_VGA_HEIGHT) {
+                               ball_step_y = -ball_step_y;
+                               ball.y += ball_step_y;
+                       }
                }
-       }
 
-       /* player missed */
-       if (ball.x <= 0)
-               return 1;
+               /* player missed */
+               if (ball.x <= 0)
+                       return 1;
 
-       if (ball.x >= AO_VGA_WIDTH - BALL_WIDTH)
-               return -1;
+               if (ball.x >= AO_VGA_WIDTH - BALL_WIDTH)
+                       return -1;
 
-       /* bounce off paddles */
+               /* bounce off paddles */
 
-       for (p = 0; p < 2; p++) {
-               if (intersect(&ball, &player[p])) {
+               for (p = 0; p < 2; p++) {
+                       if (intersect(&ball, &player[p])) {
 
-                       volley_count++;
+                               volley_count++;
 
-                       ball_speed = 3 + volley_count / 4;
+                               ball_speed = 3 + volley_count / 4;
 
-                       int     dy = (2 * (ball.y - player[p].y) + ball.h - player[p].h);
-                       int     dx = 20;
+                               int     dy = (2 * (ball.y - player[p].y) + ball.h - player[p].h);
+                               int     dx = 20;
 
-                       ball_step_x = -ball_step_x;
-                       ball.x += ball_step_x * ball_speed;
-                       ball_step_y = 1;
-                       if (dy < 0) {
-                               ball_step_y = -1;
-                               dy = -dy;
+                               ball_step_x = -ball_step_x;
+                               ball.x += ball_step_x;
+                               ball_step_y = 1;
+                               if (dy < 0) {
+                                       ball_step_y = -1;
+                                       dy = -dy;
+                               }
+                               ball_e = 0;
+                               ball_dx = dx;
+                               ball_dy = dy;
                        }
-                       ball_e = 0;
-                       ball_dx = dx;
-                       ball_dy = dy;
                }
        }