-
Notifications
You must be signed in to change notification settings - Fork 44
/
PatternPongClock.h
396 lines (341 loc) · 12.3 KB
/
PatternPongClock.h
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
/*
*
* RGB Pong Clock - Andrew Holmes @pongclock
* https://github.com/pkourany/RGBPongClock
*
* Inspired by, and shamelessly derived from
* Nick's LED Projects
* https://123led.wordpress.com/about/
*
* Modified for SmartMatrix by Jason Coon
*
* Aurora: https://github.com/pixelmatix/aurora
* Copyright (c) 2014 Jason Coon
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#ifndef PatternPongClock_H
#define PatternPongClock_H
#define BAT1_X 2 // Pong left bat x pos (this is where the ball collision occurs, the bat is drawn 1 behind these coords)
#define BAT2_X 28
class PatternPongClock : public Drawable {
private:
float ballpos_x, ballpos_y;
byte erase_x = 10; //holds ball old pos so we can erase it, set to blank area of screen initially.
byte erase_y = 10;
float ballvel_x, ballvel_y;
int bat1_y = 5; //bat starting y positions
int bat2_y = 5;
int bat1_target_y = 5; //bat targets for bats to move to
int bat2_target_y = 5;
byte bat1_update = 1; //flags - set to update bat position
byte bat2_update = 1;
byte bat1miss, bat2miss; //flags set on the minute or hour that trigger the bats to miss the ball, thus upping the score to match the time.
byte restart = 1; //game restart flag - set to 1 initially to setup 1st game
byte mins;
byte hours;
public:
PatternPongClock() {
name = (char *)"PongClock";
}
void start() {
int ampm = clockDigitalShort.twentyFourHour ? 0 : 1;
//update score / time
mins = time.Minute;
hours = time.Hour;
if (hours > 12) {
hours = hours - ampm * 12;
}
if (hours < 1) {
hours = hours + ampm * 12;
}
}
unsigned int drawFrame() {
clockDisplay.readTime();
backgroundLayer.fillScreen(black);
//draw pitch centre line
for (byte i = 0; i < 16; i += 2) {
backgroundLayer.drawPixel(16, i, white);
}
// draw hh:mm seperator colon that blinks once per second
if (time.Second % 2 == 0) {
backgroundLayer.drawPixel(16, 2, gray);
backgroundLayer.drawPixel(16, 4, gray);
}
backgroundLayer.setFont(font3x5);
char buffer[3];
sprintf(buffer, "%02d", hours);
backgroundLayer.drawString(8, 1, white, buffer);
sprintf(buffer, "%02d", mins);
backgroundLayer.drawString(18, 1, white, buffer);
//if restart flag is 1, setup a new game
if (restart) {
//set ball start pos
ballpos_x = 16;
ballpos_y = random(4, 12);
//pick random ball direction
if (random(0, 2) > 0) {
ballvel_x = 1;
}
else {
ballvel_x = -1;
}
if (random(0, 2) > 0) {
ballvel_y = 0.5;
}
else {
ballvel_y = -0.5;
}
//draw bats in initial positions
bat1miss = 0;
bat2miss = 0;
//reset game restart flag
restart = 0;
}
//if coming up to the minute: secs = 59 and mins < 59, flag bat 2 (right side) to miss the return so we inc the minutes score
if (time.Second == 59 && time.Minute < 59) {
bat1miss = 1;
}
// if coming up to the hour: secs = 59 and mins = 59, flag bat 1 (left side) to miss the return, so we inc the hours score.
if (time.Second == 59 && time.Minute == 59) {
bat2miss = 1;
}
//AI - we run 2 sets of 'AI' for each bat to work out where to go to hit the ball back
//very basic AI...
// For each bat, First just tell the bat to move to the height of the ball when we get to a random location.
//for bat1
if (ballpos_x == random(18, 32)) {
bat1_target_y = ballpos_y;
}
//for bat2
if (ballpos_x == random(4, 16)) {
bat2_target_y = ballpos_y;
}
//when the ball is closer to the left bat, run the ball maths to find out where the ball will land
if (ballpos_x == 15 && ballvel_x < 0) {
byte end_ball_y = pong_get_ball_endpoint(ballpos_x, ballpos_y, ballvel_x, ballvel_y);
//if the miss flag is set, then the bat needs to miss the ball when it gets to end_ball_y
if (bat1miss == 1) {
bat1miss = 0;
if (end_ball_y > 8) {
bat1_target_y = random(0, 3);
}
else {
bat1_target_y = 8 + random(0, 3);
}
}
//if the miss flag isn't set, set bat target to ball end point with some randomness so its not always hitting top of bat
else {
bat1_target_y = end_ball_y - random(0, 6);
//check not less than 0
if (bat1_target_y < 0) {
bat1_target_y = 0;
}
if (bat1_target_y > 10) {
bat1_target_y = 10;
}
}
}
//right bat AI
//if positive velocity then predict for right bat - first just match ball height
//when the ball is closer to the right bat, run the ball maths to find out where it will land
if (ballpos_x == 17 && ballvel_x > 0) {
byte end_ball_y = pong_get_ball_endpoint(ballpos_x, ballpos_y, ballvel_x, ballvel_y);
//if flag set to miss, move bat out way of ball
if (bat2miss == 1) {
bat2miss = 0;
//if ball end point above 8 then move bat down, else move it up- so either way it misses
if (end_ball_y > 8) {
bat2_target_y = random(0, 3);
}
else {
bat2_target_y = 8 + random(0, 3);
}
}
else {
//set bat target to ball end point with some randomness
bat2_target_y = end_ball_y - random(0, 6);
//ensure target between 0 and 15
if (bat2_target_y < 0) {
bat2_target_y = 0;
}
if (bat2_target_y > 10) {
bat2_target_y = 10;
}
}
}
//move bat 1 towards target
//if bat y greater than target y move down until hit 0 (dont go any further or bat will move off screen)
if (bat1_y > bat1_target_y && bat1_y > 0) {
bat1_y--;
bat1_update = 1;
}
//if bat y less than target y move up until hit 10 (as bat is 6)
if (bat1_y < bat1_target_y && bat1_y < 10) {
bat1_y++;
bat1_update = 1;
}
//draw bat 1
// if (bat1_update) {
backgroundLayer.fillRectangle(BAT1_X - 1, bat1_y, BAT1_X, bat1_y + 5, white);
// }
//move bat 2 towards target (dont go any further or bat will move off screen)
//if bat y greater than target y move down until hit 0
if (bat2_y > bat2_target_y && bat2_y > 0) {
bat2_y--;
bat2_update = 1;
}
//if bat y less than target y move up until hit max of 10 (as bat is 6)
if (bat2_y < bat2_target_y && bat2_y < 10) {
bat2_y++;
bat2_update = 1;
}
//draw bat2
// if (bat2_update) {
backgroundLayer.fillRectangle(BAT2_X + 1, bat2_y, BAT2_X + 2, bat2_y + 5, white);
// }
//update the ball position using the velocity
ballpos_x = ballpos_x + ballvel_x;
ballpos_y = ballpos_y + ballvel_y;
//check ball collision with top and bottom of screen and reverse the y velocity if either is hit
if (ballpos_y <= 0) {
ballvel_y = ballvel_y * -1;
ballpos_y = 0; //make sure value goes no less that 0
}
if (ballpos_y >= 15) {
ballvel_y = ballvel_y * -1;
ballpos_y = 15; //make sure value goes no more than 15
}
//check for ball collision with bat1. check ballx is same as batx
//and also check if bally lies within width of bat i.e. baty to baty + 6. We can use the exp if(a < b && b < c)
if ((int) ballpos_x <= BAT1_X + 1 && (bat1_y <= (int) ballpos_y && (int) ballpos_y <= bat1_y + 5)) {
ballpos_x = BAT1_X + 1;
//random if bat flicks ball to return it - and therefor changes ball velocity
if (!random(0, 3)) { //not true = no flick - just straight rebound and no change to ball y vel
ballvel_x = ballvel_x * -1;
}
else {
bat1_update = 1;
byte flick; //0 = up, 1 = down.
if (bat1_y > 1 || bat1_y < 8) {
flick = random(0, 2); //pick a random dir to flick - up or down
}
//if bat 1 or 2 away from top only flick down
if (bat1_y <= 1) {
flick = 0; //move bat down 1 or 2 pixels
}
//if bat 1 or 2 away from bottom only flick up
if (bat1_y >= 8) {
flick = 1; //move bat up 1 or 2 pixels
}
switch (flick) {
//flick up
case 0:
bat1_target_y = bat1_target_y + random(1, 3);
ballvel_x = ballvel_x * -1;
if (ballvel_y < 2) {
ballvel_y = ballvel_y + 0.2;
}
break;
//flick down
case 1:
bat1_target_y = bat1_target_y - random(1, 3);
ballvel_x = ballvel_x * -1;
if (ballvel_y > 0.2) {
ballvel_y = ballvel_y - 0.2;
}
break;
}
}
}
//check for ball collision with bat2. check ballx is same as batx
//and also check if bally lies within width of bat i.e. baty to baty + 6. We can use the exp if(a < b && b < c)
if ((int) ballpos_x >= BAT2_X && (bat2_y <= (int) ballpos_y && (int) ballpos_y <= bat2_y + 5)) {
ballpos_x = BAT2_X;
//random if bat flicks ball to return it - and therefor changes ball velocity
if (!random(0, 3)) {
ballvel_x = ballvel_x * -1; //not true = no flick - just straight rebound and no change to ball y vel
}
else {
bat1_update = 1;
byte flick; //0 = up, 1 = down.
if (bat2_y > 1 || bat2_y < 8) {
flick = random(0, 2); //pick a random dir to flick - up or down
}
//if bat 1 or 2 away from top only flick down
if (bat2_y <= 1) {
flick = 0; //move bat up 1 or 2 pixels
}
//if bat 1 or 2 away from bottom only flick up
if (bat2_y >= 8) {
flick = 1; //move bat down 1 or 2 pixels
}
switch (flick) {
//flick up
case 0:
bat2_target_y = bat2_target_y + random(1, 3);
ballvel_x = ballvel_x * -1;
if (ballvel_y < 2) {
ballvel_y = ballvel_y + 0.2;
}
break;
//flick down
case 1:
bat2_target_y = bat2_target_y - random(1, 3);
ballvel_x = ballvel_x * -1;
if (ballvel_y > 0.2) {
ballvel_y = ballvel_y - 0.2;
}
break;
}
}
}
//plot the ball on the screen
byte plot_x = (int) (ballpos_x + 0.5f);
byte plot_y = (int) (ballpos_y + 0.5f);
backgroundLayer.drawPixel(plot_x, plot_y, white);
//check if a bat missed the ball. if it did, reset the game.
if ((int) ballpos_x == 0 || (int) ballpos_x == 32) {
restart = 1;
//update score / time
int ampm = clockDigitalShort.twentyFourHour ? 0 : 1;
mins = time.Minute;
hours = time.Hour;
if (hours > 12) {
hours = hours - ampm * 12;
}
if (hours < 1) {
hours = hours + ampm * 12;
}
}
return 40;
}
byte pong_get_ball_endpoint(float tempballpos_x, float tempballpos_y, float tempballvel_x, float tempballvel_y) {
//run prediction until ball hits bat
while (tempballpos_x > BAT1_X && tempballpos_x < BAT2_X) {
tempballpos_x = tempballpos_x + tempballvel_x;
tempballpos_y = tempballpos_y + tempballvel_y;
//check for collisions with top / bottom
if (tempballpos_y <= 0 || tempballpos_y >= 15) {
tempballvel_y = tempballvel_y * -1;
}
}
return tempballpos_y;
}
};
#endif