Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
main.cpp@4:3446009e2f38, 2019-05-07 (annotated)
- Committer:
- el17ttds
- Date:
- Tue May 07 19:25:20 2019 +0000
- Revision:
- 4:3446009e2f38
- Parent:
- 3:3d35ab70b565
- Child:
- 7:08f78909dda7
Free movement from engine not map
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
el17ttds | 0:7769e2ad5d7a | 1 | #include "main.h" |
el17ttds | 0:7769e2ad5d7a | 2 | |
el17ttds | 0:7769e2ad5d7a | 3 | /////////////// objects /////////////// |
el17ttds | 0:7769e2ad5d7a | 4 | N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11); |
el17ttds | 0:7769e2ad5d7a | 5 | Gamepad pad; |
el17ttds | 2:ee9b361ba6df | 6 | Engine engine; |
el17ttds | 0:7769e2ad5d7a | 7 | |
el17ttds | 0:7769e2ad5d7a | 8 | int main() { |
el17ttds | 3:3d35ab70b565 | 9 | |
el17ttds | 0:7769e2ad5d7a | 10 | init(); |
el17ttds | 0:7769e2ad5d7a | 11 | welcome(); |
el17ttds | 1:8e319bd14b84 | 12 | string option = menu(); |
el17ttds | 1:8e319bd14b84 | 13 | begin(option); |
el17ttds | 3:3d35ab70b565 | 14 | |
el17ttds | 4:3446009e2f38 | 15 | lcd.printString(" END",0,5); |
el17ttds | 4:3446009e2f38 | 16 | lcd.refresh(); |
el17ttds | 0:7769e2ad5d7a | 17 | } |
el17ttds | 0:7769e2ad5d7a | 18 | |
el17ttds | 0:7769e2ad5d7a | 19 | void init() { |
el17ttds | 3:3d35ab70b565 | 20 | |
el17ttds | 0:7769e2ad5d7a | 21 | // initialise display and peripherals |
el17ttds | 0:7769e2ad5d7a | 22 | lcd.init(); |
el17ttds | 0:7769e2ad5d7a | 23 | pad.init(); |
el17ttds | 3:3d35ab70b565 | 24 | |
el17ttds | 0:7769e2ad5d7a | 25 | // initialise any in game functions (e.g: sprites) |
el17ttds | 3:3d35ab70b565 | 26 | engine.init(MAP_WIDTH,MAP_HEIGHT,SCREEN_WIDTH,SCREEN_HEIGHT); |
el17ttds | 0:7769e2ad5d7a | 27 | } |
el17ttds | 0:7769e2ad5d7a | 28 | |
el17ttds | 0:7769e2ad5d7a | 29 | void welcome() { |
el17ttds | 0:7769e2ad5d7a | 30 | // Draw coin |
el17ttds | 0:7769e2ad5d7a | 31 | // wait |
el17ttds | 0:7769e2ad5d7a | 32 | // Draw protagonist |
el17ttds | 0:7769e2ad5d7a | 33 | // wait |
el17ttds | 0:7769e2ad5d7a | 34 | // draw enemies |
el17ttds | 0:7769e2ad5d7a | 35 | // wait |
el17ttds | 0:7769e2ad5d7a | 36 | lcd.printString(" GAME! ",0,0); |
el17ttds | 0:7769e2ad5d7a | 37 | lcd.refresh(); |
el17ttds | 0:7769e2ad5d7a | 38 | wait(1.0); |
el17ttds | 0:7769e2ad5d7a | 39 | lcd.printString("By Thomas",0,2); |
el17ttds | 0:7769e2ad5d7a | 40 | lcd.printString("Foster",0,3); |
el17ttds | 0:7769e2ad5d7a | 41 | lcd.refresh(); |
el17ttds | 0:7769e2ad5d7a | 42 | wait(2.0); |
el17ttds | 3:3d35ab70b565 | 43 | |
el17ttds | 0:7769e2ad5d7a | 44 | while (pad.check_event(Gamepad::START_PRESSED) == false) { |
el17ttds | 0:7769e2ad5d7a | 45 | lcd.printString("Press start ",0,5); |
el17ttds | 0:7769e2ad5d7a | 46 | lcd.refresh(); |
el17ttds | 0:7769e2ad5d7a | 47 | wait(0.2); |
el17ttds | 0:7769e2ad5d7a | 48 | lcd.printString("Press start. ",0,5); |
el17ttds | 0:7769e2ad5d7a | 49 | lcd.refresh(); |
el17ttds | 0:7769e2ad5d7a | 50 | wait(0.2); |
el17ttds | 0:7769e2ad5d7a | 51 | lcd.printString("Press start.. ",0,5); |
el17ttds | 0:7769e2ad5d7a | 52 | lcd.refresh(); |
el17ttds | 0:7769e2ad5d7a | 53 | wait(0.2); |
el17ttds | 0:7769e2ad5d7a | 54 | lcd.printString("Press start... ",0,5); |
el17ttds | 0:7769e2ad5d7a | 55 | wait(0.2); |
el17ttds | 0:7769e2ad5d7a | 56 | } |
el17ttds | 0:7769e2ad5d7a | 57 | } |
el17ttds | 0:7769e2ad5d7a | 58 | |
el17ttds | 1:8e319bd14b84 | 59 | string menu() { |
el17ttds | 3:3d35ab70b565 | 60 | |
el17ttds | 0:7769e2ad5d7a | 61 | string option = "0"; |
el17ttds | 0:7769e2ad5d7a | 62 | lcd.init(); |
el17ttds | 3:3d35ab70b565 | 63 | |
el17ttds | 0:7769e2ad5d7a | 64 | while(option == "0") { |
el17ttds | 3:3d35ab70b565 | 65 | |
el17ttds | 0:7769e2ad5d7a | 66 | lcd.printString("A - Play Now",0,1); |
el17ttds | 0:7769e2ad5d7a | 67 | lcd.printString("B - Tutorial",0,2); |
el17ttds | 0:7769e2ad5d7a | 68 | lcd.printString("X - Highscores",0,3); |
el17ttds | 0:7769e2ad5d7a | 69 | lcd.printString("Y - Credits",0,4); |
el17ttds | 0:7769e2ad5d7a | 70 | lcd.refresh(); |
el17ttds | 3:3d35ab70b565 | 71 | |
el17ttds | 0:7769e2ad5d7a | 72 | if (pad.check_event(Gamepad::A_PRESSED) == true) { |
el17ttds | 0:7769e2ad5d7a | 73 | option = "A"; |
el17ttds | 0:7769e2ad5d7a | 74 | } else if (pad.check_event(Gamepad::B_PRESSED) == true) { |
el17ttds | 0:7769e2ad5d7a | 75 | option = "B"; |
el17ttds | 0:7769e2ad5d7a | 76 | } else if (pad.check_event(Gamepad::X_PRESSED) == true) { |
el17ttds | 0:7769e2ad5d7a | 77 | option = "X"; |
el17ttds | 0:7769e2ad5d7a | 78 | } else if (pad.check_event(Gamepad::Y_PRESSED) == true) { |
el17ttds | 0:7769e2ad5d7a | 79 | option = "Y"; |
el17ttds | 0:7769e2ad5d7a | 80 | } |
el17ttds | 3:3d35ab70b565 | 81 | |
el17ttds | 0:7769e2ad5d7a | 82 | wait(0.2); |
el17ttds | 0:7769e2ad5d7a | 83 | } |
el17ttds | 0:7769e2ad5d7a | 84 | if (option == "A") { |
el17ttds | 0:7769e2ad5d7a | 85 | init(); |
el17ttds | 0:7769e2ad5d7a | 86 | lcd.printString(" Play game? ",0,0); |
el17ttds | 1:8e319bd14b84 | 87 | ask(option); |
el17ttds | 0:7769e2ad5d7a | 88 | } else if (option == "B") { |
el17ttds | 0:7769e2ad5d7a | 89 | init(); |
el17ttds | 0:7769e2ad5d7a | 90 | lcd.printString("Play tutorial?",0,0); |
el17ttds | 1:8e319bd14b84 | 91 | ask(option); |
el17ttds | 0:7769e2ad5d7a | 92 | } |
el17ttds | 1:8e319bd14b84 | 93 | return option; |
el17ttds | 0:7769e2ad5d7a | 94 | } |
el17ttds | 0:7769e2ad5d7a | 95 | |
el17ttds | 1:8e319bd14b84 | 96 | void ask(string option) { |
el17ttds | 3:3d35ab70b565 | 97 | |
el17ttds | 1:8e319bd14b84 | 98 | int start = -1; |
el17ttds | 0:7769e2ad5d7a | 99 | lcd.printString("Are you sure?",0,3); |
el17ttds | 0:7769e2ad5d7a | 100 | lcd.printString("Back? Start?",0,5); |
el17ttds | 0:7769e2ad5d7a | 101 | lcd.refresh(); |
el17ttds | 3:3d35ab70b565 | 102 | |
el17ttds | 1:8e319bd14b84 | 103 | while (start == -1) { |
el17ttds | 3:3d35ab70b565 | 104 | |
el17ttds | 0:7769e2ad5d7a | 105 | if (pad.check_event(Gamepad::START_PRESSED) == true) { |
el17ttds | 1:8e319bd14b84 | 106 | start = 1; |
el17ttds | 0:7769e2ad5d7a | 107 | } else if(pad.check_event(Gamepad::BACK_PRESSED) == true) { |
el17ttds | 1:8e319bd14b84 | 108 | start = 0; |
el17ttds | 0:7769e2ad5d7a | 109 | } |
el17ttds | 0:7769e2ad5d7a | 110 | wait(0.2); |
el17ttds | 0:7769e2ad5d7a | 111 | } |
el17ttds | 3:3d35ab70b565 | 112 | |
el17ttds | 1:8e319bd14b84 | 113 | if (start == 0) { |
el17ttds | 0:7769e2ad5d7a | 114 | menu(); |
el17ttds | 1:8e319bd14b84 | 115 | } |
el17ttds | 1:8e319bd14b84 | 116 | } |
el17ttds | 1:8e319bd14b84 | 117 | |
el17ttds | 1:8e319bd14b84 | 118 | void begin(string option) { |
el17ttds | 3:3d35ab70b565 | 119 | |
el17ttds | 1:8e319bd14b84 | 120 | init(); |
el17ttds | 1:8e319bd14b84 | 121 | if (option == "A") { |
el17ttds | 1:8e319bd14b84 | 122 | play(); |
el17ttds | 1:8e319bd14b84 | 123 | } else if (option == "B") { |
el17ttds | 1:8e319bd14b84 | 124 | tutorial(); |
el17ttds | 1:8e319bd14b84 | 125 | } else if (option == "X") { |
el17ttds | 1:8e319bd14b84 | 126 | highscores(); |
el17ttds | 0:7769e2ad5d7a | 127 | } else { |
el17ttds | 1:8e319bd14b84 | 128 | credits(); |
el17ttds | 0:7769e2ad5d7a | 129 | } |
el17ttds | 0:7769e2ad5d7a | 130 | } |
el17ttds | 0:7769e2ad5d7a | 131 | |
el17ttds | 1:8e319bd14b84 | 132 | void play() { |
el17ttds | 2:ee9b361ba6df | 133 | |
el17ttds | 3:3d35ab70b565 | 134 | lcd.refresh(); |
el17ttds | 2:ee9b361ba6df | 135 | wait(0.2); |
el17ttds | 3:3d35ab70b565 | 136 | |
el17ttds | 3:3d35ab70b565 | 137 | while ( pad.check_event(Gamepad::START_PRESSED) == false) { |
el17ttds | 3:3d35ab70b565 | 138 | lcd.refresh(); |
el17ttds | 2:ee9b361ba6df | 139 | engine.read(pad); |
el17ttds | 2:ee9b361ba6df | 140 | engine.write(MAX_SPEED); |
el17ttds | 2:ee9b361ba6df | 141 | engine.render(lcd); |
el17ttds | 2:ee9b361ba6df | 142 | wait(0.1); |
el17ttds | 2:ee9b361ba6df | 143 | } |
el17ttds | 1:8e319bd14b84 | 144 | } |
el17ttds | 1:8e319bd14b84 | 145 | |
el17ttds | 1:8e319bd14b84 | 146 | void tutorial() { |
el17ttds | 3:3d35ab70b565 | 147 | |
el17ttds | 1:8e319bd14b84 | 148 | lcd.printString(" Tutorial!! ",0,0); |
el17ttds | 1:8e319bd14b84 | 149 | lcd.refresh(); |
el17ttds | 1:8e319bd14b84 | 150 | } |
el17ttds | 1:8e319bd14b84 | 151 | |
el17ttds | 0:7769e2ad5d7a | 152 | void highscores() { |
el17ttds | 3:3d35ab70b565 | 153 | |
el17ttds | 0:7769e2ad5d7a | 154 | lcd.printString(" Highscores!! ",0,0); |
el17ttds | 0:7769e2ad5d7a | 155 | lcd.refresh(); |
el17ttds | 0:7769e2ad5d7a | 156 | } |
el17ttds | 0:7769e2ad5d7a | 157 | |
el17ttds | 0:7769e2ad5d7a | 158 | void credits() { |
el17ttds | 3:3d35ab70b565 | 159 | |
el17ttds | 0:7769e2ad5d7a | 160 | lcd.printString(" Credits!! ",0,0); |
el17ttds | 0:7769e2ad5d7a | 161 | lcd.refresh(); |
el17ttds | 4:3446009e2f38 | 162 | } |