![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Owen Cavender 201159294
main.cpp@1:897160a1a3ae, 2020-05-26 (annotated)
- Committer:
- el17oc
- Date:
- Tue May 26 12:17:59 2020 +0000
- Revision:
- 1:897160a1a3ae
- Parent:
- 0:b7f1f47bb26a
- Child:
- 2:44e4a6ecdbef
ELEC2645
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 |
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 | |
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 | 1:897160a1a3ae | 13 | user input (if any). The next step is to update the game engine 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 | 1:897160a1a3ae | 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 | 1:897160a1a3ae | 23 | #include "GameEngine.h" |
el17oc | 1:897160a1a3ae | 24 | |
el17oc | 1:897160a1a3ae | 25 | //objects |
el17oc | 1:897160a1a3ae | 26 | Gamepad pad; |
el17oc | 1:897160a1a3ae | 27 | Snake snake; |
el17oc | 1:897160a1a3ae | 28 | N5110 lcd; |
el17oc | 1:897160a1a3ae | 29 | GameEngine engine; |
el17oc | 1:897160a1a3ae | 30 | //functions |
el17oc | 1:897160a1a3ae | 31 | |
el17oc | 1:897160a1a3ae | 32 | void welcome(); |
el17oc | 1:897160a1a3ae | 33 | void render(); |
el17oc | 1:897160a1a3ae | 34 | void init(); |
el17oc | 1:897160a1a3ae | 35 | void update_game_state(); |
el17oc | 1:897160a1a3ae | 36 | |
el17oc | 1:897160a1a3ae | 37 | int main() //FUNCTION 0 |
el17oc | 1:897160a1a3ae | 38 | { |
el17oc | 1:897160a1a3ae | 39 | |
el17oc | 1:897160a1a3ae | 40 | int fps = 6; // frames per second |
el17oc | 1:897160a1a3ae | 41 | |
el17oc | 1:897160a1a3ae | 42 | init(); // initialise and then display welcome screen... |
el17oc | 1:897160a1a3ae | 43 | welcome(); |
el17oc | 1:897160a1a3ae | 44 | wait(1.0f/fps); // and wait for one frame period |
el17oc | 1:897160a1a3ae | 45 | render(); |
el17oc | 1:897160a1a3ae | 46 | |
el17oc | 1:897160a1a3ae | 47 | // game loop - read input, update the game state and render the display |
el17oc | 1:897160a1a3ae | 48 | while (1) { |
el17oc | 1:897160a1a3ae | 49 | snake.set_direction(pad); |
el17oc | 1:897160a1a3ae | 50 | update_game_state(); |
el17oc | 1:897160a1a3ae | 51 | wait(1.0f/fps); //need to make wait function - relate it to clock |
el17oc | 1:897160a1a3ae | 52 | } |
el17oc | 1:897160a1a3ae | 53 | |
el17oc | 1:897160a1a3ae | 54 | } |
el17oc | 1:897160a1a3ae | 55 | |
eencae | 0:b7f1f47bb26a | 56 | |
eencae | 0:b7f1f47bb26a | 57 | |
el17oc | 1:897160a1a3ae | 58 | void init() |
el17oc | 1:897160a1a3ae | 59 | { |
el17oc | 1:897160a1a3ae | 60 | |
el17oc | 1:897160a1a3ae | 61 | snake.init(); //need to initialise snake class |
el17oc | 1:897160a1a3ae | 62 | pad.init(); |
el17oc | 1:897160a1a3ae | 63 | lcd.init(); |
el17oc | 1:897160a1a3ae | 64 | engine.init(48, 24, 48, 30); // need to choose initial values A$AP shx shy apx apy |
el17oc | 1:897160a1a3ae | 65 | } |
eencae | 0:b7f1f47bb26a | 66 | |
el17oc | 1:897160a1a3ae | 67 | void update_game_state() //FUNCTION 2 |
eencae | 0:b7f1f47bb26a | 68 | { |
el17oc | 1:897160a1a3ae | 69 | |
el17oc | 1:897160a1a3ae | 70 | snake.set_direction(pad); |
el17oc | 1:897160a1a3ae | 71 | snake.move_and_draw_snake(lcd); |
el17oc | 1:897160a1a3ae | 72 | snake.gameover_true(lcd); |
el17oc | 1:897160a1a3ae | 73 | snake.check_if_scored(lcd, pad); |
el17oc | 1:897160a1a3ae | 74 | |
eencae | 0:b7f1f47bb26a | 75 | } |
eencae | 0:b7f1f47bb26a | 76 | |
el17oc | 1:897160a1a3ae | 77 | void welcome() |
el17oc | 1:897160a1a3ae | 78 | { |
el17oc | 1:897160a1a3ae | 79 | |
el17oc | 1:897160a1a3ae | 80 | lcd.printString(" SNAKE ",0,1); |
el17oc | 1:897160a1a3ae | 81 | lcd.printString(" Press Start ",0,4); |
el17oc | 1:897160a1a3ae | 82 | lcd.refresh(); |
el17oc | 1:897160a1a3ae | 83 | |
el17oc | 1:897160a1a3ae | 84 | // wait flashing LEDs until start button is pressed |
el17oc | 1:897160a1a3ae | 85 | while ( pad.start_pressed() == false) { |
el17oc | 1:897160a1a3ae | 86 | lcd.setContrast( pad.read_pot1()); |
el17oc | 1:897160a1a3ae | 87 | pad.leds_on(); |
el17oc | 1:897160a1a3ae | 88 | wait(0.1); |
el17oc | 1:897160a1a3ae | 89 | pad.leds_off(); |
el17oc | 1:897160a1a3ae | 90 | wait(0.1); |
el17oc | 1:897160a1a3ae | 91 | } |
el17oc | 1:897160a1a3ae | 92 | |
el17oc | 1:897160a1a3ae | 93 | } |
el17oc | 1:897160a1a3ae | 94 | |
el17oc | 1:897160a1a3ae | 95 | |
el17oc | 1:897160a1a3ae | 96 | //void render(N5110 &lcd) |
el17oc | 1:897160a1a3ae | 97 | //{ |
el17oc | 1:897160a1a3ae | 98 | void render() |
el17oc | 1:897160a1a3ae | 99 | { |
el17oc | 1:897160a1a3ae | 100 | // clear screen, re-draw and refresh |
el17oc | 1:897160a1a3ae | 101 | lcd.clear(); |
el17oc | 1:897160a1a3ae | 102 | lcd.drawRect(0, 0, 84, 42, FILL_TRANSPARENT); |
el17oc | 1:897160a1a3ae | 103 | lcd.refresh(); |
el17oc | 1:897160a1a3ae | 104 | } |
el17oc | 1:897160a1a3ae | 105 | |
el17oc | 1:897160a1a3ae | 106 | |
el17oc | 1:897160a1a3ae | 107 | // for (int z=0; z<=_length; z++) { |
el17oc | 1:897160a1a3ae | 108 | // lcd.setPixel(snakebody[z].x, snakebody[z].y, 1); |
el17oc | 1:897160a1a3ae | 109 | // } |
el17oc | 1:897160a1a3ae | 110 | //use to draw wall - do i need to draw it everytime or can i do it as constant |
el17oc | 1:897160a1a3ae | 111 | //} |
el17oc | 1:897160a1a3ae | 112 | |
el17oc | 1:897160a1a3ae | 113 | |
el17oc | 1:897160a1a3ae | 114 | |
el17oc | 1:897160a1a3ae | 115 | |
el17oc | 1:897160a1a3ae | 116 | |
el17oc | 1:897160a1a3ae | 117 | |
el17oc | 1:897160a1a3ae | 118 | //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 | 119 | //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 | 120 | //has button button been pressed - if so change direction accordingly |
el17oc | 1:897160a1a3ae | 121 | //max length - when length = width x height - 1 - game complete |
el17oc | 1:897160a1a3ae | 122 | //each pixel of length must follow the front pixel |
el17oc | 1:897160a1a3ae | 123 | // 2 modes - time race - "get an apple before time runs out" - resets timer --- then classic snake |
el17oc | 1:897160a1a3ae | 124 | |
el17oc | 1:897160a1a3ae | 125 | |
el17oc | 1:897160a1a3ae | 126 | |
el17oc | 1:897160a1a3ae | 127 | //need to add audio, leds, (main menu end menu and entering and leaving the while(1) loop), time trial mode, .h files |