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@3:3d35ab70b565, 2019-05-06 (annotated)
- Committer:
- el17ttds
- Date:
- Mon May 06 19:50:27 2019 +0000
- Revision:
- 3:3d35ab70b565
- Parent:
- 2:ee9b361ba6df
- Child:
- 4:3446009e2f38
Full movement on 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 | 2:ee9b361ba6df | 15 | lcd.printString(" END",0,3); |
| el17ttds | 0:7769e2ad5d7a | 16 | } |
| el17ttds | 0:7769e2ad5d7a | 17 | |
| el17ttds | 0:7769e2ad5d7a | 18 | void init() { |
| el17ttds | 3:3d35ab70b565 | 19 | |
| el17ttds | 0:7769e2ad5d7a | 20 | // initialise display and peripherals |
| el17ttds | 0:7769e2ad5d7a | 21 | lcd.init(); |
| el17ttds | 0:7769e2ad5d7a | 22 | pad.init(); |
| el17ttds | 3:3d35ab70b565 | 23 | |
| el17ttds | 0:7769e2ad5d7a | 24 | // initialise any in game functions (e.g: sprites) |
| el17ttds | 3:3d35ab70b565 | 25 | engine.init(MAP_WIDTH,MAP_HEIGHT,SCREEN_WIDTH,SCREEN_HEIGHT); |
| el17ttds | 0:7769e2ad5d7a | 26 | } |
| el17ttds | 0:7769e2ad5d7a | 27 | |
| el17ttds | 0:7769e2ad5d7a | 28 | void welcome() { |
| el17ttds | 0:7769e2ad5d7a | 29 | // Draw coin |
| el17ttds | 0:7769e2ad5d7a | 30 | // wait |
| el17ttds | 0:7769e2ad5d7a | 31 | // Draw protagonist |
| el17ttds | 0:7769e2ad5d7a | 32 | // wait |
| el17ttds | 0:7769e2ad5d7a | 33 | // draw enemies |
| el17ttds | 0:7769e2ad5d7a | 34 | // wait |
| el17ttds | 0:7769e2ad5d7a | 35 | lcd.printString(" GAME! ",0,0); |
| el17ttds | 0:7769e2ad5d7a | 36 | lcd.refresh(); |
| el17ttds | 0:7769e2ad5d7a | 37 | wait(1.0); |
| el17ttds | 0:7769e2ad5d7a | 38 | lcd.printString("By Thomas",0,2); |
| el17ttds | 0:7769e2ad5d7a | 39 | lcd.printString("Foster",0,3); |
| el17ttds | 0:7769e2ad5d7a | 40 | lcd.refresh(); |
| el17ttds | 0:7769e2ad5d7a | 41 | wait(2.0); |
| el17ttds | 3:3d35ab70b565 | 42 | |
| el17ttds | 0:7769e2ad5d7a | 43 | while (pad.check_event(Gamepad::START_PRESSED) == false) { |
| el17ttds | 0:7769e2ad5d7a | 44 | lcd.printString("Press start ",0,5); |
| el17ttds | 0:7769e2ad5d7a | 45 | lcd.refresh(); |
| el17ttds | 0:7769e2ad5d7a | 46 | wait(0.2); |
| el17ttds | 0:7769e2ad5d7a | 47 | lcd.printString("Press start. ",0,5); |
| el17ttds | 0:7769e2ad5d7a | 48 | lcd.refresh(); |
| el17ttds | 0:7769e2ad5d7a | 49 | wait(0.2); |
| el17ttds | 0:7769e2ad5d7a | 50 | lcd.printString("Press start.. ",0,5); |
| el17ttds | 0:7769e2ad5d7a | 51 | lcd.refresh(); |
| el17ttds | 0:7769e2ad5d7a | 52 | wait(0.2); |
| el17ttds | 0:7769e2ad5d7a | 53 | lcd.printString("Press start... ",0,5); |
| el17ttds | 0:7769e2ad5d7a | 54 | wait(0.2); |
| el17ttds | 0:7769e2ad5d7a | 55 | } |
| el17ttds | 0:7769e2ad5d7a | 56 | } |
| el17ttds | 0:7769e2ad5d7a | 57 | |
| el17ttds | 1:8e319bd14b84 | 58 | string menu() { |
| el17ttds | 3:3d35ab70b565 | 59 | |
| el17ttds | 0:7769e2ad5d7a | 60 | string option = "0"; |
| el17ttds | 0:7769e2ad5d7a | 61 | lcd.init(); |
| el17ttds | 3:3d35ab70b565 | 62 | |
| el17ttds | 0:7769e2ad5d7a | 63 | while(option == "0") { |
| el17ttds | 3:3d35ab70b565 | 64 | |
| el17ttds | 0:7769e2ad5d7a | 65 | lcd.printString("A - Play Now",0,1); |
| el17ttds | 0:7769e2ad5d7a | 66 | lcd.printString("B - Tutorial",0,2); |
| el17ttds | 0:7769e2ad5d7a | 67 | lcd.printString("X - Highscores",0,3); |
| el17ttds | 0:7769e2ad5d7a | 68 | lcd.printString("Y - Credits",0,4); |
| el17ttds | 0:7769e2ad5d7a | 69 | lcd.refresh(); |
| el17ttds | 3:3d35ab70b565 | 70 | |
| el17ttds | 0:7769e2ad5d7a | 71 | if (pad.check_event(Gamepad::A_PRESSED) == true) { |
| el17ttds | 0:7769e2ad5d7a | 72 | option = "A"; |
| el17ttds | 0:7769e2ad5d7a | 73 | } else if (pad.check_event(Gamepad::B_PRESSED) == true) { |
| el17ttds | 0:7769e2ad5d7a | 74 | option = "B"; |
| el17ttds | 0:7769e2ad5d7a | 75 | } else if (pad.check_event(Gamepad::X_PRESSED) == true) { |
| el17ttds | 0:7769e2ad5d7a | 76 | option = "X"; |
| el17ttds | 0:7769e2ad5d7a | 77 | } else if (pad.check_event(Gamepad::Y_PRESSED) == true) { |
| el17ttds | 0:7769e2ad5d7a | 78 | option = "Y"; |
| el17ttds | 0:7769e2ad5d7a | 79 | } |
| el17ttds | 3:3d35ab70b565 | 80 | |
| el17ttds | 0:7769e2ad5d7a | 81 | wait(0.2); |
| el17ttds | 0:7769e2ad5d7a | 82 | } |
| el17ttds | 0:7769e2ad5d7a | 83 | if (option == "A") { |
| el17ttds | 0:7769e2ad5d7a | 84 | init(); |
| el17ttds | 0:7769e2ad5d7a | 85 | lcd.printString(" Play game? ",0,0); |
| el17ttds | 1:8e319bd14b84 | 86 | ask(option); |
| el17ttds | 0:7769e2ad5d7a | 87 | } else if (option == "B") { |
| el17ttds | 0:7769e2ad5d7a | 88 | init(); |
| el17ttds | 0:7769e2ad5d7a | 89 | lcd.printString("Play tutorial?",0,0); |
| el17ttds | 1:8e319bd14b84 | 90 | ask(option); |
| el17ttds | 0:7769e2ad5d7a | 91 | } |
| el17ttds | 1:8e319bd14b84 | 92 | return option; |
| el17ttds | 0:7769e2ad5d7a | 93 | } |
| el17ttds | 0:7769e2ad5d7a | 94 | |
| el17ttds | 1:8e319bd14b84 | 95 | void ask(string option) { |
| el17ttds | 3:3d35ab70b565 | 96 | |
| el17ttds | 1:8e319bd14b84 | 97 | int start = -1; |
| el17ttds | 0:7769e2ad5d7a | 98 | lcd.printString("Are you sure?",0,3); |
| el17ttds | 0:7769e2ad5d7a | 99 | lcd.printString("Back? Start?",0,5); |
| el17ttds | 0:7769e2ad5d7a | 100 | lcd.refresh(); |
| el17ttds | 3:3d35ab70b565 | 101 | |
| el17ttds | 1:8e319bd14b84 | 102 | while (start == -1) { |
| el17ttds | 3:3d35ab70b565 | 103 | |
| el17ttds | 0:7769e2ad5d7a | 104 | if (pad.check_event(Gamepad::START_PRESSED) == true) { |
| el17ttds | 1:8e319bd14b84 | 105 | start = 1; |
| el17ttds | 0:7769e2ad5d7a | 106 | } else if(pad.check_event(Gamepad::BACK_PRESSED) == true) { |
| el17ttds | 1:8e319bd14b84 | 107 | start = 0; |
| el17ttds | 0:7769e2ad5d7a | 108 | } |
| el17ttds | 0:7769e2ad5d7a | 109 | wait(0.2); |
| el17ttds | 0:7769e2ad5d7a | 110 | } |
| el17ttds | 3:3d35ab70b565 | 111 | |
| el17ttds | 1:8e319bd14b84 | 112 | if (start == 0) { |
| el17ttds | 0:7769e2ad5d7a | 113 | menu(); |
| el17ttds | 1:8e319bd14b84 | 114 | } |
| el17ttds | 1:8e319bd14b84 | 115 | } |
| el17ttds | 1:8e319bd14b84 | 116 | |
| el17ttds | 1:8e319bd14b84 | 117 | void begin(string option) { |
| el17ttds | 3:3d35ab70b565 | 118 | |
| el17ttds | 1:8e319bd14b84 | 119 | init(); |
| el17ttds | 1:8e319bd14b84 | 120 | if (option == "A") { |
| el17ttds | 1:8e319bd14b84 | 121 | play(); |
| el17ttds | 1:8e319bd14b84 | 122 | } else if (option == "B") { |
| el17ttds | 1:8e319bd14b84 | 123 | tutorial(); |
| el17ttds | 1:8e319bd14b84 | 124 | } else if (option == "X") { |
| el17ttds | 1:8e319bd14b84 | 125 | highscores(); |
| el17ttds | 0:7769e2ad5d7a | 126 | } else { |
| el17ttds | 1:8e319bd14b84 | 127 | credits(); |
| el17ttds | 0:7769e2ad5d7a | 128 | } |
| el17ttds | 0:7769e2ad5d7a | 129 | } |
| el17ttds | 0:7769e2ad5d7a | 130 | |
| el17ttds | 1:8e319bd14b84 | 131 | void play() { |
| el17ttds | 2:ee9b361ba6df | 132 | |
| el17ttds | 3:3d35ab70b565 | 133 | lcd.refresh(); |
| el17ttds | 2:ee9b361ba6df | 134 | wait(0.2); |
| el17ttds | 3:3d35ab70b565 | 135 | |
| el17ttds | 3:3d35ab70b565 | 136 | while ( pad.check_event(Gamepad::START_PRESSED) == false) { |
| el17ttds | 3:3d35ab70b565 | 137 | lcd.refresh(); |
| el17ttds | 2:ee9b361ba6df | 138 | engine.read(pad); |
| el17ttds | 2:ee9b361ba6df | 139 | engine.write(MAX_SPEED); |
| el17ttds | 2:ee9b361ba6df | 140 | engine.render(lcd); |
| el17ttds | 2:ee9b361ba6df | 141 | wait(0.1); |
| el17ttds | 2:ee9b361ba6df | 142 | } |
| el17ttds | 1:8e319bd14b84 | 143 | } |
| el17ttds | 1:8e319bd14b84 | 144 | |
| el17ttds | 1:8e319bd14b84 | 145 | void tutorial() { |
| el17ttds | 3:3d35ab70b565 | 146 | |
| el17ttds | 1:8e319bd14b84 | 147 | lcd.printString(" Tutorial!! ",0,0); |
| el17ttds | 1:8e319bd14b84 | 148 | lcd.refresh(); |
| el17ttds | 1:8e319bd14b84 | 149 | } |
| el17ttds | 1:8e319bd14b84 | 150 | |
| el17ttds | 0:7769e2ad5d7a | 151 | void highscores() { |
| el17ttds | 3:3d35ab70b565 | 152 | |
| el17ttds | 0:7769e2ad5d7a | 153 | lcd.printString(" Highscores!! ",0,0); |
| el17ttds | 0:7769e2ad5d7a | 154 | lcd.refresh(); |
| el17ttds | 0:7769e2ad5d7a | 155 | } |
| el17ttds | 0:7769e2ad5d7a | 156 | |
| el17ttds | 0:7769e2ad5d7a | 157 | void credits() { |
| el17ttds | 3:3d35ab70b565 | 158 | |
| el17ttds | 0:7769e2ad5d7a | 159 | lcd.printString(" Credits!! ",0,0); |
| el17ttds | 0:7769e2ad5d7a | 160 | lcd.refresh(); |
| el17ttds | 0:7769e2ad5d7a | 161 | } |