]> code.delx.au - pong/blobdiff - pong.c
Tweaked speeds
[pong] / pong.c
diff --git a/pong.c b/pong.c
index 6f2ec4ce02c40741ab36994d997cc0580b23e450..f09d718534b770632f7da0133c5452981c14afc9 100644 (file)
--- a/pong.c
+++ b/pong.c
@@ -9,9 +9,13 @@
 
 #include <stdio.h>
 #include <stdlib.h>
+#include <math.h>
 
-#define SPEEDINC    1.15
-#define PADDLESPEED 2.5
+// Amount to increase speed by on x, y axis, and when bouncing
+#define SPEEDXINC   1.1
+#define SPEEDYINC   1.1
+#define SPEEDBINC   0.15
+#define PADDLESPEED 3.0
 #define FRAME       40
 #define PADDLESIZE  10
 #define HEIGHT      100
@@ -53,18 +57,28 @@ static void run(void) {
        // Move the paddles
        paddle1 += p1move * PADDLESPEED;
        paddle2 += p2move * PADDLESPEED;
+       if(paddle1 < -HEIGHT)
+               paddle1 = -HEIGHT;
+       if(paddle1 > HEIGHT)
+               paddle1 = HEIGHT;
+       if(paddle2 < -HEIGHT)
+               paddle2 = -HEIGHT;
+       if(paddle2 > HEIGHT)
+               paddle2 = HEIGHT;
 
        // Check for collisions with paddles
        if(ballVecX < 0 && ballX <= -HEIGHT + -ballVecX * 1.5) {
                if(ballY >= paddle1 - PADDLESIZE && ballY <= paddle1 + PADDLESIZE) {
-                       ballVecX *= -SPEEDINC;
-                       ballVecY *= SPEEDINC;
+                       GLdouble speedChange = fmax(1.0, fabs(paddle1 - ballY) * SPEEDBINC);
+                       ballVecX *= -speedChange * SPEEDXINC;
+                       ballVecY *= speedChange * SPEEDYINC;
                }
        }
        if(ballVecX > 0 && ballX >=  HEIGHT - ballVecX * 1.5) {
                if(ballY >= paddle2 - PADDLESIZE && ballY <= paddle2 + PADDLESIZE) {
-                       ballVecX *= -SPEEDINC;
-                       ballVecY *= SPEEDINC;
+                       GLdouble speedChange = fmax(1.0, fabs(paddle2 - ballY) * SPEEDBINC);
+                       ballVecX *= -speedChange * SPEEDXINC;
+                       ballVecY *= speedChange * SPEEDYINC;
                }
        }