-
Notifications
You must be signed in to change notification settings - Fork 1
/
pong.cpp
237 lines (200 loc) · 8.23 KB
/
pong.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
#include "pong.h"
const static int BALL_WIDTH = 2;
const static int BALL_HEIGHT = 2;
const static int PADDLE_SPACE = 5;
const static int PADDLE_WIDTH = 10;
const static int PADDLE_HEIGHT = 40;
const static int PADDLE_1_COLOUR = 0xE0;
const static int PADDLE_2_COLOUR = 0x03;
const static int BALL_COLOUR = 0xFF;
const static int BACKGROUND_COLOUR = 0x00;
const static int TEXT_COLOUR = 0xFF;
PongGame *pongGame = nullptr;
void pongDisplay(GameBuff *gameBuff) {
displayClear(gameBuff,BACKGROUND_COLOUR);
// Draw start instructions
if (pongGame->restart && (gameBuff->timeInMillis/1000) % 2 == 0) {
drawString2x(gameBuff,(char*)"Press A or B",gameBuff->WIDTH/2-(16*6),gameBuff->HEIGHT/2,TEXT_COLOUR,0);
drawString2x(gameBuff,(char*)"To Play",gameBuff->WIDTH/2-(16*4),gameBuff->HEIGHT/2+20,TEXT_COLOUR,0);
}
// Draw Scores
char score_char[5];
sprintf(score_char,"%4d",pongGame->score1);
drawString2x(gameBuff,score_char,40,0,PADDLE_1_COLOUR,0x00);
sprintf(score_char,"%4d",pongGame->score2);
drawString2x(gameBuff,score_char,100,0,PADDLE_2_COLOUR,0x00);
drawString2x(gameBuff,(char*)":",120,0,TEXT_COLOUR,0x00);
// Draw Ball
Dimensions dim;
dim.x = FIXP_TO_INT(pongGame->ball_x);
dim.y = FIXP_TO_INT(pongGame->ball_y);
dim.width = BALL_WIDTH;
dim.height = BALL_HEIGHT;
drawBlock(gameBuff,dim,BALL_COLOUR);
// Draw Paddle 1
dim.x = FIXP_TO_INT(pongGame->paddle_1_x);
dim.y = FIXP_TO_INT(pongGame->paddle_1_y);
dim.width = PADDLE_WIDTH;
dim.height = PADDLE_HEIGHT;
drawBlock(gameBuff,dim,PADDLE_1_COLOUR);
// Draw Paddle 2
dim.x = FIXP_TO_INT(pongGame->paddle_2_x);
dim.y = FIXP_TO_INT(pongGame->paddle_2_y);
drawBlock(gameBuff,dim,PADDLE_2_COLOUR);
}
void pongAttract(GameBuff *gameBuff) {
displayClear(gameBuff,0x00);
drawString2x(gameBuff,(char*)"PONG!",gameBuff->WIDTH/2-(16*2),0,PADDLE_1_COLOUR,-1);
if ((gameBuff->timeInMillis/1000) % 2 == 0) {
drawString2x(gameBuff,(char*)"Press Start",gameBuff->WIDTH/2-(16*5),gameBuff->HEIGHT/2,TEXT_COLOUR,0);
drawString2x(gameBuff,(char*)"To Play",gameBuff->WIDTH/2-(16*5),gameBuff->HEIGHT/2+20,TEXT_COLOUR,0);
}
}
void pongComputer(GameBuff *gameBuff) {
FIXPOINT desired = pongGame->ball_y;
if (desired < pongGame->paddle_2_y + INT_TO_FIXP(PADDLE_HEIGHT/3)) {
if (pongGame->paddle_2_y + INT_TO_FIXP(PADDLE_HEIGHT/3) - desired > FIXP_1 * 2)
pongGame->paddle_2_y -= FIXP_1 * 4;
} else if (desired > pongGame->paddle_2_y + INT_TO_FIXP(PADDLE_HEIGHT/3)) {
if (desired - pongGame->paddle_2_y + INT_TO_FIXP(PADDLE_HEIGHT/3) > FIXP_1 * 2)
pongGame->paddle_2_y += FIXP_1 * 4;
}
if (pongGame->paddle_2_y < INT_TO_FIXP(0)) {
pongGame->paddle_2_y = INT_TO_FIXP(0);
}
if (pongGame->paddle_2_y > INT_TO_FIXP(gameBuff->HEIGHT-PADDLE_HEIGHT)) {
pongGame->paddle_2_y = INT_TO_FIXP(gameBuff->HEIGHT-PADDLE_HEIGHT);
}
}
void pongStart(GameBuff *gameBuff) {
pongGame->restart = true;
pongGame->ball_dx = 0;
pongGame->ball_dy = 0;
pongGame->ball_x = INT_TO_FIXP(gameBuff->WIDTH/2);
pongGame->ball_y = INT_TO_FIXP(gameBuff->HEIGHT/2);
}
void pongInput(GameBuff *gameBuff) {
if (gameBuff->playerKeys.up) {
pongGame->paddle_1_y -= FIXP_1 * 5;
}
if (gameBuff->playerKeys.down) {
pongGame->paddle_1_y += FIXP_1 * 5;
}
if (pongGame->paddle_1_y < INT_TO_FIXP(0)) {
pongGame->paddle_1_y = INT_TO_FIXP(0);
}
if (pongGame->paddle_1_y > INT_TO_FIXP(gameBuff->HEIGHT-PADDLE_HEIGHT)) {
pongGame->paddle_1_y = INT_TO_FIXP(gameBuff->HEIGHT-PADDLE_HEIGHT);
}
}
bool pongUpdate(GameBuff *gameBuff) {
if (pongGame->restart) {
// Waiting for a keypress to Start
if (gameBuff->playerKeys.debouncedInput && (gameBuff->playerKeys.b || gameBuff->playerKeys.a)) {
pongGame->restart = false;
pongStart(gameBuff);
pongGame->ball_dx = ((rand()%10)-5) * FIXP_1;
pongGame->ball_dy = ((rand()%10)-5) * FIXP_1;
if (pongGame->ball_dx < FIXP_1 && pongGame->ball_dx > INT_TO_FIXP(0)) {
pongGame->ball_dx = FIXP_1;
}
else if (pongGame->ball_dx > FIXP_1 * -1 && pongGame->ball_dx < INT_TO_FIXP(0)) {
pongGame->ball_dx = FIXP_1 * -1;
}
pongGame->restart = false;
}
return false;
}
// Move the ball
pongGame->ball_x += pongGame->ball_dx;
pongGame->ball_y += pongGame->ball_dy;
// Check non zero speeds
if (pongGame->ball_dx == 0) pongGame->ball_dx = FIXP_1;
if (pongGame->ball_dy == 0) pongGame->ball_dy = FIXP_1;
// Check top wall bounce
if (pongGame->ball_y < INT_TO_FIXP(0)) {
pongGame->ball_dy *= -1;
}
// Check bottom wall bounce
if (pongGame->ball_y > INT_TO_FIXP(gameBuff->HEIGHT)) {
pongGame->ball_dy *= -1;
}
bool paddle_hit = false;
// Check paddle 1 bounce
if (pongGame->ball_x <= INT_TO_FIXP(PADDLE_WIDTH+PADDLE_SPACE) && pongGame->ball_x >= INT_TO_FIXP(PADDLE_SPACE)) {
if (pongGame->ball_y >= pongGame->paddle_1_y && pongGame->ball_y <= pongGame->paddle_1_y + INT_TO_FIXP(PADDLE_HEIGHT)) {
// Hit
if (pongGame->ball_dx < INT_TO_FIXP(0)) {
pongGame->ball_dx *= -1;
paddle_hit = true;
}
}
}
// Check paddle 2 bounce
if (pongGame->ball_x >= INT_TO_FIXP(gameBuff->WIDTH-(PADDLE_WIDTH+PADDLE_SPACE)) && pongGame->ball_x <= INT_TO_FIXP(gameBuff->WIDTH - PADDLE_SPACE)) {
if (pongGame->ball_y >= pongGame->paddle_2_y && pongGame->ball_y <= pongGame->paddle_2_y + INT_TO_FIXP(PADDLE_HEIGHT)) {
// Hit
if (pongGame->ball_dx > INT_TO_FIXP(0)) {
pongGame->ball_dx *= -1;
paddle_hit = true;
}
}
}
if (paddle_hit) {
int div = rand()%10;
if (div > 0) {
if (pongGame->ball_dx < INT_TO_FIXP(0)) pongGame->ball_dx -= FIXP_DIV(pongGame->ball_dx,INT_TO_FIXP(div));
else pongGame->ball_dx += FIXP_DIV(pongGame->ball_dx,INT_TO_FIXP(div));
}
div = rand()%10;
if (div > 0) {
if (pongGame->ball_dy < INT_TO_FIXP(0)) pongGame->ball_dy -= FIXP_DIV(pongGame->ball_dy,INT_TO_FIXP(div));
else pongGame->ball_dy += FIXP_DIV(pongGame->ball_dy,INT_TO_FIXP(div));
}
}
// Check paddle 2 win
if (pongGame->ball_x < INT_TO_FIXP(0)) {
pongGame->score2 += 1;
pongGame->restart = true;
pongGame->ball_x = INT_TO_FIXP(gameBuff->WIDTH/2);
pongGame->ball_y = INT_TO_FIXP(gameBuff->HEIGHT/2);
updateMinTime(1000);
}
// Check paddle 1 win
if (pongGame->ball_x > INT_TO_FIXP(gameBuff->WIDTH)) {
pongGame->score1 += 1;
pongGame->restart = true;
pongGame->ball_x = INT_TO_FIXP(gameBuff->WIDTH/2);
pongGame->ball_y = INT_TO_FIXP(gameBuff->HEIGHT/2);
updateMinTime(1000);
}
return false;
}
bool pongGameLoop(GameBuff *gameBuff) {
if (pongGame == nullptr) {
pongGame = new PongGame();
pongGame->attract = true;
}
if (pongGame->attract) {
pongAttract(gameBuff);
if (gameBuff->playerKeys.debouncedInput && (gameBuff->playerKeys.select || gameBuff->playerKeys.select)) {
pongGame->attract = false;
pongGame->score1 = 0;
pongGame->score2 = 0;
pongGame->paddle_1_x = INT_TO_FIXP(PADDLE_SPACE);
pongGame->paddle_2_x = INT_TO_FIXP(gameBuff->WIDTH-PADDLE_SPACE-PADDLE_WIDTH);
pongStart(gameBuff);
}
} else {
pongInput(gameBuff);
pongComputer(gameBuff);
pongUpdate(gameBuff);
pongDisplay(gameBuff);
}
if (gameBuff->playerKeys.debouncedInput && (gameBuff->playerKeys.start && gameBuff->playerKeys.select)) {
free(pongGame);
pongGame = nullptr;
return true;
}
return false;
}