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@16:4d329ce7b156, 2018-05-08 (annotated)
- Committer:
- Andrew_M
- Date:
- Tue May 08 14:41:56 2018 +0000
- Revision:
- 16:4d329ce7b156
- Parent:
- 15:130900e5c268
Added testing functions, found and fixed an error in the food placement (double Y cor not X and Y)
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 | 14:a57a40ff9430 | 11 | /** The Main class |
| Andrew_M | 14:a57a40ff9430 | 12 | * @brief Sends and recives data from the Menu and Engine classes, alters the refresh rate and calls all drawing from other classes |
| Andrew_M | 14:a57a40ff9430 | 13 | * @author Andrew J. Moore |
| Andrew_M | 14:a57a40ff9430 | 14 | * @date May, 2018 |
| Andrew_M | 14:a57a40ff9430 | 15 | */ |
| Andrew_M | 14:a57a40ff9430 | 16 | |
| Andrew_M | 0:66e5b37c127e | 17 | #include "mbed.h" |
| Andrew_M | 1:a14415de3ad5 | 18 | #include "Gamepad.h" |
| Andrew_M | 1:a14415de3ad5 | 19 | #include "N5110.h" |
| Andrew_M | 1:a14415de3ad5 | 20 | #include "Engine.h" |
| Andrew_M | 9:fe86ddbf7799 | 21 | #include "Menu.h" |
| Andrew_M | 0:66e5b37c127e | 22 | |
| Andrew_M | 1:a14415de3ad5 | 23 | /////////////// structs ///////////////// |
| Andrew_M | 1:a14415de3ad5 | 24 | struct UserInput { |
| Andrew_M | 1:a14415de3ad5 | 25 | Direction d; |
| Andrew_M | 1:a14415de3ad5 | 26 | }; |
| Andrew_M | 1:a14415de3ad5 | 27 | |
| Andrew_M | 1:a14415de3ad5 | 28 | /////////////// objects /////////////// |
| Andrew_M | 1:a14415de3ad5 | 29 | N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11); |
| Andrew_M | 1:a14415de3ad5 | 30 | Gamepad pad; |
| Andrew_M | 1:a14415de3ad5 | 31 | Engine gameEngine; |
| Andrew_M | 9:fe86ddbf7799 | 32 | Menu mainMenu; |
| Andrew_M | 0:66e5b37c127e | 33 | |
| Andrew_M | 1:a14415de3ad5 | 34 | ///////////// prototypes /////////////// |
| Andrew_M | 14:a57a40ff9430 | 35 | /** Initialization function */ |
| Andrew_M | 1:a14415de3ad5 | 36 | void init(); |
| Andrew_M | 14:a57a40ff9430 | 37 | |
| Andrew_M | 14:a57a40ff9430 | 38 | /** Initialization function */ |
| Andrew_M | 1:a14415de3ad5 | 39 | void update_game(UserInput input); |
| Andrew_M | 14:a57a40ff9430 | 40 | |
| Andrew_M | 14:a57a40ff9430 | 41 | /** Draws the game onto the LCD */ |
| Andrew_M | 9:fe86ddbf7799 | 42 | void renderGame(); |
| Andrew_M | 14:a57a40ff9430 | 43 | |
| Andrew_M | 14:a57a40ff9430 | 44 | /** Draws the menu onto the LCD */ |
| Andrew_M | 9:fe86ddbf7799 | 45 | void renderMenu(); |
| Andrew_M | 14:a57a40ff9430 | 46 | |
| Andrew_M | 14:a57a40ff9430 | 47 | /** Draws the welcome screen onto the LCD */ |
| Andrew_M | 1:a14415de3ad5 | 48 | void welcome(); |
| Andrew_M | 14:a57a40ff9430 | 49 | |
| Andrew_M | 14:a57a40ff9430 | 50 | /** Draws the transition screen onto the LCD */ |
| Andrew_M | 10:279d3775d52c | 51 | void transition(); |
| Andrew_M | 14:a57a40ff9430 | 52 | |
| Andrew_M | 14:a57a40ff9430 | 53 | /** Resets game and menu logic once a game over state has been reached */ |
| Andrew_M | 12:d3eef5ea3f43 | 54 | void reset(); |
| Andrew_M | 14:a57a40ff9430 | 55 | |
| Andrew_M | 14:a57a40ff9430 | 56 | /** Sets the difficulty/refresh rate */ |
| Andrew_M | 11:b25874e7efe4 | 57 | float setDif(); |
| Andrew_M | 1:a14415de3ad5 | 58 | |
| Andrew_M | 1:a14415de3ad5 | 59 | ///////////// functions //////////////// |
| Andrew_M | 1:a14415de3ad5 | 60 | int main() |
| Andrew_M | 10:279d3775d52c | 61 | { |
| Andrew_M | 16:4d329ce7b156 | 62 | printf("Loaded\n"); |
| Andrew_M | 1:a14415de3ad5 | 63 | init(); |
| Andrew_M | 1:a14415de3ad5 | 64 | welcome(); |
| Andrew_M | 8:9d01fd4a63ad | 65 | |
| Andrew_M | 10:279d3775d52c | 66 | transition(); |
| Andrew_M | 11:b25874e7efe4 | 67 | |
| Andrew_M | 2:9ca5e1c221c3 | 68 | // game loop - read input, update the game state and render the display |
| Andrew_M | 2:9ca5e1c221c3 | 69 | while (1) { |
| Andrew_M | 10:279d3775d52c | 70 | if (!mainMenu.started()) { //menu logic and rendering |
| Andrew_M | 10:279d3775d52c | 71 | mainMenu.read_input(pad); |
| Andrew_M | 10:279d3775d52c | 72 | mainMenu.update(); |
| Andrew_M | 12:d3eef5ea3f43 | 73 | gameEngine.setLvl(mainMenu.getLvl()); |
| Andrew_M | 10:279d3775d52c | 74 | renderMenu(); |
| Andrew_M | 12:d3eef5ea3f43 | 75 | } else if (gameEngine.getGameOver()) { //resets the game once a game over state has been reached |
| Andrew_M | 12:d3eef5ea3f43 | 76 | gameEngine.gameOverScreen(lcd); |
| Andrew_M | 12:d3eef5ea3f43 | 77 | reset(); |
| Andrew_M | 10:279d3775d52c | 78 | } else { //game logic and rendering |
| Andrew_M | 9:fe86ddbf7799 | 79 | gameEngine.read_input(pad); |
| Andrew_M | 9:fe86ddbf7799 | 80 | gameEngine.update(pad); |
| Andrew_M | 9:fe86ddbf7799 | 81 | renderGame(); |
| Andrew_M | 9:fe86ddbf7799 | 82 | } |
| Andrew_M | 2:9ca5e1c221c3 | 83 | } |
| Andrew_M | 1:a14415de3ad5 | 84 | } |
| Andrew_M | 1:a14415de3ad5 | 85 | |
| Andrew_M | 1:a14415de3ad5 | 86 | void welcome() |
| Andrew_M | 1:a14415de3ad5 | 87 | { |
| Andrew_M | 16:4d329ce7b156 | 88 | printf("Welcome Screen\n"); |
| Andrew_M | 16:4d329ce7b156 | 89 | |
| Andrew_M | 8:9d01fd4a63ad | 90 | lcd.printString(" Snake! ",0,1); |
| Andrew_M | 1:a14415de3ad5 | 91 | lcd.printString(" Press Start ",0,4); |
| Andrew_M | 1:a14415de3ad5 | 92 | lcd.refresh(); |
| Andrew_M | 8:9d01fd4a63ad | 93 | |
| Andrew_M | 8:9d01fd4a63ad | 94 | // wait flashing LEDs until start button is pressed |
| Andrew_M | 1:a14415de3ad5 | 95 | while ( pad.check_event(Gamepad::START_PRESSED) == false) { |
| Andrew_M | 1:a14415de3ad5 | 96 | pad.leds_on(); |
| Andrew_M | 1:a14415de3ad5 | 97 | wait(0.1); |
| Andrew_M | 1:a14415de3ad5 | 98 | pad.leds_off(); |
| Andrew_M | 1:a14415de3ad5 | 99 | wait(0.1); |
| Andrew_M | 0:66e5b37c127e | 100 | } |
| Andrew_M | 15:130900e5c268 | 101 | wait(0.1); |
| Andrew_M | 9:fe86ddbf7799 | 102 | |
| Andrew_M | 0:66e5b37c127e | 103 | } |
| Andrew_M | 0:66e5b37c127e | 104 | |
| Andrew_M | 1:a14415de3ad5 | 105 | void init() |
| Andrew_M | 1:a14415de3ad5 | 106 | { |
| Andrew_M | 8:9d01fd4a63ad | 107 | // need to initialise LCD and Gamepad |
| Andrew_M | 3:6253a2d374fa | 108 | gameEngine.init(); |
| Andrew_M | 1:a14415de3ad5 | 109 | lcd.init(); |
| Andrew_M | 8:9d01fd4a63ad | 110 | pad.init(); |
| Andrew_M | 9:fe86ddbf7799 | 111 | mainMenu.init(); |
| Andrew_M | 16:4d329ce7b156 | 112 | |
| Andrew_M | 16:4d329ce7b156 | 113 | printf("Initialised\n"); |
| Andrew_M | 1:a14415de3ad5 | 114 | |
| Andrew_M | 1:a14415de3ad5 | 115 | } |
| Andrew_M | 2:9ca5e1c221c3 | 116 | |
| Andrew_M | 12:d3eef5ea3f43 | 117 | void reset() |
| Andrew_M | 12:d3eef5ea3f43 | 118 | { |
| Andrew_M | 15:130900e5c268 | 119 | mainMenu.init(); //resets both the mainMenu and gameEngine |
| Andrew_M | 15:130900e5c268 | 120 | gameEngine.init(); //the gameEngine will reset the food and the snake in the process |
| Andrew_M | 16:4d329ce7b156 | 121 | |
| Andrew_M | 16:4d329ce7b156 | 122 | printf("Reset\n"); |
| Andrew_M | 12:d3eef5ea3f43 | 123 | } |
| Andrew_M | 12:d3eef5ea3f43 | 124 | |
| Andrew_M | 9:fe86ddbf7799 | 125 | void renderGame() |
| Andrew_M | 2:9ca5e1c221c3 | 126 | { |
| Andrew_M | 2:9ca5e1c221c3 | 127 | // clear screen, re-draw and refresh |
| Andrew_M | 8:9d01fd4a63ad | 128 | lcd.clear(); |
| Andrew_M | 2:9ca5e1c221c3 | 129 | gameEngine.draw(lcd); |
| Andrew_M | 2:9ca5e1c221c3 | 130 | lcd.refresh(); |
| Andrew_M | 15:130900e5c268 | 131 | wait(setDif()); //adaptive fps depending on difficulty |
| Andrew_M | 16:4d329ce7b156 | 132 | |
| Andrew_M | 16:4d329ce7b156 | 133 | printf("Game Rending\n"); |
| Andrew_M | 2:9ca5e1c221c3 | 134 | } |
| Andrew_M | 8:9d01fd4a63ad | 135 | |
| Andrew_M | 9:fe86ddbf7799 | 136 | void renderMenu() |
| Andrew_M | 9:fe86ddbf7799 | 137 | { |
| Andrew_M | 9:fe86ddbf7799 | 138 | // clear screen, re-draw and refresh |
| Andrew_M | 9:fe86ddbf7799 | 139 | lcd.clear(); |
| Andrew_M | 9:fe86ddbf7799 | 140 | mainMenu.draw(lcd); |
| Andrew_M | 9:fe86ddbf7799 | 141 | lcd.refresh(); |
| Andrew_M | 15:130900e5c268 | 142 | wait(1.0f/8); //fixed at 8 fps |
| Andrew_M | 16:4d329ce7b156 | 143 | |
| Andrew_M | 16:4d329ce7b156 | 144 | printf("Menu Rending\n"); |
| Andrew_M | 9:fe86ddbf7799 | 145 | } |
| Andrew_M | 9:fe86ddbf7799 | 146 | |
| Andrew_M | 10:279d3775d52c | 147 | void transition() |
| Andrew_M | 11:b25874e7efe4 | 148 | { |
| Andrew_M | 15:130900e5c268 | 149 | //transition animation between modes to reduce 'ghosting' of inputs |
| Andrew_M | 15:130900e5c268 | 150 | |
| Andrew_M | 16:4d329ce7b156 | 151 | printf("Transisitoning\n"); |
| Andrew_M | 16:4d329ce7b156 | 152 | |
| Andrew_M | 15:130900e5c268 | 153 | for (int i = 0; i <= 11; i++) { //max values for i and j were chosen as the screen is 84x44 and the sqaures being drawn are 4x4 |
| Andrew_M | 10:279d3775d52c | 154 | for (int j = 0; j <= 21; j++) { |
| Andrew_M | 15:130900e5c268 | 155 | lcd.drawRect(j*4,i*4,4,4,FILL_BLACK); //covers the entire screen in 4x4 squares |
| Andrew_M | 15:130900e5c268 | 156 | wait(0.005); //waits a small amount of time to allow the screen to keep up |
| Andrew_M | 10:279d3775d52c | 157 | lcd.refresh(); |
| Andrew_M | 10:279d3775d52c | 158 | } |
| Andrew_M | 10:279d3775d52c | 159 | } |
| Andrew_M | 10:279d3775d52c | 160 | |
| Andrew_M | 10:279d3775d52c | 161 | } |
| Andrew_M | 10:279d3775d52c | 162 | |
| Andrew_M | 11:b25874e7efe4 | 163 | float setDif() |
| Andrew_M | 11:b25874e7efe4 | 164 | { |
| Andrew_M | 11:b25874e7efe4 | 165 | int _dif = mainMenu.getDif(); |
| Andrew_M | 11:b25874e7efe4 | 166 | |
| Andrew_M | 15:130900e5c268 | 167 | if (_dif == 1) { //Low difficulty |
| Andrew_M | 11:b25874e7efe4 | 168 | return 1.0f/4; |
| Andrew_M | 15:130900e5c268 | 169 | } else if (_dif == 2) { //Medium difficulty |
| Andrew_M | 11:b25874e7efe4 | 170 | return 1.0f/8; |
| Andrew_M | 15:130900e5c268 | 171 | } else if (_dif == 3) { //Hard difficulty |
| Andrew_M | 11:b25874e7efe4 | 172 | return 1.0f/12; |
| Andrew_M | 15:130900e5c268 | 173 | } else { //Adaptive difficulty |
| Andrew_M | 13:81573be8fac6 | 174 | float _delta = gameEngine.getScore(); |
| Andrew_M | 16:4d329ce7b156 | 175 | |
| Andrew_M | 16:4d329ce7b156 | 176 | printf("Delta %4.2f\n", _delta); |
| Andrew_M | 16:4d329ce7b156 | 177 | |
| Andrew_M | 15:130900e5c268 | 178 | if (_delta < 25) { //Limits FPS to 25 |
| Andrew_M | 15:130900e5c268 | 179 | return ((1.0f/(_delta + 1))); //changes the fps depending on score |
| Andrew_M | 11:b25874e7efe4 | 180 | } else { |
| Andrew_M | 11:b25874e7efe4 | 181 | return (1.0f/25); |
| Andrew_M | 11:b25874e7efe4 | 182 | } |
| Andrew_M | 11:b25874e7efe4 | 183 | } |
| Andrew_M | 11:b25874e7efe4 | 184 | } |