Stick_Runner
Dependencies: FXOS8700CQ Gamepad N5110 SDFileSystem mbed
Diff: main.cpp
- Revision:
- 0:12cfe63faa6a
- Child:
- 1:db9ff66f67c8
diff -r 000000000000 -r 12cfe63faa6a main.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Fri Apr 28 09:00:21 2017 +0000 @@ -0,0 +1,270 @@ + ///////// pre-processor directives //////// +#include "mbed.h" +#include "Gamepad.h" +#include "N5110.h" +#include "Character.h" +#include "Obstacles.h" +#include "Gems.h" +#include <iostream> + +#define OBS 8 +#define GEMS 4 + +int i,j; + +/////////////// structs ///////////////// +struct UserInput { + Direction d; + float mag; +}; +/////////////// objects /////////////// +N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11); +Gamepad pad; +Character c; +Obstacles obstacle[OBS]; +Gems gems[GEMS]; + + + +///////////// prototypes /////////////// +void init(); +void update_game(UserInput input); +void render(); +void welcome(); +void menu(); +void over(); +void Instructions(); +///////////// functions //////////////// +int main() +{ + int fps = 8; // frames per second + + init(); + welcome(); + wait(1.0f/fps); + + + + render(); // draw initial frame + wait(1.0f/fps); + + // game loop - read input, update the game state and render the display + while (1) { + + + + c.update(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(); + } + + for(i=0;i<OBS;i++) + { + obstacle[i].Obstacles_Status(obstacle[i].get_pos()); + + if(!obstacle[i].get_status()) + { + obstacle[i].init(); + } + c.Character_Status(obstacle[i].get_pos()); + } + + + for(j=0;j<GEMS;j++) + { + gems[j].Gems_Status(gems[i].get_pos()); + + if(!gems[j].get_status()) + { + gems[j].init(); + } + gems[j].Gems_Score(gems[j].get_pos()); + } + i =0; + + for(i=0;i<OBS;i++) + { + obstacle[i].update(); + } + + + j =0; + + for(j=0;j<GEMS;j++) + { + gems[j].update(); + } + + render(); + + wait(1.0f/fps); + } +} + +void init() +{ + // need to initialise LCD and Gamepad + lcd.init(); + pad.init(); + c.init(); + + for(i=0;i<OBS;i++) + { + obstacle[i].init(); + } + for(j=0;j<GEMS;j++) + { + gems[j].init(); + } + + + +} + +void render() +{ + // clear screen, re-draw and refresh + lcd.clear(); + if(!c.get_status()) + { + over(); + } + if(c.get_status()) + { + c.draw(lcd); + } + //obstacle[i].draw(lcd); + + for(i=0;i<OBS;i++) + { + if(obstacle[i].get_status()) + { + obstacle[i].draw(lcd); + } + + } + + for(j=0;j<GEMS;j++) + { + if(gems[j].get_status()) + { + gems[j].draw(lcd); + } + + } + 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); + + + // wait flashing LEDs until start button is pressed + while ( pad.check_event(Gamepad::START_PRESSED) == false) { + pad.leds_on(); + wait(0.1); + pad.leds_off(); + wait(0.1); + + } + menu(); +} + +void over() { + lcd.clear(); + lcd.printString(" GAME ",0,1); + lcd.printString(" OVER!!!!! ",0,2); + lcd.printString(" PRESS START ",0,4); + + lcd.refresh(); + + // 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); + 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); + } + } + +} + +void menu() { + int fps =8; + bool i = true; + while(i == true) + { + lcd.printString(" Menu ",0,1); + lcd.printString("A)New Game ",0,2); + lcd.printString("B)Continue ",0,3); + lcd.printString("X)Instructions ",0,4); + + 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(); + + //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(100.0f/fps); + i = false; + } + + + } +} + +void Instructions() +{ + bool i = true; + lcd.clear(); + lcd.printString("INSTURCTIONS: ",0,1); + lcd.printString("Collect the ",0,2); + lcd.printString("gems to get ",0,3); + lcd.printString("points ",0,4); + lcd.refresh(); + + while(i == true){ + + if( pad.check_event(Gamepad::BACK_PRESSED) ) { + pad.tone(1000.0,0.5); + i = false; + menu(); + } + } + + } \ No newline at end of file