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@12:d3eef5ea3f43, 2018-05-07 (annotated)
- Committer:
- Andrew_M
- Date:
- Mon May 07 18:06:50 2018 +0000
- Revision:
- 12:d3eef5ea3f43
- Parent:
- 11:b25874e7efe4
- Child:
- 13:81573be8fac6
Functional level select system with 3 different levels, sprite added to main menu for decoration. Minor reorganising within classes was needed, mainly game engine
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| Andrew_M | 0:66e5b37c127e | 1 | /* |
| Andrew_M | 0:66e5b37c127e | 2 | ELEC2645 Embedded Systems Project |
| Andrew_M | 0:66e5b37c127e | 3 | School of Electronic & Electrical Engineering |
| Andrew_M | 0:66e5b37c127e | 4 | University of Leeds |
| Andrew_M | 2:9ca5e1c221c3 | 5 | Name: Andrew Moore |
| Andrew_M | 2:9ca5e1c221c3 | 6 | Username: el16ajm |
| Andrew_M | 2:9ca5e1c221c3 | 7 | Student ID Number: 201042893 |
| Andrew_M | 8:9d01fd4a63ad | 8 | Date: |
| Andrew_M | 0:66e5b37c127e | 9 | */ |
| Andrew_M | 0:66e5b37c127e | 10 | |
| Andrew_M | 0:66e5b37c127e | 11 | #include "mbed.h" |
| Andrew_M | 1:a14415de3ad5 | 12 | #include "Gamepad.h" |
| Andrew_M | 1:a14415de3ad5 | 13 | #include "N5110.h" |
| Andrew_M | 1:a14415de3ad5 | 14 | #include "Engine.h" |
| Andrew_M | 9:fe86ddbf7799 | 15 | #include "Menu.h" |
| Andrew_M | 0:66e5b37c127e | 16 | |
| Andrew_M | 1:a14415de3ad5 | 17 | /////////////// structs ///////////////// |
| Andrew_M | 1:a14415de3ad5 | 18 | struct UserInput { |
| Andrew_M | 1:a14415de3ad5 | 19 | Direction d; |
| Andrew_M | 1:a14415de3ad5 | 20 | }; |
| Andrew_M | 1:a14415de3ad5 | 21 | |
| Andrew_M | 1:a14415de3ad5 | 22 | /////////////// objects /////////////// |
| Andrew_M | 1:a14415de3ad5 | 23 | N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11); |
| Andrew_M | 1:a14415de3ad5 | 24 | Gamepad pad; |
| Andrew_M | 1:a14415de3ad5 | 25 | Engine gameEngine; |
| Andrew_M | 9:fe86ddbf7799 | 26 | Menu mainMenu; |
| Andrew_M | 0:66e5b37c127e | 27 | |
| Andrew_M | 1:a14415de3ad5 | 28 | ///////////// prototypes /////////////// |
| Andrew_M | 1:a14415de3ad5 | 29 | void init(); |
| Andrew_M | 1:a14415de3ad5 | 30 | void update_game(UserInput input); |
| Andrew_M | 9:fe86ddbf7799 | 31 | void renderGame(); |
| Andrew_M | 9:fe86ddbf7799 | 32 | void renderMenu(); |
| Andrew_M | 1:a14415de3ad5 | 33 | void welcome(); |
| Andrew_M | 10:279d3775d52c | 34 | void transition(); |
| Andrew_M | 12:d3eef5ea3f43 | 35 | void reset(); |
| Andrew_M | 11:b25874e7efe4 | 36 | float setDif(); |
| Andrew_M | 1:a14415de3ad5 | 37 | |
| Andrew_M | 1:a14415de3ad5 | 38 | ///////////// functions //////////////// |
| Andrew_M | 1:a14415de3ad5 | 39 | int main() |
| Andrew_M | 10:279d3775d52c | 40 | { |
| Andrew_M | 1:a14415de3ad5 | 41 | init(); |
| Andrew_M | 1:a14415de3ad5 | 42 | welcome(); |
| Andrew_M | 8:9d01fd4a63ad | 43 | |
| Andrew_M | 10:279d3775d52c | 44 | transition(); |
| Andrew_M | 11:b25874e7efe4 | 45 | |
| Andrew_M | 2:9ca5e1c221c3 | 46 | // game loop - read input, update the game state and render the display |
| Andrew_M | 2:9ca5e1c221c3 | 47 | while (1) { |
| Andrew_M | 10:279d3775d52c | 48 | if (!mainMenu.started()) { //menu logic and rendering |
| Andrew_M | 10:279d3775d52c | 49 | mainMenu.read_input(pad); |
| Andrew_M | 10:279d3775d52c | 50 | mainMenu.update(); |
| Andrew_M | 12:d3eef5ea3f43 | 51 | gameEngine.setLvl(mainMenu.getLvl()); |
| Andrew_M | 10:279d3775d52c | 52 | renderMenu(); |
| Andrew_M | 12:d3eef5ea3f43 | 53 | } else if (gameEngine.getGameOver()) { //resets the game once a game over state has been reached |
| Andrew_M | 12:d3eef5ea3f43 | 54 | gameEngine.gameOverScreen(lcd); |
| Andrew_M | 12:d3eef5ea3f43 | 55 | reset(); |
| Andrew_M | 10:279d3775d52c | 56 | } else { //game logic and rendering |
| Andrew_M | 9:fe86ddbf7799 | 57 | gameEngine.read_input(pad); |
| Andrew_M | 9:fe86ddbf7799 | 58 | gameEngine.update(pad); |
| Andrew_M | 9:fe86ddbf7799 | 59 | renderGame(); |
| Andrew_M | 9:fe86ddbf7799 | 60 | } |
| Andrew_M | 2:9ca5e1c221c3 | 61 | } |
| Andrew_M | 1:a14415de3ad5 | 62 | } |
| Andrew_M | 1:a14415de3ad5 | 63 | |
| Andrew_M | 1:a14415de3ad5 | 64 | void welcome() |
| Andrew_M | 1:a14415de3ad5 | 65 | { |
| Andrew_M | 8:9d01fd4a63ad | 66 | lcd.printString(" Snake! ",0,1); |
| Andrew_M | 1:a14415de3ad5 | 67 | lcd.printString(" Press Start ",0,4); |
| Andrew_M | 1:a14415de3ad5 | 68 | lcd.refresh(); |
| Andrew_M | 8:9d01fd4a63ad | 69 | |
| Andrew_M | 8:9d01fd4a63ad | 70 | // wait flashing LEDs until start button is pressed |
| Andrew_M | 1:a14415de3ad5 | 71 | while ( pad.check_event(Gamepad::START_PRESSED) == false) { |
| Andrew_M | 1:a14415de3ad5 | 72 | pad.leds_on(); |
| Andrew_M | 1:a14415de3ad5 | 73 | wait(0.1); |
| Andrew_M | 1:a14415de3ad5 | 74 | pad.leds_off(); |
| Andrew_M | 1:a14415de3ad5 | 75 | wait(0.1); |
| Andrew_M | 0:66e5b37c127e | 76 | } |
| Andrew_M | 9:fe86ddbf7799 | 77 | |
| Andrew_M | 0:66e5b37c127e | 78 | } |
| Andrew_M | 0:66e5b37c127e | 79 | |
| Andrew_M | 1:a14415de3ad5 | 80 | void init() |
| Andrew_M | 1:a14415de3ad5 | 81 | { |
| Andrew_M | 8:9d01fd4a63ad | 82 | // need to initialise LCD and Gamepad |
| Andrew_M | 3:6253a2d374fa | 83 | gameEngine.init(); |
| Andrew_M | 1:a14415de3ad5 | 84 | lcd.init(); |
| Andrew_M | 8:9d01fd4a63ad | 85 | pad.init(); |
| Andrew_M | 9:fe86ddbf7799 | 86 | mainMenu.init(); |
| Andrew_M | 1:a14415de3ad5 | 87 | |
| Andrew_M | 1:a14415de3ad5 | 88 | } |
| Andrew_M | 2:9ca5e1c221c3 | 89 | |
| Andrew_M | 12:d3eef5ea3f43 | 90 | void reset() |
| Andrew_M | 12:d3eef5ea3f43 | 91 | { |
| Andrew_M | 12:d3eef5ea3f43 | 92 | mainMenu.init(); |
| Andrew_M | 12:d3eef5ea3f43 | 93 | gameEngine.init(); |
| Andrew_M | 12:d3eef5ea3f43 | 94 | } |
| Andrew_M | 12:d3eef5ea3f43 | 95 | |
| Andrew_M | 9:fe86ddbf7799 | 96 | void renderGame() |
| Andrew_M | 2:9ca5e1c221c3 | 97 | { |
| Andrew_M | 2:9ca5e1c221c3 | 98 | // clear screen, re-draw and refresh |
| Andrew_M | 8:9d01fd4a63ad | 99 | lcd.clear(); |
| Andrew_M | 2:9ca5e1c221c3 | 100 | gameEngine.draw(lcd); |
| Andrew_M | 2:9ca5e1c221c3 | 101 | lcd.refresh(); |
| Andrew_M | 11:b25874e7efe4 | 102 | wait(setDif()); |
| Andrew_M | 2:9ca5e1c221c3 | 103 | } |
| Andrew_M | 8:9d01fd4a63ad | 104 | |
| Andrew_M | 9:fe86ddbf7799 | 105 | void renderMenu() |
| Andrew_M | 9:fe86ddbf7799 | 106 | { |
| Andrew_M | 9:fe86ddbf7799 | 107 | // clear screen, re-draw and refresh |
| Andrew_M | 9:fe86ddbf7799 | 108 | lcd.clear(); |
| Andrew_M | 9:fe86ddbf7799 | 109 | mainMenu.draw(lcd); |
| Andrew_M | 9:fe86ddbf7799 | 110 | lcd.refresh(); |
| Andrew_M | 9:fe86ddbf7799 | 111 | wait(1.0f/8); |
| Andrew_M | 9:fe86ddbf7799 | 112 | } |
| Andrew_M | 9:fe86ddbf7799 | 113 | |
| Andrew_M | 10:279d3775d52c | 114 | void transition() |
| Andrew_M | 11:b25874e7efe4 | 115 | { |
| Andrew_M | 11:b25874e7efe4 | 116 | //transition animation between modes to stop 'ghosting' of inputs |
| Andrew_M | 10:279d3775d52c | 117 | for (int i = 0; i <= 11; i++) { |
| Andrew_M | 10:279d3775d52c | 118 | for (int j = 0; j <= 21; j++) { |
| Andrew_M | 10:279d3775d52c | 119 | lcd.drawRect(j*4,i*4,4,4,FILL_BLACK); |
| Andrew_M | 10:279d3775d52c | 120 | wait(0.005); |
| Andrew_M | 10:279d3775d52c | 121 | lcd.refresh(); |
| Andrew_M | 10:279d3775d52c | 122 | } |
| Andrew_M | 10:279d3775d52c | 123 | } |
| Andrew_M | 10:279d3775d52c | 124 | |
| Andrew_M | 10:279d3775d52c | 125 | } |
| Andrew_M | 10:279d3775d52c | 126 | |
| Andrew_M | 11:b25874e7efe4 | 127 | float setDif() |
| Andrew_M | 11:b25874e7efe4 | 128 | { |
| Andrew_M | 11:b25874e7efe4 | 129 | int _dif = mainMenu.getDif(); |
| Andrew_M | 11:b25874e7efe4 | 130 | |
| Andrew_M | 11:b25874e7efe4 | 131 | if (_dif == 1) { |
| Andrew_M | 11:b25874e7efe4 | 132 | return 1.0f/4; |
| Andrew_M | 11:b25874e7efe4 | 133 | } else if (_dif == 2) { |
| Andrew_M | 11:b25874e7efe4 | 134 | return 1.0f/8; |
| Andrew_M | 11:b25874e7efe4 | 135 | } else if (_dif == 3) { |
| Andrew_M | 11:b25874e7efe4 | 136 | return 1.0f/12; |
| Andrew_M | 12:d3eef5ea3f43 | 137 | } else if (_dif == 4) { |
| Andrew_M | 11:b25874e7efe4 | 138 | int _delta = gameEngine.getScore(); |
| Andrew_M | 11:b25874e7efe4 | 139 | if (_delta < 38) { |
| Andrew_M | 11:b25874e7efe4 | 140 | return ((1.0f/(_delta/2)) + 6); |
| Andrew_M | 11:b25874e7efe4 | 141 | } else { |
| Andrew_M | 11:b25874e7efe4 | 142 | return (1.0f/25); |
| Andrew_M | 11:b25874e7efe4 | 143 | } |
| Andrew_M | 11:b25874e7efe4 | 144 | } |
| Andrew_M | 11:b25874e7efe4 | 145 | return 1.0f; |
| Andrew_M | 11:b25874e7efe4 | 146 | } |