ELEC2645 (2019/20) / Mbed 2 deprecated ELEC2645_Project_el18s2a_2

Dependencies:   mbed

Committer:
Psy1990
Date:
Thu Jun 04 21:04:55 2020 +0000
Revision:
7:9bd49beccdd1
Parent:
6:8e2fca142827
Child:
8:32825d724856
Started to move code into classes have started with the SnakeEngine class which currently draws the game area that was previously in the main.cpp and shows the score.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Psy1990 1:09a835e6d063 1 /*
Psy1990 1:09a835e6d063 2 ELEC2645 Embedded Systems Project
Psy1990 1:09a835e6d063 3 School of Electronic & Electrical Engineering
Psy1990 1:09a835e6d063 4 University of Leeds
Psy1990 1:09a835e6d063 5 2019/20
Psy1990 1:09a835e6d063 6
Psy1990 1:09a835e6d063 7 Name: Simon Thackray Atkinson
Psy1990 1:09a835e6d063 8 Username: el18s2a
Psy1990 1:09a835e6d063 9 Student ID Number: 201255483
Psy1990 1:09a835e6d063 10 Date: 05/03/2020
Psy1990 1:09a835e6d063 11 */
Psy1990 1:09a835e6d063 12
eencae 0:7423345f87c5 13 ///////// pre-processor directives ////////
eencae 0:7423345f87c5 14 #include "mbed.h"
eencae 0:7423345f87c5 15 #include "Gamepad.h"
eencae 0:7423345f87c5 16 #include "N5110.h"
Psy1990 6:8e2fca142827 17 #include "Bitmap.h"
Psy1990 7:9bd49beccdd1 18 #include "SnakeEngine.h"
Psy1990 7:9bd49beccdd1 19
Psy1990 7:9bd49beccdd1 20 #define SCORE 2
Psy1990 7:9bd49beccdd1 21
Psy1990 7:9bd49beccdd1 22
Psy1990 7:9bd49beccdd1 23 /////////////// structs /////////////////
Psy1990 7:9bd49beccdd1 24 struct UserInput {
Psy1990 7:9bd49beccdd1 25 Direction d;
Psy1990 7:9bd49beccdd1 26 float mag;
Psy1990 7:9bd49beccdd1 27 };
Psy1990 7:9bd49beccdd1 28 /////////////// objects ///////////////
Psy1990 7:9bd49beccdd1 29 N5110 lcd;
Psy1990 7:9bd49beccdd1 30 Gamepad pad;
Psy1990 7:9bd49beccdd1 31 SnakeEngine snake;
Psy1990 7:9bd49beccdd1 32
Psy1990 7:9bd49beccdd1 33 ///////////// prototypes ///////////////
Psy1990 7:9bd49beccdd1 34 void init();
Psy1990 7:9bd49beccdd1 35 void update_game(UserInput input);
Psy1990 7:9bd49beccdd1 36 void render();
Psy1990 7:9bd49beccdd1 37 void welcome();
Psy1990 7:9bd49beccdd1 38
Psy1990 7:9bd49beccdd1 39 ///////////// functions ////////////////
Psy1990 7:9bd49beccdd1 40 int main()
Psy1990 7:9bd49beccdd1 41 {
Psy1990 7:9bd49beccdd1 42
Psy1990 7:9bd49beccdd1 43
Psy1990 7:9bd49beccdd1 44 int fps = 6; // frames per second
Psy1990 7:9bd49beccdd1 45
Psy1990 7:9bd49beccdd1 46 init(); // initialise and then display welcome screen...
Psy1990 7:9bd49beccdd1 47 welcome(); // waiting for the user to start
Psy1990 7:9bd49beccdd1 48
Psy1990 7:9bd49beccdd1 49 render(); // first draw the initial frame
Psy1990 7:9bd49beccdd1 50 wait(1.0f/fps); // and wait for one frame period
Psy1990 7:9bd49beccdd1 51
Psy1990 7:9bd49beccdd1 52
Psy1990 7:9bd49beccdd1 53 // game loop - read input, update the game state and render the display
Psy1990 7:9bd49beccdd1 54 while (1) {
Psy1990 7:9bd49beccdd1 55 snake.read_input(pad);
Psy1990 7:9bd49beccdd1 56 snake.update(pad);
Psy1990 7:9bd49beccdd1 57 render();
Psy1990 7:9bd49beccdd1 58 wait(1.0f/fps);
Psy1990 7:9bd49beccdd1 59 }
Psy1990 7:9bd49beccdd1 60 }
Psy1990 7:9bd49beccdd1 61
Psy1990 7:9bd49beccdd1 62 // initialies all classes and libraries
Psy1990 7:9bd49beccdd1 63 void init()
Psy1990 7:9bd49beccdd1 64 {
Psy1990 7:9bd49beccdd1 65 // need to initialise LCD and Gamepad
Psy1990 7:9bd49beccdd1 66 lcd.init();
Psy1990 7:9bd49beccdd1 67 pad.init();
Psy1990 7:9bd49beccdd1 68
Psy1990 7:9bd49beccdd1 69
Psy1990 7:9bd49beccdd1 70
Psy1990 7:9bd49beccdd1 71 }
Psy1990 7:9bd49beccdd1 72
Psy1990 7:9bd49beccdd1 73 // this function draws each frame on the LCD
Psy1990 7:9bd49beccdd1 74 void render()
Psy1990 7:9bd49beccdd1 75 {
Psy1990 7:9bd49beccdd1 76 // clear screen, re-draw and refresh
Psy1990 7:9bd49beccdd1 77 lcd.clear();
Psy1990 7:9bd49beccdd1 78 snake.draw(lcd);
Psy1990 7:9bd49beccdd1 79 lcd.refresh();
Psy1990 7:9bd49beccdd1 80 }
Psy1990 7:9bd49beccdd1 81
Psy1990 7:9bd49beccdd1 82 // simple splash screen displayed on start-up
Psy1990 7:9bd49beccdd1 83 void welcome() {
Psy1990 7:9bd49beccdd1 84 pad.led(3,1); // Only Show Green LEDS
Psy1990 7:9bd49beccdd1 85 pad.led(6,1); //
Psy1990 7:9bd49beccdd1 86
Psy1990 7:9bd49beccdd1 87 // main menu screen no interaction yet pressing start won't do anything but its a start if you pardon the pun!
Psy1990 7:9bd49beccdd1 88 lcd.clear();
Psy1990 7:9bd49beccdd1 89 lcd.printString(" Welcome to ",0,1);
Psy1990 7:9bd49beccdd1 90 lcd.printString(" Snake! ",0,2);
Psy1990 7:9bd49beccdd1 91 lcd.printString(" Press Start ",0,4);
Psy1990 7:9bd49beccdd1 92 lcd.refresh();
Psy1990 7:9bd49beccdd1 93 lcd.clear();
Psy1990 7:9bd49beccdd1 94 while ( pad.start_pressed() == false) {
Psy1990 7:9bd49beccdd1 95 }
Psy1990 7:9bd49beccdd1 96
Psy1990 7:9bd49beccdd1 97 }
eencae 0:7423345f87c5 98
eencae 0:7423345f87c5 99
eencae 0:7423345f87c5 100
Psy1990 5:e63e1024294e 101
Psy1990 7:9bd49beccdd1 102 /*
Psy1990 1:09a835e6d063 103
eencae 0:7423345f87c5 104
Psy1990 6:8e2fca142827 105 bool endGame;
Psy1990 6:8e2fca142827 106 int borderW, borderH, appleX, appleY, snakeX, snakeY, score;
Psy1990 3:bd4c7eccde17 107 enum eDirection {STOP = 0, LEFT, RIGHT, UP, DOWN};
Psy1990 3:bd4c7eccde17 108 eDirection dir;
Psy1990 6:8e2fca142827 109
Psy1990 4:07aada4c8421 110
Psy1990 3:bd4c7eccde17 111
Psy1990 6:8e2fca142827 112 void setup() // Sets up the values for the game
Psy1990 3:bd4c7eccde17 113 {
Psy1990 6:8e2fca142827 114 srand(time(0)); // Seed the generator, give it a starting value
Psy1990 6:8e2fca142827 115 endGame = false; // Sets the game as not finished
Psy1990 5:e63e1024294e 116 dir = STOP; // When the game starts the snake isn't moving
Psy1990 6:8e2fca142827 117
Psy1990 6:8e2fca142827 118 borderW = WIDTH; // Border width and height
Psy1990 6:8e2fca142827 119 borderH = HEIGHT-8; // Uses - 8 so we can get a line of text for the score
Psy1990 6:8e2fca142827 120
Psy1990 5:e63e1024294e 121 snakeX = borderW/2; // Snake Starts at Center
Psy1990 5:e63e1024294e 122 snakeY = borderH/2+8;
Psy1990 6:8e2fca142827 123
Psy1990 6:8e2fca142827 124 appleX = rand() % borderW; // Apple position
Psy1990 5:e63e1024294e 125 appleY = rand() % borderH;
Psy1990 6:8e2fca142827 126
Psy1990 6:8e2fca142827 127 score = 0; // Sets the initial Score to 0
Psy1990 6:8e2fca142827 128
Psy1990 3:bd4c7eccde17 129 }
Psy1990 3:bd4c7eccde17 130
Psy1990 6:8e2fca142827 131 void display() // Deals with the fuctions of the display
Psy1990 6:8e2fca142827 132 {
Psy1990 6:8e2fca142827 133 lcd.clear();
Psy1990 6:8e2fca142827 134 lcd.drawRect(0,8,borderW,borderH,FILL_TRANSPARENT); // Draws a border showing the game area
Psy1990 6:8e2fca142827 135
Psy1990 6:8e2fca142827 136 char buffer[14]; // each character is 6 pixels wide, screen is 84 pixels (84/6 = 14)
Psy1990 6:8e2fca142827 137 int ScoreLength = sprintf(buffer,"Score:%2d",score); // Shows the player the score
Psy1990 5:e63e1024294e 138 if (ScoreLength <= 14)
Psy1990 6:8e2fca142827 139 lcd.printString(buffer,WIDTH/2-25,0); // centers the score
Psy1990 5:e63e1024294e 140
Psy1990 6:8e2fca142827 141
Psy1990 6:8e2fca142827 142 lcd.drawRect(snakeX,snakeY,5,5,FILL_BLACK); // Draws the initial snake head
Psy1990 5:e63e1024294e 143
Psy1990 5:e63e1024294e 144
Psy1990 6:8e2fca142827 145 lcd.drawCircle(appleX,appleY,2,FILL_BLACK); // Draws a circle representing an apple
Psy1990 5:e63e1024294e 146
Psy1990 3:bd4c7eccde17 147 lcd.refresh();
Psy1990 3:bd4c7eccde17 148
Psy1990 5:e63e1024294e 149
Psy1990 5:e63e1024294e 150 }
Psy1990 3:bd4c7eccde17 151
Psy1990 6:8e2fca142827 152 void control() // Reads the controls
Psy1990 6:8e2fca142827 153 {
Psy1990 6:8e2fca142827 154 if (pad.A_pressed()){
Psy1990 6:8e2fca142827 155 dir = RIGHT;
Psy1990 6:8e2fca142827 156 }
Psy1990 6:8e2fca142827 157 if (pad.B_pressed()){
Psy1990 6:8e2fca142827 158 dir = DOWN;
Psy1990 6:8e2fca142827 159 }
Psy1990 6:8e2fca142827 160 if (pad.X_pressed()){
Psy1990 6:8e2fca142827 161 dir = UP;
Psy1990 6:8e2fca142827 162 }
Psy1990 6:8e2fca142827 163 if (pad.Y_pressed()){
Psy1990 6:8e2fca142827 164 dir = LEFT;
Psy1990 7:9bd49beccdd1 165 }
Psy1990 7:9bd49beccdd1 166 if (pad.Y_pressed()){
Psy1990 7:9bd49beccdd1 167 dir = LEFT;
Psy1990 7:9bd49beccdd1 168 }
Psy1990 7:9bd49beccdd1 169 if (pad.start_pressed()){ // Stops the Movement
Psy1990 7:9bd49beccdd1 170 dir = STOP;
Psy1990 7:9bd49beccdd1 171 }
Psy1990 7:9bd49beccdd1 172
Psy1990 7:9bd49beccdd1 173
Psy1990 6:8e2fca142827 174
Psy1990 6:8e2fca142827 175 }
Psy1990 6:8e2fca142827 176
Psy1990 6:8e2fca142827 177 void gameplay() {
Psy1990 6:8e2fca142827 178
Psy1990 6:8e2fca142827 179 // Change the direction the snake goes dependant on the input
Psy1990 6:8e2fca142827 180
Psy1990 6:8e2fca142827 181 if (dir == UP) {
Psy1990 6:8e2fca142827 182 --snakeY;
Psy1990 6:8e2fca142827 183 }
Psy1990 6:8e2fca142827 184 if (dir == DOWN) {
Psy1990 6:8e2fca142827 185 ++snakeY;
Psy1990 6:8e2fca142827 186 }
Psy1990 6:8e2fca142827 187 if (dir == LEFT) {
Psy1990 6:8e2fca142827 188 --snakeX;
Psy1990 6:8e2fca142827 189 }
Psy1990 6:8e2fca142827 190 if (dir == RIGHT) {
Psy1990 6:8e2fca142827 191 ++snakeX;
Psy1990 6:8e2fca142827 192 }
Psy1990 6:8e2fca142827 193
Psy1990 6:8e2fca142827 194
Psy1990 6:8e2fca142827 195
Psy1990 6:8e2fca142827 196 }
Psy1990 4:07aada4c8421 197
Psy1990 6:8e2fca142827 198
Psy1990 7:9bd49beccdd1 199 void menu() {
Psy1990 7:9bd49beccdd1 200 while (1) {
Psy1990 2:c6772c5ab69d 201
Psy1990 2:c6772c5ab69d 202 pad.led(3,1); // Only Show Green LEDS
Psy1990 2:c6772c5ab69d 203 pad.led(6,1); //
Psy1990 2:c6772c5ab69d 204
Psy1990 1:09a835e6d063 205 // Splash Screen Info
Psy1990 1:09a835e6d063 206 lcd.clear(); // we need to clear the screen first
Psy1990 1:09a835e6d063 207 lcd.printString(" Author ",0,1);
Psy1990 1:09a835e6d063 208 lcd.printString("Simon Atkinson",0,2);
Psy1990 1:09a835e6d063 209 lcd.printString(" 201255483 ",0,3);
Psy1990 1:09a835e6d063 210 lcd.printString(" Uni of Leeds ",0,4);
Psy1990 1:09a835e6d063 211 lcd.refresh(); // need to refresh display after setting pixels or writing strings
Psy1990 1:09a835e6d063 212 wait(1.0); // we don't want this screen on long!
Psy1990 1:09a835e6d063 213
Psy1990 1:09a835e6d063 214 // main menu screen no interaction yet pressing start won't do anything but its a start if you pardon the pun!
Psy1990 1:09a835e6d063 215 lcd.clear();
Psy1990 1:09a835e6d063 216 lcd.printString(" Welcome to ",0,1);
Psy1990 1:09a835e6d063 217 lcd.printString(" Snake! ",0,2);
eencae 0:7423345f87c5 218 lcd.printString(" Press Start ",0,4);
eencae 0:7423345f87c5 219 lcd.refresh();
Psy1990 1:09a835e6d063 220 wait(45.0);
Psy1990 1:09a835e6d063 221 lcd.clear();
Psy1990 1:09a835e6d063 222
Psy1990 1:09a835e6d063 223 // Easter Egg time!
Psy1990 3:bd4c7eccde17 224 pad.led(3,0); // Turn Green LEDs Off
Psy1990 3:bd4c7eccde17 225 pad.led(6,0); //
Psy1990 3:bd4c7eccde17 226 pad.led(2,1); // Turn on Amber LEDs
Psy1990 3:bd4c7eccde17 227 pad.led(5,1); //
Psy1990 1:09a835e6d063 228 lcd.printString(" Do you need ",0,1);
Psy1990 1:09a835e6d063 229 lcd.printString(" more time? ",0,2);
Psy1990 1:09a835e6d063 230 lcd.printString(" Grandpa! ",0,3);
Psy1990 1:09a835e6d063 231 lcd.refresh();
Psy1990 1:09a835e6d063 232 wait(5.0);
Psy1990 1:09a835e6d063 233
Psy1990 1:09a835e6d063 234 //returns back to normal welcome screen
Psy1990 1:09a835e6d063 235 lcd.clear();
Psy1990 3:bd4c7eccde17 236 pad.led(3,1); // Turn Green LEDs On
Psy1990 3:bd4c7eccde17 237 pad.led(6,1); //
Psy1990 3:bd4c7eccde17 238 pad.led(2,0); // Turn off Amber LEDs
Psy1990 3:bd4c7eccde17 239 pad.led(5,0); //
Psy1990 1:09a835e6d063 240 lcd.printString(" Welcome to ",0,1);
Psy1990 1:09a835e6d063 241 lcd.printString(" Snake! ",0,2);
Psy1990 1:09a835e6d063 242 lcd.printString(" Press Start ",0,4);
Psy1990 7:9bd49beccdd1 243 lcd.refresh();
Psy1990 7:9bd49beccdd1 244 }
Psy1990 7:9bd49beccdd1 245 }
Psy1990 3:bd4c7eccde17 246
Psy1990 7:9bd49beccdd1 247 void ingame() {
Psy1990 1:09a835e6d063 248
Psy1990 7:9bd49beccdd1 249 while (!endGame)
Psy1990 7:9bd49beccdd1 250 {
Psy1990 7:9bd49beccdd1 251 display();
Psy1990 7:9bd49beccdd1 252 control();
Psy1990 7:9bd49beccdd1 253 gameplay();
Psy1990 7:9bd49beccdd1 254 wait (0.06); // Sets how often the while loop functions can adjust the speed of the game!
Psy1990 1:09a835e6d063 255 }
Psy1990 7:9bd49beccdd1 256
Psy1990 7:9bd49beccdd1 257
Psy1990 7:9bd49beccdd1 258 }
Psy1990 7:9bd49beccdd1 259
Psy1990 7:9bd49beccdd1 260
Psy1990 2:c6772c5ab69d 261
Psy1990 3:bd4c7eccde17 262
Psy1990 7:9bd49beccdd1 263 ///////////// functions ////////////////
Psy1990 7:9bd49beccdd1 264 int main()
Psy1990 7:9bd49beccdd1 265 {
Psy1990 7:9bd49beccdd1 266 //initialise Display and gamepad
Psy1990 7:9bd49beccdd1 267 lcd.init(); // Turns the LCD display on
Psy1990 7:9bd49beccdd1 268 pad.init(); // Turns the gamepad on
Psy1990 7:9bd49beccdd1 269 pad.leds_off(); // Turns the LEDs OFF
Psy1990 7:9bd49beccdd1 270
Psy1990 7:9bd49beccdd1 271 // Main Game Section
Psy1990 7:9bd49beccdd1 272
Psy1990 7:9bd49beccdd1 273 setup();
Psy1990 7:9bd49beccdd1 274
Psy1990 7:9bd49beccdd1 275
Psy1990 7:9bd49beccdd1 276
Psy1990 7:9bd49beccdd1 277 if (pad.start_pressed()){
Psy1990 7:9bd49beccdd1 278 menu();
Psy1990 7:9bd49beccdd1 279 }
Psy1990 3:bd4c7eccde17 280
Psy1990 7:9bd49beccdd1 281 else {
Psy1990 7:9bd49beccdd1 282 ingame();
Psy1990 3:bd4c7eccde17 283
Psy1990 1:09a835e6d063 284 }
Psy1990 7:9bd49beccdd1 285
Psy1990 7:9bd49beccdd1 286 } */
Psy1990 7:9bd49beccdd1 287
Psy1990 7:9bd49beccdd1 288
Psy1990 7:9bd49beccdd1 289