ELEC2645 (2019/20)
/
ELEC2645_Project_el17oc1
Owen Cavender 201159294
main.cpp@6:bf90044188d0, 2020-05-28 (annotated)
- Committer:
- el17oc
- Date:
- Thu May 28 15:02:30 2020 +0000
- Revision:
- 6:bf90044188d0
- Parent:
- 5:d716013c6a18
- Child:
- 8:997f90c88246
static snake
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 | 6:bf90044188d0 | 23 | #include "Apple.h" |
el17oc | 6:bf90044188d0 | 24 | |
el17oc | 6:bf90044188d0 | 25 | |
el17oc | 1:897160a1a3ae | 26 | |
el17oc | 1:897160a1a3ae | 27 | //objects |
el17oc | 1:897160a1a3ae | 28 | Gamepad pad; |
el17oc | 1:897160a1a3ae | 29 | Snake snake; |
el17oc | 1:897160a1a3ae | 30 | N5110 lcd; |
el17oc | 6:bf90044188d0 | 31 | Apple apple; |
el17oc | 6:bf90044188d0 | 32 | Timer timer; |
el17oc | 1:897160a1a3ae | 33 | //functions |
el17oc | 1:897160a1a3ae | 34 | |
el17oc | 1:897160a1a3ae | 35 | void welcome(); |
el17oc | 1:897160a1a3ae | 36 | void render(); |
el17oc | 1:897160a1a3ae | 37 | void init(); |
el17oc | 1:897160a1a3ae | 38 | void update_game_state(); |
el17oc | 6:bf90044188d0 | 39 | void gameover_true(); |
el17oc | 1:897160a1a3ae | 40 | |
el17oc | 6:bf90044188d0 | 41 | //Serial pc(USBTX, USBRX); |
el17oc | 6:bf90044188d0 | 42 | |
el17oc | 6:bf90044188d0 | 43 | int main() |
el17oc | 1:897160a1a3ae | 44 | { |
el17oc | 6:bf90044188d0 | 45 | printf("hello world"); |
el17oc | 1:897160a1a3ae | 46 | int fps = 6; // frames per second |
el17oc | 1:897160a1a3ae | 47 | |
el17oc | 1:897160a1a3ae | 48 | init(); // initialise and then display welcome screen... |
el17oc | 1:897160a1a3ae | 49 | welcome(); |
el17oc | 1:897160a1a3ae | 50 | wait(1.0f/fps); // and wait for one frame period |
el17oc | 1:897160a1a3ae | 51 | render(); |
el17oc | 6:bf90044188d0 | 52 | timer.start(); //ROB atm the code works to here |
el17oc | 6:bf90044188d0 | 53 | //need to draw an initial snake |
el17oc | 1:897160a1a3ae | 54 | while (1) { |
el17oc | 6:bf90044188d0 | 55 | ; |
el17oc | 6:bf90044188d0 | 56 | snake.get_position(pad); |
el17oc | 6:bf90044188d0 | 57 | snake.check_collisions(); |
el17oc | 6:bf90044188d0 | 58 | snake.check_score(lcd, pad, apple, timer); |
el17oc | 6:bf90044188d0 | 59 | snake.get_timer(timer); |
el17oc | 6:bf90044188d0 | 60 | |
el17oc | 6:bf90044188d0 | 61 | gameover_true(); |
el17oc | 6:bf90044188d0 | 62 | snake.render(lcd, apple); |
el17oc | 6:bf90044188d0 | 63 | |
el17oc | 6:bf90044188d0 | 64 | wait(1.0f/fps); |
el17oc | 1:897160a1a3ae | 65 | } |
el17oc | 1:897160a1a3ae | 66 | |
el17oc | 1:897160a1a3ae | 67 | } |
el17oc | 1:897160a1a3ae | 68 | |
eencae | 0:b7f1f47bb26a | 69 | |
eencae | 0:b7f1f47bb26a | 70 | |
el17oc | 1:897160a1a3ae | 71 | void init() |
el17oc | 1:897160a1a3ae | 72 | { |
el17oc | 1:897160a1a3ae | 73 | |
el17oc | 6:bf90044188d0 | 74 | snake.init(); //need to initialise snake class |
el17oc | 1:897160a1a3ae | 75 | pad.init(); |
el17oc | 1:897160a1a3ae | 76 | lcd.init(); |
el17oc | 6:bf90044188d0 | 77 | apple.init(48, 24); |
el17oc | 1:897160a1a3ae | 78 | |
eencae | 0:b7f1f47bb26a | 79 | } |
eencae | 0:b7f1f47bb26a | 80 | |
el17oc | 6:bf90044188d0 | 81 | |
el17oc | 6:bf90044188d0 | 82 | |
el17oc | 1:897160a1a3ae | 83 | void welcome() |
el17oc | 1:897160a1a3ae | 84 | { |
el17oc | 6:bf90044188d0 | 85 | lcd.refresh(); |
el17oc | 6:bf90044188d0 | 86 | printf("hello world"); |
el17oc | 1:897160a1a3ae | 87 | lcd.printString(" SNAKE ",0,1); |
el17oc | 1:897160a1a3ae | 88 | lcd.printString(" Press Start ",0,4); |
el17oc | 1:897160a1a3ae | 89 | |
el17oc | 1:897160a1a3ae | 90 | while ( pad.start_pressed() == false) { |
el17oc | 1:897160a1a3ae | 91 | lcd.setContrast( pad.read_pot1()); |
el17oc | 1:897160a1a3ae | 92 | pad.leds_on(); |
el17oc | 1:897160a1a3ae | 93 | wait(0.1); |
el17oc | 1:897160a1a3ae | 94 | pad.leds_off(); |
el17oc | 1:897160a1a3ae | 95 | wait(0.1); |
el17oc | 1:897160a1a3ae | 96 | } |
el17oc | 1:897160a1a3ae | 97 | |
el17oc | 1:897160a1a3ae | 98 | } |
el17oc | 1:897160a1a3ae | 99 | |
el17oc | 1:897160a1a3ae | 100 | |
el17oc | 1:897160a1a3ae | 101 | void render() |
el17oc | 1:897160a1a3ae | 102 | { |
el17oc | 1:897160a1a3ae | 103 | lcd.clear(); |
el17oc | 1:897160a1a3ae | 104 | lcd.drawRect(0, 0, 84, 42, FILL_TRANSPARENT); |
el17oc | 6:bf90044188d0 | 105 | lcd.printString(" GO! ", 0,25); |
el17oc | 6:bf90044188d0 | 106 | wait(1.0); |
el17oc | 5:d716013c6a18 | 107 | lcd.refresh(); |
el17oc | 1:897160a1a3ae | 108 | } |
el17oc | 1:897160a1a3ae | 109 | |
el17oc | 1:897160a1a3ae | 110 | |
el17oc | 1:897160a1a3ae | 111 | |
el17oc | 1:897160a1a3ae | 112 | |
el17oc | 6:bf90044188d0 | 113 | void gameover_true() //taking action if crash has occured |
el17oc | 6:bf90044188d0 | 114 | { |
el17oc | 6:bf90044188d0 | 115 | bool gameover = snake.get_gameover(); |
el17oc | 6:bf90044188d0 | 116 | int score = snake.get_score(); |
el17oc | 6:bf90044188d0 | 117 | |
el17oc | 6:bf90044188d0 | 118 | while (gameover == true) { |
el17oc | 6:bf90044188d0 | 119 | lcd.clear(); |
el17oc | 6:bf90044188d0 | 120 | lcd.refresh(); |
el17oc | 6:bf90044188d0 | 121 | lcd.printString( " Game Over L ", 0, 2 ); |
el17oc | 6:bf90044188d0 | 122 | snake.print_scores(lcd); |
el17oc | 6:bf90044188d0 | 123 | |
el17oc | 6:bf90044188d0 | 124 | if (score <= 7) { |
el17oc | 6:bf90044188d0 | 125 | lcd.printString(" Loser ", 0,10); |
el17oc | 6:bf90044188d0 | 126 | } |
el17oc | 6:bf90044188d0 | 127 | if (7< score <= 25) { |
el17oc | 6:bf90044188d0 | 128 | lcd.printString(" Good Job! ", 0,10); |
el17oc | 6:bf90044188d0 | 129 | } else { |
el17oc | 6:bf90044188d0 | 130 | lcd.printString(" SNAKE PRO ", 0,10); |
el17oc | 6:bf90044188d0 | 131 | lcd.printString(" ~.~.~.~.<8>~ ", 0, 5); |
el17oc | 6:bf90044188d0 | 132 | } |
el17oc | 6:bf90044188d0 | 133 | } |
el17oc | 1:897160a1a3ae | 134 | |
el17oc | 1:897160a1a3ae | 135 | |
el17oc | 6:bf90044188d0 | 136 | } |
el17oc | 1:897160a1a3ae | 137 | |
el17oc | 1:897160a1a3ae | 138 | |
el17oc | 1:897160a1a3ae | 139 | //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 | 140 | //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 | 141 | //has button button been pressed - if so change direction accordingly |
el17oc | 1:897160a1a3ae | 142 | //max length - when length = width x height - 1 - game complete |
el17oc | 1:897160a1a3ae | 143 | //each pixel of length must follow the front pixel |
el17oc | 1:897160a1a3ae | 144 | // 2 modes - time race - "get an apple before time runs out" - resets timer --- then classic snake |
el17oc | 1:897160a1a3ae | 145 | |
el17oc | 1:897160a1a3ae | 146 | |
el17oc | 1:897160a1a3ae | 147 | |
el17oc | 1:897160a1a3ae | 148 | //need to add audio, leds, (main menu end menu and entering and leaving the while(1) loop), time trial mode, .h files |