-
Notifications
You must be signed in to change notification settings - Fork 1
/
blockis_max7219.c
572 lines (539 loc) · 15.8 KB
/
blockis_max7219.c
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
/////////////////////////////////////////////////////////////////////
//
// Blockis by M.O.B. as MAX7219 8x8 LED matrix program in C for Linux.
// Copyright (C) 2007, 2016 by Mikael O. Bonnier, Lund, Sweden.
// License: GNU GPL v3 or later, http://www.gnu.org/licenses/gpl-3.0.txt
// Donations are welcome to PayPal [email protected].
// The source code is at <https://github.com/mobluse/blockis>.
//
// Blockis is a game similar to Tetris(R). I didn't read any other tetris
// code before writing this. I only played and studied the behaviour of a
// hand held tetris-type game I bought in 1999 (238A, E-8238, All in One,
// 238 in One, Super King for 238 Game, ...it had many names on the
// package) and studied the information on rules in Swedish Wikipedia. In
// 238A Tetris is called Game I, Original Brick Game. My implementation
// doesn't behave exactly like the one in 238A. E.g. you cannot change
// speed, level or number of game (i.e. variations). Another difference is
// that rotation cannot move the block sideways or downwards in my current
// implementation. There is no sound.
//
// It was originally developed in J2ME (Java) using WTK-2.2 and JDK 1.5 on
// Windows 2000. This C-program was translated from the Java-app into C
// using nCurses on a Raspberry Pi 2 running Raspbian Jessie (Debian)
// Linux.
//
// Revision history:
// 2007-Jan-15: v.0.0.2 Java
// 2007-Jan-17: v.0.0.3 Java
// 2007-Jan-19: v.0.1.0 Java
// 2007-Jan-20: v.0.1.1 Java
// 2007-Feb-21: v.0.1.2 Java
// 2008-May-23: v.0.1.3 Java
// 2016-Jan-10: v.0.1.3a C
// 2016-Jan-20: v.0.1.3 C
//
// Suggestions, improvements, and bug-reports
// are always welcome to:
// Mikael O. Bonnier
// Osten Undens gata 88
// SE-227 62 LUND
// SWEDEN
//
// Or use my electronic addresses:
// Web: http://www.df.lth.se.orbin.se/~mikaelb/
// Twitter: https://twitter.com/mobluse
// Scratch: https://scratch.mit.edu/users/mobluse/
// E-mail/MSN: [email protected]
// ICQ # 114635318
// Skype: mikael4u
// _____
// / / \
// ***********/ / \***********
// / / \
// *********/ / \*********
// / / / \ \
// *******/ / / \ \*******
// / / / \ \ \
// *****/ / /***\ \ \*****
// / /__ /_____\ \ \
// ***/ \ \ \***
// /_________________\ \ \
// **\ \ /**
// \______________________\/
//
// Mikael O. Bonnier
/////////////////////////////////////////////////////////////////////
// Install and configure the Raspberry Pi using ./install.sh shell-script.
// Compile using ./rebuild.sh.
// Indent after editing using ./indent.sh.
/**/
#include <ncurses.h>
#include <sys/param.h>
#include <time.h>
#include <stdlib.h>
#include <setjmp.h>
#include "max7219.h"
void init();
void clearGraphics();
void start();
void resume();
void stop();
void drawBlock();
void moveOn();
void hitGround();
bool blockCollides();
void resetBlock();
void setBlock();
void render();
void blit(void (*doCell) (int, int), int block, int rot, int row, int col);
void doCellBlockCollides(int r, int c);
void doCellResetBlock(int r, int c);
void doCellSetBlock(int r, int c);
#define _MATRIX_ROWS (8)
#define _MATRIX_COLS (8)
#define _CA (2)
const int _nDelay = 100; // ms
const int _INTERLEAVE = 8;
#define _ROT0 (0)
#define _ROT90 (1)
#define _ROT180 (2)
#define _ROT270 (3)
char _nMatrix[_MATRIX_ROWS][_MATRIX_COLS];
char _nMatrixPrev[_MATRIX_ROWS][_MATRIX_COLS];
const bool _nBI[2][4] = { {0, 0, 0, 0},
{1, 1, 1, 1}
};
const bool _nBT[2][3] = { {1, 1, 1},
{0, 1, 0}
};
const bool _nBO[2][2] = { {1, 1},
{1, 1}
};
const bool _nBL[2][3] = { {1, 1, 1},
{1, 0, 0}
};
const bool _nBJ[2][3] = { {1, 1, 1},
{0, 0, 1}
};
const bool _nBS[2][3] = { {0, 1, 1},
{1, 1, 0}
};
const bool _nBZ[2][3] = { {1, 1, 0},
{0, 1, 1}
};
#define _NOOFBLOCKS (7)
#define _NOOFCOLORS (7)
const bool *_blocks[_NOOFBLOCKS] =
{ (bool *) _nBI, (bool *) _nBT, (bool *) _nBO,
(bool *) _nBL, (bool *) _nBJ, (bool *) _nBS, (bool *) _nBZ
};
const struct {
int rows;
int cols;
} _blockInfo[_NOOFBLOCKS] = {
{
2, 4}, {
2, 3}, {
2, 2}, {
2, 3}, {
2, 3}, {
2, 3}, {
2, 3}};
bool _bRunning;
int _iBlockNext, _iBlock, _iBlockPrev;
int _nRot, _nRotPrev;
int _nMRow, _nMCol, _nMRowPrev, _nMColPrev;
char _iColorNext;
char _iColor;
bool _bNewBlock;
int _nLevel = 0;
int _nScore, _nHiScore = 0;
jmp_buf _exceptionEnv;
// SPI
int _fd;
int main(int argc, char *argv[])
{
int loopCount = 1;
init();
start();
for (;;) {
while (_bRunning) {
chtype ks = getch();
flushinp();
switch (ks) {
case KEY_LEFT:
case 'a':
--_nMCol;
break;
case KEY_RIGHT:
case 'd':
++_nMCol;
break;
case KEY_UP:
case 'w':
_nRot = _nRot >= 3 ? 0 : _nRot + 1;
break;
case KEY_DC:
case KEY_PPAGE:
case 'e':
_nRot = _nRot <= 0 ? 3 : _nRot - 1;
break;
case KEY_END:
case KEY_HOME:
case KEY_NPAGE:
case 'q':
case 'Q':
stop();
break;
}
if (_bNewBlock) {
_nMRow = _nMRowPrev = 0;
_nMCol = _nMColPrev = _MATRIX_COLS / 2 - 2;
_iBlock = _iBlockNext;
if (_iBlock == 0)
_nMRow = _nMRowPrev = -1;
_iBlockNext = rand() % _NOOFBLOCKS;
_iColor = _iColorNext;
_iColorNext = (char)(rand() % (_NOOFCOLORS - 1) + 1);
_nRot = 0;
}
drawBlock();
render();
refresh();
napms(_nDelay);
if (ks == KEY_DOWN || ks == KEY_IC || ks == 's' || loopCount % _INTERLEAVE == 0)
++_nMRow;
++loopCount; // loopCount will wrap around at INT_MAX.
}
chtype ks = getch();
flushinp();
switch (ks) {
case KEY_DOWN:
case KEY_IC:
case 's':
start();
break;
case KEY_DC:
case KEY_PPAGE:
case 'e':
resume();
break;
case KEY_END:
case KEY_HOME:
case KEY_NPAGE:
case 'q':
case 'Q':
endwin();
clear_led_matrix(_fd);
close(_fd);
return 0;
break;
}
napms(_nDelay);
}
}
void init()
{
_fd = init_max7219();
srand(time(NULL));
initscr();
cbreak();
noecho();
// scrollok(stdscr, TRUE);
nodelay(stdscr, TRUE);
keypad(stdscr, TRUE);
curs_set(0);
start_color();
init_pair(1, COLOR_RED, COLOR_RED);
init_pair(2, COLOR_GREEN, COLOR_GREEN);
init_pair(3, COLOR_YELLOW, COLOR_YELLOW);
init_pair(4, COLOR_BLUE, COLOR_BLUE);
init_pair(5, COLOR_MAGENTA, COLOR_MAGENTA);
init_pair(6, COLOR_CYAN, COLOR_CYAN);
}
/**
* This method clears the graphics.
*/
void clearGraphics()
{
// Clear the Graphics.
clear();
for (int c = 1 + _CA; c <= _MATRIX_COLS + _CA; ++c) {
mvaddch(0, c, ACS_HLINE);
mvaddch(_MATRIX_ROWS + 1, c, ACS_HLINE);
}
for (int r = 1; r <= _MATRIX_ROWS; ++r) {
mvaddch(r, _CA, ACS_VLINE);
mvaddch(r, _MATRIX_COLS + 1 + _CA, ACS_VLINE);
}
mvaddch(0, _CA, ACS_ULCORNER);
mvaddch(0, _MATRIX_COLS + 1 + _CA, ACS_URCORNER);
mvaddch(_MATRIX_ROWS + 1, _CA, ACS_LLCORNER);
mvaddch(_MATRIX_ROWS + 1, _MATRIX_COLS + 1 + _CA, ACS_LRCORNER);
mvprintw(_MATRIX_ROWS, _MATRIX_COLS + 3 + _CA, "%d", _nScore);
mvprintw(_MATRIX_ROWS - 6, _MATRIX_COLS + 3 + _CA, "%d", _nHiScore);
mvaddstr(_MATRIX_ROWS + 3, 1 + _CA, "W A S D E Q");
}
/**
* This method starts the game.
*/
void start()
{
_nScore = 0;
clearGraphics();
clear_led_matrix(_fd);
// Reset the matrix.
for (int r = 0; r < _MATRIX_ROWS; ++r)
for (int c = 0; c < _MATRIX_COLS; ++c)
_nMatrix[r][c] = _nMatrixPrev[r][c] = 0;
_bNewBlock = true;
_iBlockNext = rand() % _NOOFBLOCKS;
_iColorNext = (char)(rand() % (_NOOFCOLORS - 1) + 1);
_bRunning = true;
}
/**
* This resumes animation after a pause.
*/
void resume()
{
_bRunning = true;
}
/**
* This is called to stop the animation when pausing, destroying or game over.
*/
void stop()
{
_bRunning = false;
}
/**
* This method calls other methods which draws the falling block on the
* matrix. If there would be a collision with walls or the ground it tries
* to prevent it by prohibiting rotation and motion or restricting motion.
* This method also detects Game Over.
*/
void drawBlock()
{
if (!_bNewBlock)
resetBlock();
if (blockCollides())
if (!_bNewBlock) {
if (_nRot != _nRotPrev) {
_nRot = _nRotPrev;
if (!blockCollides()) {
moveOn();
return;
}
}
if (_nMRow != _nMRowPrev && _nMCol == _nMColPrev) {
hitGround();
} else if (_nMCol != _nMColPrev && _nMRow != _nMRowPrev) {
_nMCol = _nMColPrev;
if (blockCollides()) {
hitGround();
} else {
moveOn();
}
} else {
_nMCol = _nMColPrev;
moveOn();
}
} else {
_nHiScore = MAX(_nHiScore, _nScore);
mvprintw(_MATRIX_ROWS - 6, _MATRIX_COLS + 3 + _CA, "%d", _nHiScore);
mvaddstr(1, _MATRIX_COLS + 3 + _CA, "GAME OVER");
stop();
} else
moveOn();
}
/**
* This method is called when there is no collision or a collision that has
* been avoided. It allows the block to continue its fall.
*/
void moveOn()
{
_bNewBlock = false;
setBlock();
_iBlockPrev = _iBlock;
_nRotPrev = _nRot;
_nMRowPrev = _nMRow;
_nMColPrev = _nMCol;
}
/**
* This method is called when a block hits the ground and it checks for full
* rows and compacts according to the rules of standard tetris. Points
* calculation is done here.
*/
void hitGround()
{
_bNewBlock = true;
_nMRow = _nMRowPrev;
_nMCol = _nMColPrev;
setBlock();
int r, c, rMove;
bool fullLines[_MATRIX_ROWS];
bool existsFull, isFull;
existsFull = false;
int noOfLines = 0;
for (int r = _MATRIX_ROWS - 1; r >= 0; --r) {
isFull = true;
for (int c = 0; c < _MATRIX_COLS; ++c)
if (_nMatrix[r][c] == 0) {
isFull = false;
break;
}
if (fullLines[r] = isFull) {
existsFull = true;
++noOfLines;
for (c = 0; c < _MATRIX_COLS; ++c)
_nMatrix[r][c] = 0;
}
}
const int scores[] = { 40, 100, 300, 1200 };
if (existsFull) {
_nScore += (_nLevel + 1) * scores[noOfLines - 1];
mvprintw(_MATRIX_ROWS, _MATRIX_COLS + 3 + _CA, "%d", _nScore);
render();
refresh();
napms(_nDelay);
for (int r = _MATRIX_ROWS - 1; r >= 0; --r)
while (fullLines[r]) {
for (int rMove = r; rMove >= 1; --rMove) {
fullLines[rMove] = fullLines[rMove - 1];
for (int c = 0; c < _MATRIX_COLS; ++c)
_nMatrix[rMove][c] = _nMatrix[rMove - 1][c];
}
fullLines[0] = false;
for (c = 0; c < _MATRIX_COLS; ++c)
_nMatrix[0][c] = 0;
render();
refresh();
napms(_INTERLEAVE * _nDelay);
}
}
}
/**
* This method checks if a block will collide with the walls or the ground.
* @return It returns true if collision, false otherwise.
*/
bool blockCollides()
{
if (setjmp(_exceptionEnv)) {
return true;
}
blit(doCellBlockCollides, _iBlock, _nRot, _nMRow, _nMCol);
return false;
}
/**
* This method wipes out the previously drawn block from the matrix.
*/
void resetBlock()
{
blit(doCellResetBlock, _iBlockPrev, _nRotPrev, _nMRowPrev, _nMColPrev);
}
/**
* This method draws the current block on the matrix.
*/
void setBlock()
{
blit(doCellSetBlock, _iBlock, _nRot, _nMRow, _nMCol);
}
/**
* This method renders the tetris matrix on the Graphics with the current
* block and the next block.
* Motivation of design: Graphics.fillRect is costly and should be avoided as
* much as possible.
* Therefore the previous matrix is stored and only the changes are plotted.
*/
void render()
{
// Render the matrix.
for (int c = 0; c < _MATRIX_COLS; ++c) {
uint8_t col = 0x00;
for (int r = 0; r < _MATRIX_ROWS; ++r) {
if (_nMatrix[r][c] != _nMatrixPrev[r][c]) {
chtype ch = _nMatrix[r][c] != 0 ? ACS_CKBOARD : ' ';
attron(COLOR_PAIR(_nMatrix[r][c])); // TODO: Optimize!
mvaddch(r + 1, c + 1 + _CA, ch);
attroff(COLOR_PAIR(_nMatrix[r][c]));
_nMatrixPrev[r][c] = _nMatrix[r][c];
}
col >>= 1;
if (_nMatrix[r][c] != 0)
col |= 0x80;
}
col_write(_fd, (uint8_t) (c + 1), col);
}
// Render the next block.
// TODO: This might use Blitter.
const int BLOCK_ROWS = _blockInfo[_iBlockNext].rows,
BLOCK_COLS = _blockInfo[_iBlockNext].cols;
mvaddstr(3, _MATRIX_COLS + 3 + _CA, " ");
mvaddstr(4, _MATRIX_COLS + 3 + _CA, " ");
attron(COLOR_PAIR(_iColorNext));
for (int r = 0; r < BLOCK_ROWS; ++r)
for (int c = 0; c < BLOCK_COLS; ++c)
if (_blocks[_iBlockNext][r * BLOCK_COLS + c])
mvaddch(r + 3, c + _MATRIX_COLS + 3 + _CA, ACS_CKBOARD);
attroff(COLOR_PAIR(_iColorNext));
}
/**
* This method can blit the block on to the matrix, but exactly what it does
* depend on the implementation of doCell() in the child class.
* TODO: If r and c is outside of the matrix or if collision.
*/
void blit(void (*doCell) (int, int), int block, int rot, int row, int col)
{
const int BLOCK_ROWS = _blockInfo[block].rows,
BLOCK_COLS = _blockInfo[block].cols;
switch (rot) {
case _ROT0:
for (int r = 0; r < BLOCK_ROWS; ++r)
for (int c = 0; c < BLOCK_COLS; ++c)
if (_blocks[block][r * BLOCK_COLS + c] != 0)
(*doCell) (row + r, col + c);
break;
case _ROT90:
for (int r = 0; r < BLOCK_ROWS; ++r)
for (int c = 0; c < BLOCK_COLS; ++c)
if (_blocks[block][r * BLOCK_COLS + c] != 0)
(*doCell) (row + c, col + (BLOCK_COLS - 1 - r));
break;
case _ROT180:
for (int r = 0; r < BLOCK_ROWS; ++r)
for (int c = 0; c < BLOCK_COLS; ++c)
if (_blocks[block][r * BLOCK_COLS + c] != 0)
(*doCell)
(row + (BLOCK_COLS - 1 - r),
col + (BLOCK_COLS - 1 - c));
break;
case _ROT270:
for (int r = 0; r < BLOCK_ROWS; ++r)
for (int c = 0; c < BLOCK_COLS; ++c)
if (_blocks[block][r * BLOCK_COLS + c] != 0)
(*doCell) (row + (BLOCK_COLS - 1 - c), col + r);
break;
}
}
/**
* This doCell has block collision detection.
*/
void doCellBlockCollides(int r, int c)
{
if (_nMatrix[r][c] != 0 ||
!(0 <= r && r < _MATRIX_ROWS && 0 <= c && c < _MATRIX_COLS))
longjmp(_exceptionEnv, TRUE);
}
/**
* This doCell has block reset code.
*/
void doCellResetBlock(int r, int c)
{
_nMatrix[r][c] = 0;
}
/**
* This doCell has block set code.
*/
void doCellSetBlock(int r, int c)
{
_nMatrix[r][c] = _iColor;
}