-
Notifications
You must be signed in to change notification settings - Fork 2
/
tetris.cpp
585 lines (534 loc) · 17 KB
/
tetris.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
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
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
/**
* @file tetris.cpp
* @author techniccontroller (mail[at]techniccontroller.com)
* @brief Class implementation for tetris game
* @version 0.1
* @date 2022-03-05
*
* @copyright Copyright (c) 2022
*
* main tetris code originally written by Klaas De Craemer, Ing. David Hrbaty
*
*/
#include "tetris.h"
Tetris::Tetris(){
}
/**
* @brief Construct a new Tetris:: Tetris object
*
* @param myledmatrix pointer to LEDMatrix object, need to provide gridAddPixel(x, y, col), drawOnMatrix(), gridFlush() and printNumber(x,y,n,col)
* @param mylogger pointer to UDPLogger object, need to provide a function logString(message)
*/
Tetris::Tetris(LEDMatrix *myledmatrix, UDPLogger *mylogger){
_logger = mylogger;
_ledmatrix = myledmatrix;
_gameStatet = GAME_STATE_READYt;
}
/**
* @brief Run main loop for one cycle
*
*/
void Tetris::loopCycle(){
switch (_gameStatet) {
case GAME_STATE_READYt:
break;
case GAME_STATE_INITt:
tetrisInit();
break;
case GAME_STATE_RUNNINGt:
//If brick is still "on the loose", then move it down by one
if (_activeBrick.enabled) {
// move faster down when allow drop
if (_allowdrop) {
if (millis() > _droptime + 50) {
_droptime = millis();
shiftActiveBrick(DIR_DOWN);
printField();
}
}
// move down with regular speed
if ((millis() - _prevUpdateTime) > (_brickSpeed * _speedtetris / 100)) {
_prevUpdateTime = millis();
shiftActiveBrick(DIR_DOWN);
printField();
}
}
else {
_allowdrop = false;
//Active brick has "crashed", check for full lines
//and create new brick at top of field
checkFullLines();
newActiveBrick();
_prevUpdateTime = millis();//Reset update time to avoid brick dropping two spaces
}
break;
case GAME_STATE_PAUSEDt:
break;
case GAME_STATE_ENDt:
// at game end show all bricks on field in red color for 1.5 seconds, then show score
if (_tetrisGameOver == true) {
_tetrisGameOver = false;
(*_logger).logString("Tetris: end");
everythingRed();
_tetrisshowscore = millis();
}
if (millis() > (_tetrisshowscore + RED_END_TIME)) {
resetLEDs();
_score = _nbRowsTotal;
showscore();
_gameStatet = GAME_STATE_READYt;
}
break;
}
}
/**
* @brief Trigger control: START (& restart)
*
*/
void Tetris::ctrlStart() {
if (millis() > _lastButtonClick + DEBOUNCE_TIME)
{
_lastButtonClick = millis();
_gameStatet = GAME_STATE_INITt;
}
}
/**
* @brief Trigger control: PAUSE/PLAY
*
*/
void Tetris::ctrlPlayPause() {
if (millis() > _lastButtonClick + DEBOUNCE_TIME)
{
_lastButtonClick = millis();
if (_gameStatet == GAME_STATE_PAUSEDt) {
(*_logger).logString("Tetris: continue");
_gameStatet = GAME_STATE_RUNNINGt;
} else if (_gameStatet == GAME_STATE_RUNNINGt) {
(*_logger).logString("Tetris: pause");
_gameStatet = GAME_STATE_PAUSEDt;
}
}
}
/**
* @brief Trigger control: RIGHT
*
*/
void Tetris::ctrlRight() {
if (millis() > _lastButtonClick + DEBOUNCE_TIME && _gameStatet == GAME_STATE_RUNNINGt)
{
_lastButtonClick = millis();
shiftActiveBrick(DIR_RIGHT);
printField();
}
}
/**
* @brief Trigger control: LEFT
*
*/
void Tetris::ctrlLeft() {
if (millis() > _lastButtonClick + DEBOUNCE_TIME && _gameStatet == GAME_STATE_RUNNINGt)
{
_lastButtonClick = millis();
shiftActiveBrick(DIR_LEFT);
printField();
}
}
/**
* @brief Trigger control: UP (rotate)
*
*/
void Tetris::ctrlUp() {
if (millis() > _lastButtonClick + DEBOUNCE_TIME && _gameStatet == GAME_STATE_RUNNINGt)
{
_lastButtonClick = millis();
rotateActiveBrick();
printField();
}
}
/**
* @brief Trigger control: DOWN (drop)
*
*/
void Tetris::ctrlDown() {
// longer debounce time, to prevent immediate drop
if (millis() > _lastButtonClickr + DEBOUNCE_TIME*5 && _gameStatet == GAME_STATE_RUNNINGt)
{
_allowdrop = true;
_lastButtonClickr = millis();
}
}
/**
* @brief Set game speed
*
* @param i new speed value
*/
void Tetris::setSpeed(int32_t i) {
(*_logger).logString("setSpeed: " + String(i));
_speedtetris = -10 * i + 150;
}
/**
* @brief Clear the led matrix (turn all leds off)
*
*/
void Tetris::resetLEDs()
{
(*_ledmatrix).gridFlush();
(*_ledmatrix).drawOnMatrixInstant();
}
/**
* @brief Initialize the tetris game
*
*/
void Tetris::tetrisInit() {
(*_logger).logString("Tetris: init");
clearField();
_brickSpeed = INIT_SPEED;
_nbRowsThisLevel = 0;
_nbRowsTotal = 0;
_tetrisGameOver = false;
newActiveBrick();
_prevUpdateTime = millis();
_gameStatet = GAME_STATE_RUNNINGt;
}
/**
* @brief Draw current field representation to led matrix
*
*/
void Tetris::printField() {
int x, y;
for (x = 0; x < WIDTH; x++) {
for (y = 0; y < HEIGHT; y++) {
uint8_t activeBrickPix = 0;
if (_activeBrick.enabled) { //Only draw brick if it is enabled
//Now check if brick is "in view"
if ((x >= _activeBrick.xpos) && (x < (_activeBrick.xpos + (_activeBrick.siz)))
&& (y >= _activeBrick.ypos) && (y < (_activeBrick.ypos + (_activeBrick.siz)))) {
activeBrickPix = (_activeBrick.pix)[x - _activeBrick.xpos][y - _activeBrick.ypos];
}
}
if (_field.pix[x][y] == 1) {
(*_ledmatrix).gridAddPixel(x, y, _field.color[x][y]);
} else if (activeBrickPix == 1) {
(*_ledmatrix).gridAddPixel(x, y, _activeBrick.col);
} else {
(*_ledmatrix).gridAddPixel(x, y, 0x000000);
}
}
}
(*_ledmatrix).drawOnMatrixInstant();
}
/* *** Game functions *** */
/**
* @brief Spawn new (random) brick
*
*/
void Tetris::newActiveBrick() {
uint8_t selectedBrick = 0;
static uint8_t lastselectedBrick = 0;
// choose random next brick, but not the same as before
do {
selectedBrick = random(7);
}
while (lastselectedBrick == selectedBrick);
// Save selected brick for next round
lastselectedBrick = selectedBrick;
// every brick has its color, select corresponding color
uint32_t selectedCol = _brickLib[selectedBrick].col;
// Set properties of brick
_activeBrick.siz = _brickLib[selectedBrick].siz;
_activeBrick.yOffset = _brickLib[selectedBrick].yOffset;
_activeBrick.xpos = WIDTH / 2 - _activeBrick.siz / 2;
_activeBrick.ypos = BRICKOFFSET - _activeBrick.yOffset;
_activeBrick.enabled = true;
// Set color of brick
_activeBrick.col = selectedCol;
// _activeBrick.color = _colorLib[1];
// Copy pix array of selected Brick
uint8_t x, y;
for (y = 0; y < MAX_BRICK_SIZE; y++) {
for (x = 0; x < MAX_BRICK_SIZE; x++) {
_activeBrick.pix[x][y] = (_brickLib[selectedBrick]).pix[x][y];
}
}
// Check collision, if already, then game is over
if (checkFieldCollision(&_activeBrick)) {
_tetrisGameOver = true;
_gameStatet = GAME_STATE_ENDt;
}
}
/**
* @brief Check collision between bricks in the field and the specified brick
*
* @param brick brick to be checked for collision
* @return boolean true if collision occured
*/
boolean Tetris::checkFieldCollision(struct Brick * brick) {
uint8_t bx, by;
uint8_t fx, fy;
for (by = 0; by < MAX_BRICK_SIZE; by++) {
for (bx = 0; bx < MAX_BRICK_SIZE; bx++) {
fx = (*brick).xpos + bx;
fy = (*brick).ypos + by;
if (( (*brick).pix[bx][by] == 1)
&& ( _field.pix[fx][fy] == 1)) {
return true;
}
}
}
return false;
}
/**
* @brief Check collision between specified brick and all sides of the playing field
*
* @param brick brick to be checked for collision
* @return boolean true if collision occured
*/
boolean Tetris::checkSidesCollision(struct Brick * brick) {
//Check vertical collision with sides of field
uint8_t bx, by;
uint8_t fx, fy;
for (by = 0; by < MAX_BRICK_SIZE; by++) {
for (bx = 0; bx < MAX_BRICK_SIZE; bx++) {
if ( (*brick).pix[bx][by] == 1) {
fx = (*brick).xpos + bx;//Determine actual position in the field of the current pix of the brick
fy = (*brick).ypos + by;
if (fx < 0 || fx >= WIDTH) {
return true;
}
}
}
}
return false;
}
/**
* @brief Rotate current active brick
*
*/
void Tetris::rotateActiveBrick() {
//Copy active brick pix array to temporary pix array
uint8_t x, y;
Brick tmpBrick;
for (y = 0; y < MAX_BRICK_SIZE; y++) {
for (x = 0; x < MAX_BRICK_SIZE; x++) {
tmpBrick.pix[x][y] = _activeBrick.pix[x][y];
}
}
tmpBrick.xpos = _activeBrick.xpos;
tmpBrick.ypos = _activeBrick.ypos;
tmpBrick.siz = _activeBrick.siz;
//Depending on size of the active brick, we will rotate differently
if (_activeBrick.siz == 3) {
//Perform rotation around center pix
tmpBrick.pix[0][0] = _activeBrick.pix[0][2];
tmpBrick.pix[0][1] = _activeBrick.pix[1][2];
tmpBrick.pix[0][2] = _activeBrick.pix[2][2];
tmpBrick.pix[1][0] = _activeBrick.pix[0][1];
tmpBrick.pix[1][1] = _activeBrick.pix[1][1];
tmpBrick.pix[1][2] = _activeBrick.pix[2][1];
tmpBrick.pix[2][0] = _activeBrick.pix[0][0];
tmpBrick.pix[2][1] = _activeBrick.pix[1][0];
tmpBrick.pix[2][2] = _activeBrick.pix[2][0];
//Keep other parts of temporary block clear
tmpBrick.pix[0][3] = 0;
tmpBrick.pix[1][3] = 0;
tmpBrick.pix[2][3] = 0;
tmpBrick.pix[3][3] = 0;
tmpBrick.pix[3][2] = 0;
tmpBrick.pix[3][1] = 0;
tmpBrick.pix[3][0] = 0;
} else if (_activeBrick.siz == 4) {
//Perform rotation around center "cross"
tmpBrick.pix[0][0] = _activeBrick.pix[0][3];
tmpBrick.pix[0][1] = _activeBrick.pix[1][3];
tmpBrick.pix[0][2] = _activeBrick.pix[2][3];
tmpBrick.pix[0][3] = _activeBrick.pix[3][3];
tmpBrick.pix[1][0] = _activeBrick.pix[0][2];
tmpBrick.pix[1][1] = _activeBrick.pix[1][2];
tmpBrick.pix[1][2] = _activeBrick.pix[2][2];
tmpBrick.pix[1][3] = _activeBrick.pix[3][2];
tmpBrick.pix[2][0] = _activeBrick.pix[0][1];
tmpBrick.pix[2][1] = _activeBrick.pix[1][1];
tmpBrick.pix[2][2] = _activeBrick.pix[2][1];
tmpBrick.pix[2][3] = _activeBrick.pix[3][1];
tmpBrick.pix[3][0] = _activeBrick.pix[0][0];
tmpBrick.pix[3][1] = _activeBrick.pix[1][0];
tmpBrick.pix[3][2] = _activeBrick.pix[2][0];
tmpBrick.pix[3][3] = _activeBrick.pix[3][0];
} else {
(*_logger).logString("Tetris: Brick size error");
}
// Now validate by checking collision.
// Collision possibilities:
// - Brick now sticks outside field
// - Brick now sticks inside fixed bricks of field
// In case of collision, we just discard the rotated temporary brick
if ((!checkSidesCollision(&tmpBrick)) && (!checkFieldCollision(&tmpBrick))) {
//Copy temporary brick pix array to active pix array
for (y = 0; y < MAX_BRICK_SIZE; y++) {
for (x = 0; x < MAX_BRICK_SIZE; x++) {
_activeBrick.pix[x][y] = tmpBrick.pix[x][y];
}
}
}
}
/**
* @brief Shift brick left/right/down by one if possible
*
* @param dir direction to be shifted
*/
void Tetris::shiftActiveBrick(int dir) {
// Change position of active brick (no copy to temporary needed)
if (dir == DIR_LEFT) {
_activeBrick.xpos--;
} else if (dir == DIR_RIGHT) {
_activeBrick.xpos++;
} else if (dir == DIR_DOWN) {
_activeBrick.ypos++;
}
// Check position of active brick
// Two possibilities when collision is detected:
// - Direction was LEFT/RIGHT, just revert position back
// - Direction was DOWN, revert position and fix block to field on collision
// When no collision, keep _activeBrick coordinates
if ((checkSidesCollision(&_activeBrick)) || (checkFieldCollision(&_activeBrick))) {
if (dir == DIR_LEFT) {
_activeBrick.xpos++;
} else if (dir == DIR_RIGHT) {
_activeBrick.xpos--;
} else if (dir == DIR_DOWN) {
_activeBrick.ypos--;// Go back up one
addActiveBrickToField();
_activeBrick.enabled = false;// Disable brick, it is no longer moving
}
}
}
/**
* @brief Copy active pixels to field, including color
*
*/
void Tetris::addActiveBrickToField() {
uint8_t bx, by;
uint8_t fx, fy;
for (by = 0; by < MAX_BRICK_SIZE; by++) {
for (bx = 0; bx < MAX_BRICK_SIZE; bx++) {
fx = _activeBrick.xpos + bx;
fy = _activeBrick.ypos + by;
if (fx >= 0 && fy >= 0 && fx < WIDTH && fy < HEIGHT && _activeBrick.pix[bx][by]) { // Check if inside playing field
// _field.pix[fx][fy] = _field.pix[fx][fy] || _activeBrick.pix[bx][by];
_field.pix[fx][fy] = _activeBrick.pix[bx][by];
_field.color[fx][fy] = _activeBrick.col;
}
}
}
}
/**
* @brief Move all pix from the field above startRow down by one. startRow is overwritten
*
* @param startRow
*/
void Tetris::moveFieldDownOne(uint8_t startRow) {
if (startRow == 0) { // Topmost row has nothing on top to move...
return;
}
uint8_t x, y;
for (y = startRow - 1; y > 0; y--) {
for (x = 0; x < WIDTH; x++) {
_field.pix[x][y + 1] = _field.pix[x][y];
_field.color[x][y + 1] = _field.color[x][y];
}
}
}
/**
* @brief Check if a line is complete
*
*/
void Tetris::checkFullLines() {
int x, y;
int minY = 0;
for (y = (HEIGHT - 1); y >= minY; y--) {
uint8_t rowSum = 0;
for (x = 0; x < WIDTH; x++) {
rowSum = rowSum + (_field.pix[x][y]);
}
if (rowSum >= WIDTH) {
// Found full row, animate its removal
_activeBrick.enabled = false;
for (x = 0; x < WIDTH; x++) {
_field.pix[x][y] = 0;
printField();
delay(100);
}
// Move all upper rows down by one
moveFieldDownOne(y);
y++; minY++;
printField();
delay(100);
_nbRowsThisLevel++; _nbRowsTotal++;
if (_nbRowsThisLevel >= LEVELUP) {
_nbRowsThisLevel = 0;
_brickSpeed = _brickSpeed - SPEED_STEP;
if (_brickSpeed < 200) {
_brickSpeed = 200;
}
}
}
}
}
/**
* @brief Clear field
*
*/
void Tetris::clearField() {
uint8_t x, y;
for (y = 0; y < HEIGHT; y++) {
for (x = 0; x < WIDTH; x++) {
_field.pix[x][y] = 0;
_field.color[x][y] = 0;
}
}
for (x = 0; x < WIDTH; x++) { //This last row is invisible to the player and only used for the collision detection routine
_field.pix[x][HEIGHT] = 1;
}
}
/**
* @brief Color all bricks on the field red
*
*/
void Tetris::everythingRed() {
int x, y;
for (x = 0; x < WIDTH; x++) {
for (y = 0; y < HEIGHT; y++) {
uint8_t activeBrickPix = 0;
if (_activeBrick.enabled) { //Only draw brick if it is enabled
//Now check if brick is "in view"
if ((x >= _activeBrick.xpos) && (x < (_activeBrick.xpos + (_activeBrick.siz)))
&& (y >= _activeBrick.ypos) && (y < (_activeBrick.ypos + (_activeBrick.siz)))) {
activeBrickPix = (_activeBrick.pix)[x - _activeBrick.xpos][y - _activeBrick.ypos];
}
}
if (_field.pix[x][y] == 1) {
(*_ledmatrix).gridAddPixel(x, y, RED);
} else if (activeBrickPix == 1) {
(*_ledmatrix).gridAddPixel(x, y, RED);
} else {
(*_ledmatrix).gridAddPixel(x, y, 0x000000);
}
}
}
(*_ledmatrix).drawOnMatrixInstant();
}
/**
* @brief Draw score to led matrix
*
*/
void Tetris::showscore() {
uint32_t color = LEDMatrix::Color24bit(255, 170, 0);
(*_ledmatrix).gridFlush();
if(_score > 9){
(*_ledmatrix).printNumber(2, 3, _score/10, color);
(*_ledmatrix).printNumber(6, 3, _score%10, color);
}else{
(*_ledmatrix).printNumber(4, 3, _score, color);
}
(*_ledmatrix).drawOnMatrixInstant();
}