![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
Owen Cavender 201159294
main.cpp@11:e7c56013acd9, 2020-05-28 (annotated)
- Committer:
- el17oc
- Date:
- Thu May 28 21:59:12 2020 +0000
- Revision:
- 11:e7c56013acd9
- Parent:
- 10:ee781d18e0f6
messy test
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 | 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 | 11:e7c56013acd9 | 41 | void _time(); |
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 | 1:897160a1a3ae | 48 | int fps = 6; // frames per second |
el17oc | 1:897160a1a3ae | 49 | |
el17oc | 1:897160a1a3ae | 50 | init(); // initialise and then display welcome screen... |
el17oc | 1:897160a1a3ae | 51 | welcome(); |
el17oc | 1:897160a1a3ae | 52 | wait(1.0f/fps); // and wait for one frame period |
el17oc | 1:897160a1a3ae | 53 | render(); |
el17oc | 6:bf90044188d0 | 54 | timer.start(); //ROB atm the code works to here |
el17oc | 6:bf90044188d0 | 55 | //need to draw an initial snake |
el17oc | 8:997f90c88246 | 56 | |
el17oc | 1:897160a1a3ae | 57 | while (1) { |
el17oc | 8:997f90c88246 | 58 | |
el17oc | 8:997f90c88246 | 59 | snake.get_direction(pad); |
el17oc | 10:ee781d18e0f6 | 60 | snake.render_clear_tail(lcd); |
el17oc | 10:ee781d18e0f6 | 61 | engine.get_LEDs(pad, snake); // =needs to be cleared before _x3, _y3 is updated |
el17oc | 11:e7c56013acd9 | 62 | // snake.move_snake(); |
el17oc | 8:997f90c88246 | 63 | |
el17oc | 6:bf90044188d0 | 64 | snake.check_collisions(); |
el17oc | 9:a69a6a06dddf | 65 | snake.apple_collected(lcd, pad, timer); |
el17oc | 8:997f90c88246 | 66 | |
el17oc | 11:e7c56013acd9 | 67 | _time(); |
el17oc | 9:a69a6a06dddf | 68 | engine.get_LEDs(pad, snake); |
el17oc | 11:e7c56013acd9 | 69 | |
el17oc | 6:bf90044188d0 | 70 | gameover_true(); |
el17oc | 8:997f90c88246 | 71 | snake.render(lcd); |
el17oc | 11:e7c56013acd9 | 72 | // snake.print_display_time(lcd); |
el17oc | 8:997f90c88246 | 73 | |
el17oc | 6:bf90044188d0 | 74 | |
el17oc | 6:bf90044188d0 | 75 | wait(1.0f/fps); |
el17oc | 11:e7c56013acd9 | 76 | } |
el17oc | 11:e7c56013acd9 | 77 | } |
el17oc | 11:e7c56013acd9 | 78 | |
el17oc | 11:e7c56013acd9 | 79 | void _time() |
el17oc | 11:e7c56013acd9 | 80 | { |
el17oc | 11:e7c56013acd9 | 81 | int _display_time; |
el17oc | 11:e7c56013acd9 | 82 | int Reset_value; |
el17oc | 11:e7c56013acd9 | 83 | int _realtime = timer.read(); |
el17oc | 11:e7c56013acd9 | 84 | _display_time = Reset_value - _realtime; |
el17oc | 11:e7c56013acd9 | 85 | |
el17oc | 11:e7c56013acd9 | 86 | if(_realtime == Reset_value) { |
el17oc | 11:e7c56013acd9 | 87 | lcd.printString("timer workd", 5,5); |
el17oc | 1:897160a1a3ae | 88 | } |
el17oc | 1:897160a1a3ae | 89 | |
el17oc | 11:e7c56013acd9 | 90 | /*/ if (0 <= _score && _score < 10) { |
el17oc | 11:e7c56013acd9 | 91 | Reset_value = 12; |
el17oc | 11:e7c56013acd9 | 92 | } |
el17oc | 11:e7c56013acd9 | 93 | // if (10 <= _score && _score < 15) { |
el17oc | 11:e7c56013acd9 | 94 | Reset_value = 10; |
el17oc | 11:e7c56013acd9 | 95 | } |
el17oc | 11:e7c56013acd9 | 96 | if (15 <= _score && _score < 20) { |
el17oc | 11:e7c56013acd9 | 97 | Reset_value = 8; |
el17oc | 11:e7c56013acd9 | 98 | } else { |
el17oc | 11:e7c56013acd9 | 99 | Reset_value = 6; |
el17oc | 11:e7c56013acd9 | 100 | } |
el17oc | 1:897160a1a3ae | 101 | } |
el17oc | 11:e7c56013acd9 | 102 | */ |
el17oc | 11:e7c56013acd9 | 103 | |
el17oc | 11:e7c56013acd9 | 104 | |
el17oc | 11:e7c56013acd9 | 105 | |
el17oc | 11:e7c56013acd9 | 106 | |
el17oc | 11:e7c56013acd9 | 107 | |
el17oc | 11:e7c56013acd9 | 108 | |
el17oc | 11:e7c56013acd9 | 109 | |
el17oc | 11:e7c56013acd9 | 110 | //void Snake::print_display_time(N5110 &lcd) |
el17oc | 11:e7c56013acd9 | 111 | |
el17oc | 11:e7c56013acd9 | 112 | // int countdown = _realtime; |
el17oc | 11:e7c56013acd9 | 113 | |
el17oc | 11:e7c56013acd9 | 114 | |
el17oc | 11:e7c56013acd9 | 115 | |
el17oc | 11:e7c56013acd9 | 116 | // _realtime; |
el17oc | 11:e7c56013acd9 | 117 | |
el17oc | 11:e7c56013acd9 | 118 | char buffer1[14]; |
el17oc | 11:e7c56013acd9 | 119 | sprintf(buffer1,"%2d",_realtime); |
el17oc | 11:e7c56013acd9 | 120 | lcd.printString(buffer1,WIDTH/2,30); // font is 8 wide, so leave 4 pixel gape from middle assuming two digits |
el17oc | 11:e7c56013acd9 | 121 | char buffer2[14]; |
el17oc | 11:e7c56013acd9 | 122 | |
el17oc | 11:e7c56013acd9 | 123 | } |
el17oc | 11:e7c56013acd9 | 124 | |
el17oc | 1:897160a1a3ae | 125 | |
el17oc | 10:ee781d18e0f6 | 126 | //added old score - changed postion of timer and code |
eencae | 0:b7f1f47bb26a | 127 | |
el17oc | 1:897160a1a3ae | 128 | void init() |
el17oc | 1:897160a1a3ae | 129 | { |
el17oc | 1:897160a1a3ae | 130 | |
el17oc | 6:bf90044188d0 | 131 | snake.init(); //need to initialise snake class |
el17oc | 1:897160a1a3ae | 132 | pad.init(); |
el17oc | 1:897160a1a3ae | 133 | lcd.init(); |
eencae | 0:b7f1f47bb26a | 134 | } |
eencae | 0:b7f1f47bb26a | 135 | |
el17oc | 6:bf90044188d0 | 136 | |
el17oc | 6:bf90044188d0 | 137 | |
el17oc | 1:897160a1a3ae | 138 | void welcome() |
el17oc | 1:897160a1a3ae | 139 | { |
el17oc | 1:897160a1a3ae | 140 | lcd.printString(" SNAKE ",0,1); |
el17oc | 1:897160a1a3ae | 141 | lcd.printString(" Press Start ",0,4); |
el17oc | 1:897160a1a3ae | 142 | |
el17oc | 1:897160a1a3ae | 143 | while ( pad.start_pressed() == false) { |
el17oc | 1:897160a1a3ae | 144 | lcd.setContrast( pad.read_pot1()); |
el17oc | 1:897160a1a3ae | 145 | pad.leds_on(); |
el17oc | 1:897160a1a3ae | 146 | wait(0.1); |
el17oc | 1:897160a1a3ae | 147 | pad.leds_off(); |
el17oc | 1:897160a1a3ae | 148 | wait(0.1); |
el17oc | 1:897160a1a3ae | 149 | } |
el17oc | 8:997f90c88246 | 150 | lcd.refresh(); |
el17oc | 1:897160a1a3ae | 151 | } |
el17oc | 1:897160a1a3ae | 152 | |
el17oc | 1:897160a1a3ae | 153 | |
el17oc | 1:897160a1a3ae | 154 | void render() |
el17oc | 1:897160a1a3ae | 155 | { |
el17oc | 1:897160a1a3ae | 156 | lcd.clear(); |
el17oc | 1:897160a1a3ae | 157 | lcd.drawRect(0, 0, 84, 42, FILL_TRANSPARENT); |
el17oc | 6:bf90044188d0 | 158 | lcd.printString(" GO! ", 0,25); |
el17oc | 6:bf90044188d0 | 159 | wait(1.0); |
el17oc | 5:d716013c6a18 | 160 | lcd.refresh(); |
el17oc | 1:897160a1a3ae | 161 | } |
el17oc | 1:897160a1a3ae | 162 | |
el17oc | 1:897160a1a3ae | 163 | |
el17oc | 1:897160a1a3ae | 164 | |
el17oc | 1:897160a1a3ae | 165 | |
el17oc | 6:bf90044188d0 | 166 | void gameover_true() //taking action if crash has occured |
el17oc | 6:bf90044188d0 | 167 | { |
el17oc | 6:bf90044188d0 | 168 | bool gameover = snake.get_gameover(); |
el17oc | 6:bf90044188d0 | 169 | int score = snake.get_score(); |
el17oc | 6:bf90044188d0 | 170 | |
el17oc | 6:bf90044188d0 | 171 | while (gameover == true) { |
el17oc | 6:bf90044188d0 | 172 | lcd.clear(); |
el17oc | 6:bf90044188d0 | 173 | lcd.printString( " Game Over L ", 0, 2 ); |
el17oc | 9:a69a6a06dddf | 174 | engine.print_scores(lcd, snake); |
el17oc | 6:bf90044188d0 | 175 | |
el17oc | 6:bf90044188d0 | 176 | if (score <= 7) { |
el17oc | 6:bf90044188d0 | 177 | lcd.printString(" Loser ", 0,10); |
el17oc | 6:bf90044188d0 | 178 | } |
el17oc | 6:bf90044188d0 | 179 | if (7< score <= 25) { |
el17oc | 6:bf90044188d0 | 180 | lcd.printString(" Good Job! ", 0,10); |
el17oc | 6:bf90044188d0 | 181 | } else { |
el17oc | 6:bf90044188d0 | 182 | lcd.printString(" SNAKE PRO ", 0,10); |
el17oc | 6:bf90044188d0 | 183 | lcd.printString(" ~.~.~.~.<8>~ ", 0, 5); |
el17oc | 6:bf90044188d0 | 184 | } |
el17oc | 10:ee781d18e0f6 | 185 | wait(3); |
el17oc | 10:ee781d18e0f6 | 186 | |
el17oc | 10:ee781d18e0f6 | 187 | lcd.refresh(); |
el17oc | 6:bf90044188d0 | 188 | } |
el17oc | 10:ee781d18e0f6 | 189 | } |
el17oc | 1:897160a1a3ae | 190 | |
el17oc | 1:897160a1a3ae | 191 | //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 | 192 | //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 | 193 | //has button button been pressed - if so change direction accordingly |
el17oc | 1:897160a1a3ae | 194 | //max length - when length = width x height - 1 - game complete |
el17oc | 1:897160a1a3ae | 195 | //each pixel of length must follow the front pixel |
el17oc | 1:897160a1a3ae | 196 | // 2 modes - time race - "get an apple before time runs out" - resets timer --- then classic snake |
el17oc | 1:897160a1a3ae | 197 | |
el17oc | 1:897160a1a3ae | 198 | |
el17oc | 1:897160a1a3ae | 199 | |
el17oc | 1:897160a1a3ae | 200 | //need to add audio, leds, (main menu end menu and entering and leaving the while(1) loop), time trial mode, .h files |