
Stick_Runner
Dependencies: FXOS8700CQ Gamepad N5110 SDFileSystem mbed
Revision 5:1bf7c83f86cc, committed 2017-05-04
- Comitter:
- el15ss
- Date:
- Thu May 04 09:50:18 2017 +0000
- Parent:
- 4:2fdafb53eac2
- Child:
- 6:bf601a65cb27
- Commit message:
- Got rid of wait() functions and added sleep(). Cleaned up the code
Changed in this revision
--- a/Character/Character.cpp Wed May 03 18:00:37 2017 +0000 +++ b/Character/Character.cpp Thu May 04 09:50:18 2017 +0000 @@ -1,5 +1,3 @@ - - #include "Character.h" @@ -20,7 +18,7 @@ { - //Drawing the character + //Drawing the character lcd.setPixel(charPosX,charPosY); lcd.setPixel(charPosX-1,charPosY); lcd.setPixel(charPosX+1,charPosY); @@ -48,7 +46,7 @@ } - +//Function to move the character using the joystick void Character::updateCharacter(Direction d,float mag) { _speed = int(mag*10.0f); // scale is arbitrary, could be changed in future @@ -139,7 +137,7 @@ //Using the dimensions of the character we check if there any pixels near it if(((charPosX-5<p.x)&&(charPosX+5>p.x))&&((charPosY-5<p.y)&&(charPosY+5>p.y))) { - //Updateing the status + //Updating the status charStatus = false; } } @@ -147,7 +145,7 @@ //Function to return the status of the character bool Character::getCharacterStatus() { - //Used to determine whether the character has been and hit and the game is over + //Used to determine whether the character has been and hit and charstatus returns false the game is over return charStatus; }
--- a/Gems/Gems.cpp Wed May 03 18:00:37 2017 +0000 +++ b/Gems/Gems.cpp Thu May 04 09:50:18 2017 +0000 @@ -15,7 +15,7 @@ void Gems::draw(N5110 &lcd) { - //Drawing the gems + //Drawing the gems lcd.setPixel(gemPosX,gemPosY); lcd.setPixel(gemPosX+1,gemPosY); lcd.setPixel(gemPosX-1,gemPosY);
--- a/Gems/Gems.h Wed May 03 18:00:37 2017 +0000 +++ b/Gems/Gems.h Thu May 04 09:50:18 2017 +0000 @@ -66,7 +66,7 @@ private: -//Variables + //Variables int gemPosX; // X cocordinate of the gem int gemPosY; // Y cocordinate of the gem bool gStatus; //Variable to store the Obstacles status
--- a/main.cpp Wed May 03 18:00:37 2017 +0000 +++ b/main.cpp Thu May 04 09:50:18 2017 +0000 @@ -24,7 +24,8 @@ char score[50]; //Structs -struct UserInput { +struct UserInput +{ Direction d; float mag; }; @@ -58,7 +59,7 @@ /* Intialization */ init(); -/* Drawing the intial frame */ +/* Drawing the initial frame */ welcome(); } @@ -92,7 +93,8 @@ //Funstion to display the Welcome page -void welcome() { +void welcome() +{ @@ -119,11 +121,12 @@ //Function to display the Menu page -void menu() { - //int fps =8; - bool i = true; +void menu() +{ + - lcd.clear(); + + lcd.clear(); lcd.printString(" Menu ",0,0); lcd.printString("A)New Game ",0,2); lcd.printString("B)Continue ",0,3); @@ -133,54 +136,62 @@ 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; + while(1) + { + // 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); + - //Clear, refresh and intialize the game again so we can start a new game - lcd.clear(); - lcd.refresh(); - init(); - stickRunner(); + //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; + //To continue the same game + else if( pad.check_event(Gamepad::B_PRESSED) ) + { + + // pad.tone(1000.0,0.5); + - //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(); + //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(); + //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(); + //To see the game high score + else if( pad.check_event(Gamepad::Y_PRESSED) ) + { + //pad.tone(1000.0,0.5); + displayHighScore(); - i = false; - } + + + } + + sleep(); - } + } } @@ -191,12 +202,13 @@ { int fps = 8; - render(); - wait(1.0f/fps); + render(); + wait(1.0f/fps); /* Main game loop to read input, render the display and update the game state */ - while (1) { + while (1) + { //As long as the character survives update the score counter++; @@ -225,7 +237,7 @@ //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()); + c.characterStatus(obstacle[i].getObstaclePos()); } @@ -288,10 +300,10 @@ c.draw(lcd); } - if(c.getCharacterStatus() == false) - { - over(); - } + if(c.getCharacterStatus() == false) + { + over(); + } //Draws the obstacles if the status returned is true @@ -305,7 +317,7 @@ } //Draws the gems if the status returned is true - for(j=0;j<No_GEMS;j++) + for(j=0;j<No_GEMS;j++) { if(gems[j].getGemStatus()) { @@ -314,13 +326,9 @@ } - } - - - lcd.refresh(); } @@ -328,13 +336,14 @@ //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() { +void over() +{ //pad.tone(1000.0,0.5); pad.init(); - // lcd.init(); - // lcd.clear(); + // lcd.init(); + // lcd.clear(); - //Mounting on the SD card to read/write in it + //Mounting on the SD card to read/write in it //sd.mount(); //Converting the counter into a string 'score' to display on the lcd @@ -353,25 +362,26 @@ file = fopen("/sd/scoreFile.txt", "w"); fprintf(file,"%d",counter); fclose(file); - lcd.printString("HIGH SCORE",0,3); + 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); + else + { + fscanf(file,"%d", &highScore); + fclose(file); - if(counter>highScore) - { + if(counter>highScore) + { - file = fopen("/sd/scoreFile.txt", "w"); - fprintf(file,"%d",counter); - fclose(file); - lcd.printString("HIGH SCORE",0,3); - } + file = fopen("/sd/scoreFile.txt", "w"); + fprintf(file,"%d",counter); + fclose(file); + lcd.printString("HIGH SCORE",0,3); + } - }*/ + }*/ lcd.printString(" PRESS START ",0,5); @@ -380,17 +390,19 @@ //sd.unmount(); //Takes the user back to the main for a new game - while ( pad.check_event(Gamepad::START_PRESSED) == false) { + while ( pad.check_event(Gamepad::START_PRESSED) == false) + { pad.leds_on(); //pad.tone(1000.0,0.5); wait(0.1); pad.leds_off(); - // pad.tone(1000.0,0.5); + // pad.tone(1000.0,0.5); wait(0.1); - if( pad.check_event(Gamepad::START_PRESSED)) { + if( pad.check_event(Gamepad::START_PRESSED)) + { main(); wait(1); - } + } } } @@ -402,48 +414,53 @@ sd.mount(); lcd.clear(); - //Open file - file = fopen("/sd/scoreFile.txt", "r"); - if(file ==NULL) + //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); + 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); lcd.printString(" START - reset ",0,4); lcd.printString(" BACK - menu ",0,5); - lcd.refresh(); - sd.unmount(); + lcd.refresh(); + sd.unmount(); - while(1) - { + 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 + 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"); + remove("/sd/scoreFile.txt"); } - sd.unmount(); - displayHighScore(); + sd.unmount(); + displayHighScore(); } + //Back to menu - if( pad.check_event(Gamepad::BACK_PRESSED)) { - menu(); + if( pad.check_event(Gamepad::BACK_PRESSED)) + { + menu(); } sleep(); @@ -458,8 +475,8 @@ //Function to display the Instructions for the game void Instructions() { - bool i = true; - lcd.clear(); + bool i = true; + lcd.clear(); lcd.printString("INSTURCTIONS: ",0,0); lcd.printString("Collect the ",0,2); lcd.printString("gems and dodge ",0,3); @@ -467,13 +484,15 @@ lcd.printString("to get points ",0,5); lcd.refresh(); - while(i == true){ + while(i == true) + { - if( pad.check_event(Gamepad::BACK_PRESSED) ) { + if( pad.check_event(Gamepad::BACK_PRESSED) ) + { //pad.tone(1000.0,0.5); - i = false; - menu(); - } + i = false; + menu(); + } } - } \ No newline at end of file +} \ No newline at end of file