-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.c
250 lines (190 loc) · 7.52 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
#include <stdbool.h>
#include <stdio.h>
#include "myLib.h"
#include "game.h"
// #include "logic.h"
#include "images/background.h"
enum directionState {
UP,
DOWN,
LEFT,
RIGHT,
BEGIN,
};
int startGame(void) {
// typedef struct {
// int timeLeft;
// char dispalyStr[];
// } display;
display display1;
display1.timeLeft = 20;
display1.length = 0;
REG_DISPCNT = MODE3 | BG2_ENABLE;
// int buttondown[NBUTTONS] = {0};
// int button[NBUTTONS] = {0};
u32 previousButtons = BUTTONS;
u32 currentButtons = BUTTONS;
enum directionState state = BEGIN;
// background drawing
drawRect3(0, 0, 240, 160, BLACK);
drawImage3(15, 60, 120, 120, background);
drawRect3(50, 80, 30, 5, LIGHTGRAY);
drawRect3(50, 80, 5, 50, LIGHTGRAY);
drawRect3(50, 105, 5, 30, LIGHTGRAY);
drawRect3(80, 105, 70, 5, LIGHTGRAY);
drawRect3(95, 80, 90, 5, LIGHTGRAY);
drawRect3(48, 90, 3, 7, GRAY);
setPixel(47, 91, GRAY);
setPixel(46, 91, GRAY);
setPixel(45, 91, GRAY);
setPixel(45, 91, GRAY);
// start point
setPixel(90, 172, WHITE);
int row = 90;
int col = 172;
int speed = 1;
int speedControl = 5;
int counter = 0;
bool playing = true;
int indicate = 0; // 1:fail
// 2:interrupt
// 3:success
while(playing) { // game loop
// if (KEY_JUST_PRESSED(BUTTON_A, currentButtons, previousButtons)) {
// indicate = 2; // interrupt
// playing = false;
// }
if (counter % 50 == 0) {
display1.timeLeft -= 1;
} // end vBlankCounter if
setPixel(row, col, WHITE); // keep going
if (counter % speedControl == 0) {
display1.length += 1;
}
counter++;
currentButtons = BUTTONS;
// for (int i = 0; i < NBUTTONS; i++) {
// button[i] = KEY_DOWN_NOW(i) == 0 && buttondown[i];
// buttondown[i] = KEY_DOWN_NOW(i) != 0;
// }
switch(state) {
// all cases below:
case BEGIN:
if (KEY_JUST_PRESSED(BUTTON_A, currentButtons, previousButtons)) {
indicate = 2; // interrupt
playing = false;
}
if (counter % speedControl == 0) {
row = row - speed;
col = col;
} // end vBlankCounter if
if (KEY_JUST_PRESSED(BUTTON_UP, currentButtons, previousButtons)) {state = UP;}
if (KEY_JUST_PRESSED(BUTTON_DOWN, currentButtons, previousButtons)) {state = DOWN;}
if(KEY_JUST_PRESSED(BUTTON_RIGHT, currentButtons, previousButtons)) {state = RIGHT;}
if(KEY_JUST_PRESSED(BUTTON_LEFT, currentButtons, previousButtons)) {state = LEFT;}
break;
case UP:
if (KEY_JUST_PRESSED(BUTTON_A, currentButtons, previousButtons)) {
indicate = 2; // interrupt
playing = false;
}
if (counter % speedControl == 0) {
row = row - speed;
col = col;
} // end vBlankCounter if
if (KEY_JUST_PRESSED(BUTTON_UP, currentButtons, previousButtons)) {state = UP;}
if (KEY_JUST_PRESSED(BUTTON_DOWN, currentButtons, previousButtons)) {state = DOWN;}
if(KEY_JUST_PRESSED(BUTTON_RIGHT, currentButtons, previousButtons)) {state = RIGHT;}
if(KEY_JUST_PRESSED(BUTTON_LEFT, currentButtons, previousButtons)) {state = LEFT;}
break;
case DOWN:
if (KEY_JUST_PRESSED(BUTTON_A, currentButtons, previousButtons)) {
indicate = 2; // interrupt
playing = false;
}
if (counter % speedControl == 0) {
row = row + speed;
col = col;
} // end vBlankCounter if
if (KEY_JUST_PRESSED(BUTTON_UP, currentButtons, previousButtons)) {state = UP;}
if (KEY_JUST_PRESSED(BUTTON_DOWN, currentButtons, previousButtons)) {state = DOWN;}
if(KEY_JUST_PRESSED(BUTTON_RIGHT, currentButtons, previousButtons)) {state = RIGHT;}
if(KEY_JUST_PRESSED(BUTTON_LEFT, currentButtons, previousButtons)) {state = LEFT;}
break;
case LEFT:
if (KEY_JUST_PRESSED(BUTTON_A, currentButtons, previousButtons)) {
indicate = 2; // interrupt
playing = false;
}
if (counter % speedControl == 0) {
row = row;
col = col - 1;
} // end vBlankCounter if
if (KEY_JUST_PRESSED(BUTTON_UP, currentButtons, previousButtons)) {state = UP;}
if (KEY_JUST_PRESSED(BUTTON_DOWN, currentButtons, previousButtons)) {state = DOWN;}
if(KEY_JUST_PRESSED(BUTTON_RIGHT, currentButtons, previousButtons)) {state = RIGHT;}
if(KEY_JUST_PRESSED(BUTTON_LEFT, currentButtons, previousButtons)) {state = LEFT;}
break;
case RIGHT:
if (KEY_JUST_PRESSED(BUTTON_A, currentButtons, previousButtons)) {
indicate = 2; // interrupt
playing = false;
}
if (counter % speedControl == 0) {
row = row;
col = col + 1;
} // end vBlankCounter if
if (KEY_JUST_PRESSED(BUTTON_UP, currentButtons, previousButtons)) {state = UP;}
if (KEY_JUST_PRESSED(BUTTON_DOWN, currentButtons, previousButtons)) {state = DOWN;}
if(KEY_JUST_PRESSED(BUTTON_RIGHT, currentButtons, previousButtons)) {state = RIGHT;}
if(KEY_JUST_PRESSED(BUTTON_LEFT, currentButtons, previousButtons)) {state = LEFT;}
break;
}
previousButtons = currentButtons;
waitForVblank();
if (display1.timeLeft < 0) {
indicate = 1;
playing = false;
}
if ((row == 54 || row == 53) && (col == 90 || col == 91 || col == 92)) {
indicate = 3; // success
playing = false;
} else if (row < 54 || row > 95 || col < 85 || col > 174) {
indicate = 1; // fail
playing = false;
} else if (row >= 54 && row <=83) {
if (col < 54 || col > 105) {
indicate = 1; // fail
playing = false;
}
} else if (row > 83 && row <= 95) {
if (col < 53 || col > 174) {
indicate = 1; // fail
playing = false;
}
}
// if (KEY_JUST_PRESSED(BUTTON_A, currentButtons, previousButtons)) {
// indicate = 2; // interrupt
// playing = false;
// }
char buffer1[51];
char buffer2[51];
int countDown = display1.timeLeft;
int totalLength = display1.length;
sprintf(buffer1, "Time Left: %d", countDown);
if (countDown > 5) {
drawString(2, 2, buffer1, CYAN);
} else {
drawString(2, 2, buffer1, RED);
}
sprintf(buffer2, "Length: %d", totalLength);
drawString(15, 2, buffer2, CYAN);
if (counter % 50 == 0) {
drawRect3(2, 60, 50, 10, BLACK);
}
if (counter % speedControl == 0) {
drawRect3(15, 45, 40, 10, BLACK);
}
} // end while(1)
return indicate;
}