Li Ruofan 201199450

Dependencies:   mbed

Committer:
DannyLee
Date:
Sun May 24 08:16:35 2020 +0000
Revision:
1:bd7c99a5bd10
Parent:
0:80a59a49d504
Li Ruofan 201199450

Who changed what in which revision?

UserRevisionLine numberNew contents of line
DannyLee 0:80a59a49d504 1 /*
DannyLee 0:80a59a49d504 2 ELEC2645 Embedded Systems Project
DannyLee 0:80a59a49d504 3 School of Electronic & Electrical Engineering
DannyLee 0:80a59a49d504 4 University of Leeds
DannyLee 0:80a59a49d504 5 Name: Ruofan Li
DannyLee 0:80a59a49d504 6 Username: el17rl
DannyLee 0:80a59a49d504 7 Student ID Number: 201199450
DannyLee 0:80a59a49d504 8 Date: May, 2020
DannyLee 0:80a59a49d504 9 */
DannyLee 0:80a59a49d504 10
DannyLee 0:80a59a49d504 11 #include "mbed.h"
DannyLee 0:80a59a49d504 12 #include "N5110.h"
DannyLee 0:80a59a49d504 13 #include "Gamepad.h"
DannyLee 0:80a59a49d504 14 #include "Snake.h"
DannyLee 0:80a59a49d504 15
DannyLee 0:80a59a49d504 16 struct UserInput {
DannyLee 0:80a59a49d504 17 Direction d;
DannyLee 0:80a59a49d504 18 };
DannyLee 0:80a59a49d504 19
DannyLee 0:80a59a49d504 20 N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11);//set LCD pins
DannyLee 0:80a59a49d504 21 Gamepad pad;
DannyLee 0:80a59a49d504 22 Snake snake;
DannyLee 0:80a59a49d504 23
DannyLee 0:80a59a49d504 24 int sound_();
DannyLee 0:80a59a49d504 25 void begin();
DannyLee 0:80a59a49d504 26 void init(Gamepad &pad);
DannyLee 1:bd7c99a5bd10 27 void game_rules();
DannyLee 0:80a59a49d504 28
DannyLee 0:80a59a49d504 29 int main()
DannyLee 0:80a59a49d504 30 {
DannyLee 0:80a59a49d504 31 init(pad);
DannyLee 0:80a59a49d504 32 while (1) {
DannyLee 0:80a59a49d504 33 snake.draw(lcd);
DannyLee 0:80a59a49d504 34 snake.update(pad);
DannyLee 0:80a59a49d504 35 }
DannyLee 0:80a59a49d504 36 }
DannyLee 0:80a59a49d504 37
DannyLee 0:80a59a49d504 38 void init(Gamepad &pad)
DannyLee 0:80a59a49d504 39 {
DannyLee 0:80a59a49d504 40 // initialise LCD and Gamepad
DannyLee 0:80a59a49d504 41 lcd.init();
DannyLee 0:80a59a49d504 42 pad.init();
DannyLee 0:80a59a49d504 43 begin();
DannyLee 0:80a59a49d504 44 snake.init(25,25,6,1);
DannyLee 0:80a59a49d504 45 // initialise parameters of the project
DannyLee 1:bd7c99a5bd10 46 // display rules
DannyLee 1:bd7c99a5bd10 47 game_rules();
DannyLee 0:80a59a49d504 48 }
DannyLee 0:80a59a49d504 49
DannyLee 0:80a59a49d504 50 void begin(){
DannyLee 0:80a59a49d504 51 lcd.printString(" Greedy Snake ",0,1);
DannyLee 0:80a59a49d504 52 lcd.printString(" Li Ruofan ",0,2);
DannyLee 0:80a59a49d504 53 lcd.printString(" 201199450 ",0,3);
DannyLee 0:80a59a49d504 54 lcd.printString(" Quickly ",0,4);
DannyLee 0:80a59a49d504 55 lcd.printString(" Push X twice ",0,5);
DannyLee 0:80a59a49d504 56 lcd.refresh();
DannyLee 0:80a59a49d504 57
DannyLee 0:80a59a49d504 58 while (pad.check_event(Gamepad::X_PRESSED) == false){
DannyLee 0:80a59a49d504 59 pad.leds_on();
DannyLee 0:80a59a49d504 60 sound_();
DannyLee 0:80a59a49d504 61 wait(0.3);
DannyLee 0:80a59a49d504 62 pad.leds_off();
DannyLee 0:80a59a49d504 63 wait(0.3);
DannyLee 0:80a59a49d504 64 }
DannyLee 0:80a59a49d504 65
DannyLee 0:80a59a49d504 66 lcd.clear();
DannyLee 0:80a59a49d504 67
DannyLee 0:80a59a49d504 68 }
DannyLee 0:80a59a49d504 69
DannyLee 1:bd7c99a5bd10 70 void game_rules(){
DannyLee 0:80a59a49d504 71 lcd.clear();
DannyLee 0:80a59a49d504 72 lcd.printString(" Greedy Snake! ",0,0);
DannyLee 0:80a59a49d504 73 lcd.printString(" Move the snake ",0,1);
DannyLee 0:80a59a49d504 74 lcd.printString(" To eat apples ",0,2);
DannyLee 0:80a59a49d504 75 lcd.printString(" Far from wall ",0,3);
DannyLee 0:80a59a49d504 76 lcd.printString(" Press start! ",0,5);
DannyLee 0:80a59a49d504 77 lcd.refresh();
DannyLee 0:80a59a49d504 78
DannyLee 0:80a59a49d504 79 while (pad.check_event(Gamepad::START_PRESSED) == false) {
DannyLee 0:80a59a49d504 80 pad.leds_on();
DannyLee 0:80a59a49d504 81 wait(0.2);
DannyLee 1:bd7c99a5bd10 82 }
DannyLee 1:bd7c99a5bd10 83
DannyLee 1:bd7c99a5bd10 84 lcd.clear();
DannyLee 0:80a59a49d504 85 }
DannyLee 0:80a59a49d504 86
DannyLee 0:80a59a49d504 87 int sound_(){
DannyLee 0:80a59a49d504 88 pad.tone(784.0,0.3);
DannyLee 0:80a59a49d504 89 wait(0.3);
DannyLee 0:80a59a49d504 90 if(pad.check_event(Gamepad::X_PRESSED) == true){
DannyLee 0:80a59a49d504 91 return 0;
DannyLee 0:80a59a49d504 92 }
DannyLee 0:80a59a49d504 93 pad.tone(784.0,0.3);
DannyLee 0:80a59a49d504 94 wait(0.3);
DannyLee 0:80a59a49d504 95 if(pad.check_event(Gamepad::X_PRESSED) == true){
DannyLee 0:80a59a49d504 96 return 0;
DannyLee 0:80a59a49d504 97 }
DannyLee 0:80a59a49d504 98 pad.tone(880.0,0.3);
DannyLee 0:80a59a49d504 99 wait(0.3);
DannyLee 0:80a59a49d504 100 if(pad.check_event(Gamepad::X_PRESSED) == true){
DannyLee 0:80a59a49d504 101 return 0;
DannyLee 0:80a59a49d504 102 }
DannyLee 0:80a59a49d504 103 pad.tone(587.0,0.6);
DannyLee 0:80a59a49d504 104 wait(0.5);
DannyLee 0:80a59a49d504 105 if(pad.check_event(Gamepad::X_PRESSED) == true){
DannyLee 0:80a59a49d504 106 return 0;
DannyLee 0:80a59a49d504 107 }
DannyLee 0:80a59a49d504 108 pad.tone(523.0,0.3);
DannyLee 0:80a59a49d504 109 wait(0.3);
DannyLee 0:80a59a49d504 110 if(pad.check_event(Gamepad::X_PRESSED) == true){
DannyLee 0:80a59a49d504 111 return 0;
DannyLee 0:80a59a49d504 112 }
DannyLee 0:80a59a49d504 113 pad.tone(523.0,0.3);
DannyLee 0:80a59a49d504 114 wait(0.3);
DannyLee 0:80a59a49d504 115 if(pad.check_event(Gamepad::X_PRESSED) == true){
DannyLee 0:80a59a49d504 116 return 0;
DannyLee 0:80a59a49d504 117 }
DannyLee 0:80a59a49d504 118 pad.tone(440.0,0.3);
DannyLee 0:80a59a49d504 119 wait(0.3);
DannyLee 0:80a59a49d504 120 if(pad.check_event(Gamepad::X_PRESSED) == true){
DannyLee 0:80a59a49d504 121 return 0;
DannyLee 0:80a59a49d504 122 }
DannyLee 0:80a59a49d504 123 pad.tone(587.0,0.6);
DannyLee 0:80a59a49d504 124 wait(0.4);
DannyLee 0:80a59a49d504 125 if(pad.check_event(Gamepad::X_PRESSED) == true){
DannyLee 0:80a59a49d504 126 return 0;
DannyLee 0:80a59a49d504 127 }
DannyLee 0:80a59a49d504 128 pad.tone(784.0,0.4);
DannyLee 0:80a59a49d504 129 wait(0.4);
DannyLee 0:80a59a49d504 130 if(pad.check_event(Gamepad::X_PRESSED) == true){
DannyLee 0:80a59a49d504 131 return 0;
DannyLee 0:80a59a49d504 132 }
DannyLee 0:80a59a49d504 133 pad.tone(784.0,0.4);
DannyLee 0:80a59a49d504 134 wait(0.4);
DannyLee 0:80a59a49d504 135 if(pad.check_event(Gamepad::X_PRESSED) == true){
DannyLee 0:80a59a49d504 136 return 0;
DannyLee 0:80a59a49d504 137 }
DannyLee 0:80a59a49d504 138 pad.tone(880.0,0.3);
DannyLee 0:80a59a49d504 139 wait(0.3);
DannyLee 0:80a59a49d504 140 if(pad.check_event(Gamepad::X_PRESSED) == true){
DannyLee 0:80a59a49d504 141 return 0;
DannyLee 0:80a59a49d504 142 }
DannyLee 0:80a59a49d504 143 pad.tone(1046.0,0.3);
DannyLee 0:80a59a49d504 144 wait(0.3);
DannyLee 0:80a59a49d504 145 if(pad.check_event(Gamepad::X_PRESSED) == true){
DannyLee 0:80a59a49d504 146 return 0;
DannyLee 0:80a59a49d504 147 }
DannyLee 0:80a59a49d504 148 pad.tone(880.0,0.3);
DannyLee 0:80a59a49d504 149 wait(0.3);
DannyLee 0:80a59a49d504 150 if(pad.check_event(Gamepad::X_PRESSED) == true){
DannyLee 0:80a59a49d504 151 return 0;
DannyLee 0:80a59a49d504 152 }
DannyLee 0:80a59a49d504 153 pad.tone(784.0,0.3);
DannyLee 0:80a59a49d504 154 wait(0.3);
DannyLee 0:80a59a49d504 155 if(pad.check_event(Gamepad::X_PRESSED) == true){
DannyLee 0:80a59a49d504 156 return 0;
DannyLee 0:80a59a49d504 157 }
DannyLee 0:80a59a49d504 158 pad.tone(523.0,0.4);
DannyLee 0:80a59a49d504 159 wait(0.4);
DannyLee 0:80a59a49d504 160 if(pad.check_event(Gamepad::X_PRESSED) == true){
DannyLee 0:80a59a49d504 161 return 0;
DannyLee 0:80a59a49d504 162 }
DannyLee 0:80a59a49d504 163 pad.tone(523.0,0.4);
DannyLee 0:80a59a49d504 164 wait(0.3);
DannyLee 0:80a59a49d504 165 if(pad.check_event(Gamepad::X_PRESSED) == true){
DannyLee 0:80a59a49d504 166 return 0;
DannyLee 0:80a59a49d504 167 }
DannyLee 0:80a59a49d504 168 pad.tone(440.0,0.3);
DannyLee 0:80a59a49d504 169 wait(0.3);
DannyLee 0:80a59a49d504 170 if(pad.check_event(Gamepad::X_PRESSED) == true){
DannyLee 0:80a59a49d504 171 return 0;
DannyLee 0:80a59a49d504 172 }
DannyLee 0:80a59a49d504 173 pad.tone(587.0,0.6);
DannyLee 0:80a59a49d504 174 wait(0.6);
DannyLee 0:80a59a49d504 175 if(pad.check_event(Gamepad::X_PRESSED) == true){
DannyLee 0:80a59a49d504 176 return 0;
DannyLee 0:80a59a49d504 177 }
DannyLee 1:bd7c99a5bd10 178 pad.tone(659.0f,0.3f);
DannyLee 1:bd7c99a5bd10 179 wait(0.3f);
DannyLee 1:bd7c99a5bd10 180 if(pad.check_event(Gamepad::X_PRESSED) == true){
DannyLee 0:80a59a49d504 181 return 0;
DannyLee 1:bd7c99a5bd10 182 }
DannyLee 1:bd7c99a5bd10 183 pad.tone(659.0f,0.2f);
DannyLee 1:bd7c99a5bd10 184 wait(0.2f);
DannyLee 1:bd7c99a5bd10 185 if(pad.check_event(Gamepad::X_PRESSED) == true){
DannyLee 1:bd7c99a5bd10 186 return 0;
DannyLee 1:bd7c99a5bd10 187 }
DannyLee 1:bd7c99a5bd10 188 pad.tone(587.0f,0.3f);
DannyLee 1:bd7c99a5bd10 189 wait(0.3f);
DannyLee 1:bd7c99a5bd10 190 if(pad.check_event(Gamepad::X_PRESSED) == true){
DannyLee 1:bd7c99a5bd10 191 return 0;
DannyLee 1:bd7c99a5bd10 192 }
DannyLee 1:bd7c99a5bd10 193 pad.tone(523.0f,0.2f);
DannyLee 1:bd7c99a5bd10 194 wait(0.2f);
DannyLee 1:bd7c99a5bd10 195 if(pad.check_event(Gamepad::X_PRESSED) == true){
DannyLee 1:bd7c99a5bd10 196 return 0;
DannyLee 1:bd7c99a5bd10 197 }
DannyLee 1:bd7c99a5bd10 198 pad.tone(523.0f,0.3f);
DannyLee 1:bd7c99a5bd10 199 wait(0.3f);
DannyLee 1:bd7c99a5bd10 200 if(pad.check_event(Gamepad::X_PRESSED) == true){
DannyLee 1:bd7c99a5bd10 201 return 0;
DannyLee 1:bd7c99a5bd10 202 }
DannyLee 1:bd7c99a5bd10 203 pad.tone(659.0f,0.2f);
DannyLee 1:bd7c99a5bd10 204 wait(0.2f);
DannyLee 1:bd7c99a5bd10 205 if(pad.check_event(Gamepad::X_PRESSED) == true){
DannyLee 1:bd7c99a5bd10 206 return 0;
DannyLee 1:bd7c99a5bd10 207 }
DannyLee 1:bd7c99a5bd10 208 pad.tone(784.0f,0.3f);
DannyLee 1:bd7c99a5bd10 209 wait(0.3f);
DannyLee 1:bd7c99a5bd10 210 if(pad.check_event(Gamepad::X_PRESSED) == true){
DannyLee 1:bd7c99a5bd10 211 return 0;
DannyLee 1:bd7c99a5bd10 212 }
DannyLee 1:bd7c99a5bd10 213 pad.tone(1046.0f,0.3f);
DannyLee 1:bd7c99a5bd10 214 wait(0.3f);
DannyLee 1:bd7c99a5bd10 215 if(pad.check_event(Gamepad::X_PRESSED) == true){
DannyLee 1:bd7c99a5bd10 216 return 0;
DannyLee 1:bd7c99a5bd10 217 }
DannyLee 1:bd7c99a5bd10 218 pad.tone(1046.0f,0.2f);
DannyLee 1:bd7c99a5bd10 219 wait(0.2f);
DannyLee 1:bd7c99a5bd10 220 if(pad.check_event(Gamepad::X_PRESSED) == true){
DannyLee 1:bd7c99a5bd10 221 return 0;
DannyLee 1:bd7c99a5bd10 222 }
DannyLee 1:bd7c99a5bd10 223 pad.tone(1046.0f,0.2f);
DannyLee 1:bd7c99a5bd10 224 wait(0.2f);
DannyLee 1:bd7c99a5bd10 225 if(pad.check_event(Gamepad::X_PRESSED) == true){
DannyLee 1:bd7c99a5bd10 226 return 0;
DannyLee 1:bd7c99a5bd10 227 }
DannyLee 1:bd7c99a5bd10 228 pad.tone(880.0f,0.5f);
DannyLee 1:bd7c99a5bd10 229 wait(0.5f);
DannyLee 1:bd7c99a5bd10 230 if(pad.check_event(Gamepad::X_PRESSED) == true){
DannyLee 1:bd7c99a5bd10 231 return 0;
DannyLee 1:bd7c99a5bd10 232 }
DannyLee 1:bd7c99a5bd10 233 pad.tone(784.0f,0.3f);
DannyLee 1:bd7c99a5bd10 234 wait(0.3f);
DannyLee 1:bd7c99a5bd10 235 if(pad.check_event(Gamepad::X_PRESSED) == true){
DannyLee 1:bd7c99a5bd10 236 return 0;
DannyLee 1:bd7c99a5bd10 237 }
DannyLee 1:bd7c99a5bd10 238 pad.tone(784.0f,0.2f);
DannyLee 1:bd7c99a5bd10 239 wait(0.2f);
DannyLee 1:bd7c99a5bd10 240 if(pad.check_event(Gamepad::X_PRESSED) == true){
DannyLee 1:bd7c99a5bd10 241 return 0;
DannyLee 1:bd7c99a5bd10 242 }
DannyLee 1:bd7c99a5bd10 243 pad.tone(880.0f,0.3f);
DannyLee 1:bd7c99a5bd10 244 wait(0.3f);
DannyLee 1:bd7c99a5bd10 245 if(pad.check_event(Gamepad::X_PRESSED) == true){
DannyLee 1:bd7c99a5bd10 246 return 0;
DannyLee 1:bd7c99a5bd10 247 }
DannyLee 1:bd7c99a5bd10 248 pad.tone(784.0f,0.3f);
DannyLee 1:bd7c99a5bd10 249 wait(0.3f);
DannyLee 1:bd7c99a5bd10 250 if(pad.check_event(Gamepad::X_PRESSED) == true){
DannyLee 1:bd7c99a5bd10 251 return 0;
DannyLee 1:bd7c99a5bd10 252 }
DannyLee 1:bd7c99a5bd10 253 pad.tone(659.0f,0.2f);
DannyLee 1:bd7c99a5bd10 254 wait(0.2f);
DannyLee 1:bd7c99a5bd10 255 if(pad.check_event(Gamepad::X_PRESSED) == true){
DannyLee 1:bd7c99a5bd10 256 return 0;
DannyLee 1:bd7c99a5bd10 257 }
DannyLee 1:bd7c99a5bd10 258 pad.tone(523.0f,0.3f);
DannyLee 1:bd7c99a5bd10 259 wait(0.3f);
DannyLee 1:bd7c99a5bd10 260 if(pad.check_event(Gamepad::X_PRESSED) == true){
DannyLee 1:bd7c99a5bd10 261 return 0;
DannyLee 1:bd7c99a5bd10 262 }
DannyLee 1:bd7c99a5bd10 263 pad.tone(587.0f,0.3f);
DannyLee 1:bd7c99a5bd10 264 wait(0.3f);
DannyLee 1:bd7c99a5bd10 265 if(pad.check_event(Gamepad::X_PRESSED) == true){
DannyLee 1:bd7c99a5bd10 266 return 0;
DannyLee 1:bd7c99a5bd10 267 }
DannyLee 1:bd7c99a5bd10 268 pad.tone(587.0f,0.2f);
DannyLee 1:bd7c99a5bd10 269 wait(0.2f);
DannyLee 1:bd7c99a5bd10 270 if(pad.check_event(Gamepad::X_PRESSED) == true){
DannyLee 1:bd7c99a5bd10 271 return 0;
DannyLee 1:bd7c99a5bd10 272 }
DannyLee 1:bd7c99a5bd10 273 pad.tone(659.0f,0.3f);
DannyLee 1:bd7c99a5bd10 274 wait(0.3f);
DannyLee 1:bd7c99a5bd10 275 if(pad.check_event(Gamepad::X_PRESSED) == true){
DannyLee 1:bd7c99a5bd10 276 return 0;
DannyLee 1:bd7c99a5bd10 277 }
DannyLee 1:bd7c99a5bd10 278 pad.tone(587.0f,0.2f);
DannyLee 1:bd7c99a5bd10 279 wait(0.2f);
DannyLee 1:bd7c99a5bd10 280 if(pad.check_event(Gamepad::X_PRESSED) == true){
DannyLee 1:bd7c99a5bd10 281 return 0;
DannyLee 1:bd7c99a5bd10 282 }
DannyLee 1:bd7c99a5bd10 283 return 0;
DannyLee 1:bd7c99a5bd10 284 }//play Chinese song "East is Red" and Australian song "Sheep clippling"