ELEC2645 (2019/20) / Mbed 2 deprecated ELEC2645_Project_el18s2a_2

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /* 
00002 ELEC2645 Embedded Systems Project
00003 School of Electronic & Electrical Engineering
00004 University of Leeds
00005 2019/20
00006 
00007 Name: Simon Thackray Atkinson
00008 Username: el18s2a   
00009 Student ID Number: 201255483
00010 Date: 05/03/2020
00011 */
00012 
00013 // pre-processor directives
00014 #include "mbed.h"
00015 #include "Gamepad.h"
00016 #include "N5110.h"
00017 #include "Bitmap.h"
00018 #include "SnakeEngine.h"
00019 #include "Snake.h"
00020 #include "Apple.h"
00021 
00022 #define APPLE_SIZE 2
00023 #define SNAKE_SPEED 1
00024 #define SNAKE_SCORE 0
00025 #define SNAKE_SNAKEHEIGHT 5  // Keep these equal to give it a square
00026 #define SNAKE_SNAKEWIDTH 5  // Keep these equal to give it a square
00027 
00028 
00029 // structs 
00030 struct UserInput {
00031     Direction d;
00032     float mag;
00033 };
00034 
00035 
00036 // objects
00037 N5110 lcd;  // Conects to LCD Display which is 84x48 pixels and can have 5 lines of text each contain 14 characters
00038 Gamepad pad; // Conects to rest of gamepad and allows access to LEDs, Controls, Sensors ect
00039 SnakeEngine snake; // Holds the majority of the game code and engine
00040 
00041 
00042 // prototypes t
00043 void init(); // Needed to setup the game and make sure everything is turned on
00044 void update_game(UserInput input);
00045 void render(); // Needed to make the screen work and update each frame
00046 void welcome(); // Displays the splash screen when the device turns on or is reset
00047 
00048 // functions 
00049 int main()    // Everything in main.cpp is contained in this 
00050 {
00051 
00052     int fps = 6;  // How many Frames Per Second (FPS)
00053 
00054     init();     // Turns on LCD and PAD
00055     welcome();  // Welcome screen to be shown before the game starts
00056     
00057     render();  // first draw the initial frame 
00058     wait(1.0f/fps);  // wait for one period
00059 
00060 
00061     // 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
00062     while (1) {
00063         snake.read_input(pad);  
00064         snake.crash(pad, lcd);      
00065         render();
00066         wait(1.0f/fps);
00067     }
00068 }
00069 
00070 // initialies all classes and libraries
00071 void init()
00072 {
00073     // need to initialise LCD and Gamepad 
00074     lcd.init(); // LCD turned on
00075     pad.init(); // GamePad turned on
00076     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
00077 }
00078 
00079 // this function draws each frame on the LCD
00080 void render()
00081 {
00082     // Each time we render we need to clear screen get the buffer and refresh
00083     lcd.clear();  
00084     snake.draw(lcd);
00085     lcd.refresh();
00086 }
00087 
00088 // Welcome Screen
00089 void welcome() {
00090     pad.led(3,1);  // Only Show Green LEDS
00091     pad.led(6,1);  //       
00092     lcd.clear();
00093        lcd.clear(); // we need to clear the screen first
00094    lcd.printString("    Author   ",0,1);  
00095    lcd.printString("Simon Atkinson",0,2); 
00096    lcd.printString("   201255483  ",0,3); 
00097    lcd.printString(" Uni of Leeds ",0,4); 
00098    lcd.refresh(); // need to refresh display after setting pixels or writing strings
00099            wait(1.0); // we don't want this screen on long!
00100                lcd.clear();
00101     lcd.printString("  Welcome to  ",0,1);  
00102     lcd.printString("    Snake!   ",0,2); 
00103     lcd.printString("  Press Start ",0,4);
00104     lcd.refresh();
00105     lcd.clear();
00106     while ( pad.start_pressed() == false) { // Will keep in the welcome state until the start button is pressed then it will go onto the game!
00107      }
00108      
00109 }  //end of main.cpp