-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGame2.ino
More file actions
83 lines (76 loc) · 2.34 KB
/
Game2.ino
File metadata and controls
83 lines (76 loc) · 2.34 KB
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
void startGame2() {
for (int i = 0; i < 4; i++) {
int tmp = random(511);
//Make sure there are no two food items on same cell
while (isFood(tmp) || ((tmp % 16) == 15)) {
tmp = random(511);
}
food[i] = tmp;
}
displayFood(true);
currentSnakeSize = 0;
}
//Reset Game 2 to start from beginning
void resetGame2() {
for (int i = 0; i<MAX_LEVEL; i++) {
tail[i][0] = -1;
tail[i][1] = matrix.Color333(0, 7, 0);
food[i] = -1;
}
tail[0][0] = 131; //Tail is always one bigger
currentX = 8;
currentY = 16;
startGame2();
timerStart = millis();
}
void playGame2(int currentPos) {
if (currentPos != tail[0][0]) {
if(isTouchingTheTail(currentPos) || timeRanout) {
int row = currentPos % 16; //y axis (0 to 7)
int col = currentPos / 16; //x axis (0 to 31)
//blink
matrix.drawPixel(col, row, matrix.Color333(0, 0, 0));
delay(500);
matrix.drawPixel(col, row, matrix.Color333(7, 7, 7));
delay(500);
matrix.drawPixel(col, row, matrix.Color333(0, 0, 0));
delay(500);
matrix.drawPixel(col, row, matrix.Color333(7, 7, 7));
if (timeRanout) {
displayTimeOut();
delay(3000);
}
displayScore(currentSnakeSize);
delay(10000);
game = -1; //go back to select game
}
if(isFood(currentPos) == true) {
currentSnakeSize = currentSnakeSize + 1;
int foodToAdd = random(1,3);
for (int i = 0; i < foodToAdd; i++) {
// Add new Random Food
int tmp = random(511);
//Make sure there are no two food items on same cell
while (isFood(tmp)) {
tmp = random(511);
}
addFood(tmp);
}
eatFood(currentPos);
}
if (currentSnakeSize >= MAX_LEVEL) {
displayWin();
delay(3000);
game = -1;
return;
}
int clearPos = addToTail(currentPos, matrix.Color333(7, 0, 0));
int row = clearPos % 16; //y axis (0 to 7)
int col = clearPos / 16; //x axis (0 to 31)
matrix.drawPixel(col, row, matrix.Color333(0, 0, 0));
}
int row = currentPos % 16; //y axis (0 to 7)
int col = currentPos / 16; //x axis (0 to 31)
blinkLED(col, row, matrix.Color333(0, 0, 7));
displayFood(false); //fix for bug when food disappear sometimes!
}