Meteor defense project

Dependencies:   N5110 mbed

Committer:
jasper0712
Date:
Mon Apr 24 23:30:54 2017 +0000
Revision:
38:c66a69c1a6de
Parent:
37:45da88e36d1d
Child:
39:f92b93efbc72
new way of handling structs at Weapons. still developing cannon. project almost done

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