Finished the game

Dependencies:   mbed

Welcome to Snake

See that the LCD is on and press start to play

The snake is in the middle of the screen, move the analogue up, down, right, left to move the snake in those directions. Note that snake doesn't travel diagonally.

Move the snake to get as much food as possible, there is plenty of food as you will find out

Beware, snake will die if you run its head into the walls, or into its body

Take care to keep moving snake fast, its body will catch up with its head and cause it to die!!!!!!

Have fun, and get that snake as long as possible :)

Committer:
el18lg
Date:
Fri Jun 05 21:21:49 2020 +0000
Revision:
12:d16d154af362
Parent:
11:c77c90ed0ad8
Final Submission. I have read and agreed with Statement of Academic Integrity; Lee Geer

Who changed what in which revision?

UserRevisionLine numberNew contents of line
eencae 0:b7f1f47bb26a 1 /*
eencae 0:b7f1f47bb26a 2 ELEC2645 Embedded Systems Project
eencae 0:b7f1f47bb26a 3 School of Electronic & Electrical Engineering
eencae 0:b7f1f47bb26a 4 University of Leeds
eencae 0:b7f1f47bb26a 5 2019/20
eencae 0:b7f1f47bb26a 6
el18lg 1:bdafa20e71a0 7 Name: Lee Geer
el18lg 1:bdafa20e71a0 8 Username: el18lg
el18lg 1:bdafa20e71a0 9 Student ID Number: 201265490
el18lg 10:dda2f2fd8b71 10 Date: 05/06/2020
eencae 0:b7f1f47bb26a 11 */
eencae 0:b7f1f47bb26a 12
eencae 0:b7f1f47bb26a 13 // includes
eencae 0:b7f1f47bb26a 14 #include "mbed.h"
eencae 0:b7f1f47bb26a 15 #include "Gamepad.h"
eencae 0:b7f1f47bb26a 16 #include "N5110.h"
el18lg 1:bdafa20e71a0 17 #include "FXOS8700CQ.h"
el18lg 2:c33ff63c0813 18 #include "SnakeEngine.h"
el18lg 9:f3259d652208 19
el18lg 3:beb0cc405b1e 20
eencae 0:b7f1f47bb26a 21
eencae 0:b7f1f47bb26a 22 // objects
eencae 0:b7f1f47bb26a 23 Gamepad pad;
eencae 0:b7f1f47bb26a 24 N5110 lcd;
el18lg 2:c33ff63c0813 25 SnakeEngine snake;
el18lg 6:266fb8fc17f4 26
el18lg 9:f3259d652208 27 //prototypes
el18lg 2:c33ff63c0813 28 void init();
el18lg 2:c33ff63c0813 29 void render();
el18lg 7:24a3f13ce36d 30 void update();
el18lg 9:f3259d652208 31 void welcome();
el18lg 9:f3259d652208 32
el18lg 9:f3259d652208 33 // function
el18lg 1:bdafa20e71a0 34 int main()
eencae 0:b7f1f47bb26a 35 {
el18lg 12:d16d154af362 36 #ifdef WITH_TESTING
el18lg 9:f3259d652208 37 //int number_of_failures = run_all_tests();
el18lg 9:f3259d652208 38
el18lg 9:f3259d652208 39 //if(number_of_failures > 0) return number_of_failures;
el18lg 9:f3259d652208 40 #endif
el18lg 2:c33ff63c0813 41 int fps = 6; // frames per second
el18lg 7:24a3f13ce36d 42
el18lg 2:c33ff63c0813 43 init(); // initialise and then display welcome screen...
el18lg 9:f3259d652208 44 welcome();
el18lg 9:f3259d652208 45 render(); // draw the initial frame
el18lg 2:c33ff63c0813 46 wait(1.0f/fps); // and wait for one frame period
el18lg 2:c33ff63c0813 47
el18lg 2:c33ff63c0813 48
el18lg 2:c33ff63c0813 49 // game loop - read input, update the game state and render the display
el18lg 2:c33ff63c0813 50 while (1) {
el18lg 2:c33ff63c0813 51 snake.read_input(pad);
el18lg 7:24a3f13ce36d 52 snake.update(pad, lcd);
el18lg 2:c33ff63c0813 53 render();
el18lg 2:c33ff63c0813 54 wait(1.0f/fps);
el18lg 2:c33ff63c0813 55 }
el18lg 9:f3259d652208 56
el18lg 2:c33ff63c0813 57 }
el18lg 9:f3259d652208 58 #define head_length 1
el18lg 9:f3259d652208 59 #define head_speed 0.5
el18lg 9:f3259d652208 60 #define food_size 1
el18lg 9:f3259d652208 61 #define score 0
el18lg 7:24a3f13ce36d 62
el18lg 2:c33ff63c0813 63 // initialies all classes and libraries
el18lg 2:c33ff63c0813 64 void init()
el18lg 2:c33ff63c0813 65 {
el18lg 7:24a3f13ce36d 66
el18lg 2:c33ff63c0813 67 // need to initialise LCD and Gamepad
el18lg 1:bdafa20e71a0 68 lcd.init();
el18lg 1:bdafa20e71a0 69 pad.init();
el18lg 2:c33ff63c0813 70
el18lg 2:c33ff63c0813 71 // initialise the game with correct ball and paddle sizes
el18lg 9:f3259d652208 72 snake.init(head_length,head_speed, food_size, score);
el18lg 7:24a3f13ce36d 73
el18lg 2:c33ff63c0813 74 }
el18lg 2:c33ff63c0813 75
el18lg 7:24a3f13ce36d 76
el18lg 2:c33ff63c0813 77 // this function draws each frame on the LCD
el18lg 2:c33ff63c0813 78 void render()
el18lg 2:c33ff63c0813 79 {
el18lg 2:c33ff63c0813 80 // clear screen, re-draw and refresh
el18lg 2:c33ff63c0813 81 lcd.clear();
el18lg 2:c33ff63c0813 82 snake.draw(lcd);
el18lg 1:bdafa20e71a0 83 lcd.refresh();
el18lg 2:c33ff63c0813 84 }
el18lg 2:c33ff63c0813 85
el18lg 9:f3259d652208 86 // this function is introduction
el18lg 9:f3259d652208 87 void welcome() {
el18lg 9:f3259d652208 88
el18lg 9:f3259d652208 89 lcd.printString("----SNAKE---O",0,1);
el18lg 12:d16d154af362 90 lcd.printString(" HOLD START ",0,4); // instruction to the user
el18lg 9:f3259d652208 91 lcd.refresh();
el18lg 9:f3259d652208 92
el18lg 9:f3259d652208 93 // wait flashing LEDs until start button is pressed
el18lg 11:c77c90ed0ad8 94 while ( pad.start_held() == false) {
el18lg 11:c77c90ed0ad8 95 lcd.setContrast( pad.read_pot1());
el18lg 11:c77c90ed0ad8 96 pad.leds_off();
el18lg 11:c77c90ed0ad8 97 wait(0.1);
el18lg 11:c77c90ed0ad8 98 pad.led(1,1);
el18lg 11:c77c90ed0ad8 99 wait(0.1);
el18lg 11:c77c90ed0ad8 100 pad.led(2,1);
el18lg 11:c77c90ed0ad8 101 wait(0.1);
el18lg 11:c77c90ed0ad8 102 pad.led(3,1);
el18lg 11:c77c90ed0ad8 103 wait(0.1);
el18lg 11:c77c90ed0ad8 104 pad.led(6,1);
el18lg 11:c77c90ed0ad8 105 wait(0.1);
el18lg 11:c77c90ed0ad8 106 pad.led(5,1);
el18lg 11:c77c90ed0ad8 107 wait(0.1);
el18lg 11:c77c90ed0ad8 108 pad.led(4,1);
el18lg 11:c77c90ed0ad8 109 wait(0.1);
el18lg 11:c77c90ed0ad8 110 pad.leds_off();
el18lg 9:f3259d652208 111 }
el18lg 2:c33ff63c0813 112
el18lg 9:f3259d652208 113 }
el18lg 2:c33ff63c0813 114