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