/
pong.ino
383 lines (321 loc) · 10.8 KB
/
pong.ino
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
/*
Pong!
Original By Pete Lamonica
modified by duboisvb
updated by James Bruce (http://www.makeuseof.com/tag/author/jbruce)
modified by Steffen Roth (http://www.dreyloft.com)
*/
#include <TVout.h>
#include <fontALL.h>
// Pins
#define WHEEL_ONE_PIN 0 // analog player 1 left
#define WHEEL_TWO_PIN 1 // analog player 2 right
#define WHEEL_THREE_PIN 2 // analog player 3 top
#define WHEEL_FOUR_PIN 3 // analog player 4 four
#define BUTTON_ONE_PIN 2 // digital start button
#define BUZZER_PIN 10 // digital for sound (buzzer)
// fixed paddle definitions
#define PADDLE_LENGTH 14
#define RIGHT_PADDLE_X 124 // (hres - 4)
#define LEFT_PADDLE_X 2
#define TOP_PADDLE_Y 2
#define BOTTOM_PADDLE_Y 94 // (vres - 4)
// game states
#define MAIN_MENU 1 // in menu state
#define GAME_OVER_SCREEN 2 // game over state
#define INFO 3 // info state
#define RUNNING_GAME 0 // in game state - draw the dynamic part of the game
#define BACKGROUND 0 // in game state - draw constants of the game box
#define XHALF 64 // x screen half
#define YHALF 49 // y screen half
// fix positions etc.
#define SCORE_Y 4
#define MAX_Y_VELOCITY 8
#define WIN_SCORE 10
#define DEFAULT_GAME_SPEED 50
#define INCREASE_GAME_SPEED 3
// sounds
#define SOUND_WALL 1500 // frequency for ball hit wall sound
#define SOUND_SCORE 500 // frequency for player scores sound
TVout TV;
// paddels
int rightPaddleY = 0;
int leftPaddleY = 0;
int topPaddleX = 0;
int bottomPaddleX = 0;
int lastContact = 0;
// ball
unsigned char ballX = XHALF;
unsigned char ballY = YHALF;
char ballVolX = 1;
char ballVolY = 1;
// player
bool fourPlayerMode = false;
char leftPlayerScore = 0;
char rightPlayerScore = 0;
char topPlayerScore = 0;
char bottomPlayerScore = 0;
// states
bool buttonStatus = false;
bool firstStart = true;
char state = MAIN_MENU;
int gameSpeed = DEFAULT_GAME_SPEED;
void buttonPress() {
buttonStatus = (digitalRead(BUTTON_ONE_PIN));
if ((buttonStatus == 0) && (state == GAME_OVER_SCREEN))
{
drawMenu();
}
}
void drawGameScreen() {
// draw right paddle
rightPaddleY = ((analogRead(WHEEL_ONE_PIN) / 8) * (98 - PADDLE_LENGTH)) / 128;
TV.draw_line(RIGHT_PADDLE_X + 1, rightPaddleY, RIGHT_PADDLE_X + 1, rightPaddleY + PADDLE_LENGTH, 1);
// draw left paddle
leftPaddleY = ((analogRead(WHEEL_TWO_PIN) / 8) * (98 - PADDLE_LENGTH)) / 128;
TV.draw_line(LEFT_PADDLE_X, leftPaddleY, LEFT_PADDLE_X, leftPaddleY + PADDLE_LENGTH, 1);
if (fourPlayerMode) {
// draw top paddle
topPaddleX = ((analogRead(WHEEL_THREE_PIN) / 8) * (98 - PADDLE_LENGTH)) / 128 + 15;
TV.draw_line(topPaddleX, TOP_PADDLE_Y, topPaddleX + PADDLE_LENGTH, TOP_PADDLE_Y, 1);
// draw bottom paddle
bottomPaddleX = ((analogRead(WHEEL_FOUR_PIN) / 8) * (98 - PADDLE_LENGTH)) / 128 + 15;
TV.draw_line(bottomPaddleX, BOTTOM_PADDLE_Y, bottomPaddleX + PADDLE_LENGTH, BOTTOM_PADDLE_Y, 1);
}
// draw score
if (!fourPlayerMode) {
TV.print_char(58, SCORE_Y, '0' + leftPlayerScore);
TV.print_char(68, SCORE_Y, '0' + rightPlayerScore);
} else {
TV.print_char(10, SCORE_Y, '0' + leftPlayerScore);
TV.print_char(113, SCORE_Y, '0' + topPlayerScore); // X = 113 -> TV.hres() -4 -10 -1 (hres -char width -space -linewidth)
TV.print_char(113, 88, '0' + rightPlayerScore); // Y = 88 -> TV.vres() -4 -6 (vres -char height -SCORE_Y)
TV.print_char(10, 88, '0' + bottomPlayerScore);
}
// draw ball
TV.set_pixel(ballX, ballY, 1);
}
// calculates the current scores
void playerScored(int lastContact) {
if (lastContact == 1) leftPlayerScore++;
if (lastContact == 2) rightPlayerScore++;
if (lastContact == 3) topPlayerScore++;
if (lastContact == 4) bottomPlayerScore++;
// check for game end
if (leftPlayerScore == WIN_SCORE || rightPlayerScore == WIN_SCORE || topPlayerScore == WIN_SCORE || bottomPlayerScore == WIN_SCORE) {
fourPlayerMode = false;
state = GAME_OVER_SCREEN;
}
ballVolX = -ballVolX;
}
// draw static play field
void drawBox() {
TV.clear_screen();
if (!fourPlayerMode) {
// net only needed in 2 player mode
for(int i = 2; i < 98 - 4; i += 6) {
TV.draw_line(XHALF, i, XHALF, i + 3, 1);
}
// lines
TV.draw_line(0, 0, 127, 0, 1); // top
TV.draw_line(0, 97, 127, 97, 1); // bottom
} else {
// lines
TV.draw_line(0,0,16,0,1); // top left
TV.draw_line(112,0,128,0,1); // top right
TV.draw_line(0,97,15,97,1); // bottom left
TV.draw_line(112,97,128,97,1); // bottom right
}
state = RUNNING_GAME;
}
// start screen
void drawMenu() {
// wait for continue
while(buttonStatus){
buttonPress();
}
int pos = 0;
int oldPos = -1; // for first start
while(!buttonStatus){
// menu refresh
oldPos = pos;
pos = analogRead(WHEEL_ONE_PIN); // pos = 0 ... 1023
// prevents unnecessary framedrops
if ((oldPos != pos) && (firstStart || (oldPos <= 500 && pos > 500) || (oldPos >= 500 && pos < 500) || (oldPos <= 1000 && pos > 1000) || (oldPos >= 1000 && pos < 1000))) {
fourPlayerMode = false;
firstStart = false;
drawBox();
// static text
printText(0, 0, 1, 45, 17, "PONG!");
TV.print(21, 85, "PRESS BUTTON TO SELECT");
// main menu game mode selection
printText(pos, 0, 500, 28, 44, "CLASSIC 2P"); // 2 palyer mode
printText(pos, 500, 1000, 28, 55, "CLASSIC 4P"); // 4 player mode
printText(pos, 1000, 1024, 28, 66, "INFO"); // smaller range because of less importance
}
buttonPress();
}
if (pos >= 500 && pos < 1000) {
fourPlayerMode = true;
}
// start game
if (pos < 1000) {
state = BACKGROUND;
} else {
state = INFO;
}
}
// center text for font4x6 -> (Screen Width / 2) - (4 * stringLength) / 2 + 1
// center text for font8x8 -> (Screen Width / 2) - (8 * stringLength) / 2 + 1
// print text function incl function to make active menu point bigger
void printText (int pos, int minEntry, int maxEntry, int xPos, int yPos, char* text){
int corr = 0;
if ( pos >= minEntry && pos < maxEntry ){
TV.select_font(font8x8);
corr = 2;
}
TV.print(xPos, yPos - corr, text);
TV.select_font(font4x6);
}
void setup() {
// setup sound and TV specs
pinMode(BUZZER_PIN, OUTPUT);
TV.begin(_NTSC, 128, 98);
}
void loop() {
if (state == MAIN_MENU) {
buttonPress();
gameSpeed = DEFAULT_GAME_SPEED;
lastContact = 0;
drawBox();
drawMenu();
}
if (state == INFO) {
drawBox();
printText(0, 0, 1, 49, 17, "INFO");
TV.print(41, 44, "(C) 2017 BY\n\n STEFFEN ROTH");
//TV.print(41, 44, "(C) 2017 BY\n\n SEBASTIAN SCHMIDT\n\n AND\n\n STEFFEN ROTH");
// debouncing / prevent douple push
while(buttonStatus) {
buttonPress();
}
while(!buttonStatus) {
buttonPress();
}
state = MAIN_MENU;
}
if (state == BACKGROUND) {
drawBox();
}
// dynamic game content
if (state == RUNNING_GAME) {
ballX += ballVolX;
ballY += ballVolY;
// change if hit top or bottom wall
if (((ballY == 1 || ballY == 97) && !fourPlayerMode) || ((ballY == 1 || ballY >= 97) && fourPlayerMode && (ballX <= 16 || ballX >= 112))){
ballVolY = -ballVolY;
tone(BUZZER_PIN, SOUND_WALL);
}
// test left side for paddle hit
if((ballX <= LEFT_PADDLE_X + 1) && (ballY >= leftPaddleY) && (ballY <= leftPaddleY + PADDLE_LENGTH)){
ballVolX = -ballVolX;
ballVolY += 2 * ((ballY - leftPaddleY) - (PADDLE_LENGTH / 2)) / (PADDLE_LENGTH / 2);
lastContact = 1;
gameSpeed = gameSpeed - INCREASE_GAME_SPEED;
tone(BUZZER_PIN, SOUND_WALL);
}
// test right side for paddle hit
if((ballX - 1 >= RIGHT_PADDLE_X - 1) && (ballY >= rightPaddleY) && (ballY <= rightPaddleY + PADDLE_LENGTH)){
ballVolX = -ballVolX;
ballVolY += 2 * ((ballY - rightPaddleY) - (PADDLE_LENGTH / 2)) / (PADDLE_LENGTH / 2);
lastContact = 2;
gameSpeed = gameSpeed - INCREASE_GAME_SPEED;
tone(BUZZER_PIN, SOUND_WALL);
}
if (fourPlayerMode) {
// test top paddle for hit
if ((ballY <= TOP_PADDLE_Y + 1) && (ballX >= topPaddleX) && (ballX <= topPaddleX + PADDLE_LENGTH)) {
ballVolY = -ballVolY;
ballVolX += 2 * ((ballX - topPaddleX) - (PADDLE_LENGTH / 2)) / (PADDLE_LENGTH / 2);
lastContact = 3;
gameSpeed = gameSpeed - INCREASE_GAME_SPEED;
tone(BUZZER_PIN, SOUND_WALL);
}
// test bottom paddle for hit
if ((ballY >= BOTTOM_PADDLE_Y - 1) && (ballX >= bottomPaddleX) && (ballX <= bottomPaddleX + PADDLE_LENGTH)) {
ballVolY = -ballVolY;
ballVolX += 2 * ((ballX - bottomPaddleX) - (PADDLE_LENGTH / 2)) / (PADDLE_LENGTH / 2);
lastContact = 4;
gameSpeed = gameSpeed - INCREASE_GAME_SPEED;
tone(BUZZER_PIN, SOUND_WALL);
}
}
// limit vertical speed
if (ballVolY > MAX_Y_VELOCITY) {
ballVolY = MAX_Y_VELOCITY;
}
if (ballVolY < -MAX_Y_VELOCITY) {
ballVolY = -MAX_Y_VELOCITY;
}
// limit horizontal speed
if (ballVolX > MAX_Y_VELOCITY / 2) {
ballVolX = MAX_Y_VELOCITY / 2;
}
if (ballVolX < -MAX_Y_VELOCITY / 2) {
ballVolX = -MAX_Y_VELOCITY / 2;
}
// a player scores
if (fourPlayerMode && (ballX <= 0 || ballX >= 128 || ballY <= 0 || ballY >= 98 )) {
playerScored(lastContact); // add point to player
lastContact = 0; // reset if four player
tone(BUZZER_PIN, SOUND_SCORE); // sound
gameSpeed = DEFAULT_GAME_SPEED;
ballX = XHALF; // place ball in screen center
ballY = YHALF;
}
if (!fourPlayerMode && (ballX <= 0 || ballX >= 128)){
if (ballX <= XHALF) { // set correct player for scoring in 2p game mode
lastContact = 2;
} else {
lastContact = 1;
}
playerScored(lastContact);
tone(BUZZER_PIN, SOUND_SCORE); // sound
gameSpeed = DEFAULT_GAME_SPEED;
ballX = XHALF; // place ball in screen center
ballY = YHALF;
}
drawGameScreen();
}
// game over screen
if (state == GAME_OVER_SCREEN) {
drawBox();
buttonStatus = false;
printText(0, 0, 1, 29, 35, "GAME OVER");
if (rightPlayerScore == WIN_SCORE){
TV.print(29, 45, "RIGHT PLAYER WINS!");
} else if (leftPlayerScore == WIN_SCORE){
TV.print(31, 45, "LEFT PLAYER WINS!");
} else if (topPlayerScore == WIN_SCORE){
TV.print(33, 45, "TOP PLAYER WINS!");
} else {
TV.print(27, 45, "BOTTOM PLAYER WINS!");
}
// debouncing / prevent douple push
while(buttonStatus) {
buttonPress();
}
while(!buttonStatus) {
buttonPress();
}
// reset the scores
leftPlayerScore = 0;
rightPlayerScore = 0;
state = MAIN_MENU;
}
// noise cancellation
TV.delay_frame(1);
if (gameSpeed >= 0) {
delay(gameSpeed);
}
}