forked from techniccontroller/wordclock_esp8266
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tetris.h
218 lines (190 loc) · 5.39 KB
/
tetris.h
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
/**
* @file tetris.h
* @author techniccontroller (mail[at]techniccontroller.com)
* @brief Class definition 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
*
*/
#ifndef tetris_h
#define tetris_h
#include <Arduino.h>
#include "ledmatrix.h"
#include "udplogger.h"
#define DEBOUNCE_TIME 100
#define RED_END_TIME 1500
#define GAME_STATE_RUNNINGt 1
#define GAME_STATE_ENDt 2
#define GAME_STATE_INITt 3
#define GAME_STATE_PAUSEDt 4
#define GAME_STATE_READYt 5
//common
#define DIR_UP 1
#define DIR_DOWN 2
#define DIR_LEFT 3
#define DIR_RIGHT 4
//Maximum size of bricks. Individual bricks can still be smaller (eg 3x3)
#define GREEN 0x008000
#define RED 0xFF0000
#define BLUE 0x0000FF
#define YELLOW 0xFFFF00
#define CHOCOLATE 0xD2691E
#define PURPLE 0xFF00FF
#define WHITE 0XFFFFFF
#define AQUA 0x00FFFF
#define HOTPINK 0xFF1493
#define DARKORANGE 0xFF8C00
#define MAX_BRICK_SIZE 4
#define BRICKOFFSET -1 // Y offset for new bricks
#define INIT_SPEED 800 // Initial delay in ms between brick drops
#define SPEED_STEP 10 // Factor for speed increase between levels, default 10
#define LEVELUP 4 // Number of rows before levelup, default 5
#define WIDTH 11
#define HEIGHT 10
class Tetris{
// Playing field
struct Field {
uint8_t pix[WIDTH][HEIGHT + 1]; //Make field one larger so that collision detection with bottom of field can be done in a uniform way
uint32_t color[WIDTH][HEIGHT];
};
//Structure to represent active brick on screen
struct Brick {
boolean enabled;//Brick is disabled when it has landed
int xpos, ypos;
int yOffset;//Y-offset to use when placing brick at top of field
uint8_t siz;
uint8_t pix[MAX_BRICK_SIZE][MAX_BRICK_SIZE];
uint32_t col;
};
//Struct to contain the different choices of blocks
struct AbstractBrick {
int yOffset;//Y-offset to use when placing brick at top of field
uint8_t siz;
uint8_t pix[MAX_BRICK_SIZE][MAX_BRICK_SIZE];
uint32_t col;
};
public:
Tetris();
Tetris(LEDMatrix *myledmatrix, UDPLogger *mylogger);
void ctrlStart();
void ctrlPlayPause();
void ctrlRight();
void ctrlLeft();
void ctrlUp();
void ctrlDown();
void setSpeed(int32_t i);
void loopCycle();
private:
void resetLEDs();
void tetrisInit();
void printField();
/* *** Game functions *** */
void newActiveBrick();
boolean checkFieldCollision(struct Brick * brick);
boolean checkSidesCollision(struct Brick * brick);
void rotateActiveBrick();
void shiftActiveBrick(int dir);
void addActiveBrickToField();
void moveFieldDownOne(uint8_t startRow);
void checkFullLines();
void clearField();
void everythingRed();
void showscore();
LEDMatrix *_ledmatrix;
UDPLogger *_logger;
Brick _activeBrick;
Field _field;
long _lastButtonClick = 0;
long _lastButtonClickr = 0;
int _score = 0;
int _gameStatet = GAME_STATE_INITt;
uint16_t _brickSpeed;
unsigned long _nbRowsThisLevel;
unsigned long _nbRowsTotal;
bool _tetrisGameOver;
unsigned long _prevUpdateTime = 0;
long _tetrisshowscore;
long _droptime = 0;
int _speedtetris = 80;
bool _allowdrop;
// color library
uint32_t _colorLib[10] = {RED, GREEN, BLUE, YELLOW, CHOCOLATE, PURPLE, WHITE, AQUA, HOTPINK, DARKORANGE};
// Brick "library"
AbstractBrick _brickLib[7] = {
{
1,//yoffset when adding brick to field
4,
{ {0, 0, 0, 0},
{0, 1, 1, 0},
{0, 1, 1, 0},
{0, 0, 0, 0}
},
WHITE
},
{
0,
4,
{ {0, 1, 0, 0},
{0, 1, 0, 0},
{0, 1, 0, 0},
{0, 1, 0, 0}
},
GREEN
},
{
1,
3,
{ {0, 0, 0, 0},
{1, 1, 1, 0},
{0, 0, 1, 0},
{0, 0, 0, 0}
},
BLUE
},
{
1,
3,
{ {0, 0, 1, 0},
{1, 1, 1, 0},
{0, 0, 0, 0},
{0, 0, 0, 0}
},
YELLOW
},
{
1,
3,
{ {0, 0, 0, 0},
{1, 1, 1, 0},
{0, 1, 0, 0},
{0, 0, 0, 0}
},
AQUA
},
{
1,
3,
{ {0, 1, 1, 0},
{1, 1, 0, 0},
{0, 0, 0, 0},
{0, 0, 0, 0}
},
HOTPINK
},
{
1,
3,
{ {1, 1, 0, 0},
{0, 1, 1, 0},
{0, 0, 0, 0},
{0, 0, 0, 0}
},
RED
}
};
};
#endif