-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.c
executable file
·278 lines (235 loc) · 6.51 KB
/
game.c
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
/**
@file game.c
@author Andrea Covre
@version 1.3 (March 22nd, 2020)
*/
#include "game.h"
#include <stdio.h>
#include <stdlib.h>
#include "gba.h"
#include "images/welcomeScreen.h"
#include "images/gameOver.h"
#include "images/garbage.h"
#include "images/target.h"
struct snake Snake;
typedef enum {
START,
WAIT_TO_START,
PLAY,
WIN,
LOSE,
} GBAState;
char startPrompt[] = "Press Start";
char winnigText[] = "Congrats, you won!";
int updateExitCode = 0;
int main(void) {
REG_DISPCNT= MODE3 | BG2_ENABLE;
REG_DISPCNT;
// Saving current and previous state of button input.
u32 previousButtons = BUTTONS;
u32 currentButtons = BUTTONS;
// Loading initial game state
GBAState state = START;
int loopCounter = 0;
char score[] = " ";
while (1) {
waitForVBlank();
currentButtons = BUTTONS; // Loading the current state of the buttons
if (KEY_JUST_PRESSED(BUTTON_SELECT, currentButtons, previousButtons)) {
state = START;
}
switch (state) {
case START:
drawFullScreenImageDMA(startScreen);
drawCenteredString(100, 95, 50, 50, startPrompt, RED);
state = WAIT_TO_START;
break;
case WAIT_TO_START:
//clearing body memory
for (int i = 0; i < 60; i++) {
for (int j = 0; j < 40; j++) {
Snake.body[i][j] = 0;
}
}
Snake.length = 1;
Snake.direction = 0;
Snake.speed = 20;
Snake.x = 30;
Snake.y = 20;
Snake.body[30][20] = 1;
Snake.xTail = 30;
Snake.yTail = 20;
Snake.tailDirection[30][20] = 0;
Snake.targetHit = 0;
newTarget();
if (KEY_JUST_PRESSED(BUTTON_START, currentButtons, previousButtons)) {
state = PLAY;
//Prepping Screen
fillScreenDMA(RED);
drawRectDMA(4, 4, 232, 152, BLACK);
}
break;
case PLAY:
drawTarget();
updateDirection(currentButtons, previousButtons);
if (loopCounter >= Snake.speed) {
int updateExitCode = updatePosition();
if(updateExitCode == 1) {
state = LOSE;
} else if (updateExitCode == 2) {
state = WIN;
} else {
drawSnake();
drawTarget();
}
}
break;
case WIN:
fillScreenDMA(BLACK);
drawCenteredString(0, 0, 240, 160, winnigText, GREEN);
break;
case LOSE:
drawFullScreenImageDMA(gameOver);
drawCenteredString(100, 95, 50, 50, startPrompt, RED);
sprintf(score, "Score: %d", (Snake.length - 1)*10);
drawCenteredString(20, 95, 50, 50, score, RED);
state = WAIT_TO_START;
break;
}
if (loopCounter >= Snake.speed) {
loopCounter = 0;
} else {
loopCounter++;
}
previousButtons = currentButtons; // storing the current state of the buttons
}
return 0;
}
void updateDirection(u32 currentButtons, u32 previousButtons) {
if (KEY_JUST_PRESSED(BUTTON_UP, currentButtons, previousButtons)) {
if (Snake.direction == 2) {
if (Snake.length < 2) {
Snake.direction = 0;
}
} else {
Snake.direction = 0;
}
} else if (KEY_JUST_PRESSED(BUTTON_RIGHT, currentButtons, previousButtons)) {
if (Snake.direction == 3) {
if (Snake.length < 2) {
Snake.direction = 1;
}
} else {
Snake.direction = 1;
}
} else if (KEY_JUST_PRESSED(BUTTON_DOWN, currentButtons, previousButtons)) {
if (Snake.direction == 0) {
if (Snake.length < 2) {
Snake.direction = 2;
}
} else {
Snake.direction = 2;
}
} else if (KEY_JUST_PRESSED(BUTTON_LEFT, currentButtons, previousButtons)) {
if (Snake.direction == 1) {
if (Snake.length < 2) {
Snake.direction = 3;
}
} else {
Snake.direction = 3;
}
}
}
int updatePosition(void) {
switch (Snake.direction) {
case 0:
Snake.tailDirection[Snake.x][Snake.y] = 0;
Snake.y--;
break;
case 1:
Snake.tailDirection[Snake.x][Snake.y] = 1;
Snake.x++;
break;
case 2:
Snake.tailDirection[Snake.x][Snake.y] = 2;
Snake.y++;
break;
case 3:
Snake.tailDirection[Snake.x][Snake.y] = 3;
Snake.x--;
break;
}
if (Snake.x == Snake.xTarget && Snake.y == Snake.yTarget) {
newTarget();
Snake.targetHit = 1;
Snake.length++;
if (Snake.length == 2400) {
return 2;
}
if (Snake.length % 5 == 0 && Snake.speed > 15) {
Snake.speed--;
}
}
if (Snake.body[Snake.x][Snake.y] == 1) {
return 1;
} else {
Snake.body[Snake.x][Snake.y] = 1;
}
if (Snake.x < 1 || Snake.x >= 59 || Snake.y < 1 || Snake.y >= 39) {
return 1;
} else {
return 0;
}
}
void drawSnake(void) {
drawRectDMA(Snake.y * 4, Snake.x * 4, 4, 4, GREEN);
drawRectDMA(Snake.y * 4 + 1, Snake.x * 4 + 1, 2, 2, YELLOW);
//setPixel(Snake.y, Snake.x, WHITE);
if(Snake.targetHit == 0) {
drawRectDMA(Snake.yTail * 4, Snake.xTail * 4, 4, 4, GRAY);
Snake.body[Snake.xTail][Snake.yTail] = 0;
switch (Snake.tailDirection[Snake.xTail][Snake.yTail]) {
case 0:
Snake.yTail--;
break;
case 1:
Snake.xTail++;
break;
case 2:
Snake.yTail++;
break;
case 3:
Snake.xTail--;
break;
}
} else {
Snake.targetHit = 0;
switch (Snake.tailDirection[Snake.x][Snake.y]) {
case 0:
Snake.yTail = Snake.yTail;
Snake.xTail = Snake.xTail;
break;
case 1:
Snake.yTail = Snake.yTail;
Snake.xTail = Snake.xTail;
break;
case 2:
Snake.yTail = Snake.yTail;
Snake.xTail = Snake.xTail;
break;
case 3:
Snake.yTail = Snake.yTail;
Snake.xTail = Snake.xTail;
break;
}
}
}
void drawTarget(void) {
drawImageDMA(Snake.yTarget * 4, Snake.xTarget * 4, 4, 4, target);
}
void newTarget(void) {
do {
Snake.xTarget = randint(1, 59);
Snake.yTarget = randint(1, 39);
} while (Snake.body[Snake.xTarget][Snake.yTarget] == 1);
}