Repository navigation
Expand file tree
/
Copy pathmain.c
More file actions
149 lines (126 loc) · 3.2 KB
/
Copy pathmain.c
File metadata and controls
149 lines (126 loc) · 3.2 KB
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
#include "myLib.h"
static char paused[7] = "PAUSED";
int main(){
REG_DISPCTL = MODE3 | BG2_ENABLE;
drawImage(0, 0, 240, 150, menu_data);//draws menu with DMA for credit.
//loop for entire game. Allows repeatedly displaying the menu.
while(1){
drawMenu();
setStringColor(RGB(20, 5, 5));
//draws start instruction
sprintf(scoreMsg, "PRESS START");
drawRect(56, 87, 66, 8, BLACK);
drawString(56, 87, scoreMsg, YELLOW);
//seeds rand()
int seed;
while(!KEY_DOWN_NOW(BUTTON_A) && !KEY_DOWN_NOW(BUTTON_START)){
seed++;
}
srand(seed);
resetVars();
drawBackground();
//waits until unpress start, otherwise pauses immediately
while(KEY_DOWN_NOW(BUTTON_START));
while(gameInProgress){//main game loop
//spawns zombies on timer. More often as level increases.
//Also needs to make sure map isn't full,
if(timer == spawnRate){ //time to spawn
timer = 0;
if(numZoms < max){//map not full
spawnZombie();
}
} else{
timer++;
}
//pauses the game if press start.
if(KEY_DOWN_NOW(BUTTON_START)){//first press
drawStringFast(72, 102, paused);
while(KEY_DOWN_NOW(BUTTON_START)){//while first pressed
waitForVblank();
waitForVblank();
}
while(!KEY_DOWN_NOW((BUTTON_START))){// while lifted up
waitForVblank();
waitForVblank();
}
//unpauses after press start again
while(KEY_DOWN_NOW(BUTTON_START)){//while second pressed
waitForVblank();
waitForVblank();
}
drawRectIMG(72, 102, 36, 8, background_data);
}
//figure out next frame
updateDirections();
updatePlayer();
updateBullets();
//sprintf(debugNumZoms, "LC = %d", levelCounter);
sprintf(scoreMsg, "%d", score);//update score string
sprintf(levelMsg, "Level %d", level);
moveZombies();
waitForVblank();
//draw frame
drawStringFast(1, 5, scoreMsg);
drawStringFast(1, 191, levelMsg);
drawBullets();
drawPlayer();
drawZombies();
//Returns to main menu if press select.
if(KEY_DOWN_NOW(BUTTON_SELECT)){
gameInProgress = 0;
drawRect(150, 0, 240, 10, BLACK);
sprintf(scoreMsg, "Score: %d", score);//update message
drawString(151, 5, scoreMsg, RGB(20, 5, 5));
}
//Plays Game Over "Animation" if died.
if(lost){
gameOver();
}
}
//Goes from Game over screen to main menu if press Select/Start
while(lost){
if(KEY_DOWN_NOW(BUTTON_START) || KEY_DOWN_NOW(BUTTON_SELECT)){
lost = 0;
}
}
}//end main loop
}//end main method
/*void delay(int n)
{
volatile int size = 0;
for(int i=0; i<n*4000; i++)
{
size = size + 1;
}
}*/
/**
*Waits until start of next Vertical blank
*/
void waitForVblank()
{
while(SCANLINECOUNTER > 161);
while(SCANLINECOUNTER < 160);
}
/**
*Reinitializes all important game variables so consecutive games are independent.
*/
void resetVars(){
gameInProgress = 1; //start game
lost = 0;
score = 0;
timer = 0;
level = 1;
numZoms = 0;
levelCounter = 5;
row = 80;
col = 80;
spawnRate = 75 - level*5;//set spawnRate
for(int i = 0; i < max; i++){//reset all zombies & bulets
zombies[i].col = 0;
zombies[i].row = 0;
zombies[i].oldRow = row;
zombies[i].oldCol = col;
zombies[i].health = 0;
bullets[i].health = 0;
}
}