-
Notifications
You must be signed in to change notification settings - Fork 1
/
PONG.ino
487 lines (382 loc) · 10.1 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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
#include <TFT_eSPI.h>
//create the screen variable from the library
TFT_eSPI tft = TFT_eSPI();
// Set up variables for the cursor and counter. Cursor starts in the middle of the screen.
int paddle1X = 50;
int paddle1Y= 100;
int oldPaddle1Y;
int paddle2X = 430;
int paddle2Y = 100;
int oldPaddle2Y;
float ballDirectionX = 1;
float ballDirectionY = 1;
float ballX, ballY, oldBallX, oldBallY;
int ballSpeed = 1; // lower numbers are faster
int paddleSpeed = 2; // higher is faster
int width = 10;
int height = 40;
int player1=0;
int player2=0;
float time1, time2;
int resetCount = 0;
// initialize variables for menu selection
int menuIndex = 0;
const int MENU_ITEMS = 3;
// Define colours in 4-digit hex
#define BLACK 0x0000
#define BLUE 0x001F
#define RED 0xF800
#define GREEN 0x07E0
#define CYAN 0x07FF
#define MAGENTA 0xF81F
#define YELLOW 0xFFE0
#define WHITE 0xFFFF
// Setting the joystick pins here so we can easily change them
#define JOYSTICK_X_PIN A14
#define JOYSTICK_Y_PIN A15
#define JOYSTICK_BUTTON_PIN 23
#define Button_A_PIN 2
#define Button_B_PIN 3
#define Button_X_PIN 4
#define Button_Y_PIN 5
void setup() {
// put your setup code here, to run once:
// Initalize the screen and set the orientation correctly, then make sure it's clear.
Serial.begin(9600);
tft.begin();
tft.setRotation(1);
tft.fillScreen(BLACK);
//activates push button on joystick
pinMode(JOYSTICK_BUTTON_PIN, INPUT_PULLUP);
// Initialize console
tft.setTextSize(5);
tft.setCursor(180, 70);
tft.println("PONG");
tft.setTextSize(2);
tft.setCursor(165, 110);
tft.setTextColor(GREEN);
tft.println("Made by JOMM");
tft.setTextColor(WHITE);
for (int i = 0; i < 4; i++) {
delay(700);
tft.setCursor(200+(i*20), 160);
tft.println(".");
tone(10,500+(i*150),100);
}
tft.fillScreen(TFT_BLACK);
}
void loop() {
// menu loop
// use joystick to select menu options
if (analogRead(A15) < 100) {
menuIndex--;
if (menuIndex < 0) {
menuIndex = MENU_ITEMS - 1;
}
delay(50);
}
else if (analogRead(A15) > 900) {
menuIndex++;
if (menuIndex >= MENU_ITEMS) {
menuIndex = 0;
}
delay(100);
}
// ------------MENU------------
// display current menu selection
tft.setTextSize(2);
switch(menuIndex) {
case 0:
tft.setCursor(150, 140);
tft.println("> 2 - Player");
tft.setTextColor(BLACK);
tft.setCursor(150, 160);
tft.println(">");
tft.setTextColor(WHITE);
tft.setCursor(150, 160);
tft.println(" 1 - Player");
tft.setTextColor(BLACK);
tft.setCursor(150, 180);
tft.println(">");
tft.setTextColor(WHITE);
tft.setCursor(150, 180);
tft.println(" High Scores");
break;
case 1:
tft.setTextColor(BLACK);
tft.setCursor(150, 140);
tft.println(">");
tft.setTextColor(WHITE);
tft.setCursor(150, 140);
tft.println(" 2 - Player");
tft.setCursor(150, 160);
tft.println("> 1 - Player");
tft.setTextColor(BLACK);
tft.setCursor(150, 180);
tft.println(">");
tft.setTextColor(WHITE);
tft.setCursor(150, 180);
tft.println(" High Scores");
break;
case 2:
tft.setTextColor(BLACK);
tft.setCursor(150, 140);
tft.println(">");
tft.setTextColor(WHITE);
tft.setCursor(150, 140);
tft.println(" 2 - Player");
tft.setTextColor(BLACK);
tft.setCursor(150, 160);
tft.println(">");
tft.setTextColor(WHITE);
tft.setCursor(150, 160);
tft.println(" 1 - Player");
tft.setCursor(150, 180);
tft.println("> High Scores");
break;
}
// wait for user to press button to select menu option
if (digitalRead(JOYSTICK_BUTTON_PIN) == 0) {
switch(menuIndex) {
case 0:
// 2 - Player start game
tft.fillScreen(TFT_BLACK);
tft.setCursor(tft.width()/3, 150);
tft.println("Starting game...");
delay(500);
tft.fillScreen(TFT_BLACK);
tft.setTextSize(10);
tft.setCursor(340, 0);
tft.print(player1);
tft.setCursor(100, 0);
tft.print(player2);
ballMotion();
while(digitalRead(JOYSTICK_BUTTON_PIN) != 0 && (player1 < 5 && player2 < 5))
pongGAME(2);
if (player1 == 5){
tft.setCursor(tft.width()/3, 150);
tft.println("Player 1 wins!");
tone(10,880,1000);
delay(1000);
tone(10,110,250);
delay(250);
tone(10,932,250);
delay(250);
tone(10,1109,250);
delay(250);
tone(10,932,250);
delay(250);
tone(10,880,1000);
}
else if (player2 == 5){
tft.setCursor(tft.width()/3, 150);
tft.println("Player 2 wins!");
tone(10,880,1000);
delay(1000);
tone(10,110,250);
delay(250);
tone(10,932,250);
delay(250);
tone(10,1109,250);
delay(250);
tone(10,932,250);
delay(250);
tone(10,880,1000);
}
player1 = 0;
player1 = 0;
tft.fillScreen(TFT_BLACK);
// need to put code for exit button
break;
case 1:
// 1 - Player start game
tft.fillScreen(TFT_BLACK);
tft.setCursor(tft.width()/3, 150);
tft.println("Starting game...");
delay(500);
tft.fillScreen(TFT_BLACK);
tft.setTextSize(10);
tft.setCursor(340, 0);
tft.print(player1);
tft.setCursor(100, 0);
tft.print(player2);
ballMotion();
while(digitalRead(JOYSTICK_BUTTON_PIN) != 0)
pongGAME(1);
tft.fillScreen(TFT_BLACK);
break;
case 2:
// High Scores
tft.fillScreen(TFT_BLACK);
tft.setCursor(tft.width()/3, 150);
tft.println("High Scores");
tft.println();
tft.println("haha high scores are for losers");
tft.println("JK NYI, press _____ to go back.");
tft.fillScreen(TFT_BLACK);
break;
}
// wait for user to release button
}
delay(50);
}
void pongGAME(int x) {
int myWidth = tft.width();
int myHeight = tft.height();
paddle1Y = paddle1Y + map(analogRead(A15), 0, 1023, -paddleSpeed, paddleSpeed); //paddle 1
if (0 >= paddle1Y)
paddle1Y = 0;
if (280 <= (paddle1Y))
paddle1Y = 280;
if (oldPaddle1Y != paddle1Y) {
tft.drawRect(paddle1X, oldPaddle1Y, width, height, BLACK);
}
else{
delay(1);
}
tft.drawRect(paddle1X, paddle1Y, width, height, WHITE);
oldPaddle1Y = paddle1Y;
/////////////////////////////////////////////////
if (x == 1)
if (ballY > paddle2Y + 20)
paddle2Y += paddleSpeed;
else
paddle2Y -= paddleSpeed;
else
paddle2Y = paddle2Y + map(digitalRead(3), LOW, HIGH, 0, paddleSpeed) + map(digitalRead(4), LOW, HIGH, 0, -paddleSpeed);
if (0 >= paddle2Y)
paddle2Y = 0;
if (280 <= paddle2Y)
paddle2Y = 280;
if (oldPaddle2Y != paddle2Y) {
tft.drawRect(paddle2X, oldPaddle2Y, width, height, BLACK);
}
else{
delay(1);
}
tft.drawRect(paddle2X, paddle2Y, width, height, WHITE);
oldPaddle2Y = paddle2Y;
/////////////////////////////////////////////
// update the ball's position and draw it on screen
if (millis() % ballSpeed < 2) {
moveBall();
}
//2345 digital
if (digitalRead(JOYSTICK_BUTTON_PIN) == 0){
tft.fillScreen(BLACK);
ballMotion();
player1=0;
player2=0;
paddle2Y = 100;
paddle1Y = 100;
tft.setTextSize(10);
tft.setCursor(340, 0);
tft.print(player1);
tft.setCursor(100, 0);
tft.print(player2);
}
if (digitalRead(5) == HIGH) {
tft.setCursor(tft.width()/2, 150);
tft.print("Game Paused!");
delay(500);
while (digitalRead(5) == LOW) {
delay(100);
}
delay(1000);
tft.setTextColor(BLACK);
tft.setCursor(tft.width()/2, 150);
tft.print("Game Paused!");
}
}
void moveBall() {
if (ballX > tft.width()) {
player2 += 1;
point();
ballMotion();
}
if (ballX < 0) {
player1 += 1;
point();
ballMotion();
}
if (ballY > tft.height() || ballY < 0) {
ballDirectionY = -ballDirectionY;
}
if (inPaddle(ballX, ballY, paddle1X, paddle1Y, width, height)) {
ballDirectionX = -ballDirectionX;
tone(10,500,50);
float r = random(1,9);
float sum = 0.5+r/8;
Serial.print(sum);
ballDirectionY = sum*ballDirectionY;
}
if (inPaddle(ballX, ballY, paddle2X, paddle2Y, width, height)) {
ballDirectionX = -ballDirectionX;
tone(10,500,50);
float r = random(1,9);
float sum = 0.5+r/8;
Serial.print(sum);
ballDirectionY = sum*ballDirectionY;
}
ballX += ballDirectionX;
ballY += ballDirectionY;
if (oldBallX != ballX || oldBallY != ballY) {
tft.drawCircle(oldBallX, oldBallY, 5, BLACK);
}
tft.drawCircle(ballX, ballY, 5, WHITE);
oldBallX = ballX;
oldBallY = ballY;
}
void point() {
tone(10, 800, 80);
delay(80);
tone(10, 600, 80);
delay(80);
tone(10, 400, 80);
tft.setTextColor(WHITE);
tft.setTextSize(16);
tft.setCursor(130, 60);
tft.println("POINT!");
delay(1000);
tft.setTextSize(4);
tft.setTextSize(4);
tft.setCursor(0, 120);
tft.println("player 2: ");
tft.setCursor(240, 120);
tft.println("player 1: ");
tft.setTextSize(10);
tft.setCursor(340, 160);
tft.print(player1);
tft.setCursor(100, 160);
tft.print(player2);
delay(1000);
tft.fillScreen(BLACK);
ballDirectionX = 1;
ballDirectionY = 1;
ballX = tft.width()/2;
ballY = tft.height()/2;
paddle2Y = 100;
paddle1Y = 100;
tft.setTextSize(10);
tft.setCursor(340, 0);
tft.print(player1);
tft.setCursor(100, 0);
tft.print(player2);
}
boolean inPaddle(int x, int y, int rectX, int rectY, int rectWidth, int rectHeight) {
boolean result = false;
if ((x >= rectX && x <= (rectX + rectWidth)) &&
(y >= rectY && y <= (rectY + rectHeight))) {
result = true;
}
return result;
}
void ballMotion() {
ballDirectionX =random(2) ? -1:1;
ballDirectionY =random(2) ? -1:1;
if (ballDirectionX > 0) {
ballX = 70;
} else {
ballX = 410;
}
}