-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
1056 lines (955 loc) · 33.2 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
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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "FEHLCD.h"
#include "FEHImages.h"
#include "FEHRandom.h"
#include "FEHUtility.h"
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include <stdio.h>
#include <fstream>
/********************************************************************************************************************************
* FEH Software Design Project - Rockstar Games (Group B1)
* Shape Run by Avery Abke, Naomi Mukka, and Samantha Smith
*******************************************************************************************************************************/
/*FUNCTION PROTOTYPES*/
void createButton(char [], int, int); //creates indivudal buttons with text at a specified point
void mainMenu(); //displays the main menu
void changeScreen(int); //switches to the proper menu screen
void statsScreen(); //displays the stats screen
void instructionsScreen(); //displays the instructions screen
void creditsScreen(); //displays the credits screen
void selectDifficultyScreen(); //displays the select difficulty/start screen
void selectCharacterScreen(); //displays the character select screen
void game(int); //main game code
void generateObstacle(); //used to create new obstacles as game runs
void refresh(); //refreshes screen
void drawBackground(int); //writes the correct background to the screen
void moveBackground(); //makes the background scroll
void flushList(); //clears out the generated list
/*CONSTANTS*/
#define SCREEN_HEIGHT 239
#define SCREEN_WIDTH 319
#define BUTTON_COLOR BLUE //used for the background of buttons
#define OUTLINE_COLOR WHITE //used for button outlines
#define FLOOR_HEIGHT 206
/*VARIABLES*/
int currentScreen = 0; //0 - main menu, 1 - stats, 2 - instructions, 3 - credits, 4 - start/difficulty, 5 - actual game
int xTouch, yTouch;
int difficulty; //1 - easiest -> 3 - hardest
int highScores[5]; //keeps track of top 5 scores
bool gameRunning;
bool playerDead = false;
int jumpingCounter = 0; //used to check which frame the jump is on
int generatedIndex = 0;
bool jumpingUp; //true if movement is up, false if movement is down
int bgx = 0; //background position
int frequency; //time between obstacle spawns
int speed; //speed of obstacles
/********************************************************************************************************************************
CLASSES
********************************************************************************************************************************/
/****************************************************************
* Player Class
****************************************************************/
class Player{
private:
int score;
bool updateStats(); //updates stats of player
int x = 36, y = SCREEN_HEIGHT - 64;
public:
char spriteFileName[30];
void gameOver();
bool jump(int); //makes the player jump
void drawPlayer(); //writes player to screen
int getX(); //returns x value of player
int getY(); //returns y value of player
void setY(int); //sets Y value of player
int getScore(); //returns player score
void setScore(int); //updates player score
int yAcceleration;
};
/****************************************************************
* Obstacle class
****************************************************************/
class Obstacle{
private:
int posx, posy, width, height;
int type; // 0-spikes, 1-tall, 2-long, 3-triangle
public:
Obstacle(int);
Obstacle();
bool onScreen;
void moveObstacle(); //moves obstacle along the screen
void drawObstacle(); //writes the obstacle to the screen
bool checkCollisions(Player); //returns if a collision was detected
};
/****************************************************************
* Button class
****************************************************************/
class Button{
private:
char printString[100];
int posx, posy, height, width;
public:
Button(char [], int, int);
Button(int, int, int, int);
Button();
void drawButton(); //writes button to the screen
bool pressed(); //returns true if button is pressed
bool hovered(); //returns true if the cursor is on the button
};
/****************************************************************
* Character class
****************************************************************/
class Character{
private:
int posx, posy, height, width;
public:
Button button;
char fname[100],name[20];
Character(int, int, char[], char[]);
void drawCharacter(); //draws the character on the screen
};
/*OBJECTS*/
Button stats("Stats", 7, 215);
Button instructions("Instructions", 75, 215);
Button credits("Credits", 228, 215);
Button start("Start", 130, 170);
Button easy("Easy",40,110);
Button medium("Medium",122,110);
Button hard("Hard",228,110);
Button back("back",0,0);
Character guy(81,82,"guy","Assets/guyFEH.pic");
Character smile(123,82,"smile","Assets/smileFEH.pic");
Character brutus(165,82,"brutus","Assets/brutusFEH.pic");
Character dogBrutus(207,82,"Brutus","Assets/brutusDogFEH.pic");
Character annie(81,120,"annie","Assets/annieFEH.pic");
Character jason(123,120,"jason","Assets/jasonFEH.pic");
Character adam(165,120,"adam","Assets/adamAFEH.pic");
Character pom(207,120,"pomeranian","Assets/pomFEH.pic");
Character ben(81,162,"ben","Assets/benFEH.pic");
Character jimmy(123,162,"jimmy","Assets/jimmyFEH.pic");
Character alex(165,162,"alex","Assets/alexFEH.pic");
Character stevan(207,162,"stevan","Assets/stevanFEH.pic");
Player player;
Obstacle generated[10];
Character characters[12] = {guy,smile,brutus,dogBrutus,annie,jason,adam,pom,ben,jimmy,alex,stevan};
/********************************************************************************************************************************
MAIN FUNCTION
Authors: Samantha Smith, Avery Abke, Naomi Mukka
********************************************************************************************************************************/
int main()
{
//Create Start Screen
mainMenu();
while (1) {
/*MAIN MENU*/
while (currentScreen == 0){ //MAIN MENU BUTTONS
if (LCD.Touch(&xTouch, &yTouch)){
//stats button pressed
if (stats.pressed()){
changeScreen(1);
}
//instructions button pressed
if (instructions.pressed()){
changeScreen(2);
}
//credits button pressed
if (credits.pressed()){
changeScreen(3);
}
//start button pressed
if (start.pressed()){
changeScreen(6);
}
}
} //end main menu buttons
/*CHARACTER SELECT SCREEN*/
bool sameScreen = true;
while (currentScreen == 6){//CHARACTER SELECT BUTTONS
for(int i=0;i<12;i++) {
//Check if the cursor is on one of the characters
while (characters[i].button.hovered()) {
//displays name of character
LCD.SetFontColor(WHITE);
LCD.WriteAt(characters[i].name,159-strlen(characters[i].name)*12/2,46);
//character pressed
if(LCD.Touch(&xTouch,&yTouch)) {
strcpy(player.spriteFileName,characters[i].fname);
Sleep(500);
changeScreen(4);
sameScreen = false;
break;
}
}
if(sameScreen) {
LCD.Clear();
selectCharacterScreen();
}
}
//back button pressed
if (back.pressed()){
changeScreen(0);
Sleep(500);
}
}//end character select buttons
/*DIFFICULTY SELECT SCREEN*/
while (currentScreen == 4){//DIFFICULTY SELECT BUTTONS
if (LCD.Touch(&xTouch, &yTouch)){
//easy button pressed
if (easy.pressed()){
difficulty = 1;
changeScreen(5);
}
//medium button pressed
if (medium.pressed()){
difficulty = 2;
changeScreen(5);
}
//hard button pressed
if (hard.pressed()){
difficulty = 3;
changeScreen(5);
}
//back button pressed
if (back.pressed()){
changeScreen(0);
}
}
}//end difficulty select buttons
/*Return to main menu if the back button is pressed on any screen*/
if(back.pressed()){
changeScreen(0);
}
LCD.Update();
}//program loop
return 0;
}
/********************************************************************************************************************************
FUNCTIONS
********************************************************************************************************************************/
//Button class member functions
/****************************************************************
* Creates a properly sized rectangle with text inside
* Authors: Samantha Smith
* @param printString[] String to be printed
* @param x x position
* @param y y position
****************************************************************/
Button::Button(char string[], int x, int y){
strcpy(printString,string);
posx = x;
posy = y;
width = strlen(string)*12+2;
height = 19;
}
/****************************************************************
* Creates a rectangular button with no text
* Authors: Naomi Mukka
* @param x x position
* @param y y position
* @param w width
* @param h height
****************************************************************/
Button::Button(int x, int y, int w, int h){
posx = x;
posy = y;
width = w;
height = h;
strcpy(printString,"");
}
/****************************************************************
* Default Button Constructor - creates a NULL button
* Authors: Naomi Mukka
****************************************************************/
Button::Button() {
posx=posy=width=height=NULL;
}
/****************************************************************
* Displays the button on the screen
* Authors: Samantha Smith
****************************************************************/
void Button::drawButton() {
LCD.SetFontColor(BUTTON_COLOR);
LCD.FillRectangle(posx, posy, width,height);
LCD.SetFontColor(OUTLINE_COLOR);
LCD.DrawRectangle(posx, posy, width,height);
LCD.WriteAt(printString, posx, posy+3);
}
/****************************************************************
* Checks if the button is pressed
* Authors: Samantha Smith
****************************************************************/
bool Button::pressed() {
if (LCD.Touch(&xTouch,&yTouch)) {
if(yTouch >= posy && yTouch <= posy+height && xTouch >= posx && xTouch <= posx+width) {
return true;
} else {
return false;
}
} else {
return false;
}
}
/****************************************************************
* Checks if the cursor is on the button
* Authors: Samantha Smith
****************************************************************/
bool Button::hovered() {
LCD.Touch(&xTouch,&yTouch);
if(yTouch >= posy && yTouch <= posy+height && xTouch >= posx && xTouch <= posx+width) {
return true;
} else {
return false;
}
}
//Obstacle class member functions
/****************************************************************
* Obstacle Constructor - creates an obstacle based on a preset
* size and places it into the correct starting position
* Authors: Samantha Smith
* @param t type of obstacle to create
****************************************************************/
Obstacle::Obstacle(int t) {
type = t;
switch (type){
case 0: //spike
width = 32;
height = 16;
break;
case 1: //tall
width = 32;
height = 57;
break;
case 2: //long
width = 64;
height = 30;
break;
case 3: //triangle
width = 31;
height = 27;
break;
}
posx = SCREEN_WIDTH-width;
posy = FLOOR_HEIGHT-height+1;
}
/****************************************************************
* Default Obstacle Constructor - creates a NULL obstacle
* Authors: Naomi Mukka
****************************************************************/
Obstacle::Obstacle(){
posx=posy=width=height=NULL;
}
/****************************************************************
* Move Obstacle member function - moves the obstacle forward if it
* is not hitting the end of the screen
* Also clears out the generated list if the index reaches the
* end of the list
* Authors: Naomi Mukka, Samantha Smith
****************************************************************/
void Obstacle::moveObstacle() {
//float start = TimeNow();
if(posx<=10) {
onScreen = false;
if (generatedIndex == 10) {
flushList();
}
} else {
posx -= speed;
}
}
/****************************************************************
* Flush List Function - wipes the generated list to prevent
* seg faults
* Authors: Samantha Smith
****************************************************************/
void flushList(){
for (int i = 0; i < 10; i++){
generated[i] = NULL;
}
generatedIndex = 0;
}
/****************************************************************
* Draw Obstacle Member function - draws the proper obstacle image
* to the screen based on the obstacle's type
* Authors:
****************************************************************/
void Obstacle::drawObstacle() {
FEHImage obstacleSprite; //sprite
switch (type){
case 0:
obstacleSprite.Open("Assets/spikesFEH.pic"); //get sprite
break;
case 1:
obstacleSprite.Open("Assets/tallObstacleFEH.pic"); //get sprite
break;
case 2:
obstacleSprite.Open("Assets/longObstacleFEH.pic"); //get sprite
break;
case 3:
obstacleSprite.Open("Assets/triangleFEH.pic"); //get sprite
break;
}
obstacleSprite.Draw(posx,posy); // draw the character
obstacleSprite.Close(); //close
}
/****************************************************************
* Check Collisions Member Function
* Authors: Samantha Smith
* @param player the player object
* @return Returns true if a collision is detected between the player
* and the obstacle
****************************************************************/
bool Obstacle::checkCollisions(Player p){
bool collision = false;
//get x/y value of each side of the player
int playerLeft = p.getX();
int playerRight = (playerLeft + 32);
int playerTop = p.getY();
int playerBottom = (playerTop + 32);
//get x/y value of each side of the obstacle
int obstacleLeft = posx;
int obstacleRight = obstacleLeft + width;
int obstacleTop = posy;
int obstacleBottom = obstacleTop + height;
//for all normal shapes except triangle
if (type != 3){
//collisions for right side of player body
if ((playerRight >= obstacleLeft && playerRight <= obstacleRight) && (playerBottom >= obstacleTop && playerBottom <= obstacleBottom)) {
collision = true;
}
//collisions for left side of body
else if ((playerLeft <= obstacleRight && playerLeft >= obstacleLeft) && (playerBottom >= obstacleTop && playerBottom <= obstacleBottom)) {
collision = true;
}
}
//collisions with triangle
else {
//triangle collision with right side of body to left side of obstacle
if ((playerRight >= obstacleLeft)&& playerRight <= obstacleLeft + width/2 && playerBottom >= obstacleTop && playerBottom >= (pow(3, 0.5)*(playerRight - obstacleLeft) +obstacleTop)) {
collision = true;
}
//triangle collision with left side of body to left side of obstacle
else if ((playerLeft >= obstacleLeft)&& playerLeft <= obstacleLeft + width/2 && playerBottom >= obstacleTop && playerBottom >= (pow(3, 0.5)*(obstacleLeft- playerLeft) +obstacleTop)) {
collision = true;
}
//triangle collision with left side of body with right side of obstacle
if ((playerLeft >= obstacleLeft+ width/2)&& playerLeft <= obstacleRight && playerBottom >= obstacleTop && playerBottom >= (-pow(3, 0.5)*(obstacleRight - playerLeft)+obstacleTop)) {
collision = true;
}
//triangle collision with right side of body with right side of obstacle
else if ((playerRight >= obstacleLeft+ width/2)&& playerRight <= obstacleRight && playerBottom >= obstacleTop && playerBottom >= (-pow(3, 0.5)*(playerRight - obstacleRight)+obstacleTop)) {
collision = true;
}
}
//no collsion detected
if (collision){
return true;
}
else {
return false;
}
}
//Player class member functions
/****************************************************************
* Player Jump Member Function - Makes the player jump
* in a parabolic shape
* Authors: Samantha Smith, Avery Abke
* @param time time for calculations
* @return true if the player has completed the jump
****************************************************************/
bool Player::jump(int time){
//Adjusts speed of jump based on difficulty
switch (difficulty) {
case 1:
break;
case 2:
time *= 2;
break;
case 3:
time *= 2;
break;
}
yAcceleration = -2;
int initialVelocity = 20;
int yInitial = SCREEN_HEIGHT - 64; //175
//calculate the height and set y to that height
setY(SCREEN_HEIGHT - (yInitial + initialVelocity*(time-5) + 0.5 *yAcceleration*pow(time,2)));
//check if the player is still in the process of jumping
if(getY() <= yInitial){
return true;
}
//stop the jump process
else{
setY(yInitial);
return false;
}
}
/****************************************************************
* Player Draw Member Function - Writes the player image to the screen
* in the correct position
* Authors: Avery Abke
****************************************************************/
void Player::drawPlayer(){
FEHImage playerSprite; //sprite
playerSprite.Open(spriteFileName); //get sprite
playerSprite.Draw(player.getX(), player.getY()); // draw the character
playerSprite.Close(); //close
}
/****************************************************************
* Player Get X Member Function
* Authors: Avery Abke
* @return x value of player
****************************************************************/
int Player::getX(){
return x;
}
/****************************************************************
* Player Get Y Member Function
* Authors: Avery Abke
* @return y value of player
****************************************************************/
int Player::getY(){
return y;
}
/****************************************************************
* Player Set Y Member Function
* Authors: Avery Abke
* @param newY new y value of the player
****************************************************************/
void Player::setY(int newY){
y = newY;
}
/****************************************************************
* Player Get Score Member Function
* Authors: Avery Abke
* @return score of the player
****************************************************************/
int Player::getScore(){
return score;
}
/****************************************************************
* Player Get Score Member Function
* Authors: Avery Abke
* @param timeElapsed time the score should be set to
****************************************************************/
void Player::setScore(int timeElapsed){
score = timeElapsed;
}
//Character class member functions
/****************************************************************
* Character Constructor - creates a 32x32 character
* Authors: Naomi Mukka
* @param x x position
* @param y y position
* @param n character name
* @param f character sprite file name
****************************************************************/
Character::Character (int x, int y, char n[], char f[]) {
strcpy(name,n);
strcpy(fname,f);
posx = x;
posy = y;
height = width = 32;
Button b(posx, posy, width, height);
button = b;
}
/****************************************************************
* Draw Character Member function - draws the character to the
* screen
* Authors: Naomi Mukka
****************************************************************/
void Character::drawCharacter() {
button.drawButton();
FEHImage name;
name.Open(fname);
name.Draw(posx,posy);
name.Close();
}
//MENU STUFF
/****************************************************************
* Creates the main menu screen
* Authors: Samantha Smith
****************************************************************/
void mainMenu(){
stats.drawButton();
instructions.drawButton();
credits.drawButton();
start.drawButton();
FEHImage title;
title.Open("Assets/titleFEH.pic");
title.Draw(SCREEN_WIDTH/2 - 78/2, 50);
title.Close();
FEHImage playerImage;
playerImage.Open("Assets/guyFEH.pic");
playerImage.Draw(45, 80+32);
playerImage.Close();
FEHImage obstacleImage;
obstacleImage.Open("Assets/spikesFEH.pic");
obstacleImage.Draw(100, 80+48);
obstacleImage.Draw(180, 80+48);
obstacleImage.Close();
obstacleImage.Open("Assets/tallObstacleFEH.pic");
obstacleImage.Draw(240, 80+10);
obstacleImage.Close();
obstacleImage.Open("Assets/triangleFEH.pic");
obstacleImage.Draw(290, 80+37);
obstacleImage.Close();
LCD.SetFontColor(WHITE);
}
/****************************************************************
* Changes to the specified screen
* Authors: Samantha Smith
* @param screen which screen the PROTEUS should display to the user:
* 0 - Main Menu,
* 1 - Stats,
* 2 - Instructions,
* 3 - Credits,
* 4 - Start,
* 5 - Game
****************************************************************/
void changeScreen(int screen){
LCD.Clear();
currentScreen = screen;
switch (screen){
case 0: //main menu
mainMenu();
break;
case 1: //stats
statsScreen();
break;
case 2: //instructions
instructionsScreen();
break;
case 3: //Credits
creditsScreen();
break;
case 4: //start
selectDifficultyScreen();
break;
case 5: //game start
game(difficulty);
break;
case 6: //character select screen
selectCharacterScreen();
break;
}
}
/****************************************************************
* Creates the stats screen
* Authors: Naomi Mukka
****************************************************************/
void statsScreen(){
LCD.WriteAt("High Scores",94,60);
LCD.DrawHorizontalLine(80,94,226);
//Display top 5 scores
int y = 90;
for(int i=0;i<5;i++) {
LCD.WriteAt(highScores[i],130,y);
LCD.WriteAt(" m",166,y);
y += 20;
}
//Add back button
back.drawButton();
}
/****************************************************************
* Creates the credits screen
* Authors: Naomi Mukka
****************************************************************/
void creditsScreen(){
LCD.WriteAt("Developers:",80,80);
LCD.SetFontColor(PALETURQUOISE);
LCD.WriteAt("Avery Abke",80,100);
LCD.WriteAt("Naomi Mukka",80,120);
LCD.WriteAt("Samantha Smith",80,140);
back.drawButton();
}
/****************************************************************
* Creates the instructions screen
* Authors: Avery Abke
****************************************************************/
void instructionsScreen(){
back.drawButton();
LCD.WriteAt("How to Play:", 80, 40);
LCD.SetFontColor(MAGENTA);
LCD.WriteAt("---------------", 60,50);
LCD.SetFontColor(WHITE);
LCD.WriteAt("Tap to Jump", 80, 80);
LCD.WriteAt("Avoid anything with spikes", 3, 110);
LCD.WriteAt("Go for as long as you can", 10, 140);
}
/****************************************************************
* Creates the start/difficulty select screen
* Authors: Naomi Mukka
****************************************************************/
void selectDifficultyScreen(){
easy.drawButton();
medium.drawButton();
hard.drawButton();
back.drawButton();
}
/****************************************************************
* Creates the character select screen
* Authors: Naomi Mukka
****************************************************************/
void selectCharacterScreen(){
back.drawButton();
LCD.SetFontColor(WHITE);
LCD.DrawRectangle(96,40,126,30);
for(int i=0;i<12;i++) {
characters[i].drawCharacter();
}
}
//GAME STUFF
/****************************************************************
* Refresh Function - redraws the screen and moves the obstacles
* Authors: Naomi Mukka
****************************************************************/
void refresh() {
LCD.Clear();
//draws the background based on the difficulty level
drawBackground(difficulty);
//draws the floor
LCD.SetFontColor(BLACK);
LCD.FillRectangle(0, FLOOR_HEIGHT, 320, 35);
//draws the obstacles currently on screen
for(int i=0; i<10;i++) {
if (generated[i].onScreen) {
generated[i].moveObstacle();
generated[i].drawObstacle();
}
}
//draws the player
player.drawPlayer();
//writes the score
LCD.WriteAt(player.getScore(), SCREEN_WIDTH - 40, 5);
//Updates screen
Sleep(1);
LCD.Update();
}
/****************************************************************
* Main Game function
* Authors: Samantha Smith
* @param difficulty (1 easiest -> 3 hardest)
****************************************************************/
void game(int difficulty) {
//initialize values
jumpingCounter = 0;
int onStartWait = 3;
gameRunning = false;
generatedIndex = 0;
player.setY(SCREEN_HEIGHT-64);
player.setScore(0);
flushList();
drawBackground(difficulty);
//Adjusts speed and obstacle frequency based on the difficulty selected
switch (difficulty){
case 1:
speed = 10;
frequency = 3;
break;
case 2:
speed = 14;
frequency = 2;
break;
case 3:
speed = 19;
frequency = 1;
break;
}
LCD.SetFontColor(WHITE);
LCD.WriteAt("Press Screen to Start!", 20, 120);
back.drawButton();
Sleep(500);
//wait until player presses the screen to start the game
while (gameRunning == false){
if (!(back.pressed())){ //triggers if the screen is pressed and not the back button
if (LCD.Touch(&xTouch, &yTouch)){
gameRunning = true;
break;
}
}
else { //triggers if back is pressed to return to menu
changeScreen(0);
gameRunning = false;
break;
}
}
bool jumping = false; //detects if the player is in the air or not, prevents double jumping
bool generatedThisSecond = false;
int startTime;
//GAMEPLAY LOOP
playerDead = false;
Sleep(500);
while (gameRunning){
//CREATES A COUNTDOWN IN THE FIRST 3 SECONDS
if (onStartWait > 0) {
refresh();
LCD.WriteAt(onStartWait, SCREEN_WIDTH/2-12, 60);
LCD.Update();
Sleep(1.0);
onStartWait--;
startTime = time(NULL);
continue;
}
else if (onStartWait == 0){
refresh();
LCD.WriteAt("GO!", SCREEN_WIDTH/2-24, 60);
LCD.Update();
Sleep(1.0);
onStartWait--;
startTime = time(NULL);
}
player.setScore(time(NULL)-startTime);
if (playerDead == true){ //check if the player is still alive
player.gameOver();
gameRunning = false;
break;
}
//Generates an obstacle based on the frequency timing
int thisSecond = time(NULL)-startTime;
int timeGenerated;
if(!generatedThisSecond && thisSecond%frequency == 0) {
generateObstacle();
timeGenerated = time(NULL)-startTime;
generatedThisSecond = true;
}
if(thisSecond != timeGenerated) {
generatedThisSecond = false;
}
//check if screen is pressed
if (LCD.Touch(&xTouch, &yTouch)){
if (jumping == false){
jumping = !(jumping);
}
}
//deals with player jumping
if (jumping == true){
if (player.jump(jumpingCounter)){ //returns true until the jump has been completed
jumpingCounter += 1; //increment the time counter for the jump function
}
//reset jump variables
else {
jumping = false;
jumpingCounter = 0;
}
}
//check for collisions with each of the obstacles in the list
for (int i = 0; i < 10; i++){
if (generated[i].onScreen){
if (generated[i].checkCollisions(player)){
playerDead = true;
}
}
}
refresh();
}//main game loop
}
/****************************************************************
* Generate Obstacles Function - creates a new obstacle object
* with a random type and adds it to the generated array
* Authors: Naomi Mukka
****************************************************************/
void generateObstacle(){
//creates an obstacle with a random type
Obstacle a(rand()%4);
//adds obstacle to screen, array of generated obstacles
a.onScreen = true;
generated[generatedIndex] = a;
generatedIndex++;
}
//POST GAME STUFF
/****************************************************************
* Game over function - called when the player dies
* Pauses gameplay and deals with post game menu displays
* Authors: Naomi Mukka
****************************************************************/
void Player::gameOver(){
//Display game over and the player's score
LCD.Clear();
LCD.WriteAt("GAME OVER",106,60);
LCD.WriteAt("Score: ",88,80);
LCD.WriteAt(score,172,80);
LCD.WriteAt(" m",208,80);
if(updateStats()){//Display if the player earned a high score
LCD.WriteAt("HIGH SCORE",100,100);
LCD.WriteAt("Congratulations!",64,120);
}
back.drawButton();
LCD.Update();
flushList();
}
/****************************************************************
* Update Stats Function - evaluates whether the player has made a
* new record and updates the other stats
* Authors: Naomi Mukka
* @return true if the player's score is a high score,
* @return false if the player's score is not a high score
****************************************************************/
bool Player::updateStats(){
int placeIndex;
//Check if the player's score is one of the top 5 scores
if(score > highScores[4]) {
//Determine what place the score belongs in
if(score>highScores[0]){
placeIndex = 0;
} else if(score>highScores[1]){
placeIndex = 1;
} else if(score>highScores[2]) {
placeIndex = 2;
} else if(score>highScores[3]) {
placeIndex = 3;
} else {
placeIndex = 4;
}