Stick_Runner
Dependencies: FXOS8700CQ Gamepad N5110 SDFileSystem mbed
Diff: main.cpp
- Revision:
- 1:db9ff66f67c8
- Parent:
- 0:12cfe63faa6a
- Child:
- 2:98a41609c827
diff -r 12cfe63faa6a -r db9ff66f67c8 main.cpp --- a/main.cpp Fri Apr 28 09:00:21 2017 +0000 +++ b/main.cpp Fri Apr 28 16:30:47 2017 +0000 @@ -1,4 +1,6 @@ - ///////// pre-processor directives //////// +/***************************************************** + Libraries and modules used * +******************************************************/ #include "mbed.h" #include "Gamepad.h" #include "N5110.h" @@ -7,26 +9,25 @@ #include "Gems.h" #include <iostream> -#define OBS 8 -#define GEMS 4 +#define No_OBS 8 +#define No_GEMS 4 -int i,j; +int i,j,counter; -/////////////// structs ///////////////// +//Structs struct UserInput { Direction d; float mag; }; -/////////////// objects /////////////// +/* Class Objects */ N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11); Gamepad pad; Character c; -Obstacles obstacle[OBS]; -Gems gems[GEMS]; +Obstacles obstacle[No_OBS]; +Gems gems[No_GEMS],g; - -///////////// prototypes /////////////// +/* Function Prototypes */ void init(); void update_game(UserInput input); void render(); @@ -34,73 +35,79 @@ void menu(); void over(); void Instructions(); -///////////// functions //////////////// + + +/* Functions */ + int main() { - int fps = 8; // frames per second + int fps = 8; +/* Intialization */ init(); + +/* Drawing the intial frame */ welcome(); wait(1.0f/fps); - - render(); // draw initial frame + + 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) { - c.update(pad.get_direction(),pad.get_mag()); + c.updateCharacter(pad.get_direction(),pad.get_mag()); if(pad.check_event(Gamepad::BACK_PRESSED)) { lcd.clear(); lcd.refresh(); - //lcd.printString(" Game Paused! ",0,1); - //lcd.printString("Press Start to continue ",0,4); - //lcd.refresh(); - // welcome(); - menu(); + menu(); } - for(i=0;i<OBS;i++) + for(i=0;i<No_OBS;i++) { - obstacle[i].Obstacles_Status(obstacle[i].get_pos()); + obstacle[i].obstacleStatus(obstacle[i].getObstaclePos()); - if(!obstacle[i].get_status()) + if(obstacle[i].getObstacleStatus() == false) { obstacle[i].init(); } - c.Character_Status(obstacle[i].get_pos()); + c.characterStatus(obstacle[i].getObstaclePos()); + obstacle[i].obstacleStatus(c.getCharacterPos()); + } - for(j=0;j<GEMS;j++) + for(j=0;j<No_GEMS;j++) { - gems[j].Gems_Status(gems[i].get_pos()); + gems[j].gemStatus(c.getCharacterPos()); - if(!gems[j].get_status()) + if(gems[j].getGemStatus() == false) { gems[j].init(); } - gems[j].Gems_Score(gems[j].get_pos()); + + } i =0; - for(i=0;i<OBS;i++) - { - obstacle[i].update(); - } + for(i=0;i<No_OBS;i++) + { + obstacle[i].updateObstacle(); + } j =0; - for(j=0;j<GEMS;j++) - { - gems[j].update(); - } + for(j=0;j<No_GEMS;j++) + { + gems[j].updateGems(); + } render(); @@ -115,14 +122,15 @@ pad.init(); c.init(); - for(i=0;i<OBS;i++) - { - obstacle[i].init(); - } - for(j=0;j<GEMS;j++) - { - gems[j].init(); - } + for(i=0;i<No_OBS;i++) + { + obstacle[i].init(); + } + + for(j=0;j<No_GEMS;j++) + { + gems[j].init(); + } @@ -132,47 +140,61 @@ { // clear screen, re-draw and refresh lcd.clear(); - if(!c.get_status()) - { - over(); - } - if(c.get_status()) - { - c.draw(lcd); - } + + if(c.getCharacterStatus()) + { + c.draw(lcd); + } //obstacle[i].draw(lcd); - for(i=0;i<OBS;i++) - { - if(obstacle[i].get_status()) - { - obstacle[i].draw(lcd); - } - - } + for(i=0;i<No_OBS;i++) + { + if(obstacle[i].getObstacleStatus()) + { + obstacle[i].draw(lcd); + } + } - for(j=0;j<GEMS;j++) - { - if(gems[j].get_status()) - { - gems[j].draw(lcd); - } + + for(j=0;j<No_GEMS;j++) + { + if(gems[j].getGemStatus()) + { + gems[j].draw(lcd); + + } + + else{ + counter++; + } - } + } + + if(c.getCharacterStatus() == false) + { + over(); + } + + lcd.refresh(); + } + 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); + // pad.tone(1500.0,0.5); + //pad.tone(1500.0,0.5); // wait flashing LEDs until start button is pressed - while ( pad.check_event(Gamepad::START_PRESSED) == false) { + while ( pad.check_event(Gamepad::START_PRESSED) == false) + { pad.leds_on(); wait(0.1); pad.leds_off(); @@ -184,8 +206,15 @@ void over() { lcd.clear(); + + char buffer[50]; + + sprintf (buffer, "%d",counter); + + lcd.printString(buffer,0,3); lcd.printString(" GAME ",0,1); lcd.printString(" OVER!!!!! ",0,2); + lcd.printString(" PRESS START ",0,4); lcd.refresh(); @@ -193,10 +222,10 @@ // 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); + //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)) { main(); @@ -220,17 +249,18 @@ // wait flashing LEDs until start button is pressed if( pad.check_event(Gamepad::A_PRESSED) ) { - pad.tone(1000.0,0.5); + //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); + // pad.tone(1000.0,0.5); i = false; lcd.refresh(); @@ -238,7 +268,7 @@ } else if( pad.check_event(Gamepad::X_PRESSED) ) { - pad.tone(1000.0,0.5); + // pad.tone(1000.0,0.5); Instructions(); wait(100.0f/fps); i = false; @@ -261,7 +291,7 @@ while(i == true){ if( pad.check_event(Gamepad::BACK_PRESSED) ) { - pad.tone(1000.0,0.5); + //pad.tone(1000.0,0.5); i = false; menu(); }