|
|
|
@ -1,3 +1,4 @@
|
|
|
|
|
#include <iostream> |
|
|
|
|
#include <functional> |
|
|
|
|
#include <string> |
|
|
|
|
#include <cmath> |
|
|
|
@ -42,11 +43,13 @@ public:
|
|
|
|
|
|
|
|
|
|
class Ball : public Drawable { |
|
|
|
|
public: |
|
|
|
|
static constexpr GLfloat defaultVeloX = 0.5; |
|
|
|
|
|
|
|
|
|
GLfloat radius; |
|
|
|
|
GLfloat x, y; |
|
|
|
|
GLfloat veloX, veloY; |
|
|
|
|
|
|
|
|
|
Ball(GLfloat x, GLfloat y) : x(x), y(y), radius(0.025), veloX(0.4) { |
|
|
|
|
Ball(GLfloat x, GLfloat y) : x(x), y(y), radius(0.025), veloX(defaultVeloX) { |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -121,7 +124,7 @@ void display(void) {
|
|
|
|
|
|
|
|
|
|
int gameTime; |
|
|
|
|
|
|
|
|
|
const GLfloat paddleMoveSpeed = 0.75; |
|
|
|
|
const GLfloat paddleMoveSpeed = 0.8; |
|
|
|
|
|
|
|
|
|
void idle() { |
|
|
|
|
int newTime = glutGet(GLUT_ELAPSED_TIME); |
|
|
|
@ -131,17 +134,55 @@ void idle() {
|
|
|
|
|
if (gameRunning) { |
|
|
|
|
ball.update(); |
|
|
|
|
|
|
|
|
|
// Move paddle
|
|
|
|
|
leftPaddle.y += leftMove * paddleMoveSpeed * deltaTime; |
|
|
|
|
rightPaddle.y += rightMove * paddleMoveSpeed * deltaTime; |
|
|
|
|
|
|
|
|
|
// Clamp paddle to stay on screen
|
|
|
|
|
if (leftPaddle.y < -1) leftPaddle.y = -1; |
|
|
|
|
if (rightPaddle.y < -1) rightPaddle.y = -1; |
|
|
|
|
if (leftPaddle.y > 1 - leftPaddle.height) leftPaddle.y = 1 - leftPaddle.height; |
|
|
|
|
if (rightPaddle.y > 1 - rightPaddle.height) rightPaddle.y = 1 - rightPaddle.height; |
|
|
|
|
// Detect ball collission
|
|
|
|
|
if (leftPaddle.x <= (ball.x + ball.radius) && (ball.x - ball.radius) <= (leftPaddle.x + leftPaddle.width)) { |
|
|
|
|
// Ball inside left-right area of left paddle
|
|
|
|
|
if (leftPaddle.y <= ball.y && ball.y <= leftPaddle.y + leftPaddle.height) { |
|
|
|
|
// Ball inside up-down area of left paddle
|
|
|
|
|
ball.veloX = ball.defaultVeloX; |
|
|
|
|
ball.veloY = Utils::nummap(ball.y, leftPaddle.y, leftPaddle.y + leftPaddle.height, (GLfloat)-1, (GLfloat)1); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
else if (rightPaddle.x <= (ball.x + ball.radius) && (ball.x - ball.radius) <= (rightPaddle.x + rightPaddle.width)) { |
|
|
|
|
// Ball inside left-right area of right paddle
|
|
|
|
|
if (rightPaddle.y <= ball.y && ball.y <= rightPaddle.y + rightPaddle.height) { |
|
|
|
|
// Ball inside up-down area of right paddle
|
|
|
|
|
ball.veloX = -ball.defaultVeloX; |
|
|
|
|
ball.veloY = Utils::nummap(ball.y, rightPaddle.y, rightPaddle.y + rightPaddle.height, (GLfloat)-1, (GLfloat)1); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
else if (ball.x + ball.radius < -1) { |
|
|
|
|
// Ball exited board on the left side
|
|
|
|
|
rightScore += 1; |
|
|
|
|
ball.x = ball.y = 0; |
|
|
|
|
gameRunning = false; |
|
|
|
|
} |
|
|
|
|
else if (ball.x - ball.radius > 1) { |
|
|
|
|
// Ball exited board on the right side
|
|
|
|
|
leftScore += 1; |
|
|
|
|
ball.x = ball.y = 0; |
|
|
|
|
gameRunning = false; |
|
|
|
|
} |
|
|
|
|
else if (ball.y - ball.radius < -1) { |
|
|
|
|
// Ball touched bottom side
|
|
|
|
|
ball.veloY *= -1; |
|
|
|
|
} |
|
|
|
|
else if (ball.y + ball.radius > 1) { |
|
|
|
|
// Ball touched top side
|
|
|
|
|
ball.veloY *= -1; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Move paddle
|
|
|
|
|
leftPaddle.y += leftMove * paddleMoveSpeed * deltaTime; |
|
|
|
|
rightPaddle.y += rightMove * paddleMoveSpeed * deltaTime; |
|
|
|
|
|
|
|
|
|
// Clamp paddle to stay on screen
|
|
|
|
|
if (leftPaddle.y < -1) leftPaddle.y = -1; |
|
|
|
|
if (rightPaddle.y < -1) rightPaddle.y = -1; |
|
|
|
|
if (leftPaddle.y > 1 - leftPaddle.height) leftPaddle.y = 1 - leftPaddle.height; |
|
|
|
|
if (rightPaddle.y > 1 - rightPaddle.height) rightPaddle.y = 1 - rightPaddle.height; |
|
|
|
|
|
|
|
|
|
glutPostRedisplay(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|