Stick_Runner
Dependencies: FXOS8700CQ Gamepad N5110 SDFileSystem mbed
Fork of Stick_Runner by
main.cpp
- Committer:
- el15ss
- Date:
- 2017-05-04
- Revision:
- 5:1bf7c83f86cc
- Parent:
- 4:2fdafb53eac2
- Child:
- 6:bf601a65cb27
File content as of revision 5:1bf7c83f86cc:
/***************************************************** Libraries and modules used * ******************************************************/ #include "mbed.h" #include "Gamepad.h" #include "N5110.h" #include "Character.h" #include "Obstacles.h" #include "Gems.h" #include "SDFileSystem.h" #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 struct UserInput { Direction d; float mag; }; /* Class Objects */ N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11); Gamepad pad; Character c; Obstacles obstacle[No_OBS]; Gems gems[No_GEMS]; SDFileSystem sd(PTE3,PTE1,PTE2,PTE4,"sd"); FILE *file; /* Function Prototypes */ void init(); void update_game(UserInput input); void render(); void welcome(); void menu(); void over(); void Instructions(); void stickRunner(); void displayHighScore(); /* Functions */ int main() { /* Intialization */ init(); /* Drawing the initial frame */ welcome(); } 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() { 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(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(); } //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(); } //To read the game instructions else if( pad.check_event(Gamepad::X_PRESSED) ) { // pad.tone(1000.0,0.5); Instructions(); } //To see the game high score else if( pad.check_event(Gamepad::Y_PRESSED) ) { //pad.tone(1000.0,0.5); displayHighScore(); } sleep(); } } //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 */ 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(); lcd.refresh(); 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()); } //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) { gems[j].init(); } } //To make the obstacles and gems move along the screen i =0; for(i=0;i<No_OBS;i++) { obstacle[i].updateObstacle(); } j =0; for(j=0;j<No_GEMS;j++) { gems[j].updateGems(); } render(); wait(1.0f/fps); } } //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); } if(c.getCharacterStatus() == false) { over(); } //Draws the obstacles if the status returned is true for(i=0;i<No_OBS;i++) { if(obstacle[i].getObstacleStatus()) { obstacle[i].draw(lcd); } } //Draws the gems if the status returned is true for(j=0;j<No_GEMS;j++) { if(gems[j].getGemStatus()) { gems[j].draw(lcd); } } lcd.refresh(); } //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.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"); fprintf(file,"%d",counter); fclose(file); 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); if(counter>highScore) { file = fopen("/sd/scoreFile.txt", "w"); fprintf(file,"%d",counter); fclose(file); lcd.printString("HIGH SCORE",0,3); } }*/ lcd.printString(" PRESS START ",0,5); 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); wait(0.1); pad.leds_off(); // pad.tone(1000.0,0.5); wait(0.1); if( pad.check_event(Gamepad::START_PRESSED)) { main(); wait(1); } } } //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 { //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); lcd.printString(" START - reset ",0,4); lcd.printString(" BACK - menu ",0,5); lcd.refresh(); sd.unmount(); 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"); } sd.unmount(); displayHighScore(); } //Back to menu if( pad.check_event(Gamepad::BACK_PRESSED)) { menu(); } sleep(); } } //Function to display the Instructions for the game void Instructions() { bool i = true; lcd.clear(); lcd.printString("INSTURCTIONS: ",0,0); lcd.printString("Collect the ",0,2); lcd.printString("gems and dodge ",0,3); lcd.printString("the obstacles ",0,4); lcd.printString("to get points ",0,5); lcd.refresh(); while(i == true) { if( pad.check_event(Gamepad::BACK_PRESSED) ) { //pad.tone(1000.0,0.5); i = false; menu(); } } }