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.
Dependencies: mbed
main.cpp@12:8eb40a18f15d, 2020-06-05 (annotated)
- Committer:
- Psy1990
- Date:
- Fri Jun 05 20:47:43 2020 +0000
- Revision:
- 12:8eb40a18f15d
- Parent:
- 10:3e37b58e8600
- Child:
- 13:c20acb3b1adf
Commented out the safe wall and added the wall of death that kills the snake on touch. When the snake dies the game ends and the score is still displayed with instructions how to the restart the game. The LEDs turn to flashing red!
Who changed what in which revision?
| User | Revision | Line number | New 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 | 7:9bd49beccdd1 | 25 | |
| Psy1990 | 7:9bd49beccdd1 | 26 | |
| Psy1990 | 10:3e37b58e8600 | 27 | // structs |
| Psy1990 | 7:9bd49beccdd1 | 28 | struct UserInput { |
| Psy1990 | 7:9bd49beccdd1 | 29 | Direction d; |
| Psy1990 | 7:9bd49beccdd1 | 30 | float mag; |
| Psy1990 | 7:9bd49beccdd1 | 31 | }; |
| Psy1990 | 9:25597bc0cecc | 32 | |
| Psy1990 | 9:25597bc0cecc | 33 | |
| Psy1990 | 10:3e37b58e8600 | 34 | // objects |
| Psy1990 | 10:3e37b58e8600 | 35 | N5110 lcd; // Conects to LCD Display which is 84x48 pixels and can have 5 lines of text each contain 14 characters |
| Psy1990 | 10:3e37b58e8600 | 36 | Gamepad pad; // Conects to rest of gamepad and allows access to LEDs, Controls, Sensors ect |
| Psy1990 | 10:3e37b58e8600 | 37 | SnakeEngine snake; // Holds the majority of the game code and engine |
| Psy1990 | 7:9bd49beccdd1 | 38 | |
| Psy1990 | 8:32825d724856 | 39 | |
| Psy1990 | 10:3e37b58e8600 | 40 | // prototypes t |
| Psy1990 | 10:3e37b58e8600 | 41 | void init(); // Needed to setup the game and make sure everything is turned on |
| Psy1990 | 7:9bd49beccdd1 | 42 | void update_game(UserInput input); |
| Psy1990 | 10:3e37b58e8600 | 43 | void render(); // Needed to make the screen work and update each frame |
| Psy1990 | 10:3e37b58e8600 | 44 | void welcome(); // Displays the splash screen when the device turns on or is reset |
| Psy1990 | 7:9bd49beccdd1 | 45 | |
| Psy1990 | 10:3e37b58e8600 | 46 | // functions |
| Psy1990 | 10:3e37b58e8600 | 47 | int main() // Everything in main.cpp is contained in this |
| Psy1990 | 7:9bd49beccdd1 | 48 | { |
| Psy1990 | 7:9bd49beccdd1 | 49 | |
| Psy1990 | 9:25597bc0cecc | 50 | int fps = 6; // How many Frames Per Second (FPS) |
| Psy1990 | 7:9bd49beccdd1 | 51 | |
| Psy1990 | 10:3e37b58e8600 | 52 | init(); // Turns on LCD and PAD |
| Psy1990 | 10:3e37b58e8600 | 53 | welcome(); // Welcome screen to be shown before the game starts |
| Psy1990 | 7:9bd49beccdd1 | 54 | |
| Psy1990 | 7:9bd49beccdd1 | 55 | render(); // first draw the initial frame |
| Psy1990 | 10:3e37b58e8600 | 56 | wait(1.0f/fps); // wait for one period |
| Psy1990 | 7:9bd49beccdd1 | 57 | |
| Psy1990 | 7:9bd49beccdd1 | 58 | |
| Psy1990 | 10:3e37b58e8600 | 59 | // 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 | 60 | while (1) { |
| Psy1990 | 10:3e37b58e8600 | 61 | snake.read_input(pad); |
| Psy1990 | 12:8eb40a18f15d | 62 | snake.update(pad, lcd); |
| Psy1990 | 7:9bd49beccdd1 | 63 | render(); |
| Psy1990 | 7:9bd49beccdd1 | 64 | wait(1.0f/fps); |
| Psy1990 | 7:9bd49beccdd1 | 65 | } |
| Psy1990 | 7:9bd49beccdd1 | 66 | } |
| Psy1990 | 7:9bd49beccdd1 | 67 | |
| Psy1990 | 7:9bd49beccdd1 | 68 | // initialies all classes and libraries |
| Psy1990 | 7:9bd49beccdd1 | 69 | void init() |
| Psy1990 | 7:9bd49beccdd1 | 70 | { |
| Psy1990 | 7:9bd49beccdd1 | 71 | // need to initialise LCD and Gamepad |
| Psy1990 | 10:3e37b58e8600 | 72 | lcd.init(); // LCD turned on |
| Psy1990 | 10:3e37b58e8600 | 73 | pad.init(); // GamePad turned on |
| Psy1990 | 10:3e37b58e8600 | 74 | snake.init(APPLE_SIZE,SNAKE_SCORE, SNAKE_SPEED); //Initiates the values based on what is on the top of this file |
| Psy1990 | 7:9bd49beccdd1 | 75 | } |
| Psy1990 | 7:9bd49beccdd1 | 76 | |
| Psy1990 | 7:9bd49beccdd1 | 77 | // this function draws each frame on the LCD |
| Psy1990 | 7:9bd49beccdd1 | 78 | void render() |
| Psy1990 | 7:9bd49beccdd1 | 79 | { |
| Psy1990 | 9:25597bc0cecc | 80 | // Each time we render we need to clear screen get the buffer and refresh |
| Psy1990 | 7:9bd49beccdd1 | 81 | lcd.clear(); |
| Psy1990 | 7:9bd49beccdd1 | 82 | snake.draw(lcd); |
| Psy1990 | 7:9bd49beccdd1 | 83 | lcd.refresh(); |
| Psy1990 | 7:9bd49beccdd1 | 84 | } |
| Psy1990 | 7:9bd49beccdd1 | 85 | |
| Psy1990 | 9:25597bc0cecc | 86 | // Welcome Screen |
| Psy1990 | 7:9bd49beccdd1 | 87 | void welcome() { |
| Psy1990 | 7:9bd49beccdd1 | 88 | pad.led(3,1); // Only Show Green LEDS |
| Psy1990 | 8:32825d724856 | 89 | pad.led(6,1); // |
| Psy1990 | 7:9bd49beccdd1 | 90 | lcd.clear(); |
| Psy1990 | 8:32825d724856 | 91 | lcd.clear(); // we need to clear the screen first |
| Psy1990 | 8:32825d724856 | 92 | lcd.printString(" Author ",0,1); |
| Psy1990 | 8:32825d724856 | 93 | lcd.printString("Simon Atkinson",0,2); |
| Psy1990 | 8:32825d724856 | 94 | lcd.printString(" 201255483 ",0,3); |
| Psy1990 | 8:32825d724856 | 95 | lcd.printString(" Uni of Leeds ",0,4); |
| Psy1990 | 8:32825d724856 | 96 | lcd.refresh(); // need to refresh display after setting pixels or writing strings |
| Psy1990 | 8:32825d724856 | 97 | wait(1.0); // we don't want this screen on long! |
| Psy1990 | 8:32825d724856 | 98 | lcd.clear(); |
| Psy1990 | 7:9bd49beccdd1 | 99 | lcd.printString(" Welcome to ",0,1); |
| Psy1990 | 7:9bd49beccdd1 | 100 | lcd.printString(" Snake! ",0,2); |
| Psy1990 | 7:9bd49beccdd1 | 101 | lcd.printString(" Press Start ",0,4); |
| Psy1990 | 7:9bd49beccdd1 | 102 | lcd.refresh(); |
| Psy1990 | 7:9bd49beccdd1 | 103 | lcd.clear(); |
| Psy1990 | 10:3e37b58e8600 | 104 | 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 | 105 | } |
| Psy1990 | 7:9bd49beccdd1 | 106 | |
| Psy1990 | 10:3e37b58e8600 | 107 | } //end of main.cpp |