-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.cpp
More file actions
366 lines (280 loc) · 8.18 KB
/
Copy pathmain.cpp
File metadata and controls
366 lines (280 loc) · 8.18 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
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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
// game constants
#define SCREEN_SIZE_X 320.0
#define SCREEN_SIZE_Y 240.0
// gameplay params
#define GAME_SCALE 3
#define MENU_SCALE 4
#define TIME_BETWEEN_FRUITS 2.0
// sim libraries
#include "simulator_libraries/FEHLCD.h"
#include "simulator_libraries/FEHUtility.h"
#include "simulator_libraries/FEHRandom.h"
// paige libraries :)
#include "custom_libraries/Vector.cpp"
#include "custom_libraries/Sprite.cpp"
#include "custom_libraries/Fruit.cpp"
// c++ libraries
#include <fstream>
#include <iostream>
#include <vector>
void showMenu();
void showStatsScreen();
void showHowToScreen();
void showCredits();
void quitGame();
void doGameplayLoop();
Fruit* makeRandomFruit();
void showGameOverScreen(float);
void waitForBackButtonPress();
void waitForTouch();
void waitForNoTouch();
void waitForTap();
void writeTime(float);
float playerHighScore();
int gameCount();
/*
* Entry point to the application
*/
int main() {
showMenu();
return 0;
}
/*
* menu screens!
*/
void showMenu() {
// display menu
Sprite logo("menu/logo", Vector2(41, 21));
logo.scale(MENU_SCALE);
logo.move(Vector2(7, 4) * MENU_SCALE);
Sprite play("menu/play_button", Vector2(46, 25));
play.scale(MENU_SCALE);
play.move(Vector2(4, 30) * MENU_SCALE);
Sprite stats("menu/stats_button", Vector2(26, 19));
stats.scale(MENU_SCALE);
stats.move(Vector2(52, 2) * MENU_SCALE);
Sprite howto("menu/how_to_button", Vector2(27, 16));
howto.scale(MENU_SCALE);
howto.move(Vector2(52, 22) * MENU_SCALE);
Sprite credits("menu/credits_button", Vector2(26, 15));
credits.scale(MENU_SCALE);
credits.move(Vector2(52, 41) * MENU_SCALE);
// start the menu
while (true) {
LCD.SetBackgroundColor(BLACK);
LCD.Clear();
// draw menu
logo.draw();
play.draw();
stats.draw();
howto.draw();
credits.draw();
// check for button presses
int xpos, ypos;
if (LCD.Touch(&xpos, &ypos)) {
Vector2 screenPoint(xpos, ypos);
if (play.isPointWithin(screenPoint)) {
doGameplayLoop();
} else if (stats.isPointWithin(screenPoint)) {
showStatsScreen();
} else if (howto.isPointWithin(screenPoint)) {
showHowToScreen();
} else if (credits.isPointWithin(screenPoint)) {
showCredits();
}
waitForNoTouch();
}
// update screen
LCD.Update();
}
}
void showStatsScreen() {
LCD.Clear();
// draw stats background in the center of the screen
Sprite statsBack("menu/stats_background", Vector2(80, 60));
statsBack.scale(3);
statsBack.anchorPoint(Vector2(0.5, 0.5));
statsBack.pos(Vector2(SCREEN_SIZE_X/2, SCREEN_SIZE_Y/2));
statsBack.draw();
//display high score
float highScore = playerHighScore();
LCD.SetFontColor(BLACK);
LCD.WriteAt(highScore, 200, 91);
//display game count
int gameCounter = gameCount();
LCD.SetFontColor(BLACK);
LCD.WriteAt(gameCounter, 250, 130);
LCD.Update();
waitForBackButtonPress();
}
void showHowToScreen() {
LCD.Clear();
// draw howto screen in the middle
Sprite howToBack("menu/instruction_background", Vector2(80, 60));
howToBack.scale(3);
howToBack.anchorPoint(Vector2(0.5, 0.5));
howToBack.pos(Vector2(SCREEN_SIZE_X/2, SCREEN_SIZE_Y/2));
howToBack.draw();
LCD.Update();
waitForBackButtonPress();
}
void showCredits() {
LCD.Clear();
Sprite creditsBack("menu/credits_background", Vector2(80, 60));
creditsBack.scale(4);
creditsBack.anchorPoint(Vector2(0.5, 0.5));
creditsBack.pos(Vector2(SCREEN_SIZE_X/2, SCREEN_SIZE_Y/2));
creditsBack.draw();
LCD.Update();
waitForBackButtonPress();
}
/*
* gameplay!!
*/
void doGameplayLoop() {
// make character
Sprite character("pointer", Vector2(8, 8));
character.anchorPoint(Vector2(0.5, 0.5));
character.scale(2);
// fruit list
vector<Fruit*> projectiles;
// timing vars
float lastTime = TimeNow();
float lastFruitSpawnTime = TimeNow() - 4;
// gameplay vars
float playerTime = 0.0;
bool gameOver = false;
// game should keep playing as long as the screen is being touched
int xpos, ypos;
while (LCD.Touch(&xpos, &ypos) && !gameOver) {
Vector2 mousePos = Vector2(xpos, ypos);
// clear
LCD.Clear();
// draw background
LCD.SetBackgroundColor(BLACK);
// draw character
character.move(mousePos);
// step fruits
float dt = TimeNow()-lastTime;
for(Fruit* f : projectiles) {
f->stepPath(dt);
// check if its hitting the mouse
if(f->sprite()->isPointWithin(mousePos)){
gameOver = true;
}
}
lastTime = TimeNow();
// create new fruit
if (TimeNow() - lastFruitSpawnTime > 3) {
projectiles.push_back(makeRandomFruit());
lastFruitSpawnTime = TimeNow();
}
//increment timer
playerTime = playerTime + dt;
//write current time to top right corner of screen
LCD.WriteAt(playerTime, 240, 15);
// update screen
LCD.Update();
}
// garbage collect fruit
for(Fruit* f : projectiles) {
delete f;
}
showGameOverScreen(playerTime);
// write time to file
writeTime(playerTime);
waitForTap();
}
// makes and returns a new random fruit to be used in gameplay
Fruit* makeRandomFruit() {
// list all possible fruits
string fruits[] = {LEMON, APPLE, WATERMELON, TANGERINE};
// choose a random one
string chosenFruit = fruits[Random.RandInt() % 4];
// choose a spawning position for the fruit
Vector2 spawnPos = Vector2(Random.RandInt(), Random.RandInt());
// choose a random speed for the fruit between 1 and 2
float speed = 1 + (Random.RandInt() % 10)/10.0;
// make the fruit and scale it
Fruit* newFruit = new Fruit(chosenFruit, speed, spawnPos);
newFruit->sprite()->anchorPoint(Vector2(0.5, 0.5));//
newFruit->sprite()->scale(2);
return newFruit;
}
// final screen after the player touches a fruit
void showGameOverScreen(float finalTime) {
LCD.SetBackgroundColor(BLACK);
LCD.Clear();
// draw gameover screen in the middle
Sprite gameOverScreen("menu/game_over_background", Vector2(80, 60));
gameOverScreen.anchorPoint(Vector2(0.5, 0.5));
gameOverScreen.move(Vector2(SCREEN_SIZE_X/2, SCREEN_SIZE_Y/2));
// write final time to screen
LCD.SetFontColor(CYAN);
LCD.WriteAt(finalTime, 187, 166);
LCD.Update();
}
void waitForBackButtonPress() {
// draw back button
Sprite backButton("menu/back_button", Vector2(16, 8));
backButton.anchorPoint(Vector2(0, 1));
backButton.pos(Vector2(8, SCREEN_SIZE_Y - 8));//
backButton.draw();
LCD.Update();
// wait for player to touch the button
bool touching = false;
int xpos=0, ypos=0;
while(!(touching && backButton.isPointWithin(Vector2(xpos, ypos)))) {
touching = LCD.Touch(&xpos, &ypos);
}
}
// waiting functions
void waitForNoTouch() {
int x,y;
while (LCD.Touch(&x, &y)) {
Sleep(1.0/60.0);
}
}
void waitForTouch() {
int x,y;
while (!LCD.Touch(&x, &y)) {
Sleep(1.0/60.0);
}
}
void waitForTap() {
waitForNoTouch();
waitForTouch();
waitForNoTouch();
}
// write time to file
void writeTime(float t) {
ofstream playerTimeFile;
playerTimeFile.open("playerTimeFile.txt", ofstream::app);
playerTimeFile << t << endl;
playerTimeFile.close();
}
// find the player's highscore from the file
float playerHighScore() {
ifstream playerTimeFile;
playerTimeFile.open("playerTimeFile.txt");
float score, highScore=0;
while (playerTimeFile >> score){
if (highScore < score) {
highScore = score;
}
}
playerTimeFile.close();
return highScore;
}
// count the saved games
int gameCount() {
ifstream playerTimeFile;
playerTimeFile.open("playerTimeFile.txt");
int counter = 0;
float temp;
while (playerTimeFile >> temp){
counter++;
}
playerTimeFile.close();
return counter;
}