ELEC2645 (2019/20)
/
ELEC2645_Project_el17oc1
Owen Cavender 201159294
main.cpp@14:7fb3c93343b6, 2020-05-30 (annotated)
- Committer:
- el17oc
- Date:
- Sat May 30 02:31:43 2020 +0000
- Revision:
- 14:7fb3c93343b6
- Parent:
- 12:60c856354406
- Child:
- 16:9500059ad5d8
ff
Who changed what in which revision?
User | Revision | Line number | New 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 | 14:7fb3c93343b6 | 14 | moving a character or checking for collisions. The final step is to update the display and |
el17oc | 14:7fb3c93343b6 | 15 | |
el17oc | 4:748e3ff14e72 | 16 | this on the LCD. There is usual//// |
eencae | 0:b7f1f47bb26a | 17 | */ |
eencae | 0:b7f1f47bb26a | 18 | |
eencae | 0:b7f1f47bb26a | 19 | // includes |
eencae | 0:b7f1f47bb26a | 20 | #include "mbed.h" |
eencae | 0:b7f1f47bb26a | 21 | #include "Gamepad.h" |
eencae | 0:b7f1f47bb26a | 22 | #include "N5110.h" |
el17oc | 1:897160a1a3ae | 23 | #include "snake.h" |
el17oc | 8:997f90c88246 | 24 | #include "GameEngine.h" |
el17oc | 8:997f90c88246 | 25 | |
el17oc | 14:7fb3c93343b6 | 26 | //write spec for game |
el17oc | 6:bf90044188d0 | 27 | |
el17oc | 6:bf90044188d0 | 28 | |
el17oc | 1:897160a1a3ae | 29 | |
el17oc | 1:897160a1a3ae | 30 | //objects |
el17oc | 1:897160a1a3ae | 31 | Gamepad pad; |
el17oc | 1:897160a1a3ae | 32 | Snake snake; |
el17oc | 1:897160a1a3ae | 33 | N5110 lcd; |
el17oc | 8:997f90c88246 | 34 | GameEngine engine; |
el17oc | 14:7fb3c93343b6 | 35 | |
el17oc | 1:897160a1a3ae | 36 | //functions |
el17oc | 1:897160a1a3ae | 37 | void welcome(); |
el17oc | 1:897160a1a3ae | 38 | void render(); |
el17oc | 1:897160a1a3ae | 39 | void init(); |
el17oc | 14:7fb3c93343b6 | 40 | void update_snake(); |
el17oc | 12:60c856354406 | 41 | |
el17oc | 1:897160a1a3ae | 42 | |
el17oc | 6:bf90044188d0 | 43 | //Serial pc(USBTX, USBRX); |
el17oc | 6:bf90044188d0 | 44 | |
el17oc | 6:bf90044188d0 | 45 | int main() |
el17oc | 1:897160a1a3ae | 46 | { |
el17oc | 10:ee781d18e0f6 | 47 | |
el17oc | 14:7fb3c93343b6 | 48 | //int fps = 6; |
el17oc | 12:60c856354406 | 49 | init(); // frames per second |
el17oc | 1:897160a1a3ae | 50 | welcome(); |
el17oc | 14:7fb3c93343b6 | 51 | |
el17oc | 12:60c856354406 | 52 | // initialise and then display welcome screen... |
el17oc | 12:60c856354406 | 53 | |
el17oc | 1:897160a1a3ae | 54 | while (1) { |
el17oc | 12:60c856354406 | 55 | |
el17oc | 12:60c856354406 | 56 | |
el17oc | 14:7fb3c93343b6 | 57 | update_snake(); |
el17oc | 12:60c856354406 | 58 | snake.check_gameover(lcd); |
el17oc | 14:7fb3c93343b6 | 59 | snake.gameover_true(lcd, pad); |
el17oc | 14:7fb3c93343b6 | 60 | |
el17oc | 12:60c856354406 | 61 | snake.apple_collected(lcd, pad); |
el17oc | 12:60c856354406 | 62 | snake.get_Apple_position(lcd); |
el17oc | 12:60c856354406 | 63 | |
el17oc | 14:7fb3c93343b6 | 64 | |
el17oc | 9:a69a6a06dddf | 65 | engine.get_LEDs(pad, snake); |
el17oc | 14:7fb3c93343b6 | 66 | engine.print_countdown(lcd, snake); |
el17oc | 12:60c856354406 | 67 | snake.render(lcd); |
el17oc | 14:7fb3c93343b6 | 68 | // snake.print_display_time(lcd); |
el17oc | 6:bf90044188d0 | 69 | |
el17oc | 6:bf90044188d0 | 70 | |
el17oc | 14:7fb3c93343b6 | 71 | wait(0.01); |
el17oc | 1:897160a1a3ae | 72 | } |
el17oc | 1:897160a1a3ae | 73 | |
el17oc | 1:897160a1a3ae | 74 | } |
el17oc | 1:897160a1a3ae | 75 | |
el17oc | 12:60c856354406 | 76 | //apples //timer check |
eencae | 0:b7f1f47bb26a | 77 | |
el17oc | 1:897160a1a3ae | 78 | void init() |
el17oc | 1:897160a1a3ae | 79 | { |
el17oc | 14:7fb3c93343b6 | 80 | lcd.init(); |
el17oc | 1:897160a1a3ae | 81 | pad.init(); |
el17oc | 14:7fb3c93343b6 | 82 | snake.init(); //need to initialise snake class |
el17oc | 14:7fb3c93343b6 | 83 | |
el17oc | 14:7fb3c93343b6 | 84 | |
eencae | 0:b7f1f47bb26a | 85 | } |
eencae | 0:b7f1f47bb26a | 86 | |
el17oc | 14:7fb3c93343b6 | 87 | void update_snake() |
el17oc | 12:60c856354406 | 88 | { |
el17oc | 14:7fb3c93343b6 | 89 | snake.get_direction(pad); //sets the direction based on buttons pressed on the Gamepad |
el17oc | 14:7fb3c93343b6 | 90 | 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 | 91 | // =needs to be cleared before _x3, _y3 is updated |
el17oc | 14:7fb3c93343b6 | 92 | snake.move_snake(); //alters the coordinate of each snake bit in accordance with the Gamepad instruction |
el17oc | 14:7fb3c93343b6 | 93 | engine.get_LEDs(pad, snake); //Turns on on LEDS based on where the snake is in the box |
el17oc | 12:60c856354406 | 94 | } |
el17oc | 6:bf90044188d0 | 95 | |
el17oc | 1:897160a1a3ae | 96 | void welcome() |
el17oc | 1:897160a1a3ae | 97 | { |
el17oc | 14:7fb3c93343b6 | 98 | |
el17oc | 1:897160a1a3ae | 99 | |
el17oc | 1:897160a1a3ae | 100 | while ( pad.start_pressed() == false) { |
el17oc | 14:7fb3c93343b6 | 101 | lcd.printString(" SNAKE ",0,1); |
el17oc | 14:7fb3c93343b6 | 102 | lcd.printString("COLLECT APPLES",0,3); |
el17oc | 14:7fb3c93343b6 | 103 | lcd.printString(" Press Start ",0,5); |
el17oc | 1:897160a1a3ae | 104 | lcd.setContrast( pad.read_pot1()); |
el17oc | 1:897160a1a3ae | 105 | |
el17oc | 1:897160a1a3ae | 106 | |
el17oc | 14:7fb3c93343b6 | 107 | pad.leds_off(); |
el17oc | 14:7fb3c93343b6 | 108 | pad.led(1,1); |
el17oc | 14:7fb3c93343b6 | 109 | wait(0.2); |
el17oc | 14:7fb3c93343b6 | 110 | pad.led(1,0); //turning on the LEDS in a sequence which reflects the snakes movement |
el17oc | 14:7fb3c93343b6 | 111 | pad.led(2,1); |
el17oc | 14:7fb3c93343b6 | 112 | wait (0.2); |
el17oc | 14:7fb3c93343b6 | 113 | pad.led(2,0); |
el17oc | 14:7fb3c93343b6 | 114 | pad.led(3,1); |
el17oc | 14:7fb3c93343b6 | 115 | wait(0.2); |
el17oc | 14:7fb3c93343b6 | 116 | pad.led(3,0); |
el17oc | 14:7fb3c93343b6 | 117 | wait(0.1); |
el17oc | 14:7fb3c93343b6 | 118 | pad.led(6,1); |
el17oc | 14:7fb3c93343b6 | 119 | wait (0.2); |
el17oc | 14:7fb3c93343b6 | 120 | pad.led(6,0); |
el17oc | 14:7fb3c93343b6 | 121 | pad.led(5,1); |
el17oc | 14:7fb3c93343b6 | 122 | wait (0.2); |
el17oc | 14:7fb3c93343b6 | 123 | pad.led(5,0); |
el17oc | 14:7fb3c93343b6 | 124 | pad.led(4,1); |
el17oc | 14:7fb3c93343b6 | 125 | wait(0.2); |
el17oc | 14:7fb3c93343b6 | 126 | |
el17oc | 14:7fb3c93343b6 | 127 | lcd.refresh(); |
el17oc | 14:7fb3c93343b6 | 128 | } |
el17oc | 12:60c856354406 | 129 | |
el17oc | 1:897160a1a3ae | 130 | } |
el17oc | 1:897160a1a3ae | 131 | |
el17oc | 1:897160a1a3ae | 132 | |
el17oc | 1:897160a1a3ae | 133 | |
el17oc | 1:897160a1a3ae | 134 | |
el17oc | 1:897160a1a3ae | 135 | |
el17oc | 1:897160a1a3ae | 136 | //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 | 137 | //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 | 138 | //has button button been pressed - if so change direction accordingly |
el17oc | 1:897160a1a3ae | 139 | //max length - when length = width x height - 1 - game complete |
el17oc | 1:897160a1a3ae | 140 | //each pixel of length must follow the front pixel |
el17oc | 1:897160a1a3ae | 141 | // 2 modes - time race - "get an apple before time runs out" - resets timer --- then classic snake |
el17oc | 1:897160a1a3ae | 142 | |
el17oc | 1:897160a1a3ae | 143 | |
el17oc | 1:897160a1a3ae | 144 | |
el17oc | 1:897160a1a3ae | 145 | //need to add audio, leds, (main menu end menu and entering and leaving the while(1) loop), time trial mode, .h files |