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@3:bd4c7eccde17, 2020-06-04 (annotated)
- Committer:
- Psy1990
- Date:
- Thu Jun 04 15:57:13 2020 +0000
- Revision:
- 3:bd4c7eccde17
- Parent:
- 2:c6772c5ab69d
- Child:
- 4:07aada4c8421
Working on the game now drawing out the game area
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 | |
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" |
eencae | 0:7423345f87c5 | 17 | |
eencae | 0:7423345f87c5 | 18 | |
eencae | 0:7423345f87c5 | 19 | |
eencae | 0:7423345f87c5 | 20 | /////////////// structs ///////////////// |
eencae | 0:7423345f87c5 | 21 | struct UserInput { |
eencae | 0:7423345f87c5 | 22 | Direction d; |
eencae | 0:7423345f87c5 | 23 | float mag; |
eencae | 0:7423345f87c5 | 24 | }; |
eencae | 0:7423345f87c5 | 25 | /////////////// objects /////////////// |
eencae | 0:7423345f87c5 | 26 | N5110 lcd; |
eencae | 0:7423345f87c5 | 27 | Gamepad pad; |
Psy1990 | 1:09a835e6d063 | 28 | |
eencae | 0:7423345f87c5 | 29 | |
Psy1990 | 1:09a835e6d063 | 30 | |
eencae | 0:7423345f87c5 | 31 | void welcome(); |
eencae | 0:7423345f87c5 | 32 | |
Psy1990 | 3:bd4c7eccde17 | 33 | bool gameOver; |
Psy1990 | 3:bd4c7eccde17 | 34 | int borderW, borderH, appleX, appleY, score; |
Psy1990 | 3:bd4c7eccde17 | 35 | enum eDirection {STOP = 0, LEFT, RIGHT, UP, DOWN}; |
Psy1990 | 3:bd4c7eccde17 | 36 | eDirection dir; |
Psy1990 | 3:bd4c7eccde17 | 37 | |
Psy1990 | 3:bd4c7eccde17 | 38 | |
Psy1990 | 3:bd4c7eccde17 | 39 | |
Psy1990 | 3:bd4c7eccde17 | 40 | |
Psy1990 | 3:bd4c7eccde17 | 41 | |
Psy1990 | 3:bd4c7eccde17 | 42 | void setup() |
Psy1990 | 3:bd4c7eccde17 | 43 | { |
Psy1990 | 3:bd4c7eccde17 | 44 | gameOver = false; |
Psy1990 | 3:bd4c7eccde17 | 45 | dir = STOP; |
Psy1990 | 3:bd4c7eccde17 | 46 | borderW = WIDTH; //Fence width and height |
Psy1990 | 3:bd4c7eccde17 | 47 | borderH = HEIGHT; |
Psy1990 | 3:bd4c7eccde17 | 48 | appleX = rand() % borderW; |
Psy1990 | 3:bd4c7eccde17 | 49 | appleY = rand() % borderH; |
Psy1990 | 3:bd4c7eccde17 | 50 | score = 0; |
Psy1990 | 3:bd4c7eccde17 | 51 | } |
Psy1990 | 3:bd4c7eccde17 | 52 | |
Psy1990 | 3:bd4c7eccde17 | 53 | void draw() { |
Psy1990 | 3:bd4c7eccde17 | 54 | lcd.clear(); |
Psy1990 | 3:bd4c7eccde17 | 55 | |
Psy1990 | 3:bd4c7eccde17 | 56 | lcd.drawRect(0,0,borderW,borderH,FILL_TRANSPARENT); |
Psy1990 | 3:bd4c7eccde17 | 57 | lcd.refresh(); |
Psy1990 | 3:bd4c7eccde17 | 58 | |
Psy1990 | 3:bd4c7eccde17 | 59 | } |
Psy1990 | 3:bd4c7eccde17 | 60 | |
eencae | 0:7423345f87c5 | 61 | ///////////// functions //////////////// |
eencae | 0:7423345f87c5 | 62 | int main() |
eencae | 0:7423345f87c5 | 63 | { |
Psy1990 | 3:bd4c7eccde17 | 64 | //initialise Display and gamepad |
Psy1990 | 3:bd4c7eccde17 | 65 | lcd.init(); |
Psy1990 | 3:bd4c7eccde17 | 66 | pad.leds_off(); |
Psy1990 | 3:bd4c7eccde17 | 67 | |
Psy1990 | 3:bd4c7eccde17 | 68 | setup(); |
Psy1990 | 3:bd4c7eccde17 | 69 | while (!gameOver) |
Psy1990 | 3:bd4c7eccde17 | 70 | { |
Psy1990 | 3:bd4c7eccde17 | 71 | draw(); |
Psy1990 | 3:bd4c7eccde17 | 72 | |
Psy1990 | 3:bd4c7eccde17 | 73 | } |
Psy1990 | 3:bd4c7eccde17 | 74 | |
Psy1990 | 3:bd4c7eccde17 | 75 | |
Psy1990 | 3:bd4c7eccde17 | 76 | |
Psy1990 | 3:bd4c7eccde17 | 77 | /* Working on game will bring welcome screen back in |
Psy1990 | 3:bd4c7eccde17 | 78 | |
Psy1990 | 3:bd4c7eccde17 | 79 | // first need to initialise display |
Psy1990 | 1:09a835e6d063 | 80 | lcd.init(); |
Psy1990 | 2:c6772c5ab69d | 81 | pad.leds_off(); |
eencae | 0:7423345f87c5 | 82 | |
Psy1990 | 2:c6772c5ab69d | 83 | while ( pad.start_pressed() == false) { |
Psy1990 | 2:c6772c5ab69d | 84 | |
Psy1990 | 2:c6772c5ab69d | 85 | pad.led(3,1); // Only Show Green LEDS |
Psy1990 | 2:c6772c5ab69d | 86 | pad.led(6,1); // |
Psy1990 | 2:c6772c5ab69d | 87 | |
Psy1990 | 1:09a835e6d063 | 88 | // Splash Screen Info |
Psy1990 | 1:09a835e6d063 | 89 | lcd.clear(); // we need to clear the screen first |
Psy1990 | 1:09a835e6d063 | 90 | lcd.printString(" Author ",0,1); |
Psy1990 | 1:09a835e6d063 | 91 | lcd.printString("Simon Atkinson",0,2); |
Psy1990 | 1:09a835e6d063 | 92 | lcd.printString(" 201255483 ",0,3); |
Psy1990 | 1:09a835e6d063 | 93 | lcd.printString(" Uni of Leeds ",0,4); |
Psy1990 | 1:09a835e6d063 | 94 | lcd.refresh(); // need to refresh display after setting pixels or writing strings |
Psy1990 | 1:09a835e6d063 | 95 | wait(1.0); // we don't want this screen on long! |
Psy1990 | 1:09a835e6d063 | 96 | |
Psy1990 | 1:09a835e6d063 | 97 | // main menu screen no interaction yet pressing start won't do anything but its a start if you pardon the pun! |
Psy1990 | 1:09a835e6d063 | 98 | lcd.clear(); |
Psy1990 | 1:09a835e6d063 | 99 | lcd.printString(" Welcome to ",0,1); |
Psy1990 | 1:09a835e6d063 | 100 | lcd.printString(" Snake! ",0,2); |
eencae | 0:7423345f87c5 | 101 | lcd.printString(" Press Start ",0,4); |
eencae | 0:7423345f87c5 | 102 | lcd.refresh(); |
Psy1990 | 1:09a835e6d063 | 103 | wait(45.0); |
Psy1990 | 1:09a835e6d063 | 104 | lcd.clear(); |
Psy1990 | 1:09a835e6d063 | 105 | |
Psy1990 | 1:09a835e6d063 | 106 | // Easter Egg time! |
Psy1990 | 3:bd4c7eccde17 | 107 | pad.led(3,0); // Turn Green LEDs Off |
Psy1990 | 3:bd4c7eccde17 | 108 | pad.led(6,0); // |
Psy1990 | 3:bd4c7eccde17 | 109 | pad.led(2,1); // Turn on Amber LEDs |
Psy1990 | 3:bd4c7eccde17 | 110 | pad.led(5,1); // |
Psy1990 | 1:09a835e6d063 | 111 | lcd.printString(" Do you need ",0,1); |
Psy1990 | 1:09a835e6d063 | 112 | lcd.printString(" more time? ",0,2); |
Psy1990 | 1:09a835e6d063 | 113 | lcd.printString(" Grandpa! ",0,3); |
Psy1990 | 1:09a835e6d063 | 114 | lcd.refresh(); |
Psy1990 | 1:09a835e6d063 | 115 | wait(5.0); |
Psy1990 | 1:09a835e6d063 | 116 | |
Psy1990 | 1:09a835e6d063 | 117 | //returns back to normal welcome screen |
Psy1990 | 1:09a835e6d063 | 118 | lcd.clear(); |
Psy1990 | 3:bd4c7eccde17 | 119 | pad.led(3,1); // Turn Green LEDs On |
Psy1990 | 3:bd4c7eccde17 | 120 | pad.led(6,1); // |
Psy1990 | 3:bd4c7eccde17 | 121 | pad.led(2,0); // Turn off Amber LEDs |
Psy1990 | 3:bd4c7eccde17 | 122 | pad.led(5,0); // |
Psy1990 | 1:09a835e6d063 | 123 | lcd.printString(" Welcome to ",0,1); |
Psy1990 | 1:09a835e6d063 | 124 | lcd.printString(" Snake! ",0,2); |
Psy1990 | 1:09a835e6d063 | 125 | lcd.printString(" Press Start ",0,4); |
Psy1990 | 1:09a835e6d063 | 126 | lcd.refresh(); |
Psy1990 | 3:bd4c7eccde17 | 127 | |
Psy1990 | 1:09a835e6d063 | 128 | |
Psy1990 | 1:09a835e6d063 | 129 | } |
Psy1990 | 2:c6772c5ab69d | 130 | |
Psy1990 | 3:bd4c7eccde17 | 131 | |
Psy1990 | 3:bd4c7eccde17 | 132 | while ( pad.start_pressed() == true) { |
Psy1990 | 3:bd4c7eccde17 | 133 | pad.leds_off(); |
Psy1990 | 3:bd4c7eccde17 | 134 | pad.led(0,1); // Only Show Red LEDS |
Psy1990 | 3:bd4c7eccde17 | 135 | pad.led(3,1); |
Psy1990 | 3:bd4c7eccde17 | 136 | |
Psy1990 | 3:bd4c7eccde17 | 137 | // Splash Screen Info |
Psy1990 | 3:bd4c7eccde17 | 138 | lcd.clear(); // we need to clear the screen first |
Psy1990 | 3:bd4c7eccde17 | 139 | lcd.printString(" Placeholder ",0,1); |
Psy1990 | 3:bd4c7eccde17 | 140 | lcd.printString(" Game Coming ",0,2); |
Psy1990 | 3:bd4c7eccde17 | 141 | lcd.printString(" Soon ",0,3); |
Psy1990 | 3:bd4c7eccde17 | 142 | lcd.printString(" :) ",0,4); |
Psy1990 | 3:bd4c7eccde17 | 143 | lcd.refresh(); // need to refresh display after setting pixels or writing strings |
Psy1990 | 3:bd4c7eccde17 | 144 | |
Psy1990 | 3:bd4c7eccde17 | 145 | |
Psy1990 | 3:bd4c7eccde17 | 146 | } */ |
Psy1990 | 3:bd4c7eccde17 | 147 | |
Psy1990 | 1:09a835e6d063 | 148 | } |