-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBlockBreakerGame.ino
497 lines (434 loc) · 17 KB
/
BlockBreakerGame.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
488
489
490
491
492
493
494
495
496
497
/**************************************************************
Block Breaker! The greatest game ever!
Brian Schimke, 2020
**************************************************************/
void blockbreaker_game()
{
//Make sure we have the right font and color
u8g2.setFont(u8g2_font_profont12_mf);
u8g2.setDrawColor(1);
//Draw the borders
u8g2.drawVLine(0, 0, SCREEN_HEIGHT);
u8g2.drawVLine(SCREEN_WIDTH, 0, SCREEN_HEIGHT);
u8g2.drawHLine(0, 0, SCREEN_WIDTH);
if (blockBreaker.messageTimer > 0)
{
switch (blockBreaker.message)
{
case 1:
u8g2.setCursor(5, (SCREEN_HEIGHT / 1.5));
u8g2.print("! N I C E S A V E !");
break;
case 2:
u8g2.setCursor((SCREEN_WIDTH / 2) - 10, SCREEN_HEIGHT / 1.5);
u8g2.print(":(");
break;
}
blockBreaker.messageTimer = blockBreaker.messageTimer - 1;
}
//Add input from the other knob and clear it
player.X += player.Y;
player.Y = 0;
//Make sure we're in range
if (player.X < 0)
{
player.X = 0;
}
if (player.X > (SCREEN_WIDTH - blockBreaker.paddleWidth))
{
player.X = SCREEN_WIDTH - blockBreaker.paddleWidth;
}
//Draw the blocks
for (int i = 0; i < 70; i++)
{
if (blockBreaker.block[i].Health == 0)
{
//Draw nothing in there is no health
}
else if (blockBreaker.block[i].Health < 254)
{
u8g2.drawBox(blockBreaker.block[i].X, blockBreaker.block[i].Y, blockBreaker.blockWidth, blockBreaker.blockHeight);
//u8g2.drawBox(player.X, SCREEN_HEIGHT - blockBreaker.paddleHeight, blockBreaker.paddleWidth, 4);
}
else if (blockBreaker.block[i].Health == 255)
{
u8g2.drawFrame(blockBreaker.block[i].X, blockBreaker.block[i].Y, blockBreaker.blockWidth, blockBreaker.blockHeight);
}
}
//Draw the paddle
u8g2.drawBox(player.X, SCREEN_HEIGHT - blockBreaker.paddleHeight, blockBreaker.paddleWidth, 4);
//Are we running the game?
if (blockBreaker.running == 1)
{
//Change ball position by velocities
blockBreaker.ballX += blockBreaker.ballXvel;
blockBreaker.ballY += blockBreaker.ballYvel;
}
else
{
u8g2.setCursor(SCREEN_WIDTH / 7, (SCREEN_HEIGHT / 1.3));
//u8g2.print("! C L I C K !");
//Ball on paddle
blockBreaker.ballY = SCREEN_HEIGHT - (blockBreaker.paddleHeight + (blockBreaker.ballWidth / 2) + 1);
blockBreaker.ballX = player.X + (blockBreaker.paddleWidth /2) + 5; //+5 is small left offset
//A we waiting for a click to start?
if ((knob1.click == 1) || (knob2.click == 1))
{
blockBreaker.running = 1;
}
}
//Draw the ball
u8g2.drawDisc(int(blockBreaker.ballX), int(blockBreaker.ballY), (blockBreaker.ballWidth / 2), U8G2_DRAW_ALL);
byte blockGone = 0;
int blockCount = 0;
//Did we hit a block? Check every block
for (int i = 0; i < 70; i++)
{
//Is the block "alive"?
if (blockBreaker.block[i].Health != 0)
{
//There is a block here!
blockGone = 0;
//What direction are we going?
//Going Up
if ((blockBreaker.ballYvel <= 0) && (blockGone == 0))
{
//Check Width (X) match, Left Side && Right Side of block
if ((blockBreaker.ballX + (blockBreaker.ballWidth / 2) >= blockBreaker.block[i].X) && (blockBreaker.ballX - (blockBreaker.ballWidth / 2) <= blockBreaker.block[i].X + blockBreaker.blockWidth))
{
//Check Height (Y) match, Bottom && Top
if ((blockBreaker.ballY - (blockBreaker.ballWidth / 2) >= blockBreaker.block[i].Y + (blockBreaker.blockHeight / 2)) && (blockBreaker.ballY <= blockBreaker.block[i].Y + blockBreaker.blockHeight + (blockBreaker.ballWidth / 2)))
{
//Bounce the ball
blockBreaker.ballYvel *= -1;
//Skip to the end of block checking as this one is gone already
blockGone = 1;
if (blockBreaker.block[i].Health != 255)
{
blockBreaker.block[i].Health -= 1;
}
//Blink the Status LED and give the player a point
if (blockBreaker.block[i].Health == 0)
{
blockBreaker.score++;
ledcWrite(STATUS_LED, 4095);
}
}
}
}
//Going Right
if ((blockBreaker.ballXvel > 0) && (blockGone == 0))
{
if ((blockBreaker.ballY - (blockBreaker.ballWidth / 2) >= blockBreaker.block[i].Y) && (blockBreaker.ballY - (blockBreaker.ballWidth / 2) <= blockBreaker.block[i].Y + blockBreaker.blockHeight))
{
if ((blockBreaker.ballX + (blockBreaker.ballWidth / 2) >= blockBreaker.block[i].X) && (blockBreaker.ballX <= blockBreaker.block[i].X + (blockBreaker.blockWidth / 2)))
{
blockBreaker.ballXvel *= -1;
//Skip to the end of block checking as this one is gone already
blockGone = 1;
if (blockBreaker.block[i].Health != 255)
{
blockBreaker.block[i].Health -= 1;
}
//Blink the Status LED and give the player a point
if (blockBreaker.block[i].Health == 0)
{
blockBreaker.score++;
ledcWrite(STATUS_LED, 4095);
}
}
}
}
//Going Left
if ((blockBreaker.ballXvel < 0) && (blockGone == 0))
{
if ((blockBreaker.ballY + (blockBreaker.ballWidth / 2) >= blockBreaker.block[i].Y) && (blockBreaker.ballY - (blockBreaker.ballWidth / 2) <= blockBreaker.block[i].Y + blockBreaker.blockHeight))
{
if ((blockBreaker.ballX - (blockBreaker.ballWidth / 2) <= blockBreaker.block[i].X + blockBreaker.blockWidth) && (blockBreaker.ballX >= blockBreaker.block[i].X - (blockBreaker.blockWidth / 2)))
{
blockBreaker.ballXvel *= -1;
//Skip to the end of block checking as this one is gone already
blockGone = 1;
if (blockBreaker.block[i].Health != 255)
{
blockBreaker.block[i].Health -= 1;
}
//Blink the Status LED and give the player a point
if (blockBreaker.block[i].Health == 0)
{
blockBreaker.score++;
ledcWrite(STATUS_LED, 4095);
}
}
}
}
//Going Down
if ((blockBreaker.ballYvel >= 0) && (blockGone == 0))
{
if ((blockBreaker.ballX + (blockBreaker.ballWidth / 2) >= blockBreaker.block[i].X) && (blockBreaker.ballX - (blockBreaker.ballWidth / 2) <= blockBreaker.block[i].X + blockBreaker.blockWidth))
{
if ((blockBreaker.ballY + (blockBreaker.ballWidth / 2) >= blockBreaker.block[i].Y) && (blockBreaker.ballY - (blockBreaker.ballWidth / 2) <= blockBreaker.block[i].Y + (blockBreaker.blockHeight / 3)))
{
blockBreaker.ballYvel *= -1;
//Skip to the end of block checking as this one is gone already
blockGone = 1;
if (blockBreaker.block[i].Health != 255)
{
blockBreaker.block[i].Health -= 1;
}
//Blink the Status LED and give the player a point
if (blockBreaker.block[i].Health == 0)
{
blockBreaker.score++;
ledcWrite(STATUS_LED, 4095);
}
}
}
}
//Add to Blocks Left count if it's not a wall
if (blockBreaker.block[i].Health != 255)
{
blockCount += 1;
}
}
}
if (blockCount == 0)
{
//Advance level
blockBreaker.level ++;
u8g2.setCursor(4, SCREEN_HEIGHT - 8);
u8g2.print(" N E X T L E V E L ");
u8g2.setCursor(SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2);
u8g2.print(blockBreaker.level);
u8g2.sendBuffer();
delay(5000);
//Not running, give user a break
blockBreaker.running = 0;
//Reset blocks with new level layout
blockbreaker_setBlocks(blockBreaker.level);
}
//Did we hit the wall left wall?
if ((blockBreaker.ballX <= (blockBreaker.ballWidth / 2)) && blockBreaker.ballXvel < 0)
{
blockBreaker.ballXvel *= -1;
}
//Did we hit the right wall?
if ((blockBreaker.ballX >= (SCREEN_WIDTH - (blockBreaker.ballWidth / 2))) && blockBreaker.ballXvel > 0)
{
blockBreaker.ballXvel *= -1;
}
//Did we hit the ceiling?
if ((blockBreaker.ballY - (blockBreaker.ballWidth / 2)) <= 0)
{
blockBreaker.ballYvel *= -1;
}
//Did we hit the paddle height?
if (blockBreaker.ballY == (SCREEN_HEIGHT - blockBreaker.paddleHeight - (blockBreaker.ballWidth / 2)))
{
if (((blockBreaker.ballX + (blockBreaker.ballWidth / 2)) < player.X) || (blockBreaker.ballX > (player.X + blockBreaker.paddleWidth + (blockBreaker.ballWidth / 2))))
{
}
else if (blockBreaker.ballYvel > 0)
{
int paddleAngle = (player.X + (blockBreaker.paddleWidth / 2)) - blockBreaker.ballX;
if (paddleAngle <=-5)
{
blockBreaker.ballXvel = 2;
blockBreaker.ballYvel = -.5;
}
if (paddleAngle >=-4 && paddleAngle <=-3)
{
blockBreaker.ballXvel = 1;
blockBreaker.ballYvel = -1;
}
if (paddleAngle >=-2 && paddleAngle <=-1)
{
blockBreaker.ballXvel = .5;
blockBreaker.ballYvel = -1.5;
}
if (paddleAngle >=0 && paddleAngle <=0)
{
blockBreaker.ballXvel = 0;
blockBreaker.ballYvel = -1;
}
if (paddleAngle >=1 && paddleAngle <=2)
{
blockBreaker.ballXvel = -.5;
blockBreaker.ballYvel = -1.5;
}
if (paddleAngle >=3 && paddleAngle <=4)
{
blockBreaker.ballXvel = -1;
blockBreaker.ballYvel = -1;
}
if (paddleAngle >=5)
{
blockBreaker.ballXvel = -2;
blockBreaker.ballYvel = -.5;
}
}
}
//2nd Chance - Did we hit the paddle height?
if (blockBreaker.ballY == (SCREEN_HEIGHT - blockBreaker.paddleHeight - (blockBreaker.ballWidth / 2) + 2))
{
if (((blockBreaker.ballX + (blockBreaker.ballWidth / 2)) < player.X) || (blockBreaker.ballX > (player.X + blockBreaker.paddleWidth + (blockBreaker.ballWidth / 2))))
{
//This is game over condition area
blockBreaker.message = 2;
blockBreaker.messageTimer = 24;
}
else
{
blockBreaker.ballYvel *= -1; //Hit the paddle! Bounce the ball
blockBreaker.message = 1;
blockBreaker.messageTimer = 24;
}
}
if ((blockBreaker.ballY - 10) > SCREEN_HEIGHT)
{
//Place the cursor and draw some game over message
u8g2.setCursor(4, SCREEN_HEIGHT - 8);
u8g2.print("G A M E O V E R !!!");
//Draw the ?
u8g2.setCursor(35, (SCREEN_HEIGHT / 1.5));
u8g2.print(": ");
u8g2.setCursor(75, (SCREEN_HEIGHT / 1.5));
u8g2.print(blockBreaker.score);
//Send display out
u8g2.sendBuffer();
delay(5000);
endGameMode();
}
}
void blockbreaker_setBlocks(byte level)
{
for (int i = 0; i < 70; i++)
{
blockBreaker.block[i].Health = 0;
}
//Reset ball velocity
blockBreaker.ballXvel = 1;
blockBreaker.ballYvel = -1;
Serial.println("Block [i]: X, Y, Health");
switch (level)
{
case 0:
{
Serial.println("**** LEVEL 1 ****");
blockBreaker.blockWidth = 20;
blockBreaker.blockHeight = 10;
blockBreaker.paddleWidth = 25;
blockBreaker.ballWidth = 6;
for (int i = 0; i < 3; i++)
{
//for (int ii = 0; ii < 10; ii++)
for (int ii = 0; ii < 5; ii++)
{
byte iii = (i * 10) + ii;
blockBreaker.block[iii].X = ii * (blockBreaker.blockWidth + 4) + 5;
blockBreaker.block[iii].Y = i * (blockBreaker.blockHeight + 4) + 3;
blockBreaker.block[iii].Health = 1;
/*
if (iii % 2)
{
blockBreaker.block[iii].Health = 255;
}
else
{
blockBreaker.block[iii].Health = 1;
}
*/
Serial.print("Block ");
Serial.print(iii);
Serial.print(": ");
Serial.print(blockBreaker.block[iii].X);
Serial.print(",");
Serial.print(blockBreaker.block[iii].Y);
Serial.print(",");
Serial.println(blockBreaker.block[iii].Health);
}
}
}
break;
case 1:
{
blockBreaker.blockWidth = 15;
blockBreaker.blockHeight = 8;
blockBreaker.paddleWidth = 16;
blockBreaker.ballWidth = 4;
for (int i = 0; i < 3; i++)
{
for (int ii = 0; ii < 7; ii++)
{
blockBreaker.block[(i * 10) + ii].X = ii * (blockBreaker.blockWidth + 2) + 5;
blockBreaker.block[(i * 10) + ii].Y = i * (blockBreaker.blockHeight + 2) + 3;
blockBreaker.block[(i * 10) + ii].Health = 1;
}
}
}
break;
case 2:
{
blockBreaker.blockWidth = 10;
blockBreaker.blockHeight = 5;
blockBreaker.paddleWidth = 16;
blockBreaker.ballWidth = 4;
for (int i = 0; i < 3; i++)
{
for (int ii = 0; ii < 10; ii++)
{
blockBreaker.block[(i * 10) + ii].X = ii * (blockBreaker.blockWidth + 2) + 5;
blockBreaker.block[(i * 10) + ii].Y = i * (blockBreaker.blockHeight + 2) + 3;
blockBreaker.block[(i * 10) + ii].Health = 1;
}
}
}
break;
case 3:
{
for (int i = 0; i < 5; i++)
{
for (int ii = 0; ii < 10; ii++)
{
blockBreaker.block[(i * 10) + ii].X = ii * (blockBreaker.blockWidth + 2) + 5;
blockBreaker.block[(i * 10) + ii].Y = i * (blockBreaker.blockHeight + 2) + 3;
blockBreaker.block[(i * 10) + ii].Health = 1;
}
}
}
break;
case 4:
{
for (int i = 0; i < 7; i++)
{
for (int ii = 0; ii < 10; ii++)
{
blockBreaker.block[(i * 10) + ii].X = ii * (blockBreaker.blockWidth + 2) + 5;
blockBreaker.block[(i * 10) + ii].Y = i * (blockBreaker.blockHeight + 2) + 3;
blockBreaker.block[(i * 10) + ii].Health = 1;
}
}
}
break;
}
}
void blockbreaker_reset()
{
//Not running
blockBreaker.running = 0;
//Current is back to 0
blockBreaker.score = 0;
blockBreaker.message = 0;
blockBreaker.messageTimer = 0;
blockBreaker.ballXvel = 1;
blockBreaker.ballYvel = -1;
//player. to the middle of the screen
player.X = SCREEN_WIDTH / 2;
player.Y = 0;
//And at the bottom
blockBreaker.paddleHeight = 2;
//Level back to 0
blockBreaker.level = 0;
//Reset block layout
blockbreaker_setBlocks(blockBreaker.level);
}