Owen Cavender 201159294

Dependencies:   mbed Gamepad2

Committer:
el17oc
Date:
Sat May 30 05:43:50 2020 +0000
Revision:
16:9500059ad5d8
Parent:
14:7fb3c93343b6
Final Submission. I have read and agreed with Statement of Academic Integrity.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
el17oc 1:897160a1a3ae 1 /*
eencae 0:b7f1f47bb26a 2 ELEC2645 Embedded Systems Project
el17oc 6:bf90044188d0 3 School of Electronic & Electrical appleering
eencae 0:b7f1f47bb26a 4 University of Leeds
eencae 0:b7f1f47bb26a 5 2019/20
eencae 0:b7f1f47bb26a 6
el17oc 16:9500059ad5d8 7 /////////////SNAKE ///////////
el17oc 1:897160a1a3ae 8 Name: Owen Cavender
el17oc 1:897160a1a3ae 9 Username: el17oc
el17oc 1:897160a1a3ae 10 Student ID Number: 201159294
el17oc 1:897160a1a3ae 11 Date: 20/04/2020
eencae 0:b7f1f47bb26a 12 */
eencae 0:b7f1f47bb26a 13
el17oc 16:9500059ad5d8 14 //////////////////////////////
el17oc 16:9500059ad5d8 15
el17oc 16:9500059ad5d8 16 //includes
el17oc 16:9500059ad5d8 17 #include "mbed.h" //addresses each header file of external classes being used in the programme
eencae 0:b7f1f47bb26a 18 #include "Gamepad.h"
eencae 0:b7f1f47bb26a 19 #include "N5110.h"
el17oc 1:897160a1a3ae 20 #include "snake.h"
el17oc 8:997f90c88246 21 #include "GameEngine.h"
el17oc 8:997f90c88246 22
el17oc 1:897160a1a3ae 23
el17oc 1:897160a1a3ae 24 //objects
el17oc 1:897160a1a3ae 25 Gamepad pad;
el17oc 1:897160a1a3ae 26 Snake snake;
el17oc 1:897160a1a3ae 27 N5110 lcd;
el17oc 8:997f90c88246 28 GameEngine engine;
el17oc 14:7fb3c93343b6 29
el17oc 1:897160a1a3ae 30 //functions
el17oc 1:897160a1a3ae 31 void welcome();
el17oc 1:897160a1a3ae 32 void render();
el17oc 1:897160a1a3ae 33 void init();
el17oc 14:7fb3c93343b6 34 void update_snake();
el17oc 12:60c856354406 35
el17oc 1:897160a1a3ae 36
el17oc 6:bf90044188d0 37 //Serial pc(USBTX, USBRX);
el17oc 6:bf90044188d0 38
el17oc 6:bf90044188d0 39 int main()
el17oc 1:897160a1a3ae 40 {
el17oc 10:ee781d18e0f6 41
el17oc 16:9500059ad5d8 42 init(); //initialises game objects
el17oc 16:9500059ad5d8 43 welcome(); //displays welcome screen before entering game loop
el17oc 14:7fb3c93343b6 44
el17oc 16:9500059ad5d8 45
el17oc 12:60c856354406 46
el17oc 1:897160a1a3ae 47 while (1) {
el17oc 12:60c856354406 48
el17oc 12:60c856354406 49
el17oc 16:9500059ad5d8 50 update_snake(); //reads input from gamepad and updates the position of the snake - this is the function influences how the other functions behave
el17oc 16:9500059ad5d8 51 snake.check_gameover(lcd); //checking if any one of the three game over conditions is true
el17oc 16:9500059ad5d8 52 // snake.gameover_true(lcd, pad); //checking if an apple has been collected - increases the score and generates new apple position if apple has been collected
el17oc 12:60c856354406 53 snake.apple_collected(lcd, pad);
el17oc 12:60c856354406 54 snake.get_Apple_position(lcd);
el17oc 16:9500059ad5d8 55 engine.get_LEDs(pad, snake);
el17oc 14:7fb3c93343b6 56
el17oc 16:9500059ad5d8 57 engine.print_countdown(lcd, snake); //prints the value of the countdown counter - it will have incremented by 25 if an apple has been collected and would have decreased by 1 if not
el17oc 16:9500059ad5d8 58 snake.render(lcd, pad); //prints the snake, the apple, the score and the border
el17oc 14:7fb3c93343b6 59 // snake.print_display_time(lcd);
el17oc 6:bf90044188d0 60
el17oc 6:bf90044188d0 61
el17oc 14:7fb3c93343b6 62 wait(0.01);
el17oc 1:897160a1a3ae 63 }
el17oc 1:897160a1a3ae 64
el17oc 1:897160a1a3ae 65 }
el17oc 1:897160a1a3ae 66
el17oc 1:897160a1a3ae 67 void init()
el17oc 1:897160a1a3ae 68 {
el17oc 16:9500059ad5d8 69 lcd.init(); //initialises the lcd
el17oc 16:9500059ad5d8 70 pad.init(); //initialises the gamepad
el17oc 16:9500059ad5d8 71 snake.init(); //initialises snake class
el17oc 14:7fb3c93343b6 72
el17oc 14:7fb3c93343b6 73
eencae 0:b7f1f47bb26a 74 }
eencae 0:b7f1f47bb26a 75
el17oc 14:7fb3c93343b6 76 void update_snake()
el17oc 12:60c856354406 77 {
el17oc 14:7fb3c93343b6 78 snake.get_direction(pad); //sets the direction based on buttons pressed on the Gamepad
el17oc 14:7fb3c93343b6 79 snake.render_clear_tail(lcd); //clears the trailing pixel of the snake before updating position to avoid the snake growing with each move - the trailing pixel would be locked and fixed as 1 in its initial position without this
el17oc 12:60c856354406 80 // =needs to be cleared before _x3, _y3 is updated
el17oc 14:7fb3c93343b6 81 snake.move_snake(); //alters the coordinate of each snake bit in accordance with the Gamepad instruction
el17oc 14:7fb3c93343b6 82 engine.get_LEDs(pad, snake); //Turns on on LEDS based on where the snake is in the box
el17oc 12:60c856354406 83 }
el17oc 6:bf90044188d0 84
el17oc 1:897160a1a3ae 85 void welcome()
el17oc 1:897160a1a3ae 86 {
el17oc 14:7fb3c93343b6 87
el17oc 1:897160a1a3ae 88
el17oc 16:9500059ad5d8 89 while ( pad.start_pressed() == false) { //main menu - wont enter game loop until start is pressed
el17oc 16:9500059ad5d8 90 lcd.printString(" SNAKE ",0,1); //
el17oc 16:9500059ad5d8 91 lcd.printString("COLLECT APPLES",0,3); //game task
el17oc 16:9500059ad5d8 92 lcd.printString(" Press Start ",0,5); //instruction
el17oc 16:9500059ad5d8 93 lcd.setContrast( pad.read_pot1()); //adjust contrast
el17oc 1:897160a1a3ae 94
el17oc 1:897160a1a3ae 95
el17oc 14:7fb3c93343b6 96 pad.leds_off();
el17oc 14:7fb3c93343b6 97 pad.led(1,1);
el17oc 14:7fb3c93343b6 98 wait(0.2);
el17oc 14:7fb3c93343b6 99 pad.led(1,0); //turning on the LEDS in a sequence which reflects the snakes movement
el17oc 14:7fb3c93343b6 100 pad.led(2,1);
el17oc 14:7fb3c93343b6 101 wait (0.2);
el17oc 14:7fb3c93343b6 102 pad.led(2,0);
el17oc 14:7fb3c93343b6 103 pad.led(3,1);
el17oc 14:7fb3c93343b6 104 wait(0.2);
el17oc 14:7fb3c93343b6 105 pad.led(3,0);
el17oc 14:7fb3c93343b6 106 wait(0.1);
el17oc 14:7fb3c93343b6 107 pad.led(6,1);
el17oc 14:7fb3c93343b6 108 wait (0.2);
el17oc 14:7fb3c93343b6 109 pad.led(6,0);
el17oc 14:7fb3c93343b6 110 pad.led(5,1);
el17oc 14:7fb3c93343b6 111 wait (0.2);
el17oc 14:7fb3c93343b6 112 pad.led(5,0);
el17oc 14:7fb3c93343b6 113 pad.led(4,1);
el17oc 14:7fb3c93343b6 114 wait(0.2);
el17oc 14:7fb3c93343b6 115
el17oc 16:9500059ad5d8 116 lcd.refresh(); //updates screen
el17oc 14:7fb3c93343b6 117 }
el17oc 12:60c856354406 118
el17oc 1:897160a1a3ae 119 }
el17oc 1:897160a1a3ae 120