Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
main.cpp@12:60c856354406, 2020-05-29 (annotated)
- Committer:
- el17oc
- Date:
- Fri May 29 16:00:56 2020 +0000
- Revision:
- 12:60c856354406
- Parent:
- 10:ee781d18e0f6
- Child:
- 14:7fb3c93343b6
HH
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 | 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 | 12:60c856354406 | 32 | Ticker ticker; |
| 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 | 12:60c856354406 | 41 | void ticker_isr(); |
| el17oc | 12:60c856354406 | 42 | |
| el17oc | 12:60c856354406 | 43 | int countdown; |
| el17oc | 12:60c856354406 | 44 | |
| el17oc | 12:60c856354406 | 45 | |
| el17oc | 1:897160a1a3ae | 46 | |
| el17oc | 6:bf90044188d0 | 47 | //Serial pc(USBTX, USBRX); |
| el17oc | 6:bf90044188d0 | 48 | |
| el17oc | 6:bf90044188d0 | 49 | int main() |
| el17oc | 1:897160a1a3ae | 50 | { |
| el17oc | 10:ee781d18e0f6 | 51 | |
| el17oc | 12:60c856354406 | 52 | int fps = 6; |
| el17oc | 12:60c856354406 | 53 | init(); // frames per second |
| el17oc | 1:897160a1a3ae | 54 | welcome(); |
| el17oc | 12:60c856354406 | 55 | // initialise and then display welcome screen... |
| el17oc | 12:60c856354406 | 56 | |
| el17oc | 12:60c856354406 | 57 | // and wait for one frame period |
| el17oc | 1:897160a1a3ae | 58 | render(); |
| el17oc | 12:60c856354406 | 59 | //ROB atm the code works to here |
| el17oc | 6:bf90044188d0 | 60 | //need to draw an initial snake |
| el17oc | 12:60c856354406 | 61 | |
| el17oc | 1:897160a1a3ae | 62 | while (1) { |
| el17oc | 12:60c856354406 | 63 | |
| el17oc | 12:60c856354406 | 64 | |
| el17oc | 12:60c856354406 | 65 | update_game_state(); |
| el17oc | 12:60c856354406 | 66 | snake.check_gameover(lcd); |
| el17oc | 12:60c856354406 | 67 | snake.gameover_true(lcd); |
| el17oc | 12:60c856354406 | 68 | snake.apple_collected(lcd, pad); |
| el17oc | 12:60c856354406 | 69 | snake.get_Apple_position(lcd); |
| el17oc | 12:60c856354406 | 70 | |
| el17oc | 9:a69a6a06dddf | 71 | engine.get_LEDs(pad, snake); |
| el17oc | 8:997f90c88246 | 72 | |
| el17oc | 12:60c856354406 | 73 | snake.render(lcd); |
| el17oc | 12:60c856354406 | 74 | engine.print_countdown(lcd, snake); |
| el17oc | 12:60c856354406 | 75 | // snake.print_display_time(lcd); |
| el17oc | 6:bf90044188d0 | 76 | |
| el17oc | 6:bf90044188d0 | 77 | |
| el17oc | 12:60c856354406 | 78 | wait(1.0f/6); |
| el17oc | 1:897160a1a3ae | 79 | } |
| el17oc | 1:897160a1a3ae | 80 | |
| el17oc | 1:897160a1a3ae | 81 | } |
| el17oc | 1:897160a1a3ae | 82 | |
| el17oc | 12:60c856354406 | 83 | //apples //timer check |
| eencae | 0:b7f1f47bb26a | 84 | |
| el17oc | 1:897160a1a3ae | 85 | void init() |
| el17oc | 1:897160a1a3ae | 86 | { |
| el17oc | 6:bf90044188d0 | 87 | snake.init(); //need to initialise snake class |
| el17oc | 1:897160a1a3ae | 88 | pad.init(); |
| el17oc | 1:897160a1a3ae | 89 | lcd.init(); |
| eencae | 0:b7f1f47bb26a | 90 | } |
| eencae | 0:b7f1f47bb26a | 91 | |
| el17oc | 12:60c856354406 | 92 | void update_game_state() |
| el17oc | 12:60c856354406 | 93 | { |
| el17oc | 12:60c856354406 | 94 | snake.get_direction(pad); |
| el17oc | 12:60c856354406 | 95 | snake.render_clear_tail(lcd); |
| el17oc | 12:60c856354406 | 96 | // =needs to be cleared before _x3, _y3 is updated |
| el17oc | 12:60c856354406 | 97 | snake.move_snake(); |
| el17oc | 12:60c856354406 | 98 | engine.get_LEDs(pad, snake); |
| el17oc | 12:60c856354406 | 99 | } |
| el17oc | 6:bf90044188d0 | 100 | |
| el17oc | 1:897160a1a3ae | 101 | void welcome() |
| el17oc | 1:897160a1a3ae | 102 | { |
| el17oc | 1:897160a1a3ae | 103 | lcd.printString(" SNAKE ",0,1); |
| el17oc | 12:60c856354406 | 104 | // lcd.printString(" Press Start ",0,4); |
| el17oc | 1:897160a1a3ae | 105 | |
| el17oc | 1:897160a1a3ae | 106 | while ( pad.start_pressed() == false) { |
| el17oc | 1:897160a1a3ae | 107 | lcd.setContrast( pad.read_pot1()); |
| el17oc | 1:897160a1a3ae | 108 | pad.leds_on(); |
| el17oc | 1:897160a1a3ae | 109 | wait(0.1); |
| el17oc | 1:897160a1a3ae | 110 | pad.leds_off(); |
| el17oc | 1:897160a1a3ae | 111 | wait(0.1); |
| el17oc | 1:897160a1a3ae | 112 | } |
| el17oc | 12:60c856354406 | 113 | lcd.refresh(); |
| el17oc | 1:897160a1a3ae | 114 | } |
| el17oc | 1:897160a1a3ae | 115 | |
| el17oc | 1:897160a1a3ae | 116 | |
| el17oc | 1:897160a1a3ae | 117 | void render() |
| el17oc | 1:897160a1a3ae | 118 | { |
| el17oc | 1:897160a1a3ae | 119 | lcd.clear(); |
| el17oc | 1:897160a1a3ae | 120 | lcd.drawRect(0, 0, 84, 42, FILL_TRANSPARENT); |
| el17oc | 6:bf90044188d0 | 121 | lcd.printString(" GO! ", 0,25); |
| el17oc | 6:bf90044188d0 | 122 | wait(1.0); |
| el17oc | 12:60c856354406 | 123 | |
| el17oc | 1:897160a1a3ae | 124 | } |
| el17oc | 1:897160a1a3ae | 125 | |
| el17oc | 1:897160a1a3ae | 126 | |
| el17oc | 1:897160a1a3ae | 127 | |
| el17oc | 1:897160a1a3ae | 128 | |
| el17oc | 12:60c856354406 | 129 | //MAKE TIMER IN ONE CLASS SNAKE |
| el17oc | 12:60c856354406 | 130 | //SPAWN APPLE |
| el17oc | 12:60c856354406 | 131 | //MOVE THE SNAKE --- |
| el17oc | 12:60c856354406 | 132 | // |
| el17oc | 6:bf90044188d0 | 133 | |
| el17oc | 12:60c856354406 | 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 |