Snake vs Block Game to be run upon K64F.

Dependencies:   mbed

Committer:
AhmedPlaymaker
Date:
Fri Apr 26 17:16:22 2019 +0000
Revision:
49:441c32f6603e
Parent:
47:b448ffd073e7
Child:
50:3cf9a94a264e
Fixed multiple issues with menu code, the show credits still needs some work

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 2:83e85dea3c89 46
AhmedPlaymaker 1:32e312688a65 47 ///////////// MAIN ////////////////
AhmedPlaymaker 1:32e312688a65 48 int main()
AhmedPlaymaker 1:32e312688a65 49 {
AhmedPlaymaker 1:32e312688a65 50 init();
AhmedPlaymaker 3:fbb1fa853f09 51 _start.screen_saver(lcd, pad);
AhmedPlaymaker 3:fbb1fa853f09 52 _start.instruct(lcd, pad);
AhmedPlaymaker 32:3a3bdeffdf62 53 while(1) { //This loop is created for Play/Continue configuration
AhmedPlaymaker 49:441c32f6603e 54 _start.read_stats(sd); //this is to save the current highest level in the stats class that can be used in menu.
AhmedPlaymaker 49:441c32f6603e 55 _start.main_menu(lcd, pad);
AhmedPlaymaker 49:441c32f6603e 56 _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 57 fps = _start.fps;
AhmedPlaymaker 39:210ac915e0a0 58 g_mode = _start.g_mode;
AhmedPlaymaker 25:e827f1a8fadc 59 // start the game
AhmedPlaymaker 1:32e312688a65 60 refresh_game();
AhmedPlaymaker 1:32e312688a65 61
AhmedPlaymaker 25:e827f1a8fadc 62 wait(1.0f/fps);
AhmedPlaymaker 25:e827f1a8fadc 63
AhmedPlaymaker 25:e827f1a8fadc 64 // snakeVSblock - detect input respect to the menu options, and update data and refresh screen
AhmedPlaymaker 25:e827f1a8fadc 65 while (1) {
AhmedPlaymaker 25:e827f1a8fadc 66
AhmedPlaymaker 39:210ac915e0a0 67 game.read_input(pad, device, g_mode);
AhmedPlaymaker 49:441c32f6603e 68 int back = game.update(lcd, pad, sd);
AhmedPlaymaker 25:e827f1a8fadc 69 refresh_game();
AhmedPlaymaker 25:e827f1a8fadc 70
AhmedPlaymaker 25:e827f1a8fadc 71 wait(1.0f/fps);
AhmedPlaymaker 25:e827f1a8fadc 72 if(back) {
AhmedPlaymaker 25:e827f1a8fadc 73 break;
AhmedPlaymaker 25:e827f1a8fadc 74 }
AhmedPlaymaker 25:e827f1a8fadc 75
AhmedPlaymaker 25:e827f1a8fadc 76 }
AhmedPlaymaker 1:32e312688a65 77 }
AhmedPlaymaker 1:32e312688a65 78 }
AhmedPlaymaker 1:32e312688a65 79
AhmedPlaymaker 1:32e312688a65 80 void init()
AhmedPlaymaker 1:32e312688a65 81 {
AhmedPlaymaker 1:32e312688a65 82 // need to initialise LCD and Gamepad
AhmedPlaymaker 1:32e312688a65 83 lcd.init();
AhmedPlaymaker 40:1debed7ec01c 84 device.init();
AhmedPlaymaker 1:32e312688a65 85 pad.init();
AhmedPlaymaker 6:3ffab44ed49c 86 pad.tone(1000.0,0.1);
AhmedPlaymaker 7:48ba87cd79b5 87 game.init();
AhmedPlaymaker 49:441c32f6603e 88 _start.init();
AhmedPlaymaker 47:b448ffd073e7 89 srand(100000*noisy.read_u16());
AhmedPlaymaker 1:32e312688a65 90 }
AhmedPlaymaker 1:32e312688a65 91
AhmedPlaymaker 1:32e312688a65 92 void refresh_game()
AhmedPlaymaker 1:32e312688a65 93 {
AhmedPlaymaker 3:fbb1fa853f09 94 lcd.clear();
AhmedPlaymaker 49:441c32f6603e 95 game.draw(lcd, pad);
AhmedPlaymaker 7:48ba87cd79b5 96 game.get_pos();
AhmedPlaymaker 1:32e312688a65 97 lcd.refresh();
AhmedPlaymaker 0:4b15c2d4aa58 98 }