forked from the-kenny/WiserClock
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFacepong.cpp
429 lines (363 loc) · 13.4 KB
/
Facepong.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
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
/*
*********************************************************************************************************
* AppPong.cpp, part of Wise Clock 3 library, by FlorinC (http://timewitharduino.blogspot.com/);
*
* Code copied and adapted from Nick (http://123led.wordpress.com/about/);
*
* Apr 07, 2012 (mcm) parameterized colors
* Apr 10, 2012 (mcm) Honor 12hr/24hr flag
* Apr 12, 2012 (mcm) parameterized for X_MAX, Y_MAX, BAT_HEIGHT
* Apr 13, 2012 (mcm) removed floating point code in favor of scaled integers
* Apr 14, 2012 (mcm) fixed prediction code, flick code
*********************************************************************************************************
*/
#include "FacePong.h"
#include "HT1632.h"
// (fc) dependent on the screen size;
#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 (X_MAX-3) // Pong right bat x pos (this is where the ball collision occurs, the bat is drawn 1 behind these coords)
#define BAT_HEIGHT 5 // height of bat
#define COLOR_SCORE RED
#define COLOR_PITCH GREEN
#define COLOR_BAT ORANGE
#define COLOR_BALL GREEN
void PongFace::init()
{
erase_x = 10; //holds ball old pos so we can erase it, set to blank area of screen initially.
erase_y = 10;
bat1_y = 5; //bat starting y positions
bat2_y = 5;
bat1_target_y = 5; //bat targets for bats to move to
bat2_target_y = 5;
bat1_update = 1; //flags - set to update bat position
bat2_update = 1;
restart = 1; //game restart flag - set to 1 initially to setup 1st game
ht1632_clear();
// draw pitch centre line;
for(int i = 0; i < Y_MAX; i+=2)
ht1632_plot(X_MAX/2, i, COLOR_PITCH);
}
//*****************************************************************************************************
byte PongFace::pong_get_ball_endpoint(int tempballpos_x, int tempballpos_y, int tempballvel_x, int 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 = 0;
tempballvel_y = tempballvel_y * -1;
} else if (tempballpos_y >= ((Y_MAX-1) * 256)){
tempballpos_y = (Y_MAX-1) * 256;
tempballvel_y = tempballvel_y * -1;
}
}
return tempballpos_y >> 8;
}
//*****************************************************************************************************
// this function is called as part of the main loop;
//
void PongFace::updateDisplay()
{
//if restart flag is 1, setup a new game
if (restart)
{
//erase ball pos
plot (erase_x, erase_y, 0);
/*
char buffer[3];
itoa(hours,buffer,10);
// fix - as otherwise if num has leading zero, e.g. "03" hours, itoa coverts this to chars with space "3 ".
if (hours < 10) {
buffer[1] = buffer[0];
buffer[0] = '0';
}
*/
ht1632_putSmallChar( (X_MAX/2)-11, -1,('0' + hours/10) ,COLOR_SCORE);
ht1632_putSmallChar((X_MAX/2)- 6, -1,('0' + hours % 10) , COLOR_SCORE);
/*
itoa(minutes,buffer,10);
if (minutes < 10) {
buffer[1] = buffer[0];
buffer[0] = '0';
}
*/
ht1632_putSmallChar((X_MAX/2)+ 1, -1, ('0' + minutes / 10), COLOR_SCORE);
ht1632_putSmallChar((X_MAX/2)+ 6, -1, ('0' + minutes % 10), COLOR_SCORE);
// set ball start pos
ballpos_x = X_MAX/2;
ballpos_y = random (4,Y_MAX-4) << 8;
// pick random ball direction
if (random(0,2) > 0) {
ballvel_x = 1;
}
else {
ballvel_x = -1;
}
if (random(0,2) > 0) {
ballvel_y = random(100, 150);
}
else {
ballvel_y = -random(100, 150);
}
// draw bats in initial positions
bat1miss = 0;
bat2miss = 0;
// reset game restart flag
restart = 0;
//short wait
delay(1500);
}
/*
//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 (seconds == 59 && minutes < 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 (seconds == 59 && minutes == 59){
bat2miss = 1;
}
*/
if (seconds == 59) {
if (minutes == 59) {
bat2miss = 1;
}
else {
bat1miss = 1;
}
//AI - we run 2 sets of 'AI' for each bat to work out where to go to hit the ball back
// Left bat AI
// when the ball is closer to the left bat and heading that way, run the ball maths to find out where the ball will land
if (ballpos_x == ((X_MAX/2)-1) && 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){
if (end_ball_y > (Y_MAX/2)){
bat1_target_y = end_ball_y - BAT_HEIGHT - random (1,3); // near miss appearent low part of bat
} else {
bat1_target_y = end_ball_y + random (2,4); // near miss appearent high part of bat
}
} else {
//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
bat1_target_y = end_ball_y - random(0,BAT_HEIGHT);
//check not less than 0
if (bat1_target_y < 0){
bat1_target_y = 0;
} else if (bat1_target_y > (Y_MAX - BAT_HEIGHT)){
bat1_target_y = (Y_MAX - BAT_HEIGHT);
}
}
}
// When further away, just tell the bat to move to the height of the ball when we get to a random location.
else if (ballpos_x == random(X_MAX/2 + 4, X_MAX/2 + 11)){// && ballvel_x < 0) {
bat1_target_y = ballpos_y >> 8;
}
// Right bat AI
// when the ball is closer to the right bat and heading that way, run the ball maths to find out where the ball will land
if (ballpos_x == ((X_MAX/2)+1) && 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){
//if ball end point above 8 then move bat down, else move it up- so either way it misses
if (end_ball_y > (Y_MAX/2)){
bat2_target_y = end_ball_y - BAT_HEIGHT - random (1,3); // near miss appearent low part of bat
} else {
bat2_target_y = end_ball_y + random (2,4); // near miss appearent high part of bat
}
} else {
//set bat target to ball end point with some randomness
bat2_target_y = end_ball_y - random (0,BAT_HEIGHT);
//ensure target between 0 and 15
if (bat2_target_y < 0){
bat2_target_y = 0;
} else if (bat2_target_y > (Y_MAX - BAT_HEIGHT)){
bat2_target_y = (Y_MAX - BAT_HEIGHT);
}
}
}
// When further away, just tell the bat to move to the height of the ball when we get to a random location.
else if (ballpos_x == random(X_MAX/2 - 11, X_MAX/2 - 4)){// && ballvel_x > 0) {
bat2_target_y = ballpos_y >> 8;
}
//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) {
#if Y_MAX > X_MAX/2
if ((ballvel_x < 0) && (ballpos_x - BAT1_X) < (bat1_y - bat1_target_y))
bat1_y -= 2;
else
#endif
bat1_y--;
if (bat1_y < 0) bat1_y = 0;
bat1_update = 1;
}
//if bat y less than target y move up until hit 10 (as bat is 6)
else if (bat1_y < bat1_target_y) {
#if Y_MAX > X_MAX/2
if ((ballvel_x < 0) && (ballpos_x - BAT1_X) < (bat1_target_y - bat1_y))
bat1_y += 2;
else
#endif
bat1_y++;
if (bat1_y > (Y_MAX - BAT_HEIGHT))
bat1_y = (Y_MAX - BAT_HEIGHT);
bat1_update = 1;
}
//draw bat 1
if (bat1_update){
for (byte i = 0; i < Y_MAX; i++){
if (i - bat1_y < BAT_HEIGHT && i - bat1_y > -1){
plot(BAT1_X-1, i , COLOR_BAT);
plot(BAT1_X-2, i , COLOR_BAT);
} else {
plot(BAT1_X-1, i , 0);
plot(BAT1_X-2, i , 0);
}
}
}
//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) {
#if Y_MAX > X_MAX/2
if ((ballvel_x > 0) && (BAT2_X - ballpos_x) < (bat2_y - bat2_target_y))
bat2_y -= 2;
else
#endif
bat2_y--;
if (bat2_y < 0) bat2_y = 0;
bat2_update = 1;
}
//if bat y less than target y move up until hit max of 10 (as bat is 6)
else if (bat2_y < bat2_target_y) {
#if Y_MAX > X_MAX/2
if ((ballvel_x > 0) && (BAT2_X - ballpos_x) < (bat2_target_y - bat2_y))
bat2_y += 2;
else
#endif
bat2_y++;
if (bat2_y > (Y_MAX - BAT_HEIGHT))
bat2_y = (Y_MAX - BAT_HEIGHT);
bat2_update = 1;
}
//draw bat2
if (bat2_update){
for (byte i = 0; i < Y_MAX; i++){
if (i - bat2_y < BAT_HEIGHT && i - bat2_y > -1){
plot(BAT2_X+1, i , COLOR_BAT);
plot(BAT2_X+2, i , COLOR_BAT);
}
else {
plot(BAT2_X+1, i , 0);
plot(BAT2_X+2, i , 0);
}
}
}
//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
}
else if (ballpos_y >= ((Y_MAX-1) * 256)){
ballvel_y = ballvel_y * -1;
ballpos_y = (Y_MAX-1) * 256; //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)
// Update code to reduce size Quentin Smart Mar 12
if (ballpos_x == BAT1_X && (bat1_y <= (ballpos_y >> 8) && (ballpos_y >> 8) <= bat1_y + (BAT_HEIGHT-1)) ) {
ballvel_x = ballvel_x * -1; // Happens irrespective
//random if bat flicks ball to return it - and therefor changes ball velocity
if(random(0,3)) { //true = flick
bat1_update = 1;
byte flick = 0; //0 = up, 1 = down.
// if (bat1_y > 1 || bat1_y < Y_MAX/2){
// 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 is 0 anyway.. //move bat down 1 or 2 pixels
// flick = 0;
//if bat 1 or 2 away from bottom only flick up
if (bat1_y >= (Y_MAX-BAT_HEIGHT-1) ){
flick = 1; //move bat up 1 or 2 pixels
}
if (flick == 0) {
//flick up
bat1_target_y += random(1,3);
if (ballvel_y < 0 && ballvel_y > -500)
ballvel_y -= random(30, 51);
else if (ballvel_y >= 0 && ballvel_y < 500)
ballvel_y += random(30, 51);
}
//flick down
else {
bat1_target_y -= random(1,3);
if (ballvel_y < -50)
ballvel_y += random(30, 51);
else if (ballvel_y > 50)
ballvel_y -= random(30, 51);
}
}
}
}
//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 (ballpos_x == BAT2_X && (bat2_y <= (ballpos_y >> 8) && (ballpos_y >> 8) <= bat2_y + (BAT_HEIGHT-1)) ) {
ballvel_x = ballvel_x * -1; // Happens irrespective
//random if bat flicks ball to return it - and therefor changes ball velocity
if(random(0,3)) { // true flick -
bat2_update = 1;
byte flick = 0; //0 = up, 1 = down.
if (bat2_y > 1 || bat2_y < (Y_MAX-BAT_HEIGHT)){
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 >= Y_MAX/2 ){
flick = 1; //move bat down 1 or 2 pixels
}
if (flick == 0) {
//flick up
bat2_target_y += random(1,3);
if (ballvel_y < 0 && ballvel_y > -500)
ballvel_y -= random(30, 51);
else if (ballvel_y >= 0 && ballvel_y < 500)
ballvel_y += random(30, 51);
}
//flick down
else {
bat2_target_y -= random(1,3);
if (ballvel_y < -50)
ballvel_y += random(30, 51);
else if (ballvel_y > 50)
ballvel_y -= random(30, 51);
}
}
}
//plot the ball on the screen
byte plot_x = ballpos_x;
byte plot_y = ballpos_y >> 8;
//take a snapshot of all the led states
snapshot_shadowram();
//set prev. pixel back to old color
plot(erase_x, erase_y, oldBallColor);
//Get current color of the pixel the ball will be drawn to
oldBallColor = get_shadowram(plot_x, plot_y);
//Draw the ball
plot(plot_x, plot_y, ORANGE);
//reset erase to new pos
erase_x = plot_x;
erase_y = plot_y;
//check if a bat missed the ball. if it did, reset the game.
if (ballpos_x <= (BAT1_X-2) || ballpos_x >= (BAT2_X+2)){
restart = 1;
}
delay(35);
// fade_down();
}