-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.c
231 lines (204 loc) · 5.38 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
#include <stdio.h>
#include "myLib.h"
#include "game.h"
#include "graphics.h"
#include "main.h"
GAMEState state = START;
Slider slider;
Slider *sldptr = &slider;
Ball ball;
Ball *ballptr = &ball;
Brick bricks[15];
Brick *brptr = bricks;
int numBricks;
int bricksSize;
int level = INITIAL_LEVEL;
int lives = INITIAL_LIVES;
char textBuffer[70];
int enterPressed = 0;
int zPressed = 0;
int backPressed = 0;
int main() {
// Enable GBA Mode3
REG_DISPCTL = MODE3 | BG2_ENABLE;
// Game Loop
while(1) {
waitForVblank();
switch (state) {
case START:
startScreen();
state = START_NODRAW;
break;
case START_NODRAW:
if (!enterPressed && KEY_DOWN_NOW(BUTTON_START)) {
state = LEVEL;
enterPressed = 1;
}
break;
case LEVEL:
setGameStage();
state = LEVEL_NODRAW;
break;
case LEVEL_NODRAW:
// Collision resolution for when ball hits bottom of screen
if (ballptr->row + BALLSIZE >= sldptr->row + SLIDER_HEIGHT) {
lives -= 1;
if (lives > 0) {
state = LOSE_LIFE;
} else {
state = GAME_OVER;
}
}
startLevel();
if (numBricks <= 0) {
if (level < MAXLEVEL) {
level++;
state = NEXT_LEVEL;
} else {
state = WIN_GAME;
}
}
backToMain();
break;
case NEXT_LEVEL:
setUpNextLevel();
state = NEXT_LEVEL_NO_DRAW;
break;
case NEXT_LEVEL_NO_DRAW:
if (!enterPressed && KEY_DOWN_NOW(BUTTON_START)) {
state = LEVEL;
enterPressed = 1;
}
backToMain();
break;
case LOSE_LIFE:
tryAgain();
state = LOSE_LIFE_NO_DRAW;
break;
case LOSE_LIFE_NO_DRAW:
if (!zPressed && KEY_DOWN_NOW(BUTTON_A)) {
state = LEVEL;
enterPressed = 1;
}
backToMain();
break;
case GAME_OVER:
gameOver();
state = GAME_OVER_NO_DRAW;
break;
case GAME_OVER_NO_DRAW:
if (!enterPressed && KEY_DOWN_NOW(BUTTON_START)) {
resetGame();
enterPressed = 1;
}
backToMain();
break;
case WIN_GAME:
gameWon();
state = WIN_GAME_NO_DRAW;
break;
case WIN_GAME_NO_DRAW:
if (!enterPressed && KEY_DOWN_NOW(BUTTON_START)) {
resetGame();
enterPressed = 1;
}
backToMain();
break;
}
if (!KEY_DOWN_NOW(BUTTON_START)) {
enterPressed = 0;
}
if (!KEY_DOWN_NOW(BUTTON_A)) {
zPressed = 0;
}
if (!KEY_DOWN_NOW(BUTTON_SELECT)) {
backPressed = 0;
}
}
return 0;
}
/**
* Function to setup the start screen
*/
void startScreen() {
fillScreen(BGCOLOR);
drawImage3(0, 0, START_SCREEN_HEIGHT, START_SCREEN_WIDTH, start_screen);
drawString3(105, 50, "Press ENTER to Start Game", YELLOW);
}
/**
* Function to generate basic game stage
*/
void setGameStage() {
fillScreen(BGCOLOR);
updateScreenText();
drawSlider(sldptr);
createBall(sldptr->row - 5, sldptr->col + (SLIDER_WIDTH / 2), BALLSIZE, ballptr);
numBricks = level * 3;
bricksSize = level * 3;
generateBricks(brptr, bricksSize);
}
/**
* Function to begin the level
*/
void startLevel() {
enableSlider(sldptr);
ballMovement(ballptr, sldptr, brptr, &numBricks, bricksSize);
}
/**
* Function to notify player that the game has ended and they lost
*/
void gameOver() {
drawImage3(0, 0, GAME_OVER_HEIGHT, GAME_OVER_WIDTH, game_over);
drawString3(140, 130, "GAME OVER :(", RED);
drawString3(149, 85, "PRESS ENTER TO PLAY AGAIN", RED);
}
/**
* Function to notify that the player has lost a life
*/
void tryAgain() {
fillScreen(BGCOLOR);
drawString3(70, 67, "YOU LOST A LIFE.", RED);
drawString3(79, 60, "HIT Z TO TRY AGAIN", RED);
}
/**
* Function to notify that the player has won the game
*/
void gameWon() {
fillScreen(BGCOLOR);
drawString3(70, 50, "CONGRATULATIONS, YOU WON!", GREEN);
drawString3(79, 53, "PRESS ENTER TO PLAY AGAIN", GREEN);
}
/**
* Function to reset the game
*/
void resetGame() {
level = INITIAL_LEVEL;
lives = INITIAL_LIVES;
state = START;
}
/**
* Function to notify player that they made it to the next level
*/
void setUpNextLevel() {
fillScreen(BGCOLOR);
sprintf(textBuffer, "LEVEL %d. PRESS ENTER TO START", level);
drawString3(79, 36, textBuffer, YELLOW);
}
/**
* Function to update text on the screen
*/
void updateScreenText() {
sprintf(textBuffer, "LEVEL %d", level);
drawString3(SCREENHEIGHT - 10, 5, textBuffer, GREEN);
sprintf(textBuffer, "LIVES: %d", lives);
drawString3(SCREENHEIGHT - 10, SCREENWIDTH - sizeof(textBuffer) + 20, textBuffer, GREEN);
}
/**
* Function to go back to start screen
*/
void backToMain() {
if (!backPressed && KEY_DOWN_NOW(BUTTON_SELECT)) {
state = START;
backPressed = 1;
}
}