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@9:96969b1c6bde, 2019-04-24 (annotated)
- Committer:
- joshdavy
- Date:
- Wed Apr 24 10:18:45 2019 +0000
- Revision:
- 9:96969b1c6bde
- Parent:
- 8:21b6d4dbce44
- Child:
- 10:58cf89dd878c
Added win screen aswell as multiple levels.
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| joshdavy | 0:4916a63a6cbf | 1 | /* |
| joshdavy | 0:4916a63a6cbf | 2 | ELEC2645 Embedded Systems Project |
| joshdavy | 0:4916a63a6cbf | 3 | School of Electronic & Electrical Engineering |
| joshdavy | 0:4916a63a6cbf | 4 | University of Leeds |
| joshdavy | 0:4916a63a6cbf | 5 | Name: Joshua Davy |
| joshdavy | 0:4916a63a6cbf | 6 | Username: el17jd |
| joshdavy | 0:4916a63a6cbf | 7 | Student ID Number: 201148379 |
| joshdavy | 0:4916a63a6cbf | 8 | Date: 12/03/2019 |
| joshdavy | 1:37802772843e | 9 | */ |
| joshdavy | 2:b62e8be35a5d | 10 | |
| joshdavy | 1:37802772843e | 11 | |
| joshdavy | 1:37802772843e | 12 | ///////// pre-processor directives //////// |
| joshdavy | 1:37802772843e | 13 | #include "mbed.h" |
| joshdavy | 1:37802772843e | 14 | #include "Gamepad.h" |
| joshdavy | 1:37802772843e | 15 | #include "N5110.h" |
| joshdavy | 1:37802772843e | 16 | #include "Sprite.h" |
| joshdavy | 1:37802772843e | 17 | #include "Game.h" |
| joshdavy | 5:b9cf407bcc63 | 18 | #include "Music.h" |
| joshdavy | 5:b9cf407bcc63 | 19 | #include "SoundData.h" |
| joshdavy | 8:21b6d4dbce44 | 20 | #include "SplashScreen.h" |
| joshdavy | 8:21b6d4dbce44 | 21 | #include "MenuScreen.h" |
| joshdavy | 9:96969b1c6bde | 22 | #include "WinScreen.h" |
| joshdavy | 9:96969b1c6bde | 23 | |
| joshdavy | 8:21b6d4dbce44 | 24 | Timer game_timer; |
| joshdavy | 8:21b6d4dbce44 | 25 | Timer music_timer; |
| joshdavy | 1:37802772843e | 26 | |
| joshdavy | 2:b62e8be35a5d | 27 | ////// Constants ////// |
| joshdavy | 3:b34685dbdb8d | 28 | |
| joshdavy | 2:b62e8be35a5d | 29 | |
| joshdavy | 2:b62e8be35a5d | 30 | |
| joshdavy | 2:b62e8be35a5d | 31 | |
| joshdavy | 1:37802772843e | 32 | /////////////// objects /////////////// |
| joshdavy | 1:37802772843e | 33 | N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11); |
| joshdavy | 1:37802772843e | 34 | Gamepad pad; |
| joshdavy | 1:37802772843e | 35 | Game game; |
| joshdavy | 9:96969b1c6bde | 36 | Music music; |
| joshdavy | 1:37802772843e | 37 | |
| joshdavy | 1:37802772843e | 38 | |
| joshdavy | 2:b62e8be35a5d | 39 | |
| joshdavy | 1:37802772843e | 40 | ///////////// prototypes /////////////// |
| joshdavy | 1:37802772843e | 41 | void init(); |
| joshdavy | 1:37802772843e | 42 | void welcome(); |
| joshdavy | 9:96969b1c6bde | 43 | void menu(); |
| joshdavy | 9:96969b1c6bde | 44 | void how_to_play(); |
| joshdavy | 9:96969b1c6bde | 45 | void you_win(); |
| joshdavy | 9:96969b1c6bde | 46 | int main(); |
| joshdavy | 1:37802772843e | 47 | // initialies all classes and libraries |
| joshdavy | 1:37802772843e | 48 | void init() |
| joshdavy | 2:b62e8be35a5d | 49 | |
| joshdavy | 1:37802772843e | 50 | { |
| joshdavy | 8:21b6d4dbce44 | 51 | |
| joshdavy | 8:21b6d4dbce44 | 52 | // need to initialise LCD and Gamepad |
| joshdavy | 1:37802772843e | 53 | lcd.init(); |
| joshdavy | 2:b62e8be35a5d | 54 | lcd.setContrast(0.4); |
| joshdavy | 1:37802772843e | 55 | pad.init(); |
| joshdavy | 9:96969b1c6bde | 56 | // Initalises Music with data from SoundData.h and game |
| joshdavy | 9:96969b1c6bde | 57 | music.init(data1,NUM_ELEMENTS); |
| joshdavy | 7:68e06dda79f7 | 58 | game.init(); |
| joshdavy | 3:b34685dbdb8d | 59 | |
| joshdavy | 1:37802772843e | 60 | } |
| joshdavy | 1:37802772843e | 61 | // simple splash screen displayed on start-up |
| joshdavy | 8:21b6d4dbce44 | 62 | void welcome() |
| joshdavy | 8:21b6d4dbce44 | 63 | { |
| joshdavy | 8:21b6d4dbce44 | 64 | |
| joshdavy | 9:96969b1c6bde | 65 | // Draws Splash Screen |
| joshdavy | 8:21b6d4dbce44 | 66 | lcd.drawSprite(0,0,48,84, (int *) splashScreen); |
| joshdavy | 1:37802772843e | 67 | lcd.refresh(); |
| joshdavy | 8:21b6d4dbce44 | 68 | |
| joshdavy | 8:21b6d4dbce44 | 69 | // wait flashing LEDs until start button is pressed |
| joshdavy | 9:96969b1c6bde | 70 | while (!pad.check_event(Gamepad::START_PRESSED)) { |
| joshdavy | 9:96969b1c6bde | 71 | pad.leds_on(); |
| joshdavy | 9:96969b1c6bde | 72 | wait(0.1); |
| joshdavy | 9:96969b1c6bde | 73 | pad.leds_off(); |
| joshdavy | 9:96969b1c6bde | 74 | wait(0.1); |
| joshdavy | 9:96969b1c6bde | 75 | } |
| joshdavy | 9:96969b1c6bde | 76 | |
| joshdavy | 9:96969b1c6bde | 77 | } |
| joshdavy | 9:96969b1c6bde | 78 | |
| joshdavy | 9:96969b1c6bde | 79 | void you_win() |
| joshdavy | 9:96969b1c6bde | 80 | { |
| joshdavy | 9:96969b1c6bde | 81 | |
| joshdavy | 9:96969b1c6bde | 82 | // Draws Win Screen |
| joshdavy | 9:96969b1c6bde | 83 | lcd.drawSprite(0,0,48,84, (int *) winScreen); |
| joshdavy | 9:96969b1c6bde | 84 | lcd.refresh(); |
| joshdavy | 9:96969b1c6bde | 85 | |
| joshdavy | 9:96969b1c6bde | 86 | // wait flashing LEDs until A button is pressed |
| joshdavy | 9:96969b1c6bde | 87 | while (!pad.check_event(Gamepad::A_PRESSED)) { |
| joshdavy | 1:37802772843e | 88 | pad.leds_on(); |
| joshdavy | 1:37802772843e | 89 | wait(0.1); |
| joshdavy | 1:37802772843e | 90 | pad.leds_off(); |
| joshdavy | 1:37802772843e | 91 | wait(0.1); |
| joshdavy | 1:37802772843e | 92 | } |
| joshdavy | 9:96969b1c6bde | 93 | |
| joshdavy | 9:96969b1c6bde | 94 | main(); |
| joshdavy | 2:b62e8be35a5d | 95 | |
| joshdavy | 1:37802772843e | 96 | } |
| joshdavy | 1:37802772843e | 97 | |
| joshdavy | 9:96969b1c6bde | 98 | |
| joshdavy | 8:21b6d4dbce44 | 99 | void how_to_play() |
| joshdavy | 8:21b6d4dbce44 | 100 | { |
| joshdavy | 9:96969b1c6bde | 101 | // Gives instructions on how to play |
| joshdavy | 9:96969b1c6bde | 102 | lcd.clear(); |
| joshdavy | 9:96969b1c6bde | 103 | lcd.printString("Use the joy -",0,0); |
| joshdavy | 9:96969b1c6bde | 104 | lcd.printString("stick to move.",0,1); |
| joshdavy | 9:96969b1c6bde | 105 | lcd.printString("Press A to ",0,2); |
| joshdavy | 9:96969b1c6bde | 106 | lcd.printString("flip gravity.",0,3); |
| joshdavy | 9:96969b1c6bde | 107 | lcd.printString("Get the flags",0,4); |
| joshdavy | 9:96969b1c6bde | 108 | lcd.printString("to win!",0,5); |
| joshdavy | 9:96969b1c6bde | 109 | lcd.refresh(); |
| joshdavy | 9:96969b1c6bde | 110 | |
| joshdavy | 9:96969b1c6bde | 111 | // Loop until A button pressed |
| joshdavy | 9:96969b1c6bde | 112 | while ( !pad.check_event(Gamepad::A_PRESSED) ) {} |
| joshdavy | 9:96969b1c6bde | 113 | |
| joshdavy | 9:96969b1c6bde | 114 | // Return to menu |
| joshdavy | 9:96969b1c6bde | 115 | menu(); |
| joshdavy | 8:21b6d4dbce44 | 116 | |
| joshdavy | 8:21b6d4dbce44 | 117 | } |
| joshdavy | 8:21b6d4dbce44 | 118 | |
| joshdavy | 8:21b6d4dbce44 | 119 | void menu() |
| joshdavy | 8:21b6d4dbce44 | 120 | { |
| joshdavy | 9:96969b1c6bde | 121 | // Main Menu |
| joshdavy | 8:21b6d4dbce44 | 122 | int menu_option = 1; |
| joshdavy | 8:21b6d4dbce44 | 123 | while ( !pad.check_event(Gamepad::A_PRESSED) ) { |
| joshdavy | 9:96969b1c6bde | 124 | |
| joshdavy | 9:96969b1c6bde | 125 | // Read the joystick value and relate it to a menu option |
| joshdavy | 9:96969b1c6bde | 126 | if (pad.get_coord().y > 0.7f) { |
| joshdavy | 8:21b6d4dbce44 | 127 | menu_option = 1; |
| joshdavy | 8:21b6d4dbce44 | 128 | } |
| joshdavy | 8:21b6d4dbce44 | 129 | if (pad.get_coord().y < -0.7f) { |
| joshdavy | 8:21b6d4dbce44 | 130 | menu_option = 0; |
| joshdavy | 8:21b6d4dbce44 | 131 | } |
| joshdavy | 9:96969b1c6bde | 132 | // Draws main menu background image |
| joshdavy | 8:21b6d4dbce44 | 133 | lcd.drawSprite(0,0,48,84, (int *) menuScreen); |
| joshdavy | 9:96969b1c6bde | 134 | |
| joshdavy | 9:96969b1c6bde | 135 | // Draws the arrow on the menu |
| joshdavy | 8:21b6d4dbce44 | 136 | if (menu_option == 1) { |
| joshdavy | 8:21b6d4dbce44 | 137 | lcd.drawSprite(40,10,7,4, (int *) arrow); |
| joshdavy | 8:21b6d4dbce44 | 138 | } else { |
| joshdavy | 8:21b6d4dbce44 | 139 | lcd.drawSprite(40,25,7,4, (int *) arrow); |
| joshdavy | 8:21b6d4dbce44 | 140 | } |
| joshdavy | 9:96969b1c6bde | 141 | |
| joshdavy | 8:21b6d4dbce44 | 142 | lcd.refresh(); |
| joshdavy | 8:21b6d4dbce44 | 143 | |
| joshdavy | 8:21b6d4dbce44 | 144 | } |
| joshdavy | 9:96969b1c6bde | 145 | // If the "how to play" option is selected run its display function |
| joshdavy | 9:96969b1c6bde | 146 | if (menu_option == 0) { |
| joshdavy | 8:21b6d4dbce44 | 147 | how_to_play(); |
| joshdavy | 8:21b6d4dbce44 | 148 | } |
| joshdavy | 8:21b6d4dbce44 | 149 | } |
| joshdavy | 8:21b6d4dbce44 | 150 | |
| joshdavy | 5:b9cf407bcc63 | 151 | |
| joshdavy | 1:37802772843e | 152 | int main() |
| joshdavy | 1:37802772843e | 153 | { |
| joshdavy | 8:21b6d4dbce44 | 154 | |
| joshdavy | 1:37802772843e | 155 | init(); // initialise and then display welcome screen... |
| joshdavy | 1:37802772843e | 156 | welcome(); // waiting for the user to start |
| joshdavy | 8:21b6d4dbce44 | 157 | menu(); // main menu |
| joshdavy | 1:37802772843e | 158 | |
| joshdavy | 1:37802772843e | 159 | // game loop - read input, update the game state and render the display |
| joshdavy | 7:68e06dda79f7 | 160 | |
| joshdavy | 7:68e06dda79f7 | 161 | game.init(); |
| joshdavy | 8:21b6d4dbce44 | 162 | |
| joshdavy | 9:96969b1c6bde | 163 | // Resets and starts the timers responsible for the game and music loop |
| joshdavy | 8:21b6d4dbce44 | 164 | game_timer.reset(); |
| joshdavy | 8:21b6d4dbce44 | 165 | music_timer.reset(); |
| joshdavy | 8:21b6d4dbce44 | 166 | game_timer.start(); |
| joshdavy | 8:21b6d4dbce44 | 167 | music_timer.start(); |
| joshdavy | 9:96969b1c6bde | 168 | |
| joshdavy | 9:96969b1c6bde | 169 | // The tick times for both timers |
| joshdavy | 8:21b6d4dbce44 | 170 | const int MUSIC_TICK_TIME = 100; //125 microseconds |
| joshdavy | 8:21b6d4dbce44 | 171 | const int GAME_TICK_TIME = 100; // 100 millieconds |
| joshdavy | 8:21b6d4dbce44 | 172 | |
| joshdavy | 9:96969b1c6bde | 173 | while (true) { |
| joshdavy | 9:96969b1c6bde | 174 | |
| joshdavy | 9:96969b1c6bde | 175 | // If the game timer is above the game tick time then main game process |
| joshdavy | 9:96969b1c6bde | 176 | // is ran |
| joshdavy | 8:21b6d4dbce44 | 177 | if (game_timer.read_ms() > GAME_TICK_TIME) { |
| joshdavy | 8:21b6d4dbce44 | 178 | game.update(pad); |
| joshdavy | 8:21b6d4dbce44 | 179 | game.draw(lcd); |
| joshdavy | 8:21b6d4dbce44 | 180 | game_timer.reset(); |
| joshdavy | 9:96969b1c6bde | 181 | if (game.game_won()) { |
| joshdavy | 9:96969b1c6bde | 182 | you_win(); |
| joshdavy | 9:96969b1c6bde | 183 | } |
| joshdavy | 8:21b6d4dbce44 | 184 | } |
| joshdavy | 9:96969b1c6bde | 185 | |
| joshdavy | 9:96969b1c6bde | 186 | // If the music timer is above the music tick time update the music object |
| joshdavy | 8:21b6d4dbce44 | 187 | if (music_timer.read_us() > MUSIC_TICK_TIME) { |
| joshdavy | 9:96969b1c6bde | 188 | music.play_next(); |
| joshdavy | 8:21b6d4dbce44 | 189 | music_timer.reset(); |
| joshdavy | 8:21b6d4dbce44 | 190 | } |
| joshdavy | 1:37802772843e | 191 | |
| joshdavy | 8:21b6d4dbce44 | 192 | |
| joshdavy | 8:21b6d4dbce44 | 193 | |
| joshdavy | 8:21b6d4dbce44 | 194 | } |
| joshdavy | 8:21b6d4dbce44 | 195 | } |