]> code.delx.au - pong/blobdiff - pong.c
Paddles move better now
[pong] / pong.c
diff --git a/pong.c b/pong.c
index 5f5ccf7c3421c59752e8e99354d0c5ace622e14c..ad2abdace89bff8d9f2535e297a082776e36347b 100644 (file)
--- a/pong.c
+++ b/pong.c
 #include <stdio.h>
 #include <stdlib.h>
 
-enum {
-       FRAME=40,
-       HEIGHT=100,
-       PADDLESIZE=10
-};
-
-static int score1;
-static int score2;
-static GLdouble paddle1;
-static GLdouble paddle2;
-static GLdouble ballX;
-static GLdouble ballY;
-static GLdouble ballVecX;
-static GLdouble ballVecY;
+#define SPEEDINC    1.15
+#define PADDLESPEED 2.5
+#define FRAME       40
+#define PADDLESIZE  10
+#define HEIGHT      100
+
+static int p1move = 0;
+static int p2move = 0;
+static int score1 = 0;
+static int score2 = 0;
+static GLdouble paddle1 = 0.0;
+static GLdouble paddle2 = 0.0;
+static GLdouble ballX = 0.0;
+static GLdouble ballY = 0.0;
+static GLdouble ballVecX = 0.0;
+static GLdouble ballVecY = 0.0;
 
 
 static void initball(void) {
        ballX = 0.0;
        ballY = 0.0;
        if(ballVecX < 0)
-               ballVecX = -1.0;
+               ballVecX = -1.2;
        else
-               ballVecX = 1.0;
-       ballVecY = 1.0;
+               ballVecX = 1.2;
+       ballVecY = 0.7;
 }
 
 static void run(void) {
+       // Move the paddles
+       paddle1 += p1move * PADDLESPEED;
+       paddle2 += p2move * PADDLESPEED;
+
        // Check for collisions with paddles
        if(ballVecX < 0 && ballX <= -HEIGHT + -ballVecX * 1.5) {
                if(ballY >= paddle1 - PADDLESIZE && ballY <= paddle1 + PADDLESIZE) {
-                       ballVecX *= -1.15;
-                       ballVecY *= 1.15;
+                       ballVecX *= -SPEEDINC;
+                       ballVecY *= SPEEDINC;
                }
        }
        if(ballVecX > 0 && ballX >=  HEIGHT - ballVecX * 1.5) {
                if(ballY >= paddle2 - PADDLESIZE && ballY <= paddle2 + PADDLESIZE) {
-                       ballVecX *= -1.15;
-                       ballVecY *= 1.15;
+                       ballVecX *= -SPEEDINC;
+                       ballVecY *= SPEEDINC;
                }
        }
 
        // Check if it's past the top or bottom of the screen
        if(ballY >= HEIGHT || ballY <= -HEIGHT) {
-               ballVecY *= -1;
+               ballVecY = -ballVecY;
        }
 
        // Check if it's past the sides of the screen
@@ -117,21 +123,17 @@ static void keyboard(unsigned char key, int x, int y) {
                        exit(0);
 
                case 'w':
-                       if(paddle1 + PADDLESIZE < HEIGHT)
-                               paddle1 += PADDLESIZE;
+                       p1move = 1;
                        break;
                case 's':
-                       if(paddle1 - PADDLESIZE > -HEIGHT)
-                               paddle1 -= PADDLESIZE;
+                       p1move = -1;
                        break;
 
                case 'i':
-                       if(paddle2 + PADDLESIZE < HEIGHT)
-                               paddle2 += PADDLESIZE;
+                       p2move = 1;
                        break;
                case 'k':
-                       if(paddle2 - PADDLESIZE > -HEIGHT)
-                               paddle2 -= PADDLESIZE;
+                       p2move = -1;
                        break;
 
                default:
@@ -140,6 +142,23 @@ static void keyboard(unsigned char key, int x, int y) {
        glutPostRedisplay();
 }
 
+static void keyboardUp(unsigned char key, int x, int y) {
+       (void)x;(void)y;
+
+       switch(key) {
+               case 'w':
+               case 's':
+                       p1move = 0;
+                       break;
+
+               case 'i':
+               case 'k':
+                       p2move = 0;
+                       p2move = 0;
+                       break;
+       }
+}
+
 static void timer(int lastTime) {
        int curTime = glutGet(GLUT_ELAPSED_TIME);
        do {
@@ -164,6 +183,7 @@ int main(int argc, char *argv[]) {
        glutDisplayFunc(display);
        glutReshapeFunc(resize);
        glutKeyboardFunc(keyboard);
+       glutKeyboardUpFunc(keyboardUp);
        glutTimerFunc(FRAME, timer, glutGet(GLUT_ELAPSED_TIME));
        init();
        glutMainLoop();