JianWei Lee
/
project_game
Meteor defense project
main.cpp@33:61c69c46a941, 2017-04-21 (annotated)
- Committer:
- jasper0712
- Date:
- Fri Apr 21 14:12:28 2017 +0000
- Revision:
- 33:61c69c46a941
- Parent:
- 32:580d74825810
- Child:
- 37:45da88e36d1d
i think the game crash caused by bomb is solved. now i can put the spawn struct back
Who changed what in which revision?
User | Revision | Line number | New 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 | 28:450ab72fabdc | 32 | |
jasper0712 | 0:5fbc0fbc9d30 | 33 | int main() |
jasper0712 | 0:5fbc0fbc9d30 | 34 | { |
jasper0712 | 32:580d74825810 | 35 | init(); |
jasper0712 | 32:580d74825810 | 36 | gameMenu(); |
jasper0712 | 28:450ab72fabdc | 37 | setTicker = 1; //set the ticker. |
jasper0712 | 0:5fbc0fbc9d30 | 38 | while (true) { |
jasper0712 | 28:450ab72fabdc | 39 | if (setTicker == 1) { |
jasper0712 | 28:450ab72fabdc | 40 | sprintf(wave, "wave %d", waveNumber + 1); //+1 because initially waveNumber = 0 |
jasper0712 | 28:450ab72fabdc | 41 | lcd.printString(wave,10,3); |
jasper0712 | 28:450ab72fabdc | 42 | render(); |
jasper0712 | 28:450ab72fabdc | 43 | wait(2.0); |
jasper0712 | 32:580d74825810 | 44 | game.init(waveNumber); |
jasper0712 | 32:580d74825810 | 45 | waveCompleteTicker.attach(&waveComplete_isr,60); //60 seconds for a wave. |
jasper0712 | 28:450ab72fabdc | 46 | setTicker = 0; //clear set ticker flag |
jasper0712 | 28:450ab72fabdc | 47 | } |
jasper0712 | 29:6632dd9c48d8 | 48 | |
jasper0712 | 29:6632dd9c48d8 | 49 | game.update(pad, lcd); //this is the game |
jasper0712 | 29:6632dd9c48d8 | 50 | |
jasper0712 | 32:580d74825810 | 51 | while(waveComplete_flag == 1) { //after every wave, go to upgrades |
jasper0712 | 28:450ab72fabdc | 52 | waveCompleteTicker.detach(); |
jasper0712 | 33:61c69c46a941 | 53 | lcd.printString("Wave complete",1,3); |
jasper0712 | 28:450ab72fabdc | 54 | lcd.printString("Start to Upgrade",1,5); |
jasper0712 | 28:450ab72fabdc | 55 | render(); |
jasper0712 | 28:450ab72fabdc | 56 | game.weapUpgrade(pad, lcd); |
jasper0712 | 32:580d74825810 | 57 | if (game.doneUpgrade == 1) { |
jasper0712 | 28:450ab72fabdc | 58 | waveComplete_flag = 0; //get out of while loop |
jasper0712 | 28:450ab72fabdc | 59 | setTicker = 1; //continue the game |
jasper0712 | 32:580d74825810 | 60 | game.doneUpgrade = 0; |
jasper0712 | 31:1c0e47931e84 | 61 | printf("i got out"); |
jasper0712 | 28:450ab72fabdc | 62 | } |
jasper0712 | 28:450ab72fabdc | 63 | } |
jasper0712 | 0:5fbc0fbc9d30 | 64 | render(); |
jasper0712 | 0:5fbc0fbc9d30 | 65 | } |
jasper0712 | 0:5fbc0fbc9d30 | 66 | } |
jasper0712 | 0:5fbc0fbc9d30 | 67 | |
jasper0712 | 28:450ab72fabdc | 68 | void init() { |
jasper0712 | 0:5fbc0fbc9d30 | 69 | // need to initialise LCD and Gamepad |
jasper0712 | 0:5fbc0fbc9d30 | 70 | lcd.init(); |
jasper0712 | 0:5fbc0fbc9d30 | 71 | pad.init(); |
jasper0712 | 32:580d74825810 | 72 | game.init(waveNumber); |
jasper0712 | 0:5fbc0fbc9d30 | 73 | // initialise the game |
jasper0712 | 0:5fbc0fbc9d30 | 74 | } |
jasper0712 | 28:450ab72fabdc | 75 | void render() { |
jasper0712 | 0:5fbc0fbc9d30 | 76 | // clear screen, re-draw and refresh |
jasper0712 | 0:5fbc0fbc9d30 | 77 | lcd.refresh(); |
jasper0712 | 0:5fbc0fbc9d30 | 78 | wait(1.0/gamefps); |
jasper0712 | 0:5fbc0fbc9d30 | 79 | lcd.clear(); |
jasper0712 | 28:450ab72fabdc | 80 | } |
jasper0712 | 28:450ab72fabdc | 81 | void waveComplete_isr() { |
jasper0712 | 28:450ab72fabdc | 82 | waveComplete_flag = 1; |
jasper0712 | 28:450ab72fabdc | 83 | waveNumber += 1; |
jasper0712 | 32:580d74825810 | 84 | } |
jasper0712 | 32:580d74825810 | 85 | void gameMenu() { |
jasper0712 | 32:580d74825810 | 86 | |
jasper0712 | 32:580d74825810 | 87 | while ( pad.check_event(Gamepad::START_PRESSED) == false) { |
jasper0712 | 32:580d74825810 | 88 | game.startingMenu(pad, lcd); |
jasper0712 | 32:580d74825810 | 89 | lcd.refresh(); |
jasper0712 | 32:580d74825810 | 90 | wait(0.2); |
jasper0712 | 32:580d74825810 | 91 | } |
jasper0712 | 32:580d74825810 | 92 | lcd.clear(); |
jasper0712 | 0:5fbc0fbc9d30 | 93 | } |