Meteor defense project

Dependencies:   N5110 mbed

Committer:
jasper0712
Date:
Sat Apr 29 19:27:20 2017 +0000
Revision:
40:3a0c66a0e10e
Parent:
39:f92b93efbc72
Child:
41:1b396334256b
done commenting. MAYBE. implemented RETRY function in the game. need further testing

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jasper0712 0:5fbc0fbc9d30 1 #include "mbed.h"
jasper0712 0:5fbc0fbc9d30 2 #include "N5110.h"
jasper0712 0:5fbc0fbc9d30 3 #include "Gamepad.h"
jasper0712 4:4145abd09075 4 #include "GameEngine.h"
jasper0712 0:5fbc0fbc9d30 5
jasper0712 0:5fbc0fbc9d30 6 #define PI 3.14159265
jasper0712 0:5fbc0fbc9d30 7 //Y rows and X columns
jasper0712 1:f5cda0674f24 8 #define Rows 48
jasper0712 1:f5cda0674f24 9 #define Cols 84
jasper0712 0:5fbc0fbc9d30 10
jasper0712 0:5fbc0fbc9d30 11 N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11);
jasper0712 0:5fbc0fbc9d30 12 Gamepad pad;
jasper0712 4:4145abd09075 13 GameEngine game;
jasper0712 5:c74bbdda06f4 14
jasper0712 29:6632dd9c48d8 15 int gamefps = 15;
jasper0712 28:450ab72fabdc 16
jasper0712 28:450ab72fabdc 17 Ticker waveCompleteTicker;
jasper0712 28:450ab72fabdc 18 int setTicker;
jasper0712 28:450ab72fabdc 19 int waveComplete_flag;
jasper0712 28:450ab72fabdc 20 int waveNumber;
jasper0712 32:580d74825810 21 int menu;
jasper0712 28:450ab72fabdc 22
jasper0712 32:580d74825810 23 char wave[10];
jasper0712 28:450ab72fabdc 24
jasper0712 0:5fbc0fbc9d30 25 void init();
jasper0712 0:5fbc0fbc9d30 26 void render();
jasper0712 32:580d74825810 27 void waveComplete_isr();
jasper0712 32:580d74825810 28 void gameMenu();
jasper0712 37:45da88e36d1d 29 void startGame();
jasper0712 37:45da88e36d1d 30 void gameUpgrades();
jasper0712 28:450ab72fabdc 31
jasper0712 0:5fbc0fbc9d30 32 int main()
jasper0712 0:5fbc0fbc9d30 33 {
jasper0712 32:580d74825810 34 init();
jasper0712 40:3a0c66a0e10e 35 game.gameMenu_flag = 1;
jasper0712 0:5fbc0fbc9d30 36 while (true) {
jasper0712 40:3a0c66a0e10e 37 if (game.gameMenu_flag == 1) {
jasper0712 40:3a0c66a0e10e 38 gameMenu();
jasper0712 40:3a0c66a0e10e 39 setTicker = 1; //set the ticker.
jasper0712 40:3a0c66a0e10e 40 game.gameMenu_flag = 0; //remove flag
jasper0712 40:3a0c66a0e10e 41 }
jasper0712 40:3a0c66a0e10e 42 if (setTicker == 1 || game.retry_flag == 1) {
jasper0712 40:3a0c66a0e10e 43 game.retry_flag = 0; //clear flag
jasper0712 37:45da88e36d1d 44 startGame();
jasper0712 28:450ab72fabdc 45 }
jasper0712 29:6632dd9c48d8 46
jasper0712 37:45da88e36d1d 47 //this is the game
jasper0712 37:45da88e36d1d 48 game.update(pad, lcd);
jasper0712 29:6632dd9c48d8 49
jasper0712 40:3a0c66a0e10e 50 while (waveComplete_flag == 1) { //after every wave, go to upgrades
jasper0712 37:45da88e36d1d 51 gameUpgrades();
jasper0712 28:450ab72fabdc 52 }
jasper0712 0:5fbc0fbc9d30 53 render();
jasper0712 0:5fbc0fbc9d30 54 }
jasper0712 0:5fbc0fbc9d30 55 }
jasper0712 0:5fbc0fbc9d30 56
jasper0712 28:450ab72fabdc 57 void init() {
jasper0712 0:5fbc0fbc9d30 58 // need to initialise LCD and Gamepad
jasper0712 0:5fbc0fbc9d30 59 lcd.init();
jasper0712 0:5fbc0fbc9d30 60 pad.init();
jasper0712 32:580d74825810 61 game.init(waveNumber);
jasper0712 0:5fbc0fbc9d30 62 // initialise the game
jasper0712 0:5fbc0fbc9d30 63 }
jasper0712 28:450ab72fabdc 64 void render() {
jasper0712 0:5fbc0fbc9d30 65 // clear screen, re-draw and refresh
jasper0712 0:5fbc0fbc9d30 66 lcd.refresh();
jasper0712 0:5fbc0fbc9d30 67 wait(1.0/gamefps);
jasper0712 0:5fbc0fbc9d30 68 lcd.clear();
jasper0712 28:450ab72fabdc 69 }
jasper0712 37:45da88e36d1d 70 void startGame() {
jasper0712 37:45da88e36d1d 71 sprintf(wave, "wave %d", waveNumber + 1); //+1 because initially waveNumber = 0
jasper0712 37:45da88e36d1d 72 lcd.printString(wave,10,3);
jasper0712 37:45da88e36d1d 73 render();
jasper0712 37:45da88e36d1d 74 wait(2.0);
jasper0712 37:45da88e36d1d 75 game.init(waveNumber);
jasper0712 40:3a0c66a0e10e 76 game.update_RetryToMain(); //bring back the previous game state
jasper0712 39:f92b93efbc72 77 waveCompleteTicker.attach(&waveComplete_isr,60); //60 seconds for a wave.
jasper0712 37:45da88e36d1d 78 setTicker = 0; //clear set ticker flag
jasper0712 37:45da88e36d1d 79 }
jasper0712 37:45da88e36d1d 80 void gameUpgrades() {
jasper0712 37:45da88e36d1d 81 waveCompleteTicker.detach();
jasper0712 37:45da88e36d1d 82 lcd.printString("Wave complete",1,3);
jasper0712 37:45da88e36d1d 83 lcd.printString("Start to Upgrade",1,5);
jasper0712 37:45da88e36d1d 84 render();
jasper0712 37:45da88e36d1d 85 game.weapUpgrade(pad, lcd);
jasper0712 37:45da88e36d1d 86
jasper0712 37:45da88e36d1d 87 if (game.doneUpgrade_flag == 1) {
jasper0712 37:45da88e36d1d 88 waveComplete_flag = 0; //get out of while loop
jasper0712 37:45da88e36d1d 89 setTicker = 1; //continue the game
jasper0712 37:45da88e36d1d 90 game.doneUpgrade_flag = 0;
jasper0712 40:3a0c66a0e10e 91 //printf("i got out");
jasper0712 37:45da88e36d1d 92 }
jasper0712 37:45da88e36d1d 93 }
jasper0712 28:450ab72fabdc 94 void waveComplete_isr() {
jasper0712 28:450ab72fabdc 95 waveComplete_flag = 1;
jasper0712 28:450ab72fabdc 96 waveNumber += 1;
jasper0712 32:580d74825810 97 }
jasper0712 32:580d74825810 98 void gameMenu() {
jasper0712 32:580d74825810 99
jasper0712 32:580d74825810 100 while ( pad.check_event(Gamepad::START_PRESSED) == false) {
jasper0712 32:580d74825810 101 game.startingMenu(pad, lcd);
jasper0712 32:580d74825810 102 lcd.refresh();
jasper0712 32:580d74825810 103 wait(0.2);
jasper0712 32:580d74825810 104 }
jasper0712 32:580d74825810 105 lcd.clear();
jasper0712 0:5fbc0fbc9d30 106 }