This repository has been archived by the owner on Dec 31, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathengine.cpp
291 lines (257 loc) · 5.94 KB
/
engine.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
/*
Author: Hai Yang Xu
Student ID: <redacted>
Author: John Beckingham
Student ID: <redacted>
Tetris 2 game engine.
*/
#include <Arduino.h>
#include <Adafruit_ILI9341.h>
#include "constants.h"
#include "assets.h"
#include "engine.h"
#include "graphics.h"
// Game mode
enum GameMode { ONE_PLAYER, TWO_PLAYER };
GameMode gameMode;
// Block drop period in milliseconds
int msPerDrop;
// Current score, only used in one-player mode
uint32_t score;
// Get current score
uint32_t getCurrentScore() {
return score;
}
// Whether this player lost, only used in two-player mode
bool lost = false;
// Get if this player lost
bool getIfLost() {
return lost;
}
// Current block
bool currentBlock[4 * 4];
uint16_t currentColor;
uint32_t lastTime;
int x;
int y;
// Input control
#define INPUT_HOLD 200
uint32_t lastLeftTime;
uint32_t lastRightTime;
uint32_t lastUpTime;
uint32_t lastDownTime;
// Reset the state of the current block, and generate a new block
void generateNewBlock() {
getRandomBlock(currentBlock);
getRandomColor(currentColor);
lastTime = millis();
x = SCREEN_WIDTH / 2 - 2;
y = -4;
}
// Check for non-removable rows sent from opponent then generate new block
void generateNewBlockMiddleware(Adafruit_ILI9341 &tft) {
// Check for non-removable rows
if (gameMode == TWO_PLAYER) {
while (Serial3.available() > 0) {
char temp = Serial3.read();
// One block can clear up to 4 lines
bool moved = false;
if (temp == '1') {
moved = true;
if (!mapMoveUp(1)) {
lost = true;
}
} else if (temp == '2') {
moved = true;
if (!mapMoveUp(2)) {
lost = true;
}
} else if (temp == '3') {
moved = true;
if (!mapMoveUp(3)) {
lost = true;
}
} else if (temp == '4') {
moved = true;
if (!mapMoveUp(4)) {
lost = true;
}
}
// Redraw map if needed
if (moved && !lost) {
mapRedraw(tft);
}
}
}
// Generate new block
generateNewBlock();
}
// Check for full rows and send non-removable rows to opponent if needed
void mapRemoveFullRowsMiddleware(Adafruit_ILI9341 &tft) {
// Check for full rows
uint32_t rowsRemoved = mapRemoveFullRows(tft);
if (rowsRemoved == 0UL) {
return;
}
// Update score
if (gameMode == ONE_PLAYER) {
score = score + rowsRemoved;
}
// Send non-removable rows
if (gameMode == TWO_PLAYER) {
Serial3.print((int)rowsRemoved);
}
}
// Initialize one-player mode
void initOnePlayer(Adafruit_ILI9341 &tft) {
// Set game mode
gameMode = ONE_PLAYER;
// Clear screen
tft.fillScreen(ILI9341_BLACK);
mapClear();
// Reset basic state
msPerDrop = 500;
score = 0UL;
// Reset block state
generateNewBlock();
// Reset input state
uint32_t lastLeftTime = 0UL;
uint32_t lastRightTime = 0UL;
uint32_t lastUpTime = 0UL;
uint32_t lastDownTime = 0UL;
}
// Tick for one-player mode, returns whether the game ended
bool onePlayerTick(
bool up, bool down, bool left, bool right, bool btn,
Adafruit_ILI9341 &tft
) {
uint32_t now = millis();
// Process block drop
if (lastTime + msPerDrop < now) {
lastTime = now;
if (mapCheckCollision(x, y + 1, currentBlock)) {
// Can't move further
if (y < 0) {
// Out of space, GG
return true;
} else {
// Commit this block
commitBlock(x, y, currentBlock, currentColor);
// Check for row completion then generate a new block
mapRemoveFullRowsMiddleware(tft);
generateNewBlockMiddleware(tft);
}
} else {
// Drop one "pixel"
moveBlock(x, y, 0, 1, currentBlock, currentColor, tft);
}
}
// Apply input control
if (left) {
if (lastLeftTime + INPUT_HOLD > now) {
// Too soon for another tick
left = false;
} else {
// Save the new timestamp and let this tick though
lastLeftTime = now;
}
}
if (right) {
if (lastRightTime + INPUT_HOLD > now) {
right = false;
} else {
lastRightTime = now;
}
}
if (up) {
if (lastUpTime + INPUT_HOLD * 2 > now) {
up = false;
} else {
lastUpTime = now;
}
}
if (down) {
if (lastDownTime + INPUT_HOLD * 2 > now) {
down = false;
} else {
lastDownTime = now;
}
}
// Handle player input
if (left) { // Move left
if (!mapCheckCollision(x - 1, y, currentBlock)) {
moveBlock(x, y, -1, 0, currentBlock, currentColor, tft);
}
} else if (right) { // Move right
if (!mapCheckCollision(x + 1, y, currentBlock)) {
moveBlock(x, y, 1, 0, currentBlock, currentColor, tft);
}
}
if (up) { // Rotate right (clockwise)
// Get the rotated block
bool tempBlock[4 * 4];
getRightRotation(tempBlock);
if (mapCheckCollision(x, y, tempBlock)) {
// Can't fit, rotate back
setLeftRotation();
} else {
// Undraw current block
undrawBlock(x, y, currentBlock, tft);
// Save the rotated block then draw it
memcpy(currentBlock, tempBlock, 4 * 4 * sizeof(bool));
drawBlock(x, y, currentBlock, currentColor, tft);
}
} else if (down) { // Rotate left (counterclockwise)
bool tempBlock[4 * 4];
getLeftRotation(tempBlock);
if (mapCheckCollision(x, y, tempBlock)) {
setRightRotation();
} else {
undrawBlock(x, y, currentBlock, tft);
memcpy(currentBlock, tempBlock, 4 * 4 * sizeof(bool));
drawBlock(x, y, currentBlock, currentColor, tft);
}
}
if (btn) { // Joystick button, drop faster
msPerDrop = 50;
} else {
msPerDrop = 500;
}
// Game has not ended
return false;
}
// Initialize two-player mode
void initTwoPlayer(Adafruit_ILI9341 &tft) {
// Call "super"
initOnePlayer(tft);
// Set game mode
gameMode = TWO_PLAYER;
// Reset lost flag
lost = false;
}
// Tick for two-player mode, returns whether the game ended
bool twoPlayerTick(
bool up, bool down, bool left, bool right, bool btn,
Adafruit_ILI9341 &tft
) {
// Check for game end
if (lost) {
return true;
}
// Check for opponent lose
if (Serial3.available() > 0) {
if (Serial3.peek() == 'E') {
Serial3.read();
return true;
}
}
// Call "super"
if (onePlayerTick(up, down, left, right, btn, tft)) {
// Tell opponent that this player lost
Serial3.print('E');
lost = true;
return true;
} else {
return false;
}
}