-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
executable file
·335 lines (274 loc) · 10.3 KB
/
main.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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
#include "main.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/start.h"
#include "images/gradient.h"
#include "images/target.h"
#include "images/wasted.h"
#include "images/win.h"
/* TODO: */
// Add any additional states you need for your app. You are not requried to use
// these specific provided states.
int main(void) {
/* TODO: */
// Manipulate REG_DISPCNT here to set Mode 3. //
REG_DISPCNT = MODE3 | BG2_ENABLE;
// Save current and previous state of button input.
u32 previousButtons = BUTTONS;
u32 currentButtons = BUTTONS;
// Load initial application state
enum gba_state state = START;
int score = 0;
int final_score;
int high_score = 0;
while (1) {
currentButtons = BUTTONS; // Load the current state of the buttons
/* TODO: */
// Manipulate the state machine below as needed //
// NOTE: Call waitForVBlank() before you draw
waitForVBlank();
switch (state) {
case START:
drawFullScreenImageDMA(start);
int col = 130;
int row = 95;
int speed = 2;
while (1) {
delay(1);
drawRectDMA(row, col, 105, 10, BACKGROUND);
waitForVBlank();
col += speed;
if (col < 0) {
col = 0;
speed = -speed;
}
if (col > WIDTH - 105) {
col = WIDTH - 105;
speed = -speed;
}
drawString(row, col, "PRESS UP TO START", WHITE);
if (KEY_DOWN(BUTTON_UP, BUTTONS)) {
drawRectDMA(0, 0, WIDTH, HEIGHT, BLACK);
state = PLAY;
break;
}
}
// state = ?
break;
case PLAY:
;
score = 0;
char result[30];
int player_velocity = 3;
int ball_velocity = 2;
struct target_state cts, pts;
struct g_target * tp, * otp;
for (int i = 0; i < NUM_TILES; i++) {
cts.targets[i].hit = 0;
}
struct ball * bp, * obp;
struct state cs, ps;
cs.gamestate = GS_INITBOUNCE;
cs.size = 5;
bp = & cs.balls;
bp -> row = 80;
bp -> col = 120;
bp -> cd = ball_velocity;
bp -> rd = ball_velocity;
bp -> color = WHITE;
struct player * rp, * orp;
struct player_state crs, prs;
crs.width = 24;
crs.height = 8;
rp = & crs.players;
rp -> row = 120;
rp -> col = 120;
rp -> cd = player_velocity;
rp -> rd = player_velocity;
// Run forever (or until the power runs out)
while (1) {
if (KEY_DOWN(BUTTON_SELECT, BUTTONS)) {
state = START;
break;
}
//score
drawString(140, 10, "SCORE: ", WHITE);
sprintf(result, "%d", score);
drawRectDMA(140, 50, 20, 20, BLACK);
drawString(140, 50, result, WHITE);
if (score == NUM_TILES) {
final_score = score;
high_score = 0;
if (high_score > final_score) {
high_score = final_score;
}
state = WIN;
break;
}
//change ball velocity depending on number of tiles left:
if (score > 10 && score < 20) {
ball_velocity = 3;
player_velocity = 5;
}
if (score >= 20) {
ball_velocity = 4;
player_velocity = 7;
}
//tiles
pts = cts;
int row_disp = 0;
int col_disp = 3;
for (int i = 0; i < NUM_TILES; i++) {
cts.targets[i].row = 10 + row_disp;
cts.targets[i].col = col_disp;
cts.targets[i].width = 20;
cts.targets[i].height = 10;
if (col_disp > 200) {
col_disp = 3;
row_disp += 20;
} else {
col_disp += 21;
}
}
// See if we need to bounce off a wall
ps = cs;
bp = & cs.balls;
bp -> cd = ball_velocity * (bp -> cd < 0 ? -1 : 1);
bp -> rd = ball_velocity * (bp -> rd < 0 ? -1 : 1);
rp -> cd = player_velocity;
bp -> row = bp -> row + bp -> rd;
bp -> col += bp -> cd;
if (bp -> row < 0) {
bp -> row = 0;
bp -> rd = -bp -> rd;
}
if (bp -> row > HEIGHT - 5) {
// bp->row = HEIGHT - 5;
// bp->rd = -bp->rd;
state = LOSE;
final_score = score;
break;
}
if (bp -> col < 0) {
bp -> col = 0;
bp -> cd = -bp -> cd;
}
if (bp -> col > WIDTH - 5) {
bp -> col = WIDTH - 5;
bp -> cd = -bp -> cd;
}
if (((bp -> row > rp -> row && bp -> row < rp -> row + crs.height) || (bp -> row + cs.size > rp -> row && bp -> row + cs.size < rp -> row + crs.height)) && bp -> col > rp -> col && bp -> col < rp -> col + crs.width) {
bp -> rd = -bp -> rd;
}
for (int i = 0; i < NUM_TILES; i++) {
if (cts.targets[i].hit == 0 && ((bp -> row > cts.targets[i].row && bp -> row < cts.targets[i].row + cts.targets[i].height) || (bp -> row + cs.size > cts.targets[i].row && bp -> row + cs.size < cts.targets[i].row + cts.targets[i].height)) && bp -> col > cts.targets[i].col && bp -> col < cts.targets[i].col + cts.targets[i].width) {
bp -> rd = -bp -> rd;
cts.targets[i].hit = 1;
score++;
}
}
//player
prs = crs;
rp = & crs.players;
if (KEY_DOWN(BUTTON_LEFT, BUTTONS)) {
if (rp -> col < 5) {
rp -> col = 5;
}
rp -> col = rp -> col - rp -> cd;
}
if (KEY_DOWN(BUTTON_RIGHT, BUTTONS)) {
if (rp -> col > WIDTH - 5 - crs.width) {
rp -> col = WIDTH - 5 - crs.width;
}
rp -> col = rp -> col + rp -> cd;
}
// Draw the ball
delay(1);
waitForVBlank();
for (int i = 0; i < NUM_TILES; i++) {
otp = & pts.targets[i];
if (otp -> hit == 1) {
drawRectDMA(otp -> row, otp -> col, 20, 10, BLACK);
} else {
continue;
}
}
for (int i = 0; i < NUM_TILES; i++) {
tp = & cts.targets[i];
if (tp -> hit == 0) {
drawImageDMA(tp -> row, tp -> col, 20, 10, target);
} else {
continue;
}
}
obp = & ps.balls;
drawRectDMA(obp -> row, obp -> col,
ps.size, ps.size,
BLACK);
bp = & cs.balls;
drawRectDMA(bp -> row, bp -> col, cs.size, cs.size, bp -> color);
orp = & prs.players;
drawRectDMA(orp -> row, orp -> col, prs.width, prs.height, BLACK);
rp = & crs.players;
drawImageDMA(rp -> row, rp -> col, crs.width, crs.height, gradient);
} // Game Loop
// state = ?
break;
case WIN:
drawFullScreenImageDMA(win);
final_score = score;
if (final_score > high_score) {
high_score = final_score;
}
char result3[30];
char result4[30];
while (1) {
drawString(10, 10, "PRESS BACKSPACE TO START AGAIN", WHITE);
drawString(140, 10, "YOUR SCORE: ", WHITE);
sprintf(result3, "%d", final_score);
drawString(140, 80, result3, WHITE);
drawString(150, 10, "HIGH SCORE: ", WHITE);
sprintf(result4, "%d", high_score);
drawString(150, 80, result4, WHITE);
if (KEY_DOWN(BUTTON_SELECT, BUTTONS)) {
state = START;
break;
}
}
// state = ?
break;
case LOSE:
drawFullScreenImageDMA(wasted);
final_score = score;
if (final_score > high_score) {
high_score = final_score;
}
char result1[30];
char result2[30];
while (1) {
drawString(10, 10, "PRESS BACKSPACE TO START AGAIN", WHITE);
drawString(140, 10, "YOUR SCORE: ", WHITE);
sprintf(result1, "%d", final_score);
drawString(140, 80, result1, WHITE);
drawString(150, 10, "HIGH SCORE: ", WHITE);
sprintf(result2, "%d", high_score);
drawString(150, 80, result2, WHITE);
if (KEY_DOWN(BUTTON_SELECT, BUTTONS)) {
state = START;
break;
}
}
// state = ?
break;
}
previousButtons = currentButtons; // Store the current state of the buttons
}
UNUSED(previousButtons); // You can remove this once previousButtons is used
return 0;
}