Snake vs Block Game to be run upon K64F.

Dependencies:   mbed

Committer:
AhmedPlaymaker
Date:
Fri Apr 26 19:32:57 2019 +0000
Revision:
54:20abd16c7d74
Parent:
50:3cf9a94a264e
Child:
56:142e9fdb77a8
There was a bug where the snake didnt detect the block on some occasions , i think it's fixed now.

Who changed what in which revision?

UserRevisionLine numberNew 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 49:441c32f6603e 31 Stats stats;
AhmedPlaymaker 7:48ba87cd79b5 32 SnakevsBlock game;
AhmedPlaymaker 32:3a3bdeffdf62 33 AnalogIn noisy(PTB0); //This creates a random noise which I can use to seed the random numbers.
AhmedPlaymaker 33:249cf423fb18 34 // Connections to SD card holder on K64F (SPI interface)
AhmedPlaymaker 33:249cf423fb18 35 SDFileSystem sd(PTE3, PTE1, PTE2, PTE4, "sd"); // MOSI, MISO, SCK, CS
AhmedPlaymaker 33:249cf423fb18 36 //Serial serial(USBTX, USBRX); // for PC debug
AhmedPlaymaker 1:32e312688a65 37
AhmedPlaymaker 7:48ba87cd79b5 38 ///////////// prototypes //////////////
AhmedPlaymaker 1:32e312688a65 39 void init();
AhmedPlaymaker 7:48ba87cd79b5 40 void update_game(UserInput input);
AhmedPlaymaker 1:32e312688a65 41 void refresh_game();
AhmedPlaymaker 1:32e312688a65 42
AhmedPlaymaker 2:83e85dea3c89 43 //Constants//
AhmedPlaymaker 46:dc7dccae9f9e 44 int fps = 40; // frames per second, this will be changed in menu.
AhmedPlaymaker 39:210ac915e0a0 45 int g_mode = 1; //selects between joystick and motion control.
AhmedPlaymaker 54:20abd16c7d74 46 int back; //this allows us to use the back key to exit the game loop;
AhmedPlaymaker 2:83e85dea3c89 47
AhmedPlaymaker 1:32e312688a65 48 ///////////// MAIN ////////////////
AhmedPlaymaker 1:32e312688a65 49 int main()
AhmedPlaymaker 1:32e312688a65 50 {
AhmedPlaymaker 1:32e312688a65 51 init();
AhmedPlaymaker 3:fbb1fa853f09 52 _start.screen_saver(lcd, pad);
AhmedPlaymaker 3:fbb1fa853f09 53 _start.instruct(lcd, pad);
AhmedPlaymaker 32:3a3bdeffdf62 54 while(1) { //This loop is created for Play/Continue configuration
AhmedPlaymaker 49:441c32f6603e 55 _start.read_stats(sd); //this is to save the current highest level in the stats class that can be used in menu.
AhmedPlaymaker 50:3cf9a94a264e 56 stats.write(1, sd); //this tells the stats class that the game has started from level 1;
AhmedPlaymaker 49:441c32f6603e 57 _start.main_menu(lcd, pad);
AhmedPlaymaker 49:441c32f6603e 58 _start.credits(lcd); // this is after the menu to allow us to hide credits if we want to play the game fast.
AhmedPlaymaker 25:e827f1a8fadc 59 fps = _start.fps;
AhmedPlaymaker 39:210ac915e0a0 60 g_mode = _start.g_mode;
AhmedPlaymaker 25:e827f1a8fadc 61 // start the game
AhmedPlaymaker 1:32e312688a65 62 refresh_game();
AhmedPlaymaker 1:32e312688a65 63
AhmedPlaymaker 25:e827f1a8fadc 64 wait(1.0f/fps);
AhmedPlaymaker 25:e827f1a8fadc 65
AhmedPlaymaker 25:e827f1a8fadc 66 // snakeVSblock - detect input respect to the menu options, and update data and refresh screen
AhmedPlaymaker 25:e827f1a8fadc 67 while (1) {
AhmedPlaymaker 25:e827f1a8fadc 68
AhmedPlaymaker 39:210ac915e0a0 69 game.read_input(pad, device, g_mode);
AhmedPlaymaker 54:20abd16c7d74 70 back = game.update(lcd, pad, sd);
AhmedPlaymaker 25:e827f1a8fadc 71 refresh_game();
AhmedPlaymaker 25:e827f1a8fadc 72
AhmedPlaymaker 25:e827f1a8fadc 73 wait(1.0f/fps);
AhmedPlaymaker 25:e827f1a8fadc 74 if(back) {
AhmedPlaymaker 25:e827f1a8fadc 75 break;
AhmedPlaymaker 25:e827f1a8fadc 76 }
AhmedPlaymaker 25:e827f1a8fadc 77
AhmedPlaymaker 25:e827f1a8fadc 78 }
AhmedPlaymaker 1:32e312688a65 79 }
AhmedPlaymaker 1:32e312688a65 80 }
AhmedPlaymaker 1:32e312688a65 81
AhmedPlaymaker 1:32e312688a65 82 void init()
AhmedPlaymaker 1:32e312688a65 83 {
AhmedPlaymaker 1:32e312688a65 84 // need to initialise LCD and Gamepad
AhmedPlaymaker 1:32e312688a65 85 lcd.init();
AhmedPlaymaker 40:1debed7ec01c 86 device.init();
AhmedPlaymaker 1:32e312688a65 87 pad.init();
AhmedPlaymaker 6:3ffab44ed49c 88 pad.tone(1000.0,0.1);
AhmedPlaymaker 7:48ba87cd79b5 89 game.init();
AhmedPlaymaker 49:441c32f6603e 90 _start.init();
AhmedPlaymaker 47:b448ffd073e7 91 srand(100000*noisy.read_u16());
AhmedPlaymaker 1:32e312688a65 92 }
AhmedPlaymaker 1:32e312688a65 93
AhmedPlaymaker 1:32e312688a65 94 void refresh_game()
AhmedPlaymaker 1:32e312688a65 95 {
AhmedPlaymaker 54:20abd16c7d74 96 lcd.clear();
AhmedPlaymaker 49:441c32f6603e 97 game.draw(lcd, pad);
AhmedPlaymaker 7:48ba87cd79b5 98 game.get_pos();
AhmedPlaymaker 1:32e312688a65 99 lcd.refresh();
AhmedPlaymaker 0:4b15c2d4aa58 100 }