-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
170 lines (123 loc) · 4.02 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
#include "main.h"
#include <stdio.h>
#include <stdlib.h>
/* TODO: */
/* TODO: */
// Include any header files for title screen or exit
// screen images generated by nin10kit. Example for the provided garbage
// image:
// screen images
#include "images/start_screen.h"
#include "images/snow_scene.h"
#include "images/win_screen.h"
#include "images/lose_screen.h"
/* TODO: */
// Add any additional states you need for your app. You are not requried to use
// these specific provided states.
enum gba_state {
START,
PLAY,
WIN,
LOSE,
};
struct penguin player;
struct penguin *playerP = &player;
struct puffle puffles[9];
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;
//initial setup of puffles and penguin
setup();
// initialize score
int score = 0;
//initialize timer to 20 seconds
int timer = 60 * 20;
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
// reset game
if (KEY_JUST_PRESSED(BUTTON_SELECT, currentButtons, previousButtons)) {
setup();
score = 0;
timer = 60 * 20;
state = START;
}
if (score == 9) {
state = WIN;
}
//if timer just ran out of time
if (timer <= 0 && timer > -200) {
state = LOSE;
}
timer--;
waitForVBlank();
switch (state) {
case START:
drawFullScreenImageDMA(start_screen);
if (KEY_JUST_PRESSED(BUTTON_START, currentButtons, previousButtons)) {
drawFullScreenImageDMA(snow_scene);
state = PLAY;
}
break;
case PLAY:
// draw penguin
drawImageDMA(playerP -> row, playerP -> col, playerP -> width, playerP -> height, playerP -> image);
// undraw old puffles
for (int i = 0; i < 9; i++) {
if (puffles[i].show) {
undrawImageDMA(puffles[i].row, puffles[i].col, puffles[i].row + puffles[i].height, puffles[i].col + puffles[i].width, snow_scene);
}
}
// draw new puffles
for (int i = 0; i < 9; i++) {
if (!puffles[i].show) continue;
// check for collision
if (abs(puffles[i].row - (playerP -> row)) < 30 && abs(puffles[i].col - (playerP -> col)) < 25) {
puffles[i].show = 0;
score++;
// undraw old score
undrawImageDMA(150, 185, 160, 240, snow_scene);
continue;
}
if (puffles[i].row + puffles[i].vertDis < 0 || puffles[i].row + puffles[i].vertDis > HEIGHT - puffles[i].height) {
puffles[i].vertDis = -puffles[i].vertDis;
}
if (puffles[i].col + puffles[i].horDis < 0 || puffles[i].col + puffles[i].horDis > WIDTH - puffles[i].width) {
puffles[i].horDis = -puffles[i].horDis;
}
puffles[i].row += puffles[i].vertDis;
puffles[i].col += puffles[i].horDis;
drawImageDMA(puffles[i].row, puffles[i].col, puffles[i].width, puffles[i].height, puffles[i].image);
}
control(currentButtons, previousButtons);
// draw score
char buffer[51];
sprintf(buffer, "Score: %d", score);
drawString(150, 185, buffer, BLUE);
// undraw old timer
undrawImageDMA(130, 185, 140, 240, snow_scene);
// draw timer
char timeBuffer[51];
sprintf(timeBuffer, "Time Left: %d", timer / 60);
drawString(130, 155, timeBuffer, RED);
break;
case WIN:
timer = -200;
drawFullScreenImageDMA(win_screen);
break;
case LOSE:
drawFullScreenImageDMA(lose_screen);
break;
}
previousButtons = currentButtons; // Store the current state of the buttons
}
return 0;
}