-
Notifications
You must be signed in to change notification settings - Fork 0
/
GameManager.cpp
120 lines (94 loc) · 3.42 KB
/
GameManager.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#include "GameManager.h"
using namespace std;
namespace Pong {
GameManager::GameManager() {
// TODO Auto-generated constructor stub
}
void GameManager::showPreLaunch() {
SDL_Delay(1000);
cout << "Controls :" << endl;
cout << endl << "==================================================================" << endl;
cout << endl << "Paddle up: a Paddle up: k" << endl;
cout << endl << "Paddle down: s Paddle down: l" << endl;
cout << endl << "Quit: q" << endl;
cout << endl << "==================================================================" << endl;
printf("\nStarting in ..\n\n%d\n", PRELAUNCH_TIME);
for (int time = PRELAUNCH_TIME - 1; time > 0; time--) {
SDL_Delay(1000);
printf("%d\n", time);
}
}
void GameManager::processCollisions(Ball &ball, Paddle &paddle1, Paddle &paddle2) {
bool collisionPaddle1 = ((ball.m_xpos - Ball::BALL_RADIUS)
<= (paddle1.m_xpos + Paddle::PADDLE_WIDTH / 2)
&& ball.m_ypos >= (paddle1.m_ypos - Paddle::PADDLE_HEIGHT / 2)
&& ball.m_ypos <= (paddle1.m_ypos + Paddle::PADDLE_HEIGHT / 2));
bool collisionPaddle2 = ((ball.m_xpos + Ball::BALL_RADIUS)
>= (paddle2.m_xpos - Paddle::PADDLE_WIDTH / 2)
&& ball.m_ypos >= (paddle2.m_ypos - Paddle::PADDLE_HEIGHT / 2)
&& ball.m_ypos <= (paddle2.m_ypos + Paddle::PADDLE_HEIGHT / 2));
if (collisionPaddle1 || collisionPaddle2) {
ball.m_xspeed = -ball.m_xspeed;
}
}
void GameManager::updateScore(Ball &ball, Player &player1, Player &player2) {
bool player1scored = (ball.m_xpos + Ball::BALL_RADIUS) >= Screen::SCREEN_WIDTH;
bool player2scored = (ball.m_xpos - Ball::BALL_RADIUS) <= 0;
if (player1scored || player2scored) {
cout << "============" << endl;
cout << "!! SCORED !!" << endl;
cout << "============" << endl;
if (player1scored) {
player1.m_score++;
cout << endl << player1.m_name << ": " << player1.m_score << "\t"
<< player2.m_name << ": " << player2.m_score << endl;
}
else if (player2scored) {
player2.m_score++;
cout << endl << player1.m_name << ": " << player1.m_score << "\t"
<< player2.m_name << ": " << player2.m_score << endl;
}
}
}
bool GameManager::processEvents(Player &player1, Player &player2, Paddle &paddle1, Paddle &paddle2) {
SDL_Event event;
while (SDL_PollEvent(&event)) {
if (event.type == SDL_KEYDOWN) {
switch (event.key.keysym.sym) {
case SDLK_q: // Quit
cout << "==================" << endl;
cout << "!! FINAL SCORES !!" << endl;
cout << "==================" << endl;
cout << endl << player1.m_name << ": " << player1.m_score
<< "\t" << player2.m_name << ": " << player2.m_score
<< endl;
return false;
break;
case SDLK_a: // Paddle 1 move down
if ((paddle1.m_ypos + Paddle::PADDLE_HEIGHT)
<= Screen::SCREEN_HEIGHT) {
paddle1.m_ypos += Paddle::PADDLE_SPEED;
}
break;
case SDLK_s: // Paddle 1 move up
if ((paddle1.m_ypos - Paddle::PADDLE_HEIGHT) >= 0) {
paddle1.m_ypos -= Paddle::PADDLE_SPEED;
}
break;
case SDLK_k: // Paddle 2 move down
if ((paddle2.m_ypos + Paddle::PADDLE_HEIGHT)
<= Screen::SCREEN_HEIGHT) {
paddle2.m_ypos += Paddle::PADDLE_SPEED;
}
break;
case SDLK_l: // Paddle 2 move up
if ((paddle2.m_ypos - Paddle::PADDLE_HEIGHT) >= 0) {
paddle2.m_ypos -= Paddle::PADDLE_SPEED;
}
break;
} /* switch */
} /* if */
} /* while */
return true;
}
} /* namespace Pong */