![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Stick_Runner
Dependencies: FXOS8700CQ Gamepad N5110 SDFileSystem mbed
Diff: main.cpp
- Revision:
- 2:98a41609c827
- Parent:
- 1:db9ff66f67c8
- Child:
- 3:0c690f1c04d8
--- a/main.cpp Fri Apr 28 16:30:47 2017 +0000 +++ b/main.cpp Tue May 02 18:42:45 2017 +0000 @@ -7,12 +7,14 @@ #include "Character.h" #include "Obstacles.h" #include "Gems.h" -#include <iostream> +#include "SDFileSystem.h" + #define No_OBS 8 #define No_GEMS 4 -int i,j,counter; +int i,j,counter,highScore; + char score[50]; //Structs struct UserInput { @@ -26,6 +28,9 @@ Obstacles obstacle[No_OBS]; Gems gems[No_GEMS],g; +SDFileSystem sd(PTE3,PTE1,PTE2,PTE4,"sd"); +FILE *file; + /* Function Prototypes */ void init(); @@ -59,7 +64,7 @@ // game loop - read input, update the game state and render the display while (1) { - + counter++; c.updateCharacter(pad.get_direction(),pad.get_mag()); if(pad.check_event(Gamepad::BACK_PRESSED)) @@ -78,7 +83,7 @@ obstacle[i].init(); } c.characterStatus(obstacle[i].getObstaclePos()); - obstacle[i].obstacleStatus(c.getCharacterPos()); + // obstacle[i].obstacleStatus(c.getCharacterPos()); } @@ -90,10 +95,13 @@ 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++) @@ -107,6 +115,7 @@ for(j=0;j<No_GEMS;j++) { gems[j].updateGems(); + } render(); @@ -117,7 +126,7 @@ void init() { - // need to initialise LCD and Gamepad + lcd.init(); pad.init(); c.init(); @@ -138,13 +147,20 @@ void render() { - // clear screen, re-draw and refresh + lcd.clear(); if(c.getCharacterStatus()) { c.draw(lcd); } + + if(c.getCharacterStatus() == false) + { + over(); + } + + //obstacle[i].draw(lcd); for(i=0;i<No_OBS;i++) @@ -164,16 +180,14 @@ } - else{ + /*else{ counter++; - } + }*/ } + - if(c.getCharacterStatus() == false) - { - over(); - } + lcd.refresh(); @@ -192,7 +206,7 @@ //pad.tone(1500.0,0.5); - // wait flashing LEDs until start button is pressed + while ( pad.check_event(Gamepad::START_PRESSED) == false) { pad.leds_on(); @@ -205,21 +219,49 @@ } void over() { - lcd.clear(); + + pad.init(); + // lcd.clear(); + sd.mount(); + - char buffer[50]; + sprintf (score, " Score : %d",counter); + + lcd.printString(score,0,2); + lcd.printString("GAME OVER!! ",0,0); + //lcd.printString(" ",0,1); - sprintf (buffer, "%d",counter); - - lcd.printString(buffer,0,3); - lcd.printString(" GAME ",0,1); - lcd.printString(" OVER!!!!! ",0,2); + file = fopen("/sd/scoreFile.txt", "r"); + if(file ==NULL) + { + file = fopen("/sd/scoreFile.txt", "w"); + fprintf(file,"%d",counter); + fclose(file); + lcd.printString("HIGH SCORE",0,3); + + } + 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,4); lcd.refresh(); + sd.unmount(); - // wait flashing LEDs until start button is pressed + while ( pad.check_event(Gamepad::START_PRESSED) == false) { pad.leds_on(); //pad.tone(1000.0,0.5); @@ -235,15 +277,70 @@ } +void displayHighScore() +{ + sd.mount(); + lcd.clear(); + + + file = fopen("/sd/scoreFile.txt", "r"); + if(file ==NULL) + { + highScore = 0; + + } + else{ + fscanf(file,"%d", &highScore); + fclose(file); + + } + + 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) + { + if( pad.check_event(Gamepad::START_PRESSED)) { + sd.mount(); + file = fopen("/sd/scoreFile.txt", "r"); + if(!file ==NULL) + { + fclose(file); + remove("/sd/scoreFile.txt"); + + } + sd.unmount(); + displayHighScore(); + + } + + if( pad.check_event(Gamepad::BACK_PRESSED)) { + menu(); + } + + sleep(); + + } + + +} + void menu() { int fps =8; bool i = true; while(i == true) { - lcd.printString(" Menu ",0,1); + 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(); @@ -270,7 +367,14 @@ else if( pad.check_event(Gamepad::X_PRESSED) ) { // pad.tone(1000.0,0.5); Instructions(); - wait(100.0f/fps); + 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; } @@ -282,10 +386,11 @@ { bool i = true; lcd.clear(); - lcd.printString("INSTURCTIONS: ",0,1); + lcd.printString("INSTURCTIONS: ",0,0); lcd.printString("Collect the ",0,2); - lcd.printString("gems to get ",0,3); - lcd.printString("points ",0,4); + 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){