-
Notifications
You must be signed in to change notification settings - Fork 0
/
backuo
262 lines (209 loc) · 6.04 KB
/
backuo
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
#include "game.h"
#include <stdio.h>
#include <stdlib.h>
#include "gba.h"
/* TODO: */
// Include any header files for title screen or exit
// screen images generated by nin10kit. Example for the provided garbage
// image:
// #include "images/garbage.h"
#include "images/welcomeScreen.h"
#include "images/gameOver.h"
#include "images/garbage.h"
struct snake Snake;
/* TODO: */
// Add any additional states you need for your app.
typedef enum {
START,
WAIT_TO_START,
PLAY,
WIN,
LOSE,
} GBAState;
//Prototypes
void delay(int n);
void updateDirection(u32 currentButtons, u32 previousButtons);
void drawSnake(void);
int updatePosition(void);
void drawTarget(void);
void newTarget(void);
//void drawBodySegment(void);
//void refresh();
//My own stuff -----------------------------------------
char startPrompt[] = "Press Start";
//------------------------------------------------------
int main(void) {
/* TODO: */
// Manipulate REG_DISPCNT here to set Mode 3. //
REG_DISPCNT= MODE3 | BG2_ENABLE;
REG_DISPCNT;
// Save current and previous state of button input.
u32 previousButtons = BUTTONS;
u32 currentButtons = BUTTONS;
// Load initial game state
GBAState state = START;
int loopCounter = 0;
char score[] = " ";
while (1) {
waitForVBlank();
currentButtons = BUTTONS; // Load the current state of the buttons
/* TODO: */
// Manipulate the state machine below as needed //
// NOTE: Call waitForVBlank() before you draw
//drawFullScreenImageDMA(startScreen);
//drawImageDMA(10, 20, GARBAGE_WIDTH, GARBAGE_HEIGHT, garbage);
//drawRectDMA(10, 50, 80, 30, MAGENTA);
//fillScreenDMA(MAGENTA);
//drawRectDMA(int row, int col, int width, int height, volatile u16 color) {
//delay(1000);
//sprintf(score, "%d", loopCounter);
switch (state) {
case START:
drawFullScreenImageDMA(startScreen);
drawCenteredString(100, 95, 50, 50, startPrompt, RED);
state = WAIT_TO_START;
break;
case WAIT_TO_START:
Snake.length = 1;
Snake.direction = 0;
Snake.speed = 45;//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);
}
// state = ?
break;
case PLAY:
drawTarget();
updateDirection(currentButtons, previousButtons);
if (loopCounter >= Snake.speed) {
if(updatePosition()) {
state = LOSE;
}
drawSnake();
drawTarget();
}
//state = WIN;
// state = ?
break;
case WIN:
// state = ?
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;
}
//every 60 cycles (around a 1 second in real life) the loopCounter gets reseted
if (loopCounter >= Snake.speed) {
loopCounter = 0;
} else {
loopCounter++;
}
previousButtons = currentButtons; // Store the current state of the buttons
}
return 0;
}
void delay(int n) {
volatile int x = 0;
for (int i = 0; i < n * 8000; i++) {
x++;
}
}
void updateDirection(u32 currentButtons, u32 previousButtons) {
if (KEY_JUST_PRESSED(BUTTON_UP, currentButtons, previousButtons)) {
Snake.direction = 0;
} else if (KEY_JUST_PRESSED(BUTTON_RIGHT, currentButtons, previousButtons)) {
Snake.direction = 1;
} else if (KEY_JUST_PRESSED(BUTTON_DOWN, currentButtons, previousButtons)) {
Snake.direction = 2;
} else if (KEY_JUST_PRESSED(BUTTON_LEFT, currentButtons, previousButtons)) {
Snake.direction = 3;
}
}
int updatePosition(void) {
switch (Snake.direction) {
case 0:
Snake.y--;
Snake.tailDirection[Snake.x][Snake.y] = 0;
Snake.body[Snake.x][Snake.y] = 1;
break;
case 1:
Snake.x++;
Snake.tailDirection[Snake.x][Snake.y] = 1;
Snake.body[Snake.x][Snake.y] = 1;
break;
case 2:
Snake.y++;
Snake.tailDirection[Snake.x][Snake.y] = 2;
Snake.body[Snake.x][Snake.y] = 1;
break;
case 3:
Snake.x--;
Snake.tailDirection[Snake.x][Snake.y] = 3;
Snake.body[Snake.x][Snake.y] = 1;
break;
}
if (Snake.x == Snake.xTarget && Snake.y == Snake.yTarget) {
newTarget();
Snake.targetHit = 1;
Snake.length++;
if (Snake.length % 5 == 0 && Snake.speed > 10) {
Snake.speed--;
}
}
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, BLUE);
switch (Snake.tailDirection[Snake.x][Snake.y]) {
case 0:
Snake.yTail--;
break;
case 1:
Snake.xTail++;
break;
case 2:
Snake.yTail++;
break;
case 3:
Snake.xTail--;
break;
}
} else {
Snake.targetHit = 0;
}
}
void drawTarget(void) {
drawRectDMA(Snake.yTarget * 4, Snake.xTarget * 4, 4, 4, MAGENTA);
drawRectDMA(Snake.yTarget * 4 + 1, Snake.xTarget * 4 + 1, 2, 2, RED);
}
void newTarget(void) {
Snake.xTarget = randint(1, 59);
Snake.yTarget = randint(1, 39);
}
//void drawBodySegment(void) {
// }
//void refresh() {
//}