Ahmed Adamjee
/
SnakeVSBlock
Snake vs Block Game to be run upon K64F.
main.cpp@41:4edac50f010d, 2019-04-23 (annotated)
- Committer:
- AhmedPlaymaker
- Date:
- Tue Apr 23 12:25:49 2019 +0000
- Revision:
- 41:4edac50f010d
- Parent:
- 40:1debed7ec01c
- Child:
- 46:dc7dccae9f9e
This version has a separate class to interface with length, and also changed a lot of the code to interface with it.
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 | 33:249cf423fb18 | 18 | #include "SDFileSystem.h" |
AhmedPlaymaker | 6:3ffab44ed49c | 19 | |
AhmedPlaymaker | 6:3ffab44ed49c | 20 | /////////////// structs ///////////////// |
AhmedPlaymaker | 6:3ffab44ed49c | 21 | struct UserInput { |
AhmedPlaymaker | 6:3ffab44ed49c | 22 | Direction d; |
AhmedPlaymaker | 6:3ffab44ed49c | 23 | float mag; |
AhmedPlaymaker | 6:3ffab44ed49c | 24 | }; |
AhmedPlaymaker | 1:32e312688a65 | 25 | |
AhmedPlaymaker | 1:32e312688a65 | 26 | /////////////// objects /////////////// |
AhmedPlaymaker | 1:32e312688a65 | 27 | N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11); |
AhmedPlaymaker | 1:32e312688a65 | 28 | Gamepad pad; |
AhmedPlaymaker | 1:32e312688a65 | 29 | FXOS8700CQ device(I2C_SDA,I2C_SCL); |
AhmedPlaymaker | 3:fbb1fa853f09 | 30 | StartScreen _start; |
AhmedPlaymaker | 7:48ba87cd79b5 | 31 | SnakevsBlock game; |
AhmedPlaymaker | 32:3a3bdeffdf62 | 32 | AnalogIn noisy(PTB0); //This creates a random noise which I can use to seed the random numbers. |
AhmedPlaymaker | 33:249cf423fb18 | 33 | // Connections to SD card holder on K64F (SPI interface) |
AhmedPlaymaker | 33:249cf423fb18 | 34 | SDFileSystem sd(PTE3, PTE1, PTE2, PTE4, "sd"); // MOSI, MISO, SCK, CS |
AhmedPlaymaker | 33:249cf423fb18 | 35 | //Serial serial(USBTX, USBRX); // for PC debug |
AhmedPlaymaker | 1:32e312688a65 | 36 | |
AhmedPlaymaker | 7:48ba87cd79b5 | 37 | ///////////// prototypes ////////////// |
AhmedPlaymaker | 1:32e312688a65 | 38 | void init(); |
AhmedPlaymaker | 7:48ba87cd79b5 | 39 | void update_game(UserInput input); |
AhmedPlaymaker | 1:32e312688a65 | 40 | void refresh_game(); |
AhmedPlaymaker | 1:32e312688a65 | 41 | |
AhmedPlaymaker | 2:83e85dea3c89 | 42 | //Constants// |
AhmedPlaymaker | 16:7b474f873683 | 43 | int fps = 40; // frames per second |
AhmedPlaymaker | 39:210ac915e0a0 | 44 | int g_mode = 1; //selects between joystick and motion control. |
AhmedPlaymaker | 2:83e85dea3c89 | 45 | |
AhmedPlaymaker | 1:32e312688a65 | 46 | ///////////// MAIN //////////////// |
AhmedPlaymaker | 1:32e312688a65 | 47 | int main() |
AhmedPlaymaker | 1:32e312688a65 | 48 | { |
AhmedPlaymaker | 1:32e312688a65 | 49 | init(); |
AhmedPlaymaker | 3:fbb1fa853f09 | 50 | _start.screen_saver(lcd, pad); |
AhmedPlaymaker | 3:fbb1fa853f09 | 51 | _start.instruct(lcd, pad); |
AhmedPlaymaker | 3:fbb1fa853f09 | 52 | _start.info(lcd, pad); |
AhmedPlaymaker | 32:3a3bdeffdf62 | 53 | while(1) { //This loop is created for Play/Continue configuration |
AhmedPlaymaker | 33:249cf423fb18 | 54 | _start.main_menu(lcd, pad, sd); |
AhmedPlaymaker | 25:e827f1a8fadc | 55 | fps = _start.fps; |
AhmedPlaymaker | 39:210ac915e0a0 | 56 | g_mode = _start.g_mode; |
AhmedPlaymaker | 25:e827f1a8fadc | 57 | // start the game |
AhmedPlaymaker | 1:32e312688a65 | 58 | refresh_game(); |
AhmedPlaymaker | 1:32e312688a65 | 59 | |
AhmedPlaymaker | 25:e827f1a8fadc | 60 | wait(1.0f/fps); |
AhmedPlaymaker | 25:e827f1a8fadc | 61 | |
AhmedPlaymaker | 25:e827f1a8fadc | 62 | // snakeVSblock - detect input respect to the menu options, and update data and refresh screen |
AhmedPlaymaker | 25:e827f1a8fadc | 63 | while (1) { |
AhmedPlaymaker | 25:e827f1a8fadc | 64 | |
AhmedPlaymaker | 39:210ac915e0a0 | 65 | game.read_input(pad, device, g_mode); |
AhmedPlaymaker | 41:4edac50f010d | 66 | int back = game.update(pad); |
AhmedPlaymaker | 25:e827f1a8fadc | 67 | refresh_game(); |
AhmedPlaymaker | 25:e827f1a8fadc | 68 | |
AhmedPlaymaker | 25:e827f1a8fadc | 69 | wait(1.0f/fps); |
AhmedPlaymaker | 25:e827f1a8fadc | 70 | if(back) { |
AhmedPlaymaker | 25:e827f1a8fadc | 71 | break; |
AhmedPlaymaker | 25:e827f1a8fadc | 72 | } |
AhmedPlaymaker | 25:e827f1a8fadc | 73 | |
AhmedPlaymaker | 25:e827f1a8fadc | 74 | } |
AhmedPlaymaker | 1:32e312688a65 | 75 | } |
AhmedPlaymaker | 1:32e312688a65 | 76 | } |
AhmedPlaymaker | 1:32e312688a65 | 77 | |
AhmedPlaymaker | 1:32e312688a65 | 78 | void init() |
AhmedPlaymaker | 1:32e312688a65 | 79 | { |
AhmedPlaymaker | 1:32e312688a65 | 80 | // need to initialise LCD and Gamepad |
AhmedPlaymaker | 1:32e312688a65 | 81 | lcd.init(); |
AhmedPlaymaker | 40:1debed7ec01c | 82 | device.init(); |
AhmedPlaymaker | 1:32e312688a65 | 83 | pad.init(); |
AhmedPlaymaker | 6:3ffab44ed49c | 84 | pad.tone(1000.0,0.1); |
AhmedPlaymaker | 7:48ba87cd79b5 | 85 | game.init(); |
AhmedPlaymaker | 11:d6ceff1ff6d7 | 86 | srand(100000*noisy.read()); |
AhmedPlaymaker | 1:32e312688a65 | 87 | } |
AhmedPlaymaker | 1:32e312688a65 | 88 | |
AhmedPlaymaker | 1:32e312688a65 | 89 | void refresh_game() |
AhmedPlaymaker | 1:32e312688a65 | 90 | { |
AhmedPlaymaker | 3:fbb1fa853f09 | 91 | lcd.clear(); |
AhmedPlaymaker | 41:4edac50f010d | 92 | game.draw(lcd, pad, sd); |
AhmedPlaymaker | 7:48ba87cd79b5 | 93 | game.get_pos(); |
AhmedPlaymaker | 1:32e312688a65 | 94 | lcd.refresh(); |
AhmedPlaymaker | 0:4b15c2d4aa58 | 95 | } |