#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
-static int AI = 0;
+#define STATE_UI 0
+#define STATE_1P 1
+#define STATE_2P 2
+
+static char* menu = "";
+static int state = 0;
static int p1move = 0;
static int p2move = 0;
static int score1 = 0;
static GLdouble ballVecY = 0.0;
-static void initball(void) {
+static void initBall(void) {
ballX = 0.0;
ballY = 0.0;
if(ballVecX < 0)
}
static void run(void) {
- // AI
- if(AI) {
+ // AI is on
+ if(state == STATE_1P) {
if(ballY < paddle2 - PADDLESIZE)
p2move = -1;
else if(ballY > paddle2 + PADDLESIZE)
// 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;
}
}
// Check if it's past the sides of the screen
if(ballX >= HEIGHT) {
++score1;
- initball();
+ initBall();
}
if(ballX <= -HEIGHT) {
++score2;
- initball();
+ initBall();
}
// Move the ball
// Check scores for winners..
if(score1 == 9) {
- // Player 1 wins
+ menu = "Player 1 wins!";
+ state = STATE_UI;
score1 = score2 = 0;
}
if(score2 == 9) {
- // Player 2 wins
+ menu = "Player 2 wins!";
+ state = STATE_UI;
score1 = score2 = 0;
}
}
-static void display(void) {
- glClear(GL_COLOR_BUFFER_BIT);
- glColor3d(0.0, 0.0, 0.0);
+static void timer(int lastTime) {
+ if(state == STATE_UI) {
+ return;
+ }
+
+ int curTime = glutGet(GLUT_ELAPSED_TIME);
+ do {
+ lastTime += FRAME;
+ run();
+ } while(lastTime + FRAME < curTime);
+ glutPostRedisplay();
+ glutTimerFunc(FRAME - (curTime - lastTime), timer, curTime);
+}
+static void startGame(void) {
+ glutTimerFunc(FRAME, timer, glutGet(GLUT_ELAPSED_TIME));
+ initBall();
+}
+
+static void drawText(double x, double y, char* str) {
+ glRasterPos2d(x, y);
+ while(*str) {
+ glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, *str);
+ ++str;
+ }
+}
+
+static void menuDisplay(void) {
+ drawText(-20, 50, "Awesome PONG!");
+ drawText(-50, -50, "Press 1 for a single player game.");
+ drawText(-50, -60, "Press 2 for a multi player game.");
+ drawText(-50, -70, "Keys: w/s and i/k");
+ drawText(-20, 0, menu);
+}
+
+static void gameDisplay(void) {
// Draw the paddles
glLineWidth(2.0);
glBegin(GL_LINES);
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, '0' + score1);
glRasterPos2d( 5.0, HEIGHT - 10.0);
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, '0' + score2);
+}
+
+static void display(void) {
+ glClear(GL_COLOR_BUFFER_BIT);
+ glColor3d(0.0, 0.0, 0.0);
+
+ if(state == STATE_UI) {
+ menuDisplay();
+ } else {
+ gameDisplay();
+ }
glutSwapBuffers();
}
glOrtho(-HEIGHT, HEIGHT, -HEIGHT, HEIGHT, -1.0, 1.0);
}
-static void keyboard(unsigned char key, int x, int y) {
- (void)x;(void)y;
-
+static void menuKeyboard(unsigned char key) {
switch(key) {
- case 'q':
- case 'Q':
- case '\033':
- exit(0);
+ case '1':
+ state = STATE_1P;
+ break;
+ case '2':
+ state = STATE_2P;
+ break;
+
+ default:
+ return;
+ }
+ startGame();
+}
+
+static void gameKeyboard(unsigned char key) {
+ switch(key) {
case 'w':
p1move = 1;
break;
glutPostRedisplay();
}
+static void keyboard(unsigned char key, int x, int y) {
+ (void)x;(void)y;
+
+ switch(key) {
+ case 'q':
+ case 'Q':
+ case '\033':
+ exit(0);
+ }
+
+
+ if(state == STATE_UI) {
+ menuKeyboard(key);
+ } else {
+ gameKeyboard(key);
+ }
+}
+
static void keyboardUp(unsigned char key, int x, int y) {
(void)x;(void)y;
}
}
-static void timer(int lastTime) {
- int curTime = glutGet(GLUT_ELAPSED_TIME);
- do {
- lastTime += FRAME;
- run();
- } while(lastTime + FRAME < curTime);
- glutPostRedisplay();
- glutTimerFunc(FRAME - (curTime - lastTime), timer, curTime);
-}
-
-static void init(void) {
- glClearColor(1.0, 1.0, 1.0, 0.0);
- initball();
-}
-
int main(int argc, char *argv[]) {
- if(argc == 2 && *argv[1] == '1') {
- AI = 1;
- }
-
glutInitWindowPosition(100, 100);
glutInitWindowSize(640, 480);
glutInit(&argc, argv);
glutReshapeFunc(resize);
glutKeyboardFunc(keyboard);
glutKeyboardUpFunc(keyboardUp);
- glutTimerFunc(FRAME, timer, glutGet(GLUT_ELAPSED_TIME));
- init();
+ glClearColor(1.0, 1.0, 1.0, 0.0);
glutMainLoop();
return 0;
}