Meteor defense project

Dependencies:   N5110 mbed

Committer:
jasper0712
Date:
Sun Apr 30 22:06:22 2017 +0000
Revision:
42:152e84494ad9
Parent:
41:1b396334256b
Child:
44:aa086363d09f
fixed a cannon bug

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 41:1b396334256b 76 game.update_MainToRetry(); //save the game state at the start of each wave
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 41:1b396334256b 79 if (pad.check_event(Gamepad::START_PRESSED)) {
jasper0712 41:1b396334256b 80 //printf("start double click prevented");
jasper0712 41:1b396334256b 81 }
jasper0712 37:45da88e36d1d 82 }
jasper0712 37:45da88e36d1d 83 void gameUpgrades() {
jasper0712 37:45da88e36d1d 84 waveCompleteTicker.detach();
jasper0712 42:152e84494ad9 85 lcd.printString("Wave complete",1,2);
jasper0712 42:152e84494ad9 86 lcd.printString("Press start to",1,4);
jasper0712 42:152e84494ad9 87 lcd.printString("upgrade",30,5);
jasper0712 37:45da88e36d1d 88 render();
jasper0712 37:45da88e36d1d 89 game.weapUpgrade(pad, lcd);
jasper0712 37:45da88e36d1d 90
jasper0712 37:45da88e36d1d 91 if (game.doneUpgrade_flag == 1) {
jasper0712 37:45da88e36d1d 92 waveComplete_flag = 0; //get out of while loop
jasper0712 37:45da88e36d1d 93 setTicker = 1; //continue the game
jasper0712 37:45da88e36d1d 94 game.doneUpgrade_flag = 0;
jasper0712 40:3a0c66a0e10e 95 //printf("i got out");
jasper0712 37:45da88e36d1d 96 }
jasper0712 37:45da88e36d1d 97 }
jasper0712 28:450ab72fabdc 98 void waveComplete_isr() {
jasper0712 28:450ab72fabdc 99 waveComplete_flag = 1;
jasper0712 28:450ab72fabdc 100 waveNumber += 1;
jasper0712 32:580d74825810 101 }
jasper0712 32:580d74825810 102 void gameMenu() {
jasper0712 41:1b396334256b 103 waveNumber = 0;
jasper0712 41:1b396334256b 104 game.reset_Game();
jasper0712 32:580d74825810 105 while ( pad.check_event(Gamepad::START_PRESSED) == false) {
jasper0712 32:580d74825810 106 game.startingMenu(pad, lcd);
jasper0712 32:580d74825810 107 lcd.refresh();
jasper0712 32:580d74825810 108 wait(0.2);
jasper0712 32:580d74825810 109 }
jasper0712 32:580d74825810 110 lcd.clear();
jasper0712 0:5fbc0fbc9d30 111 }