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@8:d1c04f0e4890, 2019-05-11 (annotated)
- Committer:
- el17ttds
- Date:
- Sat May 11 08:23:54 2019 +0000
- Revision:
- 8:d1c04f0e4890
- Parent:
- 7:08f78909dda7
- Child:
- 9:3a0194c87afe
Full game.; Unfinished menu, tone glitch, score doesn't print, ammo and health to be displayed
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 | 8:d1c04f0e4890 | 3 | /* |
| el17ttds | 8:d1c04f0e4890 | 4 | ELEC2645 Embedded Systems Project |
| el17ttds | 8:d1c04f0e4890 | 5 | School of Electronic & Electrical Engineering |
| el17ttds | 8:d1c04f0e4890 | 6 | University of Leeds |
| el17ttds | 8:d1c04f0e4890 | 7 | Name: Thomas Foster |
| el17ttds | 8:d1c04f0e4890 | 8 | Username: el17ttds |
| el17ttds | 8:d1c04f0e4890 | 9 | Student ID Number: 201096550 |
| el17ttds | 8:d1c04f0e4890 | 10 | Date: 11 / 05 / 2019 |
| el17ttds | 8:d1c04f0e4890 | 11 | */ |
| el17ttds | 8:d1c04f0e4890 | 12 | |
| el17ttds | 0:7769e2ad5d7a | 13 | /////////////// objects /////////////// |
| el17ttds | 0:7769e2ad5d7a | 14 | N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11); |
| el17ttds | 0:7769e2ad5d7a | 15 | Gamepad pad; |
| el17ttds | 2:ee9b361ba6df | 16 | Engine engine; |
| el17ttds | 0:7769e2ad5d7a | 17 | |
| el17ttds | 0:7769e2ad5d7a | 18 | int main() { |
| el17ttds | 3:3d35ab70b565 | 19 | |
| el17ttds | 0:7769e2ad5d7a | 20 | init(); |
| el17ttds | 0:7769e2ad5d7a | 21 | welcome(); |
| el17ttds | 1:8e319bd14b84 | 22 | string option = menu(); |
| el17ttds | 1:8e319bd14b84 | 23 | begin(option); |
| el17ttds | 3:3d35ab70b565 | 24 | |
| el17ttds | 4:3446009e2f38 | 25 | lcd.printString(" END",0,5); |
| el17ttds | 4:3446009e2f38 | 26 | lcd.refresh(); |
| el17ttds | 0:7769e2ad5d7a | 27 | } |
| el17ttds | 0:7769e2ad5d7a | 28 | |
| el17ttds | 0:7769e2ad5d7a | 29 | void init() { |
| el17ttds | 8:d1c04f0e4890 | 30 | health = 3; |
| el17ttds | 8:d1c04f0e4890 | 31 | score = 0; |
| el17ttds | 7:08f78909dda7 | 32 | // initialise display and peripherals |
| el17ttds | 7:08f78909dda7 | 33 | lcd.init(); |
| el17ttds | 7:08f78909dda7 | 34 | pad.init(); |
| el17ttds | 3:3d35ab70b565 | 35 | |
| el17ttds | 0:7769e2ad5d7a | 36 | // initialise any in game functions (e.g: sprites) |
| el17ttds | 8:d1c04f0e4890 | 37 | engine.init(MAP_WIDTH,MAP_HEIGHT,SCREEN_WIDTH,SCREEN_HEIGHT); |
| el17ttds | 0:7769e2ad5d7a | 38 | } |
| el17ttds | 0:7769e2ad5d7a | 39 | |
| el17ttds | 0:7769e2ad5d7a | 40 | void welcome() { |
| el17ttds | 0:7769e2ad5d7a | 41 | // Draw coin |
| el17ttds | 0:7769e2ad5d7a | 42 | // wait |
| el17ttds | 0:7769e2ad5d7a | 43 | // Draw protagonist |
| el17ttds | 0:7769e2ad5d7a | 44 | // wait |
| el17ttds | 0:7769e2ad5d7a | 45 | // draw enemies |
| el17ttds | 0:7769e2ad5d7a | 46 | // wait |
| el17ttds | 0:7769e2ad5d7a | 47 | lcd.printString(" GAME! ",0,0); |
| el17ttds | 0:7769e2ad5d7a | 48 | lcd.refresh(); |
| el17ttds | 0:7769e2ad5d7a | 49 | wait(1.0); |
| el17ttds | 0:7769e2ad5d7a | 50 | lcd.printString("By Thomas",0,2); |
| el17ttds | 0:7769e2ad5d7a | 51 | lcd.printString("Foster",0,3); |
| el17ttds | 0:7769e2ad5d7a | 52 | lcd.refresh(); |
| el17ttds | 0:7769e2ad5d7a | 53 | wait(2.0); |
| el17ttds | 3:3d35ab70b565 | 54 | |
| el17ttds | 0:7769e2ad5d7a | 55 | while (pad.check_event(Gamepad::START_PRESSED) == false) { |
| el17ttds | 7:08f78909dda7 | 56 | lcd.printString("Press Start ",0,5); |
| el17ttds | 0:7769e2ad5d7a | 57 | lcd.refresh(); |
| el17ttds | 0:7769e2ad5d7a | 58 | wait(0.2); |
| el17ttds | 7:08f78909dda7 | 59 | lcd.printString("Press Start. ",0,5); |
| el17ttds | 0:7769e2ad5d7a | 60 | lcd.refresh(); |
| el17ttds | 0:7769e2ad5d7a | 61 | wait(0.2); |
| el17ttds | 7:08f78909dda7 | 62 | lcd.printString("Press Start.. ",0,5); |
| el17ttds | 0:7769e2ad5d7a | 63 | lcd.refresh(); |
| el17ttds | 0:7769e2ad5d7a | 64 | wait(0.2); |
| el17ttds | 7:08f78909dda7 | 65 | lcd.printString("Press Start... ",0,5); |
| el17ttds | 0:7769e2ad5d7a | 66 | wait(0.2); |
| el17ttds | 0:7769e2ad5d7a | 67 | } |
| el17ttds | 0:7769e2ad5d7a | 68 | } |
| el17ttds | 0:7769e2ad5d7a | 69 | |
| el17ttds | 1:8e319bd14b84 | 70 | string menu() { |
| el17ttds | 3:3d35ab70b565 | 71 | |
| el17ttds | 0:7769e2ad5d7a | 72 | string option = "0"; |
| el17ttds | 0:7769e2ad5d7a | 73 | lcd.init(); |
| el17ttds | 3:3d35ab70b565 | 74 | |
| el17ttds | 0:7769e2ad5d7a | 75 | while(option == "0") { |
| el17ttds | 3:3d35ab70b565 | 76 | |
| el17ttds | 0:7769e2ad5d7a | 77 | lcd.printString("A - Play Now",0,1); |
| el17ttds | 0:7769e2ad5d7a | 78 | lcd.printString("B - Tutorial",0,2); |
| el17ttds | 0:7769e2ad5d7a | 79 | lcd.printString("X - Highscores",0,3); |
| el17ttds | 0:7769e2ad5d7a | 80 | lcd.printString("Y - Credits",0,4); |
| el17ttds | 0:7769e2ad5d7a | 81 | lcd.refresh(); |
| el17ttds | 3:3d35ab70b565 | 82 | |
| el17ttds | 0:7769e2ad5d7a | 83 | if (pad.check_event(Gamepad::A_PRESSED) == true) { |
| el17ttds | 0:7769e2ad5d7a | 84 | option = "A"; |
| el17ttds | 0:7769e2ad5d7a | 85 | } else if (pad.check_event(Gamepad::B_PRESSED) == true) { |
| el17ttds | 0:7769e2ad5d7a | 86 | option = "B"; |
| el17ttds | 0:7769e2ad5d7a | 87 | } else if (pad.check_event(Gamepad::X_PRESSED) == true) { |
| el17ttds | 0:7769e2ad5d7a | 88 | option = "X"; |
| el17ttds | 0:7769e2ad5d7a | 89 | } else if (pad.check_event(Gamepad::Y_PRESSED) == true) { |
| el17ttds | 0:7769e2ad5d7a | 90 | option = "Y"; |
| el17ttds | 0:7769e2ad5d7a | 91 | } |
| el17ttds | 3:3d35ab70b565 | 92 | |
| el17ttds | 0:7769e2ad5d7a | 93 | wait(0.2); |
| el17ttds | 0:7769e2ad5d7a | 94 | } |
| el17ttds | 0:7769e2ad5d7a | 95 | if (option == "A") { |
| el17ttds | 0:7769e2ad5d7a | 96 | init(); |
| el17ttds | 0:7769e2ad5d7a | 97 | lcd.printString(" Play game? ",0,0); |
| el17ttds | 1:8e319bd14b84 | 98 | ask(option); |
| el17ttds | 0:7769e2ad5d7a | 99 | } else if (option == "B") { |
| el17ttds | 0:7769e2ad5d7a | 100 | init(); |
| el17ttds | 0:7769e2ad5d7a | 101 | lcd.printString("Play tutorial?",0,0); |
| el17ttds | 1:8e319bd14b84 | 102 | ask(option); |
| el17ttds | 0:7769e2ad5d7a | 103 | } |
| el17ttds | 1:8e319bd14b84 | 104 | return option; |
| el17ttds | 0:7769e2ad5d7a | 105 | } |
| el17ttds | 0:7769e2ad5d7a | 106 | |
| el17ttds | 1:8e319bd14b84 | 107 | void ask(string option) { |
| el17ttds | 3:3d35ab70b565 | 108 | |
| el17ttds | 1:8e319bd14b84 | 109 | int start = -1; |
| el17ttds | 0:7769e2ad5d7a | 110 | lcd.printString("Are you sure?",0,3); |
| el17ttds | 0:7769e2ad5d7a | 111 | lcd.printString("Back? Start?",0,5); |
| el17ttds | 0:7769e2ad5d7a | 112 | lcd.refresh(); |
| el17ttds | 3:3d35ab70b565 | 113 | |
| el17ttds | 1:8e319bd14b84 | 114 | while (start == -1) { |
| el17ttds | 3:3d35ab70b565 | 115 | |
| el17ttds | 0:7769e2ad5d7a | 116 | if (pad.check_event(Gamepad::START_PRESSED) == true) { |
| el17ttds | 1:8e319bd14b84 | 117 | start = 1; |
| el17ttds | 0:7769e2ad5d7a | 118 | } else if(pad.check_event(Gamepad::BACK_PRESSED) == true) { |
| el17ttds | 1:8e319bd14b84 | 119 | start = 0; |
| el17ttds | 0:7769e2ad5d7a | 120 | } |
| el17ttds | 0:7769e2ad5d7a | 121 | wait(0.2); |
| el17ttds | 0:7769e2ad5d7a | 122 | } |
| el17ttds | 3:3d35ab70b565 | 123 | |
| el17ttds | 1:8e319bd14b84 | 124 | if (start == 0) { |
| el17ttds | 0:7769e2ad5d7a | 125 | menu(); |
| el17ttds | 1:8e319bd14b84 | 126 | } |
| el17ttds | 1:8e319bd14b84 | 127 | } |
| el17ttds | 1:8e319bd14b84 | 128 | |
| el17ttds | 1:8e319bd14b84 | 129 | void begin(string option) { |
| el17ttds | 3:3d35ab70b565 | 130 | |
| el17ttds | 1:8e319bd14b84 | 131 | init(); |
| el17ttds | 1:8e319bd14b84 | 132 | if (option == "A") { |
| el17ttds | 1:8e319bd14b84 | 133 | play(); |
| el17ttds | 1:8e319bd14b84 | 134 | } else if (option == "B") { |
| el17ttds | 1:8e319bd14b84 | 135 | tutorial(); |
| el17ttds | 1:8e319bd14b84 | 136 | } else if (option == "X") { |
| el17ttds | 1:8e319bd14b84 | 137 | highscores(); |
| el17ttds | 0:7769e2ad5d7a | 138 | } else { |
| el17ttds | 1:8e319bd14b84 | 139 | credits(); |
| el17ttds | 0:7769e2ad5d7a | 140 | } |
| el17ttds | 0:7769e2ad5d7a | 141 | } |
| el17ttds | 0:7769e2ad5d7a | 142 | |
| el17ttds | 7:08f78909dda7 | 143 | void play() { // My game loop |
| el17ttds | 7:08f78909dda7 | 144 | lcd.refresh(); |
| el17ttds | 7:08f78909dda7 | 145 | wait(0.2); |
| el17ttds | 7:08f78909dda7 | 146 | gameLoop(); |
| el17ttds | 7:08f78909dda7 | 147 | } |
| el17ttds | 3:3d35ab70b565 | 148 | |
| el17ttds | 7:08f78909dda7 | 149 | void gameLoop() { |
| el17ttds | 8:d1c04f0e4890 | 150 | while ( (health > 0) && (pad.check_event(Gamepad::START_PRESSED) == false) ) { |
| el17ttds | 7:08f78909dda7 | 151 | lcd.refresh(); |
| el17ttds | 7:08f78909dda7 | 152 | engine.read(pad); |
| el17ttds | 8:d1c04f0e4890 | 153 | engine.write(MAX_SPEED, pad, lcd); |
| el17ttds | 7:08f78909dda7 | 154 | engine.render(lcd); |
| el17ttds | 8:d1c04f0e4890 | 155 | health = engine.get_health(); |
| el17ttds | 8:d1c04f0e4890 | 156 | score = engine.get_score(); |
| el17ttds | 7:08f78909dda7 | 157 | wait(0.1); |
| el17ttds | 7:08f78909dda7 | 158 | } |
| el17ttds | 8:d1c04f0e4890 | 159 | wait(0.2); |
| el17ttds | 8:d1c04f0e4890 | 160 | if (health > 0) { |
| el17ttds | 8:d1c04f0e4890 | 161 | while (pad.check_event(Gamepad::A_PRESSED) == false) { |
| el17ttds | 7:08f78909dda7 | 162 | lcd.clear(); |
| el17ttds | 7:08f78909dda7 | 163 | lcd.printString(" PAUSED ", 0, 0); |
| el17ttds | 8:d1c04f0e4890 | 164 | //lcd.printString("SCORE = ", 0, 1); |
| el17ttds | 8:d1c04f0e4890 | 165 | //lcd.printString(score, 60, 1); |
| el17ttds | 7:08f78909dda7 | 166 | lcd.refresh(); |
| el17ttds | 7:08f78909dda7 | 167 | wait(0.5); |
| el17ttds | 8:d1c04f0e4890 | 168 | lcd.printString("Press A", 0, 3); |
| el17ttds | 7:08f78909dda7 | 169 | lcd.refresh(); |
| el17ttds | 8:d1c04f0e4890 | 170 | wait(0.5); |
| el17ttds | 2:ee9b361ba6df | 171 | } |
| el17ttds | 8:d1c04f0e4890 | 172 | gameLoop(); |
| el17ttds | 7:08f78909dda7 | 173 | } else { |
| el17ttds | 7:08f78909dda7 | 174 | lcd.clear(); |
| el17ttds | 7:08f78909dda7 | 175 | lcd.printString("YOU HELLA DEAD", 0, 0); |
| el17ttds | 7:08f78909dda7 | 176 | } |
| el17ttds | 1:8e319bd14b84 | 177 | } |
| el17ttds | 1:8e319bd14b84 | 178 | |
| el17ttds | 1:8e319bd14b84 | 179 | void tutorial() { |
| el17ttds | 3:3d35ab70b565 | 180 | |
| el17ttds | 1:8e319bd14b84 | 181 | lcd.printString(" Tutorial!! ",0,0); |
| el17ttds | 1:8e319bd14b84 | 182 | lcd.refresh(); |
| el17ttds | 1:8e319bd14b84 | 183 | } |
| el17ttds | 1:8e319bd14b84 | 184 | |
| el17ttds | 0:7769e2ad5d7a | 185 | void highscores() { |
| el17ttds | 3:3d35ab70b565 | 186 | |
| el17ttds | 0:7769e2ad5d7a | 187 | lcd.printString(" Highscores!! ",0,0); |
| el17ttds | 0:7769e2ad5d7a | 188 | lcd.refresh(); |
| el17ttds | 0:7769e2ad5d7a | 189 | } |
| el17ttds | 0:7769e2ad5d7a | 190 | |
| el17ttds | 0:7769e2ad5d7a | 191 | void credits() { |
| el17ttds | 3:3d35ab70b565 | 192 | |
| el17ttds | 0:7769e2ad5d7a | 193 | lcd.printString(" Credits!! ",0,0); |
| el17ttds | 0:7769e2ad5d7a | 194 | lcd.refresh(); |
| el17ttds | 4:3446009e2f38 | 195 | } |