JianWei Lee
/
project_game
Meteor defense project
main.cpp@44:aa086363d09f, 2017-05-03 (annotated)
- Committer:
- jasper0712
- Date:
- Wed May 03 21:00:43 2017 +0000
- Revision:
- 44:aa086363d09f
- Parent:
- 42:152e84494ad9
game is completed. able to submit now
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 | 44:aa086363d09f | 12 | AnalogIn brightness(PTB2); |
jasper0712 | 0:5fbc0fbc9d30 | 13 | Gamepad pad; |
jasper0712 | 4:4145abd09075 | 14 | GameEngine game; |
jasper0712 | 5:c74bbdda06f4 | 15 | |
jasper0712 | 29:6632dd9c48d8 | 16 | int gamefps = 15; |
jasper0712 | 28:450ab72fabdc | 17 | |
jasper0712 | 28:450ab72fabdc | 18 | Ticker waveCompleteTicker; |
jasper0712 | 44:aa086363d09f | 19 | int Start_The_Game_flag; |
jasper0712 | 28:450ab72fabdc | 20 | int waveComplete_flag; |
jasper0712 | 28:450ab72fabdc | 21 | int waveNumber; |
jasper0712 | 32:580d74825810 | 22 | int menu; |
jasper0712 | 28:450ab72fabdc | 23 | |
jasper0712 | 32:580d74825810 | 24 | char wave[10]; |
jasper0712 | 28:450ab72fabdc | 25 | |
jasper0712 | 0:5fbc0fbc9d30 | 26 | void init(); |
jasper0712 | 0:5fbc0fbc9d30 | 27 | void render(); |
jasper0712 | 32:580d74825810 | 28 | void waveComplete_isr(); |
jasper0712 | 32:580d74825810 | 29 | void gameMenu(); |
jasper0712 | 37:45da88e36d1d | 30 | void startGame(); |
jasper0712 | 37:45da88e36d1d | 31 | void gameUpgrades(); |
jasper0712 | 28:450ab72fabdc | 32 | |
jasper0712 | 0:5fbc0fbc9d30 | 33 | int main() |
jasper0712 | 0:5fbc0fbc9d30 | 34 | { |
jasper0712 | 32:580d74825810 | 35 | init(); |
jasper0712 | 40:3a0c66a0e10e | 36 | game.gameMenu_flag = 1; |
jasper0712 | 0:5fbc0fbc9d30 | 37 | while (true) { |
jasper0712 | 40:3a0c66a0e10e | 38 | if (game.gameMenu_flag == 1) { |
jasper0712 | 40:3a0c66a0e10e | 39 | gameMenu(); |
jasper0712 | 44:aa086363d09f | 40 | Start_The_Game_flag = 1; //set the startgame flag |
jasper0712 | 40:3a0c66a0e10e | 41 | game.gameMenu_flag = 0; //remove flag |
jasper0712 | 40:3a0c66a0e10e | 42 | } |
jasper0712 | 44:aa086363d09f | 43 | if (Start_The_Game_flag == 1 || game.retry_flag == 1) { |
jasper0712 | 40:3a0c66a0e10e | 44 | game.retry_flag = 0; //clear flag |
jasper0712 | 37:45da88e36d1d | 45 | startGame(); |
jasper0712 | 28:450ab72fabdc | 46 | } |
jasper0712 | 44:aa086363d09f | 47 | lcd.setBrightness(brightness); |
jasper0712 | 29:6632dd9c48d8 | 48 | |
jasper0712 | 37:45da88e36d1d | 49 | //this is the game |
jasper0712 | 37:45da88e36d1d | 50 | game.update(pad, lcd); |
jasper0712 | 29:6632dd9c48d8 | 51 | |
jasper0712 | 40:3a0c66a0e10e | 52 | while (waveComplete_flag == 1) { //after every wave, go to upgrades |
jasper0712 | 37:45da88e36d1d | 53 | gameUpgrades(); |
jasper0712 | 28:450ab72fabdc | 54 | } |
jasper0712 | 0:5fbc0fbc9d30 | 55 | render(); |
jasper0712 | 0:5fbc0fbc9d30 | 56 | } |
jasper0712 | 0:5fbc0fbc9d30 | 57 | } |
jasper0712 | 0:5fbc0fbc9d30 | 58 | |
jasper0712 | 28:450ab72fabdc | 59 | void init() { |
jasper0712 | 0:5fbc0fbc9d30 | 60 | // need to initialise LCD and Gamepad |
jasper0712 | 0:5fbc0fbc9d30 | 61 | lcd.init(); |
jasper0712 | 0:5fbc0fbc9d30 | 62 | pad.init(); |
jasper0712 | 32:580d74825810 | 63 | game.init(waveNumber); |
jasper0712 | 0:5fbc0fbc9d30 | 64 | // initialise the game |
jasper0712 | 0:5fbc0fbc9d30 | 65 | } |
jasper0712 | 28:450ab72fabdc | 66 | void render() { |
jasper0712 | 0:5fbc0fbc9d30 | 67 | // clear screen, re-draw and refresh |
jasper0712 | 0:5fbc0fbc9d30 | 68 | lcd.refresh(); |
jasper0712 | 0:5fbc0fbc9d30 | 69 | wait(1.0/gamefps); |
jasper0712 | 0:5fbc0fbc9d30 | 70 | lcd.clear(); |
jasper0712 | 28:450ab72fabdc | 71 | } |
jasper0712 | 37:45da88e36d1d | 72 | void startGame() { |
jasper0712 | 37:45da88e36d1d | 73 | sprintf(wave, "wave %d", waveNumber + 1); //+1 because initially waveNumber = 0 |
jasper0712 | 37:45da88e36d1d | 74 | lcd.printString(wave,10,3); |
jasper0712 | 37:45da88e36d1d | 75 | render(); |
jasper0712 | 37:45da88e36d1d | 76 | wait(2.0); |
jasper0712 | 37:45da88e36d1d | 77 | game.init(waveNumber); |
jasper0712 | 41:1b396334256b | 78 | game.update_MainToRetry(); //save the game state at the start of each wave |
jasper0712 | 39:f92b93efbc72 | 79 | waveCompleteTicker.attach(&waveComplete_isr,60); //60 seconds for a wave. |
jasper0712 | 44:aa086363d09f | 80 | Start_The_Game_flag = 0; //clear set ticker flag |
jasper0712 | 41:1b396334256b | 81 | if (pad.check_event(Gamepad::START_PRESSED)) { |
jasper0712 | 41:1b396334256b | 82 | //printf("start double click prevented"); |
jasper0712 | 41:1b396334256b | 83 | } |
jasper0712 | 37:45da88e36d1d | 84 | } |
jasper0712 | 37:45da88e36d1d | 85 | void gameUpgrades() { |
jasper0712 | 37:45da88e36d1d | 86 | waveCompleteTicker.detach(); |
jasper0712 | 42:152e84494ad9 | 87 | lcd.printString("Wave complete",1,2); |
jasper0712 | 42:152e84494ad9 | 88 | lcd.printString("Press start to",1,4); |
jasper0712 | 42:152e84494ad9 | 89 | lcd.printString("upgrade",30,5); |
jasper0712 | 37:45da88e36d1d | 90 | render(); |
jasper0712 | 37:45da88e36d1d | 91 | game.weapUpgrade(pad, lcd); |
jasper0712 | 37:45da88e36d1d | 92 | |
jasper0712 | 37:45da88e36d1d | 93 | if (game.doneUpgrade_flag == 1) { |
jasper0712 | 37:45da88e36d1d | 94 | waveComplete_flag = 0; //get out of while loop |
jasper0712 | 44:aa086363d09f | 95 | Start_The_Game_flag = 1; //continue the game |
jasper0712 | 37:45da88e36d1d | 96 | game.doneUpgrade_flag = 0; |
jasper0712 | 40:3a0c66a0e10e | 97 | //printf("i got out"); |
jasper0712 | 37:45da88e36d1d | 98 | } |
jasper0712 | 37:45da88e36d1d | 99 | } |
jasper0712 | 28:450ab72fabdc | 100 | void waveComplete_isr() { |
jasper0712 | 28:450ab72fabdc | 101 | waveComplete_flag = 1; |
jasper0712 | 28:450ab72fabdc | 102 | waveNumber += 1; |
jasper0712 | 32:580d74825810 | 103 | } |
jasper0712 | 32:580d74825810 | 104 | void gameMenu() { |
jasper0712 | 41:1b396334256b | 105 | waveNumber = 0; |
jasper0712 | 41:1b396334256b | 106 | game.reset_Game(); |
jasper0712 | 32:580d74825810 | 107 | while ( pad.check_event(Gamepad::START_PRESSED) == false) { |
jasper0712 | 32:580d74825810 | 108 | game.startingMenu(pad, lcd); |
jasper0712 | 32:580d74825810 | 109 | lcd.refresh(); |
jasper0712 | 32:580d74825810 | 110 | wait(0.2); |
jasper0712 | 32:580d74825810 | 111 | } |
jasper0712 | 32:580d74825810 | 112 | lcd.clear(); |
jasper0712 | 0:5fbc0fbc9d30 | 113 | } |