-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.h
457 lines (429 loc) · 12 KB
/
game.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
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
/*
* game.h has runGame() function used by main_menu.h
* and it contains the game logic
* in a few variables and functions
*/
#include "utils.h"
#include "song.h"
struct point {
int x;
int y;
};
// Blocks used to validate rows and complete the game
char blocks[][4][8] = {
{{0, 0, 0, 1, 0, 0, 0, 0},
{0, 0, 0, 2, 1, 0, 0, 0},
{0, 0, 0, 1, 0, 0, 0, 0}},
{{0, 0, 0, 0, 1, 0, 0, 0},
{0, 0, 0, 0, 2, 0, 0, 0},
{0, 0, 0, 0, 1, 0, 0, 0},
{0, 0, 0, 0, 1, 0, 0, 0}},
{
{0, 0, 0, 0, 1, 0, 0, 0},
{0, 0, 1, 2, 1, 0, 0, 0},
},
{
{0, 0, 0, 1, 1, 0, 0, 0},
{0, 0, 0, 1, 1, 0, 0, 0},
},
{
{0, 0, 0, 0, 1, 1, 0, 0},
{0, 0, 0, 1, 2, 0, 0, 0},
},
{
{0, 0, 0, 1, 0, 0, 0, 0},
{0, 0, 1, 2, 1, 0, 0, 0},
},
{
{0, 0, 0, 1, 1, 0, 0, 0},
{0, 0, 0, 0, 2, 1, 0, 0},
},
};
/*
* Keeps track of the moving block
* First 4 rows are not visible on LED matrix
*/
char moving[12][8];
// Keeps track of stationary block elements
char stationary[8][8];
// Game variables
int score;
int level;
int countUpdates;
unsigned long lastUpdate;
int updateInterval;
const unsigned int inputDelay = 120; // minimum delay for input (possibly more w/ debounce)
unsigned long lastInputTimer;
/*
* Function to change (x,y) for the moving block
*/
void transformMoving(int x, int y) {
char updatedArr[12][8];
int xValid = 1;
for (int i = 0; i < 12; i++) {
for (int j = 0; j < 8; j++) {
updatedArr[i][j] = 0;
}
}
for (int i = 0; i < 12 && xValid; i++) {
for (int j = 0; j < 8 && xValid; j++) {
if (moving[i][j]) {
if (j + x >= 8 || j + x < 0) {
xValid = 0;
}
}
}
}
for (int i = 0; i < 12; i++) {
for (int j = 0; j < 8; j++) {
if (moving[i][j]) {
if (i + y < 12 && i + y >= 0) {
if (xValid) {
if (i >= 4 && stationary[i - 4 + y][j + x]) {
return;
}
updatedArr[i + y][j + x] = moving[i][j];
} else {
updatedArr[i + y][j] = moving[i][j];
}
}
}
}
}
for (int i = 0; i < 12; i++) {
for (int j = 0; j < 8; j++) {
moving[i][j] = updatedArr[i][j];
}
}
}
// Game is over if first row has stationary block elements
int isGameOver() {
for (int j = 0; j < 8; j++) {
if (stationary[0][j]) {
return 1;
}
}
return 0;
}
// Rotation by 90 for moving block when pressing button
void rotate90() {
struct point pivot = {
-1,
-1
};
char updatedArr[12][8];
for (int i = 0; i < 12; i++) {
for (int j = 0; j < 8; j++) {
updatedArr[i][j] = 0;
if (moving[i][j] == 2) {
pivot.y = i;
pivot.x = j;
}
}
}
if (pivot.y == -1 || pivot.x == -1) {
return;
}
for (int i = 0; i < 12; i++) {
for (int j = 0; j < 8; j++) {
if (moving[i][j] == 1) {
struct point toCoordinate = {
j - pivot.x,
i - pivot.y
};
struct point transformedCoordinate = {
-toCoordinate.y,
toCoordinate.x
};
struct point transformedGrid = {
transformedCoordinate.x + pivot.x,
transformedCoordinate.y + pivot.y
};
if (transformedGrid.x >= 8 || transformedGrid.x < 0 || transformedGrid.y >= 12 || transformedGrid.y < 0 || (transformedGrid.y >= 4 && stationary[transformedGrid.y - 4][transformedGrid.x])) {
return;
}
updatedArr[transformedGrid.y][transformedGrid.x] = 1;
} else if (moving[i][j] == 2) {
updatedArr[i][j] = 2;
}
}
}
for (int i = 0; i < 12; i++) {
for (int j = 0; j < 8; j++) {
moving[i][j] = updatedArr[i][j];
}
}
}
// Using input to control the moving block and to pause the game
void handleInput() {
int input = none;
int X_Axis = analogRead(joystickPinX); // read the x axis value
int Y_Axis = analogRead(joystickPinY); // read the y axis value
int buttonValue = registerReadPin(buttonPin); // read the state of the button
buttonValue = map(buttonValue, 0, 1, 1, 0); // invert the input from the button to be high when pressed
if (reverseJoystick) {
X_Axis = map(X_Axis, 0, 1024, 1024, 0);
}
int pauseButtonValue = registerReadPin(pauseButtonPin);
pauseButtonValue = map(pauseButtonValue, 0, 1, 1, 0); // invert the input from the button to be high when pressed
if (pauseButtonValue == 1) {
lastPress = millis() / divideTime;
input = pause;
} else if (X_Axis >= maxThreshold) {
input = right;
} else if (X_Axis <= minThreshold) {
input = left;
} else if (Y_Axis >= maxThreshold) {
input = up;
} else if (Y_Axis <= minThreshold) {
input = down;
}
// not inside switch because I want more interactions
if (buttonValue == 1 && (millis() / divideTime - lastPress) > debounceDelay / divideTime) {
lastPress = millis() / divideTime;
rotate90();
}
switch (input) {
case left:
transformMoving(-1, 0);
break;
case right:
transformMoving(1, 0);
break;
case up:
case down:
lastUpdate -= updateInterval;
score++;
break;
case pause:
if (!isGameOver()) {
gamePaused = true;
screenStatus = STATUS_MAINMENU;
pointerMode = POINTER_SCROLL;
currentMainMenuIndex = 0;
lc.shutdown(0, true);
lcd.clear();
printLine(0, "===GAME PAUSED===");
lcd.setCursor(0, 1);
lcd.write("Please resume");
for (int i = 0; i < 3; i++) {
lcd.write("!");
delay(400);
}
debounceDelay = 800;
}
break;
}
}
// Checking if moving block has landed
int isMovingAtBottom() {
for (int i = 0; i < 12; i++) {
for (int j = 0; j < 8; j++) {
if (moving[i][j] && i == 11 || (moving[i][j] && i >= 4 && i + 1 < 12 && stationary[i - 4 + 1][j])) {
return 1;
}
}
}
return 0;
}
// Landing the moving block and become stationary
void handleAtBottom() {
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
stationary[i][j] = stationary[i][j] || moving[i + 4][j];
moving[i + 4][j] = 0;
}
}
}
// Render LED matrix
void render() {
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
lc.setLed(0, i, j, moving[i + 4][j] | stationary[i][j]);
}
}
}
// Render LCD display
void renderLcd() {
lcd.clear();
lcd.setCursor(1, 0);
lcd.print("Level: ");
lcd.print(level);
lcd.setCursor(1, 1);
lcd.print("Score: ");
lcd.print(score);
}
// Upgrade score and clear row if we finish the bottom row
void updateRows() {
int rowsUpdated = 0;
for (int i = 0; i < 8; i++) {
int validRow = 1;
for (int j = 0; j < 8; j++) {
if (!stationary[i][j]) {
validRow = 0;
}
}
if (validRow) {
tone(BUZZER, 1000);
delay(50);
noTone(BUZZER);
rowsUpdated++;
for (int j = 0; j < 8; j++) {
stationary[i][j] = 0;
}
char updated[8][8];
for (int i = 0; i < 8; i++)
for (int j = 0; j < 8; j++) {
updated[i][j] = 0;
}
for (int k = i; k >= 0; k--) {
for (int l = 0; l < 8; l++) {
if (k - 1 >= 0) {
updated[k][l] = stationary[k - 1][l];
}
}
}
for (int k = i + 1; k < 8; k++) {
for (int l = 0; l < 8; l++) {
updated[k][l] = stationary[k][l];
}
}
for (int k = 0; k < 8; k++) {
for (int l = 0; l < 8; l++) {
stationary[k][l] = updated[k][l];
}
}
}
}
switch (rowsUpdated) {
case 1:
score += 40 * (level + 1 + difficultyValue - EASY);
break;
case 2:
score += 100 * (level + 1 + difficultyValue - EASY);
break;
case 3:
score += 300 * (level + 1 + difficultyValue - EASY);
break;
case 4:
score += 1200 * (level + 1 + difficultyValue - EASY);
break;
}
}
// Generate new moving block
void queueNewBlock() {
int randomBlock = random(BLOCK_COUNT - 1);
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 8; j++) {
moving[i][j] = blocks[randomBlock][i][j];
}
}
}
// Called per updateInterval seconds
void updateState() {
if (isMovingAtBottom()) {
handleAtBottom();
queueNewBlock();
}
transformMoving(0, 1);
updateRows();
}
// Called when game is over and when booting first time
void restart() {
memset(moving, 0, sizeof(moving) * sizeof(char));
memset(stationary, 0, sizeof(stationary) * sizeof(char));
lastUpdate = 0;
lastInputTimer = 0;
score = 0;
level = 0;
countUpdates = 0;
}
// Setup for game
void bootGame() {
restart();
lc.shutdown(0, false);
pinMode(UNUSED_ANALOG, INPUT);
randomSeed(analogRead(UNUSED_ANALOG));
queueNewBlock();
lcd.begin(16, 2);
if (difficultyValue == EASY) {
for (int i = 0; i < 7; i++)
stationary[7][i] = 1;
updateInterval = 1000 - level * 4;
} else if (difficultyValue == MEDIUM) {
updateInterval = 700 - level * 8;
} else if (difficultyValue == HARD) {
updateInterval = 300 - level * 15;
}
}
// Print name & score position in the top
void congrulations(const int & score) {
int place = saveHighScore(currentUsername, score);
if (place == -1) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(currentUsername);
lcd.print(', your score');
lcd.setCursor(0, 1);
lcd.print("is not in top :(");
} else {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(currentUsername);
lcd.setCursor(0, 1);
lcd.print("you are ");
if (place == 1) {
lcd.print("1st ");
} else if (place == 2) {
lcd.print("2nd ");
} else if (place == 3) {
lcd.print("3rd ");
}
}
}
// Used by Main Menu for controlling the game state
int runGame() {
if (isGameOver()) {
lcd.clear();
lcd.setCursor(2, 0);
lcd.print("Game Over!");
lcd.setCursor(0, 1);
lcd.print("Score: ");
lcd.print(score);
for (int t = 0; t < 3; t++) {
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
lc.setLed(0, i, j, true);
}
}
delay(500);
render();
delay(500);
}
delay(3000);
congrulations(score);
lc.shutdown(0, true);
delay(3000);
return 1;
} else {
if (millis() - lastInputTimer > inputDelay) {
handleInput();
lastInputTimer = millis();
}
if (millis() - lastUpdate > updateInterval) {
tone(BUZZER, 100);
delay(2);
noTone(BUZZER);
updateState();
renderLcd();
lastUpdate = millis();
countUpdates++;
if (countUpdates == 15) {
countUpdates = 0;
level++;
}
}
render();
return 0;
}
}