-
Notifications
You must be signed in to change notification settings - Fork 7
/
SnakeGame.h
241 lines (229 loc) · 6.37 KB
/
SnakeGame.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
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////
/////////
///////// S N A K E G A M E
/////////
///////// By Stephen Cropp
/////////
/////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
class CSnakeGame : public CGame
{
int count;
public:
static void getGameIcon(byte *dst, byte count)
{
dst[ 0] = 0b00000000;
dst[ 1] = 0b01110000;
dst[ 2] = 0b01000000;
dst[ 3] = 0b01000000;
dst[ 4] = 0b01000000;
dst[ 5] = 0b01000000;
dst[ 6] = 0b00000000;
dst[ 7] = 0b00000000;
dst[ 8] = 0b00000000;
dst[ 9] = 0b00000000;
dst[10] = 0b00000000;
dst[11] = 0b00000000;
dst[12] = 0b00000000;
dst[13] = 0b00001000;
dst[14] = 0b00000000;
dst[15] = 0b00000000;
switch(count & 0b00001100)
{
case 0b00000000:
dst[ 1] = 0b01110000;
dst[ 9] = 0b00010010;
break;
case 0b00000100:
dst[ 1] = 0b01111100;
dst[ 9] = 0b00000110;
dst[ 4] = 0b00000000;
dst[ 5] = 0b00000000;
break;
case 0b00001000:
dst[ 1] = 0b01111110;
dst[ 2] = 0b00000010;
dst[10] = 0b00000010;
dst[ 3] = 0b00000000;
dst[ 4] = 0b00000000;
dst[ 5] = 0b00000000;
break;
case 0b00001100:
dst[ 1] = 0b01111110;
dst[ 2] = 0b00000010;
dst[ 3] = 0b00000010;
dst[ 4] = 0b00000010;
dst[12] = 0b00000010;
dst[ 5] = 0b00000000;
break;
}
}
// Speed
char speed;
// snake 64 vectors
char snakeX[64];
char snakeY[64];
// snake head 0-63
char snakeHead;
char snakeHeadOld;
// snake tail 0-63
char snakeTail;
char snakeTailOld;
// controls
// A Rotate Left
// B Rotate Right
char grow;
char rot;
char hdg;
char level;
char treeCount;
//score == gameScore
// Grid disp8x8 snake red
// Level disp8x8 plants green
void plantTree()
{
treeCount++;
if (treeCount == level+5) newLevel();
char planted = 0;
char X = 0;
char Y = 0;
while (!planted)
{
X = random(8);
Y = random(8);
if (Disp8x8.get(Y, X) != DISP_RED)
{
Disp8x8.set(Y, X, DISP_GREEN);
planted = -1;
}
playSound(500,100);
}
}
void newLevel()
{
playSound(500,100);
Disp8x8.delayWithRefresh(30);
playSound(600,100);
Disp8x8.delayWithRefresh(30);
playSound(700,100);
Disp8x8.delayWithRefresh(30);
playSound(800,100);
Disp8x8.delayWithRefresh(30);
Disp8x8.cls();
Timer1Period -= 10;
snakeTail = snakeHead > 1 ? snakeHead - 2 : snakeHead = 0 ? 62 : 63;
Disp8x8.set(snakeY[snakeHead], snakeX[snakeHead], DISP_RED);
Disp8x8.set(snakeY[snakeHead-1], snakeX[snakeHead-1], DISP_RED);
// Disp8x8.set(snakeY[snakeHead-2], snakeX[snakeHead-2], DISP_RED);
level++;
treeCount = 0;
plantTree();
}
void init()
{
Timer1Period = 250;
count=0;
speed=200;
hdg=0;
rot=0;
grow=0;
level=4;
newLevel();
Disp8x8.cls();
for(byte i=0;i<64;i++)
{
snakeX[i]=0; snakeY[i]=0;
}
snakeHead=1;
snakeTail=0;
snakeX[snakeHead]=2; snakeY[snakeHead]=4;
snakeX[snakeTail]=0; snakeY[snakeTail]=4;
plantTree();
}
void handleEvent(char event)
{
switch(event)
{
case EV_PRESS_B:
rot = 1;
break;
case EV_PRESS_D:
rot = -1;
break;
case EV_TIMER_1:
// move tail
if(grow)
{
gameScore += 100;
grow--;
}
else
{
Disp8x8.set(snakeY[snakeTail], snakeX[snakeTail], DISP_OFF);
snakeTailOld = snakeTail;
snakeTail = snakeTail < 63 ? snakeTail + 1 : 0;
}
// move head
Disp8x8.set(snakeY[snakeHead], snakeX[snakeHead], DISP_RED);
snakeHeadOld = snakeHead;
snakeHead = snakeHead < 63 ? snakeHead + 1 : 0;
if(rot)
{
hdg += rot;
if (hdg < 0) hdg = 3;
if (hdg > 3) hdg = 0;
rot = 0;
}
switch(hdg)
{
case 0:
snakeX[snakeHead]=snakeX[snakeHeadOld]<7?snakeX[snakeHeadOld]+1:0;
snakeY[snakeHead]=snakeY[snakeHeadOld];
break;
case 1:
snakeX[snakeHead]=snakeX[snakeHeadOld];
snakeY[snakeHead]=snakeY[snakeHeadOld]<7?snakeY[snakeHeadOld]+1:0;
break;
case 2:
snakeX[snakeHead]=snakeX[snakeHeadOld]>0?snakeX[snakeHeadOld]-1:7;
snakeY[snakeHead]=snakeY[snakeHeadOld];
break;
case 3:
snakeX[snakeHead]=snakeX[snakeHeadOld];
snakeY[snakeHead]=snakeY[snakeHeadOld]>0?snakeY[snakeHeadOld]-1:7;
break;
}
//collision detect
if(Disp8x8.get(snakeY[snakeHead], snakeX[snakeHead]) == DISP_RED)
{
Disp8x8.set(snakeY[snakeHead], snakeX[snakeHead], DISP_YELLOW);
playSound(400,100);
Disp8x8.delayWithRefresh(30);
playSound(300,100);
Disp8x8.delayWithRefresh(30);
playSound(200,100);
Disp8x8.delayWithRefresh(30);
playSound(100,100);
endGame();
}
else if(Disp8x8.get(snakeY[snakeHead], snakeX[snakeHead]) == DISP_GREEN)
{
Disp8x8.set(snakeY[snakeHead], snakeX[snakeHead], DISP_YELLOW);
grow = 1;
plantTree();
}
else
{
Disp8x8.set(snakeY[snakeHead], snakeX[snakeHead], DISP_YELLOW);
gameScore += snakeHead - snakeTail;
}
break;
}
}
};
CGameFactoryImpl<CSnakeGame> SnakeGame;