Stick_Runner

Dependencies:   FXOS8700CQ Gamepad N5110 SDFileSystem mbed

Committer:
el15ss
Date:
Wed May 03 18:00:37 2017 +0000
Revision:
4:2fdafb53eac2
Parent:
3:0c690f1c04d8
Child:
5:1bf7c83f86cc
Completed inline and DOygen commenting

Who changed what in which revision?

UserRevisionLine numberNew contents of line
el15ss 1:db9ff66f67c8 1 /*****************************************************
el15ss 1:db9ff66f67c8 2 Libraries and modules used *
el15ss 1:db9ff66f67c8 3 ******************************************************/
el15ss 0:12cfe63faa6a 4 #include "mbed.h"
el15ss 0:12cfe63faa6a 5 #include "Gamepad.h"
el15ss 0:12cfe63faa6a 6 #include "N5110.h"
el15ss 0:12cfe63faa6a 7 #include "Character.h"
el15ss 0:12cfe63faa6a 8 #include "Obstacles.h"
el15ss 0:12cfe63faa6a 9 #include "Gems.h"
el15ss 2:98a41609c827 10 #include "SDFileSystem.h"
el15ss 2:98a41609c827 11
el15ss 0:12cfe63faa6a 12
el15ss 1:db9ff66f67c8 13 #define No_OBS 8
el15ss 1:db9ff66f67c8 14 #define No_GEMS 4
el15ss 0:12cfe63faa6a 15
el15ss 3:0c690f1c04d8 16 //Variables
el15ss 3:0c690f1c04d8 17 // i - to loop through the obstacles
el15ss 3:0c690f1c04d8 18 // j - to loop through the gems
el15ss 3:0c690f1c04d8 19 // counter - to keep track of score
el15ss 3:0c690f1c04d8 20 // highScore - to store high score
el15ss 2:98a41609c827 21 int i,j,counter,highScore;
el15ss 3:0c690f1c04d8 22
el15ss 3:0c690f1c04d8 23 //To helo convert counter(int) to string to display on the screen
el15ss 2:98a41609c827 24 char score[50];
el15ss 0:12cfe63faa6a 25
el15ss 1:db9ff66f67c8 26 //Structs
el15ss 0:12cfe63faa6a 27 struct UserInput {
el15ss 0:12cfe63faa6a 28 Direction d;
el15ss 0:12cfe63faa6a 29 float mag;
el15ss 0:12cfe63faa6a 30 };
el15ss 1:db9ff66f67c8 31 /* Class Objects */
el15ss 0:12cfe63faa6a 32 N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11);
el15ss 0:12cfe63faa6a 33 Gamepad pad;
el15ss 0:12cfe63faa6a 34 Character c;
el15ss 1:db9ff66f67c8 35 Obstacles obstacle[No_OBS];
el15ss 3:0c690f1c04d8 36 Gems gems[No_GEMS];
el15ss 0:12cfe63faa6a 37
el15ss 2:98a41609c827 38 SDFileSystem sd(PTE3,PTE1,PTE2,PTE4,"sd");
el15ss 2:98a41609c827 39 FILE *file;
el15ss 2:98a41609c827 40
el15ss 0:12cfe63faa6a 41
el15ss 1:db9ff66f67c8 42 /* Function Prototypes */
el15ss 0:12cfe63faa6a 43 void init();
el15ss 0:12cfe63faa6a 44 void update_game(UserInput input);
el15ss 0:12cfe63faa6a 45 void render();
el15ss 0:12cfe63faa6a 46 void welcome();
el15ss 0:12cfe63faa6a 47 void menu();
el15ss 0:12cfe63faa6a 48 void over();
el15ss 0:12cfe63faa6a 49 void Instructions();
el15ss 3:0c690f1c04d8 50 void stickRunner();
el15ss 3:0c690f1c04d8 51 void displayHighScore();
el15ss 1:db9ff66f67c8 52 /* Functions */
el15ss 1:db9ff66f67c8 53
el15ss 0:12cfe63faa6a 54 int main()
el15ss 0:12cfe63faa6a 55 {
el15ss 3:0c690f1c04d8 56
el15ss 0:12cfe63faa6a 57
el15ss 1:db9ff66f67c8 58 /* Intialization */
el15ss 0:12cfe63faa6a 59 init();
el15ss 1:db9ff66f67c8 60
el15ss 1:db9ff66f67c8 61 /* Drawing the intial frame */
el15ss 0:12cfe63faa6a 62 welcome();
el15ss 3:0c690f1c04d8 63
el15ss 3:0c690f1c04d8 64 }
el15ss 3:0c690f1c04d8 65
el15ss 3:0c690f1c04d8 66
el15ss 3:0c690f1c04d8 67 void init()
el15ss 3:0c690f1c04d8 68 {
el15ss 3:0c690f1c04d8 69 //Need to initialize the lcd and gamepad
el15ss 3:0c690f1c04d8 70 lcd.init();
el15ss 3:0c690f1c04d8 71 pad.init();
el15ss 3:0c690f1c04d8 72
el15ss 3:0c690f1c04d8 73 //Intialzing the charachter
el15ss 3:0c690f1c04d8 74 c.init();
el15ss 3:0c690f1c04d8 75
el15ss 3:0c690f1c04d8 76 //Intialzing the obstacles
el15ss 3:0c690f1c04d8 77 for(i=0;i<No_OBS;i++)
el15ss 3:0c690f1c04d8 78 {
el15ss 3:0c690f1c04d8 79 obstacle[i].init();
el15ss 3:0c690f1c04d8 80 }
el15ss 3:0c690f1c04d8 81
el15ss 3:0c690f1c04d8 82 //Intialzing the gems
el15ss 3:0c690f1c04d8 83 for(j=0;j<No_GEMS;j++)
el15ss 3:0c690f1c04d8 84 {
el15ss 3:0c690f1c04d8 85 gems[j].init();
el15ss 3:0c690f1c04d8 86 }
el15ss 3:0c690f1c04d8 87
el15ss 3:0c690f1c04d8 88
el15ss 3:0c690f1c04d8 89
el15ss 3:0c690f1c04d8 90 }
el15ss 3:0c690f1c04d8 91
el15ss 3:0c690f1c04d8 92
el15ss 3:0c690f1c04d8 93
el15ss 3:0c690f1c04d8 94 //Funstion to display the Welcome page
el15ss 3:0c690f1c04d8 95 void welcome() {
el15ss 3:0c690f1c04d8 96
el15ss 3:0c690f1c04d8 97
el15ss 3:0c690f1c04d8 98
el15ss 3:0c690f1c04d8 99 lcd.printString("Stick Runner! ",0,1);
el15ss 3:0c690f1c04d8 100 lcd.printString(" Press Start ",0,4);
el15ss 3:0c690f1c04d8 101 lcd.refresh();
el15ss 3:0c690f1c04d8 102 // pad.tone(1500.0,0.5);
el15ss 3:0c690f1c04d8 103 // pad.tone(1500.0,0.5);
el15ss 3:0c690f1c04d8 104
el15ss 3:0c690f1c04d8 105
el15ss 3:0c690f1c04d8 106 //Flashes LEDS aslong as START is not pressed
el15ss 3:0c690f1c04d8 107 while ( pad.check_event(Gamepad::START_PRESSED) == false)
el15ss 3:0c690f1c04d8 108 {
el15ss 3:0c690f1c04d8 109 pad.leds_on();
el15ss 3:0c690f1c04d8 110 wait(0.1);
el15ss 3:0c690f1c04d8 111 pad.leds_off();
el15ss 3:0c690f1c04d8 112 wait(0.1);
el15ss 3:0c690f1c04d8 113
el15ss 3:0c690f1c04d8 114 }
el15ss 3:0c690f1c04d8 115
el15ss 3:0c690f1c04d8 116 menu();
el15ss 3:0c690f1c04d8 117 }
el15ss 3:0c690f1c04d8 118
el15ss 3:0c690f1c04d8 119
el15ss 0:12cfe63faa6a 120
el15ss 3:0c690f1c04d8 121 //Function to display the Menu page
el15ss 3:0c690f1c04d8 122 void menu() {
el15ss 3:0c690f1c04d8 123 //int fps =8;
el15ss 3:0c690f1c04d8 124 bool i = true;
el15ss 0:12cfe63faa6a 125
el15ss 3:0c690f1c04d8 126 lcd.clear();
el15ss 3:0c690f1c04d8 127 lcd.printString(" Menu ",0,0);
el15ss 3:0c690f1c04d8 128 lcd.printString("A)New Game ",0,2);
el15ss 3:0c690f1c04d8 129 lcd.printString("B)Continue ",0,3);
el15ss 3:0c690f1c04d8 130 lcd.printString("X)Instructions ",0,4);
el15ss 3:0c690f1c04d8 131 lcd.printString("Y)High Score ",0,5);
el15ss 3:0c690f1c04d8 132
el15ss 3:0c690f1c04d8 133 lcd.refresh();
el15ss 3:0c690f1c04d8 134
el15ss 3:0c690f1c04d8 135
el15ss 3:0c690f1c04d8 136 while(i == true) {
el15ss 3:0c690f1c04d8 137 // wait flashing LEDs until start button is pressed
el15ss 3:0c690f1c04d8 138 //Condition to start a new game
el15ss 3:0c690f1c04d8 139 if( pad.check_event(Gamepad::A_PRESSED) ) {
el15ss 3:0c690f1c04d8 140 //pad.tone(1000.0,0.5);
el15ss 3:0c690f1c04d8 141 i = false;
el15ss 3:0c690f1c04d8 142
el15ss 3:0c690f1c04d8 143 //Clear, refresh and intialize the game again so we can start a new game
el15ss 3:0c690f1c04d8 144 lcd.clear();
el15ss 3:0c690f1c04d8 145 lcd.refresh();
el15ss 3:0c690f1c04d8 146 init();
el15ss 3:0c690f1c04d8 147 stickRunner();
el15ss 3:0c690f1c04d8 148
el15ss 3:0c690f1c04d8 149 //break;
el15ss 3:0c690f1c04d8 150 }
el15ss 3:0c690f1c04d8 151
el15ss 3:0c690f1c04d8 152 //To continue the same game
el15ss 3:0c690f1c04d8 153 else if( pad.check_event(Gamepad::B_PRESSED) ) {
el15ss 3:0c690f1c04d8 154 //lcd.clear();
el15ss 3:0c690f1c04d8 155 // pad.tone(1000.0,0.5);
el15ss 3:0c690f1c04d8 156 i = false;
el15ss 3:0c690f1c04d8 157
el15ss 3:0c690f1c04d8 158 //Simply refreshes the page and continues from where the user left the game
el15ss 3:0c690f1c04d8 159 // as the intialize function init() is not called again
el15ss 3:0c690f1c04d8 160 lcd.refresh();
el15ss 3:0c690f1c04d8 161 stickRunner();
el15ss 3:0c690f1c04d8 162
el15ss 3:0c690f1c04d8 163
el15ss 3:0c690f1c04d8 164 }
el15ss 3:0c690f1c04d8 165
el15ss 3:0c690f1c04d8 166 //To read the game instructions
el15ss 3:0c690f1c04d8 167 else if( pad.check_event(Gamepad::X_PRESSED) ) {
el15ss 3:0c690f1c04d8 168 // pad.tone(1000.0,0.5);
el15ss 3:0c690f1c04d8 169 Instructions();
el15ss 3:0c690f1c04d8 170
el15ss 3:0c690f1c04d8 171 i = false;
el15ss 3:0c690f1c04d8 172 }
el15ss 3:0c690f1c04d8 173
el15ss 3:0c690f1c04d8 174 //To see the game high score
el15ss 3:0c690f1c04d8 175 else if( pad.check_event(Gamepad::Y_PRESSED) ) {
el15ss 3:0c690f1c04d8 176 //pad.tone(1000.0,0.5);
el15ss 3:0c690f1c04d8 177 displayHighScore();
el15ss 3:0c690f1c04d8 178
el15ss 3:0c690f1c04d8 179 i = false;
el15ss 3:0c690f1c04d8 180 }
el15ss 3:0c690f1c04d8 181
el15ss 3:0c690f1c04d8 182
el15ss 3:0c690f1c04d8 183 }
el15ss 3:0c690f1c04d8 184 }
el15ss 3:0c690f1c04d8 185
el15ss 3:0c690f1c04d8 186
el15ss 3:0c690f1c04d8 187
el15ss 3:0c690f1c04d8 188
el15ss 3:0c690f1c04d8 189 //This function is responsible for running the game
el15ss 3:0c690f1c04d8 190 void stickRunner()
el15ss 3:0c690f1c04d8 191 {
el15ss 3:0c690f1c04d8 192 int fps = 8;
el15ss 3:0c690f1c04d8 193
el15ss 1:db9ff66f67c8 194 render();
el15ss 0:12cfe63faa6a 195 wait(1.0f/fps);
el15ss 0:12cfe63faa6a 196
el15ss 1:db9ff66f67c8 197 /* Main game loop to read input, render the display and update the game state */
el15ss 3:0c690f1c04d8 198
el15ss 0:12cfe63faa6a 199 while (1) {
el15ss 0:12cfe63faa6a 200
el15ss 3:0c690f1c04d8 201 //As long as the character survives update the score
el15ss 2:98a41609c827 202 counter++;
el15ss 0:12cfe63faa6a 203
el15ss 3:0c690f1c04d8 204 //Using the gamepad library to move the character using the joystick
el15ss 1:db9ff66f67c8 205 c.updateCharacter(pad.get_direction(),pad.get_mag());
el15ss 3:0c690f1c04d8 206
el15ss 3:0c690f1c04d8 207 //Condition to ckeck if the user wants to pause the game
el15ss 0:12cfe63faa6a 208 if(pad.check_event(Gamepad::BACK_PRESSED))
el15ss 0:12cfe63faa6a 209 {
el15ss 0:12cfe63faa6a 210 lcd.clear();
el15ss 0:12cfe63faa6a 211 lcd.refresh();
el15ss 1:db9ff66f67c8 212 menu();
el15ss 0:12cfe63faa6a 213 }
el15ss 0:12cfe63faa6a 214
el15ss 3:0c690f1c04d8 215 //Loop to make the generation of obstacles a continious loop and also to check if the user has been killed
el15ss 1:db9ff66f67c8 216 for(i=0;i<No_OBS;i++)
el15ss 0:12cfe63faa6a 217 {
el15ss 3:0c690f1c04d8 218 //To retrieve the status of the obstacle on the screen
el15ss 1:db9ff66f67c8 219 obstacle[i].obstacleStatus(obstacle[i].getObstaclePos());
el15ss 0:12cfe63faa6a 220
el15ss 1:db9ff66f67c8 221 if(obstacle[i].getObstacleStatus() == false)
el15ss 0:12cfe63faa6a 222 {
el15ss 0:12cfe63faa6a 223 obstacle[i].init();
el15ss 0:12cfe63faa6a 224 }
el15ss 3:0c690f1c04d8 225
el15ss 3:0c690f1c04d8 226 //To check whether the character has been hit by an obstacle by comparing the position of each obstacle
el15ss 3:0c690f1c04d8 227 // relative to the character
el15ss 1:db9ff66f67c8 228 c.characterStatus(obstacle[i].getObstaclePos());
el15ss 3:0c690f1c04d8 229
el15ss 1:db9ff66f67c8 230
el15ss 0:12cfe63faa6a 231 }
el15ss 0:12cfe63faa6a 232
el15ss 3:0c690f1c04d8 233 //Loop to make the generation of gems a continious loop and also to check if the user has collected them
el15ss 1:db9ff66f67c8 234 for(j=0;j<No_GEMS;j++)
el15ss 0:12cfe63faa6a 235 {
el15ss 3:0c690f1c04d8 236 //To check whether the character has collected a gem by comparing the position of each gem
el15ss 3:0c690f1c04d8 237 // relative to the character
el15ss 1:db9ff66f67c8 238 gems[j].gemStatus(c.getCharacterPos());
el15ss 3:0c690f1c04d8 239
el15ss 3:0c690f1c04d8 240
el15ss 0:12cfe63faa6a 241
el15ss 1:db9ff66f67c8 242 if(gems[j].getGemStatus() == false)
el15ss 0:12cfe63faa6a 243 {
el15ss 0:12cfe63faa6a 244 gems[j].init();
el15ss 2:98a41609c827 245
el15ss 0:12cfe63faa6a 246 }
el15ss 1:db9ff66f67c8 247
el15ss 1:db9ff66f67c8 248
el15ss 0:12cfe63faa6a 249 }
el15ss 2:98a41609c827 250
el15ss 2:98a41609c827 251 //To make the obstacles and gems move along the screen
el15ss 3:0c690f1c04d8 252
el15ss 3:0c690f1c04d8 253
el15ss 0:12cfe63faa6a 254 i =0;
el15ss 0:12cfe63faa6a 255
el15ss 1:db9ff66f67c8 256 for(i=0;i<No_OBS;i++)
el15ss 1:db9ff66f67c8 257 {
el15ss 1:db9ff66f67c8 258 obstacle[i].updateObstacle();
el15ss 1:db9ff66f67c8 259 }
el15ss 0:12cfe63faa6a 260
el15ss 0:12cfe63faa6a 261
el15ss 0:12cfe63faa6a 262 j =0;
el15ss 0:12cfe63faa6a 263
el15ss 1:db9ff66f67c8 264 for(j=0;j<No_GEMS;j++)
el15ss 1:db9ff66f67c8 265 {
el15ss 1:db9ff66f67c8 266 gems[j].updateGems();
el15ss 2:98a41609c827 267
el15ss 1:db9ff66f67c8 268 }
el15ss 0:12cfe63faa6a 269
el15ss 0:12cfe63faa6a 270 render();
el15ss 0:12cfe63faa6a 271
el15ss 0:12cfe63faa6a 272 wait(1.0f/fps);
el15ss 0:12cfe63faa6a 273 }
el15ss 3:0c690f1c04d8 274
el15ss 0:12cfe63faa6a 275 }
el15ss 0:12cfe63faa6a 276
el15ss 1:db9ff66f67c8 277
el15ss 3:0c690f1c04d8 278 //Function to draw out the pixels on the screen
el15ss 0:12cfe63faa6a 279 void render()
el15ss 0:12cfe63faa6a 280 {
el15ss 2:98a41609c827 281
el15ss 0:12cfe63faa6a 282 lcd.clear();
el15ss 1:db9ff66f67c8 283
el15ss 3:0c690f1c04d8 284
el15ss 3:0c690f1c04d8 285 //Only draws the character as long as it survives
el15ss 1:db9ff66f67c8 286 if(c.getCharacterStatus())
el15ss 1:db9ff66f67c8 287 {
el15ss 1:db9ff66f67c8 288 c.draw(lcd);
el15ss 1:db9ff66f67c8 289 }
el15ss 2:98a41609c827 290
el15ss 2:98a41609c827 291 if(c.getCharacterStatus() == false)
el15ss 2:98a41609c827 292 {
el15ss 2:98a41609c827 293 over();
el15ss 2:98a41609c827 294 }
el15ss 2:98a41609c827 295
el15ss 2:98a41609c827 296
el15ss 3:0c690f1c04d8 297 //Draws the obstacles if the status returned is true
el15ss 0:12cfe63faa6a 298
el15ss 1:db9ff66f67c8 299 for(i=0;i<No_OBS;i++)
el15ss 1:db9ff66f67c8 300 {
el15ss 1:db9ff66f67c8 301 if(obstacle[i].getObstacleStatus())
el15ss 1:db9ff66f67c8 302 {
el15ss 1:db9ff66f67c8 303 obstacle[i].draw(lcd);
el15ss 1:db9ff66f67c8 304 }
el15ss 1:db9ff66f67c8 305 }
el15ss 0:12cfe63faa6a 306
el15ss 3:0c690f1c04d8 307 //Draws the gems if the status returned is true
el15ss 1:db9ff66f67c8 308 for(j=0;j<No_GEMS;j++)
el15ss 1:db9ff66f67c8 309 {
el15ss 1:db9ff66f67c8 310 if(gems[j].getGemStatus())
el15ss 1:db9ff66f67c8 311 {
el15ss 1:db9ff66f67c8 312 gems[j].draw(lcd);
el15ss 1:db9ff66f67c8 313
el15ss 1:db9ff66f67c8 314 }
el15ss 1:db9ff66f67c8 315
el15ss 3:0c690f1c04d8 316
el15ss 0:12cfe63faa6a 317
el15ss 1:db9ff66f67c8 318 }
el15ss 2:98a41609c827 319
el15ss 1:db9ff66f67c8 320
el15ss 2:98a41609c827 321
el15ss 1:db9ff66f67c8 322
el15ss 1:db9ff66f67c8 323
el15ss 0:12cfe63faa6a 324 lcd.refresh();
el15ss 1:db9ff66f67c8 325
el15ss 0:12cfe63faa6a 326 }
el15ss 0:12cfe63faa6a 327
el15ss 1:db9ff66f67c8 328
el15ss 0:12cfe63faa6a 329
el15ss 3:0c690f1c04d8 330 //Function to display end of game and also check whether the user got a new highscore and if not write it on the SD card
el15ss 0:12cfe63faa6a 331 void over() {
el15ss 3:0c690f1c04d8 332 //pad.tone(1000.0,0.5);
el15ss 2:98a41609c827 333 pad.init();
el15ss 4:2fdafb53eac2 334 // lcd.init();
el15ss 2:98a41609c827 335 // lcd.clear();
el15ss 3:0c690f1c04d8 336
el15ss 3:0c690f1c04d8 337 //Mounting on the SD card to read/write in it
el15ss 4:2fdafb53eac2 338 //sd.mount();
el15ss 2:98a41609c827 339
el15ss 3:0c690f1c04d8 340 //Converting the counter into a string 'score' to display on the lcd
el15ss 2:98a41609c827 341 sprintf (score, " Score : %d",counter);
el15ss 2:98a41609c827 342
el15ss 2:98a41609c827 343 lcd.printString(score,0,2);
el15ss 2:98a41609c827 344 lcd.printString("GAME OVER!! ",0,0);
el15ss 2:98a41609c827 345 //lcd.printString(" ",0,1);
el15ss 1:db9ff66f67c8 346
el15ss 3:0c690f1c04d8 347 //Opening file on the SD card
el15ss 4:2fdafb53eac2 348 /*file = fopen("/sd/scoreFile.txt", "r");
el15ss 3:0c690f1c04d8 349
el15ss 3:0c690f1c04d8 350 //If file is empty and score to it and display it as the High Score
el15ss 2:98a41609c827 351 if(file ==NULL)
el15ss 2:98a41609c827 352 {
el15ss 2:98a41609c827 353 file = fopen("/sd/scoreFile.txt", "w");
el15ss 2:98a41609c827 354 fprintf(file,"%d",counter);
el15ss 2:98a41609c827 355 fclose(file);
el15ss 2:98a41609c827 356 lcd.printString("HIGH SCORE",0,3);
el15ss 2:98a41609c827 357
el15ss 2:98a41609c827 358 }
el15ss 3:0c690f1c04d8 359
el15ss 3:0c690f1c04d8 360 //if not empty compare against the exsisting high score and display whether the user has made a new high score
el15ss 2:98a41609c827 361 else{
el15ss 2:98a41609c827 362 fscanf(file,"%d", &highScore);
el15ss 2:98a41609c827 363 fclose(file);
el15ss 2:98a41609c827 364
el15ss 2:98a41609c827 365 if(counter>highScore)
el15ss 2:98a41609c827 366 {
el15ss 2:98a41609c827 367
el15ss 2:98a41609c827 368 file = fopen("/sd/scoreFile.txt", "w");
el15ss 2:98a41609c827 369 fprintf(file,"%d",counter);
el15ss 2:98a41609c827 370 fclose(file);
el15ss 2:98a41609c827 371 lcd.printString("HIGH SCORE",0,3);
el15ss 2:98a41609c827 372 }
el15ss 2:98a41609c827 373
el15ss 4:2fdafb53eac2 374 }*/
el15ss 2:98a41609c827 375
el15ss 1:db9ff66f67c8 376
el15ss 4:2fdafb53eac2 377 lcd.printString(" PRESS START ",0,5);
el15ss 0:12cfe63faa6a 378
el15ss 0:12cfe63faa6a 379 lcd.refresh();
el15ss 4:2fdafb53eac2 380 //sd.unmount();
el15ss 0:12cfe63faa6a 381
el15ss 3:0c690f1c04d8 382 //Takes the user back to the main for a new game
el15ss 0:12cfe63faa6a 383 while ( pad.check_event(Gamepad::START_PRESSED) == false) {
el15ss 0:12cfe63faa6a 384 pad.leds_on();
el15ss 1:db9ff66f67c8 385 //pad.tone(1000.0,0.5);
el15ss 0:12cfe63faa6a 386 wait(0.1);
el15ss 0:12cfe63faa6a 387 pad.leds_off();
el15ss 1:db9ff66f67c8 388 // pad.tone(1000.0,0.5);
el15ss 0:12cfe63faa6a 389 wait(0.1);
el15ss 0:12cfe63faa6a 390 if( pad.check_event(Gamepad::START_PRESSED)) {
el15ss 0:12cfe63faa6a 391 main();
el15ss 0:12cfe63faa6a 392 wait(1);
el15ss 0:12cfe63faa6a 393 }
el15ss 0:12cfe63faa6a 394 }
el15ss 0:12cfe63faa6a 395
el15ss 0:12cfe63faa6a 396 }
el15ss 0:12cfe63faa6a 397
el15ss 3:0c690f1c04d8 398
el15ss 3:0c690f1c04d8 399 //Function to display the current High score fo the game and also reset it to 0
el15ss 2:98a41609c827 400 void displayHighScore()
el15ss 2:98a41609c827 401 {
el15ss 2:98a41609c827 402 sd.mount();
el15ss 2:98a41609c827 403 lcd.clear();
el15ss 2:98a41609c827 404
el15ss 3:0c690f1c04d8 405 //Open file
el15ss 2:98a41609c827 406 file = fopen("/sd/scoreFile.txt", "r");
el15ss 2:98a41609c827 407 if(file ==NULL)
el15ss 3:0c690f1c04d8 408 {
el15ss 3:0c690f1c04d8 409
el15ss 2:98a41609c827 410 highScore = 0;
el15ss 2:98a41609c827 411
el15ss 2:98a41609c827 412 }
el15ss 3:0c690f1c04d8 413 else{
el15ss 3:0c690f1c04d8 414 //Read the high score from the file
el15ss 2:98a41609c827 415 fscanf(file,"%d", &highScore);
el15ss 2:98a41609c827 416 fclose(file);
el15ss 2:98a41609c827 417
el15ss 2:98a41609c827 418 }
el15ss 3:0c690f1c04d8 419 //Convert highscore(int) to score(String) to print on the lcd
el15ss 2:98a41609c827 420 sprintf (score, "High Score : %d",highScore);
el15ss 2:98a41609c827 421
el15ss 2:98a41609c827 422 lcd.printString(score,0,2);
el15ss 2:98a41609c827 423 lcd.printString(" START - reset ",0,4);
el15ss 2:98a41609c827 424 lcd.printString(" BACK - menu ",0,5);
el15ss 2:98a41609c827 425 lcd.refresh();
el15ss 2:98a41609c827 426 sd.unmount();
el15ss 2:98a41609c827 427
el15ss 2:98a41609c827 428 while(1)
el15ss 2:98a41609c827 429 {
el15ss 3:0c690f1c04d8 430 //To reset the highscore
el15ss 2:98a41609c827 431 if( pad.check_event(Gamepad::START_PRESSED)) {
el15ss 2:98a41609c827 432 sd.mount();
el15ss 2:98a41609c827 433 file = fopen("/sd/scoreFile.txt", "r");
el15ss 2:98a41609c827 434 if(!file ==NULL)
el15ss 3:0c690f1c04d8 435 { //Delete the file if it is empty
el15ss 2:98a41609c827 436 fclose(file);
el15ss 2:98a41609c827 437 remove("/sd/scoreFile.txt");
el15ss 2:98a41609c827 438
el15ss 2:98a41609c827 439 }
el15ss 2:98a41609c827 440 sd.unmount();
el15ss 2:98a41609c827 441 displayHighScore();
el15ss 2:98a41609c827 442
el15ss 2:98a41609c827 443 }
el15ss 3:0c690f1c04d8 444 //Back to menu
el15ss 2:98a41609c827 445 if( pad.check_event(Gamepad::BACK_PRESSED)) {
el15ss 2:98a41609c827 446 menu();
el15ss 2:98a41609c827 447 }
el15ss 2:98a41609c827 448
el15ss 2:98a41609c827 449 sleep();
el15ss 2:98a41609c827 450
el15ss 2:98a41609c827 451 }
el15ss 2:98a41609c827 452
el15ss 2:98a41609c827 453
el15ss 2:98a41609c827 454 }
el15ss 2:98a41609c827 455
el15ss 3:0c690f1c04d8 456
el15ss 0:12cfe63faa6a 457
el15ss 3:0c690f1c04d8 458 //Function to display the Instructions for the game
el15ss 0:12cfe63faa6a 459 void Instructions()
el15ss 0:12cfe63faa6a 460 {
el15ss 0:12cfe63faa6a 461 bool i = true;
el15ss 0:12cfe63faa6a 462 lcd.clear();
el15ss 2:98a41609c827 463 lcd.printString("INSTURCTIONS: ",0,0);
el15ss 0:12cfe63faa6a 464 lcd.printString("Collect the ",0,2);
el15ss 2:98a41609c827 465 lcd.printString("gems and dodge ",0,3);
el15ss 2:98a41609c827 466 lcd.printString("the obstacles ",0,4);
el15ss 2:98a41609c827 467 lcd.printString("to get points ",0,5);
el15ss 0:12cfe63faa6a 468 lcd.refresh();
el15ss 0:12cfe63faa6a 469
el15ss 0:12cfe63faa6a 470 while(i == true){
el15ss 0:12cfe63faa6a 471
el15ss 0:12cfe63faa6a 472 if( pad.check_event(Gamepad::BACK_PRESSED) ) {
el15ss 1:db9ff66f67c8 473 //pad.tone(1000.0,0.5);
el15ss 0:12cfe63faa6a 474 i = false;
el15ss 0:12cfe63faa6a 475 menu();
el15ss 0:12cfe63faa6a 476 }
el15ss 0:12cfe63faa6a 477 }
el15ss 0:12cfe63faa6a 478
el15ss 0:12cfe63faa6a 479 }