-
Notifications
You must be signed in to change notification settings - Fork 38
/
Tetris.cpp
587 lines (461 loc) · 12.1 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
586
587
/*
Arduino Tetris
Copyright (C) 2015 João André Esteves Vilaça
https://github.com/vilaca/Handheld-Color-Console
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#ifndef TETRISCPP
#define TETRISCPP
#include "TFTv2_extended.h"
#include "Arduino.h"
#include "joystick.cpp"
#include "beeping.cpp"
#define LCD_WIDTH 319
#define LCD_HEIGHT 239
#define MIN(X, Y) (((X) < (Y)) ? (X) : (Y))
#define BOARD_WIDTH 11
#define BOARD_HEIGHT 20
#define BLOCK_SIZE MIN( (LCD_WIDTH-1) / BOARD_WIDTH, (LCD_HEIGHT-1) / BOARD_HEIGHT )
#define BOARD_LEFT (LCD_WIDTH - BOARD_WIDTH * BLOCK_SIZE)/4*3
#define BOARD_RIGHT (BOARD_LEFT + BLOCK_SIZE * BOARD_WIDTH)
#define BOARD_TOP (LCD_HEIGHT - BOARD_HEIGHT * BLOCK_SIZE) / 2
#define BOARD_BOTTOM (BOARD_TOP + BOARD_HEIGHT * BLOCK_SIZE)
#define PIT_COLOR CYAN
#define BG_COLOR BLACK
#define DROP_WAIT_INIT 1100
#define INPUT_WAIT_ROT 300
#define INPUT_WAIT_MOVE 150
#define INPUT_WAIT_NEW_SHAPE 400
// used to clear the position from the screen
typedef struct Backup {
byte x, y, rot;
};
class Tetris
{
// shapes definitions
byte l_shape[4][4][2] {
{{0, 0}, {0, 1}, {0, 2}, {1, 2}},
{{0, 1}, {1, 1}, {2, 0}, {2, 1}},
{{0, 0}, {1, 0}, {1, 1}, {1, 2}},
{{0, 0}, {0, 1}, {1, 0}, {2, 0}},
};
byte j_shape[4][4][2] {
{{1, 0}, {1, 1}, {0, 2}, {1, 2}},
{{0, 0}, {1, 0}, {2, 0}, {2, 1}},
{{0, 0}, {1, 0}, {0, 1}, {0, 2}},
{{0, 0}, {0, 1}, {1, 1}, {2, 1}},
};
byte o_shape[1][4][2] {
{ {0, 0}, {0, 1}, {1, 0}, {1, 1}}
};
byte s_shape[2][4][2] {
{{0, 1}, {1, 0}, {1, 1}, {2, 0}},
{{0, 0}, {0, 1}, {1, 1}, {1, 2}}
};
byte z_shape[2][4][2] {
{{0, 0}, {1, 0}, {1, 1}, {2, 1}},
{{1, 0}, {0, 1}, {1, 1}, {0, 2}}
};
byte t_shape[4][4][2] {
{{0, 0}, {1, 0}, {2, 0}, {1, 1}},
{{0, 0}, {0, 1}, {1, 1}, {0, 2}},
{{1, 0}, {0, 1}, {1, 1}, {2, 1}},
{{1, 0}, {0, 1}, {1, 1}, {1, 2}}
};
byte i_shape[2][4][2] {
{{0, 0}, {1, 0}, {2, 0}, {3, 0}},
{{0, 0}, {0, 1}, {0, 2}, {0, 3}}
};
// All game shapes and their colors
byte *all_shapes[7] = {l_shape[0][0], j_shape[0][0], o_shape[0][0], s_shape[0][0], z_shape[0][0], t_shape[0][0], i_shape[0][0]};
unsigned int colors[7] = {ORANGE, BLUE, YELLOW, GREEN, RED, MAGENTA, CYAN};
// how many rotated variations each shape has
byte shapes[7] = {4, 4, 1, 2, 2, 4, 2};
// game progress
int lines, level;
// current shapes
byte current;
// tetris guidelines have all 7 shapes
// selected in sequence to avoid
// long runs without a shape
byte next[7];
byte next_c;
unsigned long lastInput, lastDrop;
byte board[BOARD_HEIGHT][BOARD_WIDTH];
byte x, y, rot;
Backup old;
boolean newShape;
int dropWait;
public:
Tetris() : newShape(true), lines(0)
{
}
void run()
{
// analog 2 MUST NOT be connected to anything...
randomSeed(analogRead(2));
// clear board
for ( int i = 0; i < BOARD_WIDTH; i++ )
{
for ( int j = 0; j < BOARD_HEIGHT; j++)
{
board[j][i] = 0;
}
}
//next shape
randomizer();
// initialize game logic
lastInput = 0;
lastDrop = 0;
dropWait = DROP_WAIT_INIT;
level = 1;
// draw background
int c = LCD_HEIGHT / 28;
for (int i = LCD_HEIGHT - 1; i >= 0; i -= 2)
{
Tft.fillRectangle(0, i, LCD_WIDTH, 2, 0x1f - i / c);
}
Tft.fillRectangle(BOARD_LEFT, 0, BOARD_RIGHT-BOARD_LEFT-1, BOARD_BOTTOM, BG_COLOR);
// draw board left limit
Tft.drawLine (
BOARD_LEFT - 1,
BOARD_TOP,
BOARD_LEFT - 1,
BOARD_BOTTOM,
PIT_COLOR);
// draw board right limit
Tft.drawLine (
BOARD_RIGHT,
BOARD_TOP,
BOARD_RIGHT,
BOARD_BOTTOM,
PIT_COLOR);
// draw board bottom limit
Tft.drawLine (
BOARD_LEFT - 1,
BOARD_BOTTOM,
BOARD_RIGHT + 1,
BOARD_BOTTOM,
PIT_COLOR);
for ( int i = BOARD_LEFT + BLOCK_SIZE - 1; i < BOARD_RIGHT; i += BLOCK_SIZE)
{
Tft.drawLine (
i,
BOARD_TOP,
i,
BOARD_BOTTOM - 1,
GRAY2);
}
for ( int i = BOARD_TOP + BLOCK_SIZE - 1; i < BOARD_BOTTOM; i += BLOCK_SIZE)
{
Tft.drawLine (
BOARD_LEFT,
i,
BOARD_RIGHT - 1,
i,
GRAY2);
}
scoreBoard();
do {
// get clock
const unsigned long now = millis();
// display new shape
if ( newShape )
{
Joystick::waitForRelease(INPUT_WAIT_NEW_SHAPE);
newShape = false;
// a new shape enters the game
chooseNewShape();
// draw next box
Tft.fillRectangle(30, 100, BLOCK_SIZE * 6, BLOCK_SIZE * 5, BLACK);
Tft.drawRectangle(29, 99, BLOCK_SIZE * 6 + 1, BLOCK_SIZE * 5 + 1, WHITE);
byte *shape = all_shapes[next[next_c]];
for ( int i = 0; i < 4; i++ )
{
byte *block = shape + i * 2;
Tft.fillRectangleUseBevel(
30 + BLOCK_SIZE + block[0]*BLOCK_SIZE,
100 + BLOCK_SIZE + block[1]*BLOCK_SIZE,
BLOCK_SIZE - 2,
BLOCK_SIZE - 2 ,
colors[next[next_c]]);
}
// check if new shape is placed over other shape(s)
// on the board
if ( touches(0, 0, 0 ))
{
// draw shape to screen
draw();
return;
}
// draw shape to screen
draw();
}
else
{
// check if enough time has passed since last time the shape
// was moved down the board
if ( now - lastDrop > dropWait || Joystick::getY() > Joystick::NEUTRAL)
{
// update clock
lastDrop = now;
moveDown();
}
}
if (!newShape)
{
userInput(now);
}
} while (true);
}
private:
void chooseNewShape()
{
current = next[next_c];
next_c++;
if ( next_c == 7 )
{
randomizer();
}
// new shape must be positioned at the middle of
// the top of the board
// with zero rotation
rot = 0;
y = 0;
x = BOARD_WIDTH / 2;
old.rot = rot;
old.y = y;
old.x = x;
}
void userInput(unsigned long now)
{
unsigned long waited = now - lastInput;
int jx = Joystick::getX();
int move = INPUT_WAIT_MOVE / jx;
if ( jx < Joystick::NEUTRAL && waited > -move)
{
if (x > 0 && !touches(-1, 0, 0))
{
x--;
lastInput = now;
draw();
}
}
else if ( jx > Joystick::NEUTRAL && waited > move )
{
if ( x < BOARD_WIDTH && !touches(1, 0, 0))
{
x++;
lastInput = now;
draw();
}
}
if ( Joystick::fire() )
{
while ( !touches(0, 1, 0 ))
{
y++;
}
lastInput = now;
draw();
}
int my = Joystick::getY();
if (( my == -Joystick::HARD && waited > INPUT_WAIT_ROT ) || ( my == -Joystick::HARDER && waited > INPUT_WAIT_ROT / 2 ))
{
if (Joystick::getY() < 0 && !touches(0, 0, 1))
{
rot++;
rot %= shapes[current];
lastInput = now;
draw();
}
}
}
void moveDown()
{
// prepare to move down
// check if board is clear bellow
if ( touches(0, 1, 0 ))
{
// moving down touches another shape
newShape = true;
// this shape wont move again
// add it to the board
byte *shape = all_shapes[current];
for ( int i = 0; i < 4; i++ )
{
byte *block = (shape + (rot * 4 + i) * 2);
board[block[1] + y][block[0] + x] = current + 1;
}
// check if lines were made
score();
Beeping::beep(1500, 25);
}
else
{
// move shape down
y += 1;
draw();
}
}
void draw()
{
byte *shape = all_shapes[current];
for ( int i = 0; i < 4; i++ )
{
byte *block = (shape + (rot * 4 + i) * 2);
Tft.fillRectangleUseBevel(
BOARD_LEFT + block[0]*BLOCK_SIZE + BLOCK_SIZE * x,
BOARD_TOP + block[1]*BLOCK_SIZE + BLOCK_SIZE * y,
BLOCK_SIZE - 2,
BLOCK_SIZE - 2 ,
colors[current]);
board[block[1] + y][block[0] + x] = 255;
}
// erase old
for ( int i = 0; i < 4; i++ )
{
byte *block = (shape + (old.rot * 4 + i) * 2);
if ( board[block[1] + old.y][block[0] + old.x] == 255 )
continue;
Tft.fillRectangle(
BOARD_LEFT + block[0]*BLOCK_SIZE + BLOCK_SIZE * old.x,
BOARD_TOP + block[1]*BLOCK_SIZE + BLOCK_SIZE * old.y,
BLOCK_SIZE - 2,
BLOCK_SIZE - 2 ,
BG_COLOR);
}
for ( int i = 0; i < 4; i++ )
{
byte *block = (shape + (rot * 4 + i) * 2);
board[block[1] + y][block[0] + x] = 0;
}
old.x = x;
old.y = y;
old.rot = rot;
}
boolean touches(int xi, int yi, int roti)
{
byte *shape = all_shapes[current];
for ( int i = 0; i < 4; i++ )
{
byte *block = (shape + (((rot + roti) % shapes[current]) * 4 + i) * 2);
int x2 = x + block[0] + xi;
int y2 = y + block[1] + yi;
if ( y2 == BOARD_HEIGHT || x2 == BOARD_WIDTH || board[y2][x2] != 0 )
{
return true;
}
}
return false;
}
void score()
{
// we scan a max of 4 lines
int ll;
if ( y + 3 >= BOARD_HEIGHT )
{
ll = BOARD_HEIGHT - 1;
}
// scan board from current position
for (int l = y; l <= ll; l++)
{
// check if there's a complete line on the board
boolean line = true;
for ( int c = 0; c < BOARD_WIDTH; c++ )
{
if (board[l][c] == 0)
{
line = false;
break;
}
}
if ( !line )
{
// move to next line
continue;
}
Beeping::beep(3000, 50);
lines++;
if ( lines % 10 == 0 )
{
level++;
dropWait /= 2;
}
scoreBoard();
// move board down
for ( int row = l; row > 0; row -- )
{
for ( int c = 0; c < BOARD_WIDTH; c++ )
{
byte v = board[row - 1][c];
board[row][c] = v;
Tft.fillRectangleUseBevel(
BOARD_LEFT + BLOCK_SIZE * c,
BOARD_TOP + BLOCK_SIZE * row,
BLOCK_SIZE - 2,
BLOCK_SIZE - 2 ,
v == 0 ? BLACK : colors[v - 1] ) ;
}
}
// clear top line
for ( int c = 0; c < BOARD_WIDTH; c++ )
{
board[0][c] = 0;
}
Tft.fillRectangle(
BOARD_LEFT,
0,
BOARD_RIGHT - BOARD_LEFT,
BLOCK_SIZE,
BLACK ) ;
}
delay(350);
}
void scoreBoard()
{
Tft.fillRectangle(6, 3, 128, 50, BLACK);
Tft.drawString("Level", 8, 8, 2, YELLOW);
Tft.drawString("Lines", 8, 32, 2, 0x3f);
Tft.drawNumber(level, 74, 8, 2, YELLOW);
Tft.drawNumber(lines, 74, 32, 2, 0x3f);
Tft.drawRectangle(5, 2, 130, 52, 0xffff);
}
// create a sequence of 7 random shapes
void randomizer()
{
// randomize 7 shapes
for ( byte i = 0; i < 7; i ++)
{
boolean retry;
byte shape;
do
{
shape = random(7);
// check if already in sequence
retry= false;
for ( int j = 0; j < i; j++)
{
if ( shape == next[j] )
{
retry = true;
break;
}
}
} while (retry);
next[i] = shape;
}
next_c = 0;
}
};
#endif