ELEC2645 (2019/20)
/
ELEC2645_Project_el17oc1
Owen Cavender 201159294
main.cpp@5:d716013c6a18, 2020-05-27 (annotated)
- Committer:
- el17oc
- Date:
- Wed May 27 10:27:34 2020 +0000
- Revision:
- 5:d716013c6a18
- Parent:
- 4:748e3ff14e72
- Child:
- 6:bf90044188d0
not final
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 | 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 | 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 | 2:44e4a6ecdbef | 37 | int main() |
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 | 2:44e4a6ecdbef | 46 | //ROB atm the code works to here |
el17oc | 2:44e4a6ecdbef | 47 | //need to draw an initial snake |
el17oc | 1:897160a1a3ae | 48 | while (1) { |
el17oc | 1:897160a1a3ae | 49 | snake.set_direction(pad); |
el17oc | 1:897160a1a3ae | 50 | update_game_state(); |
el17oc | 2:44e4a6ecdbef | 51 | wait(1.0f/fps); |
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 | 5:d716013c6a18 | 61 | snake.init(engine); //need to initialise snake class |
el17oc | 1:897160a1a3ae | 62 | pad.init(); |
el17oc | 1:897160a1a3ae | 63 | lcd.init(); |
el17oc | 5:d716013c6a18 | 64 | engine.init(48, 24, 48, 30, 48, 23); // need to choose initial values A$AP int shx, int shy, int apx, int apy, int Oshx, int Oshy |
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 | 5:d716013c6a18 | 71 | snake.move_snakebody(engine, lcd); |
el17oc | 5:d716013c6a18 | 72 | |
el17oc | 5:d716013c6a18 | 73 | snake.check_scored(lcd, pad, engine); |
el17oc | 1:897160a1a3ae | 74 | snake.gameover_true(lcd); |
el17oc | 1:897160a1a3ae | 75 | |
eencae | 0:b7f1f47bb26a | 76 | } |
eencae | 0:b7f1f47bb26a | 77 | |
el17oc | 1:897160a1a3ae | 78 | void welcome() |
el17oc | 1:897160a1a3ae | 79 | { |
el17oc | 1:897160a1a3ae | 80 | |
el17oc | 1:897160a1a3ae | 81 | lcd.printString(" SNAKE ",0,1); |
el17oc | 1:897160a1a3ae | 82 | lcd.printString(" Press Start ",0,4); |
el17oc | 1:897160a1a3ae | 83 | lcd.refresh(); |
el17oc | 1:897160a1a3ae | 84 | |
el17oc | 1:897160a1a3ae | 85 | // wait flashing LEDs until start button is pressed |
el17oc | 1:897160a1a3ae | 86 | while ( pad.start_pressed() == false) { |
el17oc | 1:897160a1a3ae | 87 | lcd.setContrast( pad.read_pot1()); |
el17oc | 1:897160a1a3ae | 88 | pad.leds_on(); |
el17oc | 1:897160a1a3ae | 89 | wait(0.1); |
el17oc | 1:897160a1a3ae | 90 | pad.leds_off(); |
el17oc | 1:897160a1a3ae | 91 | wait(0.1); |
el17oc | 1:897160a1a3ae | 92 | } |
el17oc | 1:897160a1a3ae | 93 | |
el17oc | 1:897160a1a3ae | 94 | } |
el17oc | 1:897160a1a3ae | 95 | |
el17oc | 1:897160a1a3ae | 96 | |
el17oc | 1:897160a1a3ae | 97 | void render() |
el17oc | 1:897160a1a3ae | 98 | { |
el17oc | 5:d716013c6a18 | 99 | |
el17oc | 1:897160a1a3ae | 100 | lcd.clear(); |
el17oc | 1:897160a1a3ae | 101 | lcd.drawRect(0, 0, 84, 42, FILL_TRANSPARENT); |
el17oc | 5:d716013c6a18 | 102 | // Vector2D originalhead = engine.get_Snakehead(Snakehead); //draw constant rectangle around frame of lcd indicating walls |
el17oc | 5:d716013c6a18 | 103 | // Vector2D originalbody = engine.get_oldSnakehead(); |
el17oc | 2:44e4a6ecdbef | 104 | |
el17oc | 5:d716013c6a18 | 105 | // lcd.setPixel(originalhead.x, originalhead.y,1); |
el17oc | 5:d716013c6a18 | 106 | //lcd.setPixel(originalbody.x, originalbody.y,1); |
el17oc | 5:d716013c6a18 | 107 | lcd.refresh(); |
el17oc | 1:897160a1a3ae | 108 | } |
el17oc | 1:897160a1a3ae | 109 | |
el17oc | 1:897160a1a3ae | 110 | |
el17oc | 1:897160a1a3ae | 111 | // for (int z=0; z<=_length; z++) { |
el17oc | 1:897160a1a3ae | 112 | // lcd.setPixel(snakebody[z].x, snakebody[z].y, 1); |
el17oc | 1:897160a1a3ae | 113 | // } |
el17oc | 1:897160a1a3ae | 114 | //use to draw wall - do i need to draw it everytime or can i do it as constant |
el17oc | 1:897160a1a3ae | 115 | //} |
el17oc | 1:897160a1a3ae | 116 | |
el17oc | 1:897160a1a3ae | 117 | |
el17oc | 1:897160a1a3ae | 118 | |
el17oc | 1:897160a1a3ae | 119 | |
el17oc | 1:897160a1a3ae | 120 | |
el17oc | 1:897160a1a3ae | 121 | |
el17oc | 1:897160a1a3ae | 122 | //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 | 123 | //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 | 124 | //has button button been pressed - if so change direction accordingly |
el17oc | 1:897160a1a3ae | 125 | //max length - when length = width x height - 1 - game complete |
el17oc | 1:897160a1a3ae | 126 | //each pixel of length must follow the front pixel |
el17oc | 1:897160a1a3ae | 127 | // 2 modes - time race - "get an apple before time runs out" - resets timer --- then classic snake |
el17oc | 1:897160a1a3ae | 128 | |
el17oc | 1:897160a1a3ae | 129 | |
el17oc | 1:897160a1a3ae | 130 | |
el17oc | 1:897160a1a3ae | 131 | //need to add audio, leds, (main menu end menu and entering and leaving the while(1) loop), time trial mode, .h files |