-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
161 lines (121 loc) · 4.89 KB
/
main.cpp
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
#include "character.h"
#include "screen_manager.h"
//Main file
/* Entry point to the application */
int main() {
//Declare variables
double time;
double gameStart;
int x,y;
struct game_state state;
//Set inital game state
state.screen = MENU;
//Create the menu buttons
FEHIcon::Icon menuButtons[5];
DrawMenu(menuButtons);
//Icon and labels for returning to menu
FEHIcon::Icon menuIcon[1];
char backLabel[1][20] = {"Back"};
char menuLabel[1][20] = {"Return to Menu"};
//Character & obstacle objects
Character character;
Obstacle obstacle = Obstacle();
//Infinite loop
while(1) {
//Game screen logic
if (state.screen == GAME) {
//Calculate time elapsed since game start
time = TimeNow() - gameStart;
//Draw character & obstacle
character.drawCharacter(obstacle);
//Draw score & health HUD
LCD.WriteAt("Score: ", 180, 10);
LCD.WriteAt((int) (time*10), 255, 10);
LCD.WriteAt("Health: ", 30, 10);
LCD.WriteAt(character.getHeath(), 120, 10);
//Obstacle movement
obstacle.moveObstacle();
character.collision(&obstacle);
//Check for game over
if (character.getHeath() <= 0) {
drawGameOver((int) (time*10));
updateHighScores((int) (time*10), &state);
FEHIcon::DrawIconArray(menuIcon, 1, 1, 150, 50, 60, 60, menuLabel, WHITE, WHITE);
state.screen = GAME_OVER;
}
}
//Screen touched
if (LCD.Touch(&x, &y)) {
// If/else statments to recognize touches on different screens
if (state.screen == MENU) {
// If/else statements to recognize touches on menu buttons
if (menuButtons[0].Pressed(x,y,0)) {
//Start button pressed
//Set start time for score
gameStart = TimeNow();
//Reset & draw character
character.reset();
character.drawCharacter(obstacle);
//Sets screen to game
state.screen = GAME;
//Buffer to prevent double touch
Sleep(0.5);
} else if (menuButtons[1].Pressed(x,y,0)) {
//Instructions button pressed
//Set screen state
state.screen = OTHER;
//Draws instructions screen
displayInstructions();
FEHIcon::DrawIconArray(menuIcon, 1, 1, 210, 2, 2, 243, backLabel, WHITE, WHITE);
//Buffer to prevent double touch
Sleep(0.5);
} else if (menuButtons[2].Pressed(x,y,0)) {
//High Scores button pressed
//Set screen state
state.screen = OTHER;
//Draws high scores screen
displayHighScores(&state);
FEHIcon::DrawIconArray(menuIcon, 1, 1, 210, 2, 2, 243, backLabel, WHITE, WHITE);
//Buffer to prevent double touch
Sleep(0.5);
} else if (menuButtons[3].Pressed(x,y,0)) {
//Credits button pressed
//Set screen state
state.screen = OTHER;
//Draws credits screen
displayCredits();
FEHIcon::DrawIconArray(menuIcon, 1, 1, 210, 2, 2, 243, backLabel, WHITE, WHITE);
//Buffer to prevent double touch
Sleep(0.5);
} else if (menuButtons[4].Pressed(x,y,0)) {
//Exit button pressed
return 0;
}
} else if (state.screen == OTHER) {
if (menuIcon[0].Pressed(x,y,0)) {
//Back button pressed in instructions, high scores, or credits
//Set screen state to menu
state.screen = MENU;
//Draws menu screen
DrawMenu(menuButtons);
//Buffer to prevent double touch
Sleep(0.5);
}
} else if (state.screen == GAME) {
//Character jump
character.jump(&obstacle, gameStart);
} else if (state.screen == GAME_OVER) {
if (menuIcon[0].Pressed(x,y,0)) {
//Menu button pressed in game over
//Set screen state to menu
state.screen = MENU;
//Draws menu screen
DrawMenu(menuButtons);
//Buffer to prevent double touch
Sleep(0.5);
}
}
}
}
return 0;
}