Meteor defense project

Dependencies:   N5110 mbed

Committer:
jasper0712
Date:
Mon Apr 24 11:11:39 2017 +0000
Revision:
37:45da88e36d1d
Parent:
33:61c69c46a941
Child:
38:c66a69c1a6de
linked weapon to gui. continue on cannon now

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 4:4145abd09075 15
jasper0712 0:5fbc0fbc9d30 16
jasper0712 28:450ab72fabdc 17
jasper0712 29:6632dd9c48d8 18 int gamefps = 15;
jasper0712 28:450ab72fabdc 19
jasper0712 28:450ab72fabdc 20 Ticker waveCompleteTicker;
jasper0712 28:450ab72fabdc 21 int setTicker;
jasper0712 28:450ab72fabdc 22 int waveComplete_flag;
jasper0712 28:450ab72fabdc 23 int waveNumber;
jasper0712 32:580d74825810 24 int menu;
jasper0712 28:450ab72fabdc 25
jasper0712 32:580d74825810 26 char wave[10];
jasper0712 28:450ab72fabdc 27
jasper0712 0:5fbc0fbc9d30 28 void init();
jasper0712 0:5fbc0fbc9d30 29 void render();
jasper0712 32:580d74825810 30 void waveComplete_isr();
jasper0712 32:580d74825810 31 void gameMenu();
jasper0712 37:45da88e36d1d 32 void startGame();
jasper0712 37:45da88e36d1d 33 void gameUpgrades();
jasper0712 28:450ab72fabdc 34
jasper0712 0:5fbc0fbc9d30 35 int main()
jasper0712 0:5fbc0fbc9d30 36 {
jasper0712 32:580d74825810 37 init();
jasper0712 32:580d74825810 38 gameMenu();
jasper0712 28:450ab72fabdc 39 setTicker = 1; //set the ticker.
jasper0712 0:5fbc0fbc9d30 40 while (true) {
jasper0712 37:45da88e36d1d 41 if (setTicker == 1) {
jasper0712 37:45da88e36d1d 42 startGame();
jasper0712 28:450ab72fabdc 43 }
jasper0712 29:6632dd9c48d8 44
jasper0712 37:45da88e36d1d 45 //this is the game
jasper0712 37:45da88e36d1d 46 game.update(pad, lcd);
jasper0712 29:6632dd9c48d8 47
jasper0712 32:580d74825810 48 while(waveComplete_flag == 1) { //after every wave, go to upgrades
jasper0712 37:45da88e36d1d 49 gameUpgrades();
jasper0712 28:450ab72fabdc 50 }
jasper0712 0:5fbc0fbc9d30 51 render();
jasper0712 0:5fbc0fbc9d30 52 }
jasper0712 0:5fbc0fbc9d30 53 }
jasper0712 0:5fbc0fbc9d30 54
jasper0712 28:450ab72fabdc 55 void init() {
jasper0712 0:5fbc0fbc9d30 56 // need to initialise LCD and Gamepad
jasper0712 0:5fbc0fbc9d30 57 lcd.init();
jasper0712 0:5fbc0fbc9d30 58 pad.init();
jasper0712 32:580d74825810 59 game.init(waveNumber);
jasper0712 0:5fbc0fbc9d30 60 // initialise the game
jasper0712 0:5fbc0fbc9d30 61 }
jasper0712 28:450ab72fabdc 62 void render() {
jasper0712 0:5fbc0fbc9d30 63 // clear screen, re-draw and refresh
jasper0712 0:5fbc0fbc9d30 64 lcd.refresh();
jasper0712 0:5fbc0fbc9d30 65 wait(1.0/gamefps);
jasper0712 0:5fbc0fbc9d30 66 lcd.clear();
jasper0712 28:450ab72fabdc 67 }
jasper0712 37:45da88e36d1d 68 void startGame() {
jasper0712 37:45da88e36d1d 69 sprintf(wave, "wave %d", waveNumber + 1); //+1 because initially waveNumber = 0
jasper0712 37:45da88e36d1d 70 lcd.printString(wave,10,3);
jasper0712 37:45da88e36d1d 71 render();
jasper0712 37:45da88e36d1d 72 wait(2.0);
jasper0712 37:45da88e36d1d 73 game.init(waveNumber);
jasper0712 37:45da88e36d1d 74 waveCompleteTicker.attach(&waveComplete_isr,1); //60 seconds for a wave.
jasper0712 37:45da88e36d1d 75 setTicker = 0; //clear set ticker flag
jasper0712 37:45da88e36d1d 76 }
jasper0712 37:45da88e36d1d 77 void gameUpgrades() {
jasper0712 37:45da88e36d1d 78 waveCompleteTicker.detach();
jasper0712 37:45da88e36d1d 79 lcd.printString("Wave complete",1,3);
jasper0712 37:45da88e36d1d 80 lcd.printString("Start to Upgrade",1,5);
jasper0712 37:45da88e36d1d 81 render();
jasper0712 37:45da88e36d1d 82 game.weapUpgrade(pad, lcd);
jasper0712 37:45da88e36d1d 83
jasper0712 37:45da88e36d1d 84 if (game.doneUpgrade_flag == 1) {
jasper0712 37:45da88e36d1d 85 waveComplete_flag = 0; //get out of while loop
jasper0712 37:45da88e36d1d 86 setTicker = 1; //continue the game
jasper0712 37:45da88e36d1d 87 game.doneUpgrade_flag = 0;
jasper0712 37:45da88e36d1d 88 printf("i got out");
jasper0712 37:45da88e36d1d 89 }
jasper0712 37:45da88e36d1d 90 }
jasper0712 28:450ab72fabdc 91 void waveComplete_isr() {
jasper0712 28:450ab72fabdc 92 waveComplete_flag = 1;
jasper0712 28:450ab72fabdc 93 waveNumber += 1;
jasper0712 32:580d74825810 94 }
jasper0712 32:580d74825810 95 void gameMenu() {
jasper0712 32:580d74825810 96
jasper0712 32:580d74825810 97 while ( pad.check_event(Gamepad::START_PRESSED) == false) {
jasper0712 32:580d74825810 98 game.startingMenu(pad, lcd);
jasper0712 32:580d74825810 99 lcd.refresh();
jasper0712 32:580d74825810 100 wait(0.2);
jasper0712 32:580d74825810 101 }
jasper0712 32:580d74825810 102 lcd.clear();
jasper0712 0:5fbc0fbc9d30 103 }