Owen Cavender 201159294

Dependencies:   mbed Gamepad2

Committer:
el17oc
Date:
Thu May 28 20:58:49 2020 +0000
Revision:
10:ee781d18e0f6
Parent:
9:a69a6a06dddf
Child:
11:e7c56013acd9
Child:
12:60c856354406
kkk

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 1:897160a1a3ae 7 Name: Owen Cavender
el17oc 1:897160a1a3ae 8 Username: el17oc
el17oc 1:897160a1a3ae 9 Student ID Number: 201159294
el17oc 1:897160a1a3ae 10 Date: 20/04/2020
el17oc 1:897160a1a3ae 11
el17oc 1:897160a1a3ae 12 The game loop will be entered after the game is initialised. The first step is to process the
el17oc 6:bf90044188d0 13 user input (if any). The next step is to update the game apple depending on the input i.e.
el17oc 1:897160a1a3ae 14 moving a character or checking for collisions. The final step is to update the display and render
el17oc 4:748e3ff14e72 15 this on the LCD. There is usual////
eencae 0:b7f1f47bb26a 16 */
eencae 0:b7f1f47bb26a 17
eencae 0:b7f1f47bb26a 18 // includes
eencae 0:b7f1f47bb26a 19 #include "mbed.h"
eencae 0:b7f1f47bb26a 20 #include "Gamepad.h"
eencae 0:b7f1f47bb26a 21 #include "N5110.h"
el17oc 1:897160a1a3ae 22 #include "snake.h"
el17oc 8:997f90c88246 23 #include "GameEngine.h"
el17oc 8:997f90c88246 24
el17oc 6:bf90044188d0 25
el17oc 6:bf90044188d0 26
el17oc 1:897160a1a3ae 27
el17oc 1:897160a1a3ae 28 //objects
el17oc 1:897160a1a3ae 29 Gamepad pad;
el17oc 1:897160a1a3ae 30 Snake snake;
el17oc 1:897160a1a3ae 31 N5110 lcd;
el17oc 6:bf90044188d0 32 Timer timer;
el17oc 8:997f90c88246 33 GameEngine engine;
el17oc 1:897160a1a3ae 34 //functions
el17oc 1:897160a1a3ae 35
el17oc 1:897160a1a3ae 36 void welcome();
el17oc 1:897160a1a3ae 37 void render();
el17oc 1:897160a1a3ae 38 void init();
el17oc 1:897160a1a3ae 39 void update_game_state();
el17oc 6:bf90044188d0 40 void gameover_true();
el17oc 1:897160a1a3ae 41
el17oc 6:bf90044188d0 42 //Serial pc(USBTX, USBRX);
el17oc 6:bf90044188d0 43
el17oc 6:bf90044188d0 44 int main()
el17oc 1:897160a1a3ae 45 {
el17oc 10:ee781d18e0f6 46
el17oc 1:897160a1a3ae 47 int fps = 6; // frames per second
el17oc 1:897160a1a3ae 48
el17oc 1:897160a1a3ae 49 init(); // initialise and then display welcome screen...
el17oc 1:897160a1a3ae 50 welcome();
el17oc 1:897160a1a3ae 51 wait(1.0f/fps); // and wait for one frame period
el17oc 1:897160a1a3ae 52 render();
el17oc 6:bf90044188d0 53 timer.start(); //ROB atm the code works to here
el17oc 6:bf90044188d0 54 //need to draw an initial snake
el17oc 8:997f90c88246 55
el17oc 1:897160a1a3ae 56 while (1) {
el17oc 8:997f90c88246 57
el17oc 8:997f90c88246 58 snake.get_direction(pad);
el17oc 10:ee781d18e0f6 59 snake.render_clear_tail(lcd);
el17oc 10:ee781d18e0f6 60 engine.get_LEDs(pad, snake); // =needs to be cleared before _x3, _y3 is updated
el17oc 8:997f90c88246 61 snake.move_snake();
el17oc 8:997f90c88246 62
el17oc 6:bf90044188d0 63 snake.check_collisions();
el17oc 9:a69a6a06dddf 64 snake.apple_collected(lcd, pad, timer);
el17oc 8:997f90c88246 65
el17oc 9:a69a6a06dddf 66 snake.get_time(timer);
el17oc 9:a69a6a06dddf 67 engine.get_LEDs(pad, snake);
el17oc 8:997f90c88246 68
el17oc 6:bf90044188d0 69
el17oc 6:bf90044188d0 70 gameover_true();
el17oc 8:997f90c88246 71 snake.render(lcd);
el17oc 10:ee781d18e0f6 72 snake.print_display_time(lcd);
el17oc 8:997f90c88246 73
el17oc 6:bf90044188d0 74
el17oc 6:bf90044188d0 75 wait(1.0f/fps);
el17oc 1:897160a1a3ae 76 }
el17oc 1:897160a1a3ae 77
el17oc 1:897160a1a3ae 78 }
el17oc 1:897160a1a3ae 79
el17oc 10:ee781d18e0f6 80 //added old score - changed postion of timer and code
eencae 0:b7f1f47bb26a 81
el17oc 1:897160a1a3ae 82 void init()
el17oc 1:897160a1a3ae 83 {
el17oc 1:897160a1a3ae 84
el17oc 6:bf90044188d0 85 snake.init(); //need to initialise snake class
el17oc 1:897160a1a3ae 86 pad.init();
el17oc 1:897160a1a3ae 87 lcd.init();
eencae 0:b7f1f47bb26a 88 }
eencae 0:b7f1f47bb26a 89
el17oc 6:bf90044188d0 90
el17oc 6:bf90044188d0 91
el17oc 1:897160a1a3ae 92 void welcome()
el17oc 1:897160a1a3ae 93 {
el17oc 8:997f90c88246 94
el17oc 10:ee781d18e0f6 95
el17oc 1:897160a1a3ae 96 lcd.printString(" SNAKE ",0,1);
el17oc 1:897160a1a3ae 97 lcd.printString(" Press Start ",0,4);
el17oc 1:897160a1a3ae 98
el17oc 1:897160a1a3ae 99 while ( pad.start_pressed() == false) {
el17oc 1:897160a1a3ae 100 lcd.setContrast( pad.read_pot1());
el17oc 1:897160a1a3ae 101 pad.leds_on();
el17oc 1:897160a1a3ae 102 wait(0.1);
el17oc 1:897160a1a3ae 103 pad.leds_off();
el17oc 1:897160a1a3ae 104 wait(0.1);
el17oc 1:897160a1a3ae 105 }
el17oc 8:997f90c88246 106 lcd.refresh();
el17oc 1:897160a1a3ae 107 }
el17oc 1:897160a1a3ae 108
el17oc 1:897160a1a3ae 109
el17oc 1:897160a1a3ae 110 void render()
el17oc 1:897160a1a3ae 111 {
el17oc 1:897160a1a3ae 112 lcd.clear();
el17oc 1:897160a1a3ae 113 lcd.drawRect(0, 0, 84, 42, FILL_TRANSPARENT);
el17oc 6:bf90044188d0 114 lcd.printString(" GO! ", 0,25);
el17oc 6:bf90044188d0 115 wait(1.0);
el17oc 5:d716013c6a18 116 lcd.refresh();
el17oc 1:897160a1a3ae 117 }
el17oc 1:897160a1a3ae 118
el17oc 1:897160a1a3ae 119
el17oc 1:897160a1a3ae 120
el17oc 1:897160a1a3ae 121
el17oc 6:bf90044188d0 122 void gameover_true() //taking action if crash has occured
el17oc 6:bf90044188d0 123 {
el17oc 6:bf90044188d0 124 bool gameover = snake.get_gameover();
el17oc 6:bf90044188d0 125 int score = snake.get_score();
el17oc 6:bf90044188d0 126
el17oc 6:bf90044188d0 127 while (gameover == true) {
el17oc 6:bf90044188d0 128 lcd.clear();
el17oc 6:bf90044188d0 129 lcd.printString( " Game Over L ", 0, 2 );
el17oc 9:a69a6a06dddf 130 engine.print_scores(lcd, snake);
el17oc 6:bf90044188d0 131
el17oc 6:bf90044188d0 132 if (score <= 7) {
el17oc 6:bf90044188d0 133 lcd.printString(" Loser ", 0,10);
el17oc 6:bf90044188d0 134 }
el17oc 6:bf90044188d0 135 if (7< score <= 25) {
el17oc 6:bf90044188d0 136 lcd.printString(" Good Job! ", 0,10);
el17oc 6:bf90044188d0 137 } else {
el17oc 6:bf90044188d0 138 lcd.printString(" SNAKE PRO ", 0,10);
el17oc 6:bf90044188d0 139 lcd.printString(" ~.~.~.~.<8>~ ", 0, 5);
el17oc 6:bf90044188d0 140 }
el17oc 10:ee781d18e0f6 141 wait(3);
el17oc 10:ee781d18e0f6 142
el17oc 10:ee781d18e0f6 143 lcd.refresh();
el17oc 6:bf90044188d0 144 }
el17oc 10:ee781d18e0f6 145 }
el17oc 1:897160a1a3ae 146
el17oc 1:897160a1a3ae 147 //has an apple been collected? if so increase length by 1 increase score by 1 and makes coin collecting noise and spawn new apple
el17oc 1:897160a1a3ae 148 //has snake touched itself or wall - if so end game - present score - make xxx sound fail --- apple needs to be different to the snakes body and different to wall
el17oc 1:897160a1a3ae 149 //has button button been pressed - if so change direction accordingly
el17oc 1:897160a1a3ae 150 //max length - when length = width x height - 1 - game complete
el17oc 1:897160a1a3ae 151 //each pixel of length must follow the front pixel
el17oc 1:897160a1a3ae 152 // 2 modes - time race - "get an apple before time runs out" - resets timer --- then classic snake
el17oc 1:897160a1a3ae 153
el17oc 1:897160a1a3ae 154
el17oc 1:897160a1a3ae 155
el17oc 1:897160a1a3ae 156 //need to add audio, leds, (main menu end menu and entering and leaving the while(1) loop), time trial mode, .h files