Ahmed Adamjee
/
SnakeVSBlock
Snake vs Block Game to be run upon K64F.
main.cpp@9:d1d79d4ee673, 2019-03-27 (annotated)
- Committer:
- AhmedPlaymaker
- Date:
- Wed Mar 27 18:20:36 2019 +0000
- Revision:
- 9:d1d79d4ee673
- Parent:
- 8:890b986b16a4
- Child:
- 11:d6ceff1ff6d7
Food Created, now if the snake eats food, it's length will increase.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
AhmedPlaymaker | 0:4b15c2d4aa58 | 1 | /* |
AhmedPlaymaker | 0:4b15c2d4aa58 | 2 | ELEC2645 Embedded Systems Project |
AhmedPlaymaker | 0:4b15c2d4aa58 | 3 | School of Electronic & Electrical Engineering |
AhmedPlaymaker | 0:4b15c2d4aa58 | 4 | University of Leeds |
AhmedPlaymaker | 0:4b15c2d4aa58 | 5 | Name: Ahmed Nomaan Adamjee |
AhmedPlaymaker | 0:4b15c2d4aa58 | 6 | Username: AhmedPlaymaker |
AhmedPlaymaker | 0:4b15c2d4aa58 | 7 | Student ID Number: 201161436 |
AhmedPlaymaker | 0:4b15c2d4aa58 | 8 | Date: |
AhmedPlaymaker | 0:4b15c2d4aa58 | 9 | */ |
AhmedPlaymaker | 0:4b15c2d4aa58 | 10 | |
AhmedPlaymaker | 1:32e312688a65 | 11 | ///////// pre-processor directives //////// |
AhmedPlaymaker | 0:4b15c2d4aa58 | 12 | #include "mbed.h" |
AhmedPlaymaker | 1:32e312688a65 | 13 | #include "Gamepad.h" |
AhmedPlaymaker | 1:32e312688a65 | 14 | #include "N5110.h" |
AhmedPlaymaker | 1:32e312688a65 | 15 | #include "FXOS8700CQ.h" |
AhmedPlaymaker | 3:fbb1fa853f09 | 16 | #include "StartScreen.h" |
AhmedPlaymaker | 7:48ba87cd79b5 | 17 | #include "SnakevsBlock.h" |
AhmedPlaymaker | 6:3ffab44ed49c | 18 | |
AhmedPlaymaker | 6:3ffab44ed49c | 19 | /////////////// structs ///////////////// |
AhmedPlaymaker | 6:3ffab44ed49c | 20 | struct UserInput { |
AhmedPlaymaker | 6:3ffab44ed49c | 21 | Direction d; |
AhmedPlaymaker | 6:3ffab44ed49c | 22 | float mag; |
AhmedPlaymaker | 6:3ffab44ed49c | 23 | }; |
AhmedPlaymaker | 1:32e312688a65 | 24 | |
AhmedPlaymaker | 1:32e312688a65 | 25 | /////////////// objects /////////////// |
AhmedPlaymaker | 1:32e312688a65 | 26 | N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11); |
AhmedPlaymaker | 1:32e312688a65 | 27 | Gamepad pad; |
AhmedPlaymaker | 1:32e312688a65 | 28 | FXOS8700CQ device(I2C_SDA,I2C_SCL); |
AhmedPlaymaker | 3:fbb1fa853f09 | 29 | StartScreen _start; |
AhmedPlaymaker | 7:48ba87cd79b5 | 30 | SnakevsBlock game; |
AhmedPlaymaker | 1:32e312688a65 | 31 | |
AhmedPlaymaker | 7:48ba87cd79b5 | 32 | ///////////// prototypes ////////////// |
AhmedPlaymaker | 1:32e312688a65 | 33 | void init(); |
AhmedPlaymaker | 7:48ba87cd79b5 | 34 | void update_game(UserInput input); |
AhmedPlaymaker | 1:32e312688a65 | 35 | void refresh_game(); |
AhmedPlaymaker | 1:32e312688a65 | 36 | |
AhmedPlaymaker | 2:83e85dea3c89 | 37 | //Constants// |
AhmedPlaymaker | 9:d1d79d4ee673 | 38 | int fps = 20; // frames per second |
AhmedPlaymaker | 2:83e85dea3c89 | 39 | |
AhmedPlaymaker | 1:32e312688a65 | 40 | ///////////// MAIN //////////////// |
AhmedPlaymaker | 1:32e312688a65 | 41 | int main() |
AhmedPlaymaker | 1:32e312688a65 | 42 | { |
AhmedPlaymaker | 2:83e85dea3c89 | 43 | |
AhmedPlaymaker | 1:32e312688a65 | 44 | init(); |
AhmedPlaymaker | 3:fbb1fa853f09 | 45 | _start.screen_saver(lcd, pad); |
AhmedPlaymaker | 3:fbb1fa853f09 | 46 | _start.instruct(lcd, pad); |
AhmedPlaymaker | 3:fbb1fa853f09 | 47 | _start.info(lcd, pad); |
AhmedPlaymaker | 3:fbb1fa853f09 | 48 | _start.menu_screen1(lcd, pad); |
AhmedPlaymaker | 1:32e312688a65 | 49 | // start the game |
AhmedPlaymaker | 1:32e312688a65 | 50 | refresh_game(); |
AhmedPlaymaker | 1:32e312688a65 | 51 | wait(1.0f/fps); |
AhmedPlaymaker | 1:32e312688a65 | 52 | |
AhmedPlaymaker | 1:32e312688a65 | 53 | // snakeVSblock - detect input respect to the menu options, and update data and refresh screen |
AhmedPlaymaker | 1:32e312688a65 | 54 | while (1) { |
AhmedPlaymaker | 1:32e312688a65 | 55 | |
AhmedPlaymaker | 7:48ba87cd79b5 | 56 | game.read_input(pad); |
AhmedPlaymaker | 7:48ba87cd79b5 | 57 | game.update(pad); |
AhmedPlaymaker | 1:32e312688a65 | 58 | refresh_game(); |
AhmedPlaymaker | 1:32e312688a65 | 59 | |
AhmedPlaymaker | 1:32e312688a65 | 60 | wait(1.0f/fps); |
AhmedPlaymaker | 1:32e312688a65 | 61 | |
AhmedPlaymaker | 1:32e312688a65 | 62 | } |
AhmedPlaymaker | 1:32e312688a65 | 63 | } |
AhmedPlaymaker | 1:32e312688a65 | 64 | |
AhmedPlaymaker | 1:32e312688a65 | 65 | void init() |
AhmedPlaymaker | 1:32e312688a65 | 66 | { |
AhmedPlaymaker | 1:32e312688a65 | 67 | // need to initialise LCD and Gamepad |
AhmedPlaymaker | 1:32e312688a65 | 68 | lcd.init(); |
AhmedPlaymaker | 1:32e312688a65 | 69 | pad.init(); |
AhmedPlaymaker | 6:3ffab44ed49c | 70 | pad.tone(1000.0,0.1); |
AhmedPlaymaker | 7:48ba87cd79b5 | 71 | game.init(); |
AhmedPlaymaker | 1:32e312688a65 | 72 | |
AhmedPlaymaker | 1:32e312688a65 | 73 | } |
AhmedPlaymaker | 1:32e312688a65 | 74 | |
AhmedPlaymaker | 1:32e312688a65 | 75 | void refresh_game() |
AhmedPlaymaker | 1:32e312688a65 | 76 | { |
AhmedPlaymaker | 3:fbb1fa853f09 | 77 | lcd.clear(); |
AhmedPlaymaker | 7:48ba87cd79b5 | 78 | game.draw(lcd, pad); |
AhmedPlaymaker | 7:48ba87cd79b5 | 79 | game.get_pos(); |
AhmedPlaymaker | 1:32e312688a65 | 80 | lcd.refresh(); |
AhmedPlaymaker | 3:fbb1fa853f09 | 81 | |
AhmedPlaymaker | 0:4b15c2d4aa58 | 82 | } |