ELEC2645 (2019/20) / Mbed 2 deprecated ELEC2645_Project_el18s2a_2

Dependencies:   mbed

Committer:
Psy1990
Date:
Fri Jun 05 22:31:05 2020 +0000
Revision:
14:8d12bc972cb1
Parent:
13:c20acb3b1adf
Child:
17:4b2a105cdfd8
Documentation;

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
Psy1990 10:3e37b58e8600 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 8:32825d724856 19 #include "Snake.h"
Psy1990 8:32825d724856 20 #include "Apple.h"
Psy1990 7:9bd49beccdd1 21
Psy1990 9:25597bc0cecc 22 #define APPLE_SIZE 2
Psy1990 8:32825d724856 23 #define SNAKE_SPEED 1
Psy1990 9:25597bc0cecc 24 #define SNAKE_SCORE 0
Psy1990 14:8d12bc972cb1 25 #define SNAKE_SNAKEHEIGHT 10 // Keep these equal to give it a square
Psy1990 14:8d12bc972cb1 26 #define SNAKE_SNAKEWIDTH 10 // Keep these equal to give it a square
Psy1990 7:9bd49beccdd1 27
Psy1990 7:9bd49beccdd1 28
Psy1990 10:3e37b58e8600 29 // structs
Psy1990 7:9bd49beccdd1 30 struct UserInput {
Psy1990 7:9bd49beccdd1 31 Direction d;
Psy1990 7:9bd49beccdd1 32 float mag;
Psy1990 7:9bd49beccdd1 33 };
Psy1990 9:25597bc0cecc 34
Psy1990 9:25597bc0cecc 35
Psy1990 10:3e37b58e8600 36 // objects
Psy1990 10:3e37b58e8600 37 N5110 lcd; // Conects to LCD Display which is 84x48 pixels and can have 5 lines of text each contain 14 characters
Psy1990 10:3e37b58e8600 38 Gamepad pad; // Conects to rest of gamepad and allows access to LEDs, Controls, Sensors ect
Psy1990 10:3e37b58e8600 39 SnakeEngine snake; // Holds the majority of the game code and engine
Psy1990 7:9bd49beccdd1 40
Psy1990 8:32825d724856 41
Psy1990 10:3e37b58e8600 42 // prototypes t
Psy1990 10:3e37b58e8600 43 void init(); // Needed to setup the game and make sure everything is turned on
Psy1990 7:9bd49beccdd1 44 void update_game(UserInput input);
Psy1990 10:3e37b58e8600 45 void render(); // Needed to make the screen work and update each frame
Psy1990 10:3e37b58e8600 46 void welcome(); // Displays the splash screen when the device turns on or is reset
Psy1990 7:9bd49beccdd1 47
Psy1990 10:3e37b58e8600 48 // functions
Psy1990 10:3e37b58e8600 49 int main() // Everything in main.cpp is contained in this
Psy1990 7:9bd49beccdd1 50 {
Psy1990 7:9bd49beccdd1 51
Psy1990 9:25597bc0cecc 52 int fps = 6; // How many Frames Per Second (FPS)
Psy1990 7:9bd49beccdd1 53
Psy1990 10:3e37b58e8600 54 init(); // Turns on LCD and PAD
Psy1990 10:3e37b58e8600 55 welcome(); // Welcome screen to be shown before the game starts
Psy1990 7:9bd49beccdd1 56
Psy1990 7:9bd49beccdd1 57 render(); // first draw the initial frame
Psy1990 10:3e37b58e8600 58 wait(1.0f/fps); // wait for one period
Psy1990 7:9bd49beccdd1 59
Psy1990 7:9bd49beccdd1 60
Psy1990 10:3e37b58e8600 61 // The game loop, this reads the input of the pad updates and and renders the frame each time. The game can be slowed down or sped up depending on the wait function
Psy1990 7:9bd49beccdd1 62 while (1) {
Psy1990 10:3e37b58e8600 63 snake.read_input(pad);
Psy1990 12:8eb40a18f15d 64 snake.update(pad, lcd);
Psy1990 7:9bd49beccdd1 65 render();
Psy1990 7:9bd49beccdd1 66 wait(1.0f/fps);
Psy1990 7:9bd49beccdd1 67 }
Psy1990 7:9bd49beccdd1 68 }
Psy1990 7:9bd49beccdd1 69
Psy1990 7:9bd49beccdd1 70 // initialies all classes and libraries
Psy1990 7:9bd49beccdd1 71 void init()
Psy1990 7:9bd49beccdd1 72 {
Psy1990 7:9bd49beccdd1 73 // need to initialise LCD and Gamepad
Psy1990 10:3e37b58e8600 74 lcd.init(); // LCD turned on
Psy1990 10:3e37b58e8600 75 pad.init(); // GamePad turned on
Psy1990 13:c20acb3b1adf 76 snake.init(APPLE_SIZE,SNAKE_SCORE, SNAKE_SPEED, SNAKE_SNAKEWIDTH, SNAKE_SNAKEHEIGHT); //Initiates the values based on what is on the top of this file
Psy1990 7:9bd49beccdd1 77 }
Psy1990 7:9bd49beccdd1 78
Psy1990 7:9bd49beccdd1 79 // this function draws each frame on the LCD
Psy1990 7:9bd49beccdd1 80 void render()
Psy1990 7:9bd49beccdd1 81 {
Psy1990 9:25597bc0cecc 82 // Each time we render we need to clear screen get the buffer and refresh
Psy1990 7:9bd49beccdd1 83 lcd.clear();
Psy1990 7:9bd49beccdd1 84 snake.draw(lcd);
Psy1990 7:9bd49beccdd1 85 lcd.refresh();
Psy1990 7:9bd49beccdd1 86 }
Psy1990 7:9bd49beccdd1 87
Psy1990 9:25597bc0cecc 88 // Welcome Screen
Psy1990 7:9bd49beccdd1 89 void welcome() {
Psy1990 7:9bd49beccdd1 90 pad.led(3,1); // Only Show Green LEDS
Psy1990 8:32825d724856 91 pad.led(6,1); //
Psy1990 7:9bd49beccdd1 92 lcd.clear();
Psy1990 8:32825d724856 93 lcd.clear(); // we need to clear the screen first
Psy1990 8:32825d724856 94 lcd.printString(" Author ",0,1);
Psy1990 8:32825d724856 95 lcd.printString("Simon Atkinson",0,2);
Psy1990 8:32825d724856 96 lcd.printString(" 201255483 ",0,3);
Psy1990 8:32825d724856 97 lcd.printString(" Uni of Leeds ",0,4);
Psy1990 8:32825d724856 98 lcd.refresh(); // need to refresh display after setting pixels or writing strings
Psy1990 8:32825d724856 99 wait(1.0); // we don't want this screen on long!
Psy1990 8:32825d724856 100 lcd.clear();
Psy1990 7:9bd49beccdd1 101 lcd.printString(" Welcome to ",0,1);
Psy1990 7:9bd49beccdd1 102 lcd.printString(" Snake! ",0,2);
Psy1990 7:9bd49beccdd1 103 lcd.printString(" Press Start ",0,4);
Psy1990 7:9bd49beccdd1 104 lcd.refresh();
Psy1990 7:9bd49beccdd1 105 lcd.clear();
Psy1990 10:3e37b58e8600 106 while ( pad.start_pressed() == false) { // Will keep in the welcome state until the start button is pressed then it will go onto the game!
Psy1990 7:9bd49beccdd1 107 }
Psy1990 7:9bd49beccdd1 108
Psy1990 10:3e37b58e8600 109 } //end of main.cpp