Stick_Runner
Dependencies: FXOS8700CQ Gamepad N5110 SDFileSystem mbed
Diff: main.cpp
- Revision:
- 3:0c690f1c04d8
- Parent:
- 2:98a41609c827
- Child:
- 4:2fdafb53eac2
--- a/main.cpp Tue May 02 18:42:45 2017 +0000 +++ b/main.cpp Wed May 03 09:27:04 2017 +0000 @@ -13,7 +13,14 @@ #define No_OBS 8 #define No_GEMS 4 +//Variables +// i - to loop through the obstacles +// j - to loop through the gems +// counter - to keep track of score +// highScore - to store high score int i,j,counter,highScore; + +//To helo convert counter(int) to string to display on the screen char score[50]; //Structs @@ -26,7 +33,7 @@ Gamepad pad; Character c; Obstacles obstacle[No_OBS]; -Gems gems[No_GEMS],g; +Gems gems[No_GEMS]; SDFileSystem sd(PTE3,PTE1,PTE2,PTE4,"sd"); FILE *file; @@ -40,33 +47,164 @@ void menu(); void over(); void Instructions(); - - +void stickRunner(); +void displayHighScore(); /* Functions */ int main() { - int fps = 8; + /* Intialization */ init(); /* Drawing the intial frame */ welcome(); - wait(1.0f/fps); + +} + + +void init() +{ +//Need to initialize the lcd and gamepad + lcd.init(); + pad.init(); + +//Intialzing the charachter + c.init(); + +//Intialzing the obstacles + for(i=0;i<No_OBS;i++) + { + obstacle[i].init(); + } + +//Intialzing the gems + for(j=0;j<No_GEMS;j++) + { + gems[j].init(); + } + + + +} + + + +//Funstion to display the Welcome page +void welcome() { + + + + lcd.printString("Stick Runner! ",0,1); + lcd.printString(" Press Start ",0,4); + lcd.refresh(); + // pad.tone(1500.0,0.5); + // pad.tone(1500.0,0.5); + + + //Flashes LEDS aslong as START is not pressed + while ( pad.check_event(Gamepad::START_PRESSED) == false) + { + pad.leds_on(); + wait(0.1); + pad.leds_off(); + wait(0.1); + + } + + menu(); +} + + +//Function to display the Menu page +void menu() { + //int fps =8; + bool i = true; - + lcd.clear(); + lcd.printString(" Menu ",0,0); + lcd.printString("A)New Game ",0,2); + lcd.printString("B)Continue ",0,3); + lcd.printString("X)Instructions ",0,4); + lcd.printString("Y)High Score ",0,5); + + lcd.refresh(); + + + while(i == true) { + // wait flashing LEDs until start button is pressed + //Condition to start a new game + if( pad.check_event(Gamepad::A_PRESSED) ) { + //pad.tone(1000.0,0.5); + i = false; + + //Clear, refresh and intialize the game again so we can start a new game + lcd.clear(); + lcd.refresh(); + init(); + stickRunner(); + + //break; + } + + //To continue the same game + else if( pad.check_event(Gamepad::B_PRESSED) ) { + //lcd.clear(); + // pad.tone(1000.0,0.5); + i = false; + + //Simply refreshes the page and continues from where the user left the game + // as the intialize function init() is not called again + lcd.refresh(); + stickRunner(); + + + } + + //To read the game instructions + else if( pad.check_event(Gamepad::X_PRESSED) ) { + // pad.tone(1000.0,0.5); + Instructions(); + + i = false; + } + + //To see the game high score + else if( pad.check_event(Gamepad::Y_PRESSED) ) { + //pad.tone(1000.0,0.5); + displayHighScore(); + + i = false; + } + + + } +} + + + + +//This function is responsible for running the game +void stickRunner() +{ + int fps = 8; + render(); wait(1.0f/fps); /* Main game loop to read input, render the display and update the game state */ - // game loop - read input, update the game state and render the display + while (1) { + //As long as the character survives update the score counter++; + //Using the gamepad library to move the character using the joystick c.updateCharacter(pad.get_direction(),pad.get_mag()); + + //Condition to ckeck if the user wants to pause the game if(pad.check_event(Gamepad::BACK_PRESSED)) { lcd.clear(); @@ -74,23 +212,32 @@ menu(); } + //Loop to make the generation of obstacles a continious loop and also to check if the user has been killed for(i=0;i<No_OBS;i++) { + //To retrieve the status of the obstacle on the screen obstacle[i].obstacleStatus(obstacle[i].getObstaclePos()); if(obstacle[i].getObstacleStatus() == false) { obstacle[i].init(); } + + //To check whether the character has been hit by an obstacle by comparing the position of each obstacle + // relative to the character c.characterStatus(obstacle[i].getObstaclePos()); - // obstacle[i].obstacleStatus(c.getCharacterPos()); + } - + //Loop to make the generation of gems a continious loop and also to check if the user has collected them for(j=0;j<No_GEMS;j++) { + //To check whether the character has collected a gem by comparing the position of each gem + // relative to the character gems[j].gemStatus(c.getCharacterPos()); + + if(gems[j].getGemStatus() == false) { @@ -102,6 +249,8 @@ } //To make the obstacles and gems move along the screen + + i =0; for(i=0;i<No_OBS;i++) @@ -122,34 +271,18 @@ wait(1.0f/fps); } + } -void init() -{ - - lcd.init(); - pad.init(); - c.init(); - - for(i=0;i<No_OBS;i++) - { - obstacle[i].init(); - } - for(j=0;j<No_GEMS;j++) - { - gems[j].init(); - } - - - -} - +//Function to draw out the pixels on the screen void render() { lcd.clear(); + + //Only draws the character as long as it survives if(c.getCharacterStatus()) { c.draw(lcd); @@ -161,7 +294,7 @@ } - //obstacle[i].draw(lcd); + //Draws the obstacles if the status returned is true for(i=0;i<No_OBS;i++) { @@ -171,7 +304,7 @@ } } - + //Draws the gems if the status returned is true for(j=0;j<No_GEMS;j++) { if(gems[j].getGemStatus()) @@ -180,9 +313,7 @@ } - /*else{ - counter++; - }*/ + } @@ -195,43 +326,27 @@ } -void welcome() { - - - - lcd.printString("Stick Runner! ",0,1); - lcd.printString(" Press Start ",0,4); - lcd.refresh(); - // pad.tone(1500.0,0.5); - //pad.tone(1500.0,0.5); - - - - while ( pad.check_event(Gamepad::START_PRESSED) == false) - { - pad.leds_on(); - wait(0.1); - pad.leds_off(); - wait(0.1); - - } - menu(); -} +//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 void over() { - + //pad.tone(1000.0,0.5); pad.init(); // lcd.clear(); + + //Mounting on the SD card to read/write in it sd.mount(); - + //Converting the counter into a string 'score' to display on the lcd sprintf (score, " Score : %d",counter); lcd.printString(score,0,2); lcd.printString("GAME OVER!! ",0,0); //lcd.printString(" ",0,1); + //Opening file on the SD card file = fopen("/sd/scoreFile.txt", "r"); + + //If file is empty and score to it and display it as the High Score if(file ==NULL) { file = fopen("/sd/scoreFile.txt", "w"); @@ -240,6 +355,8 @@ lcd.printString("HIGH SCORE",0,3); } + + //if not empty compare against the exsisting high score and display whether the user has made a new high score else{ fscanf(file,"%d", &highScore); fclose(file); @@ -261,7 +378,7 @@ lcd.refresh(); sd.unmount(); - + //Takes the user back to the main for a new game while ( pad.check_event(Gamepad::START_PRESSED) == false) { pad.leds_on(); //pad.tone(1000.0,0.5); @@ -277,24 +394,28 @@ } + +//Function to display the current High score fo the game and also reset it to 0 void displayHighScore() { sd.mount(); lcd.clear(); - + //Open file file = fopen("/sd/scoreFile.txt", "r"); if(file ==NULL) - { + { + highScore = 0; } - else{ + else{ + //Read the high score from the file fscanf(file,"%d", &highScore); fclose(file); } - + //Convert highscore(int) to score(String) to print on the lcd sprintf (score, "High Score : %d",highScore); lcd.printString(score,0,2); @@ -305,11 +426,12 @@ while(1) { + //To reset the highscore if( pad.check_event(Gamepad::START_PRESSED)) { sd.mount(); file = fopen("/sd/scoreFile.txt", "r"); if(!file ==NULL) - { + { //Delete the file if it is empty fclose(file); remove("/sd/scoreFile.txt"); @@ -318,7 +440,7 @@ displayHighScore(); } - + //Back to menu if( pad.check_event(Gamepad::BACK_PRESSED)) { menu(); } @@ -330,58 +452,9 @@ } -void menu() { - int fps =8; - bool i = true; - while(i == true) - { - lcd.clear(); - lcd.printString(" Menu ",0,0); - lcd.printString("A)New Game ",0,2); - lcd.printString("B)Continue ",0,3); - lcd.printString("X)Instructions ",0,4); - lcd.printString("Y)High Score ",0,5); - - lcd.refresh(); - - // wait flashing LEDs until start button is pressed - if( pad.check_event(Gamepad::A_PRESSED) ) { - //pad.tone(1000.0,0.5); - i = false; - lcd.clear(); - lcd.refresh(); - init(); - - //break; - } - - else if( pad.check_event(Gamepad::B_PRESSED) ) { - //lcd.clear(); - // pad.tone(1000.0,0.5); - i = false; - lcd.refresh(); - - - } - - else if( pad.check_event(Gamepad::X_PRESSED) ) { - // pad.tone(1000.0,0.5); - Instructions(); - wait(1.0f/fps); - i = false; - } - - else if( pad.check_event(Gamepad::Y_PRESSED) ) { - // pad.tone(1000.0,0.5); - displayHighScore(); - wait(10.0f/fps); - i = false; - } - - - } -} + +//Function to display the Instructions for the game void Instructions() { bool i = true;