Snake vs Block Game to be run upon K64F.

Dependencies:   mbed

Committer:
AhmedPlaymaker
Date:
Wed Apr 17 07:47:06 2019 +0000
Revision:
33:249cf423fb18
Parent:
32:3a3bdeffdf62
Child:
38:30e4e6191762
Added Stats Class and now i can store the highest level.

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 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 2:83e85dea3c89 44
AhmedPlaymaker 1:32e312688a65 45 ///////////// MAIN ////////////////
AhmedPlaymaker 1:32e312688a65 46 int main()
AhmedPlaymaker 1:32e312688a65 47 {
AhmedPlaymaker 1:32e312688a65 48 init();
AhmedPlaymaker 3:fbb1fa853f09 49 _start.screen_saver(lcd, pad);
AhmedPlaymaker 3:fbb1fa853f09 50 _start.instruct(lcd, pad);
AhmedPlaymaker 3:fbb1fa853f09 51 _start.info(lcd, pad);
AhmedPlaymaker 32:3a3bdeffdf62 52 while(1) { //This loop is created for Play/Continue configuration
AhmedPlaymaker 33:249cf423fb18 53 _start.main_menu(lcd, pad, sd);
AhmedPlaymaker 25:e827f1a8fadc 54 fps = _start.fps;
AhmedPlaymaker 25:e827f1a8fadc 55 // start the game
AhmedPlaymaker 1:32e312688a65 56 refresh_game();
AhmedPlaymaker 1:32e312688a65 57
AhmedPlaymaker 25:e827f1a8fadc 58 wait(1.0f/fps);
AhmedPlaymaker 25:e827f1a8fadc 59
AhmedPlaymaker 25:e827f1a8fadc 60 // snakeVSblock - detect input respect to the menu options, and update data and refresh screen
AhmedPlaymaker 25:e827f1a8fadc 61 while (1) {
AhmedPlaymaker 25:e827f1a8fadc 62
AhmedPlaymaker 25:e827f1a8fadc 63 game.read_input(pad);
AhmedPlaymaker 33:249cf423fb18 64 int back = game.update(pad, sd);
AhmedPlaymaker 25:e827f1a8fadc 65 refresh_game();
AhmedPlaymaker 25:e827f1a8fadc 66
AhmedPlaymaker 25:e827f1a8fadc 67 wait(1.0f/fps);
AhmedPlaymaker 25:e827f1a8fadc 68 if(back) {
AhmedPlaymaker 25:e827f1a8fadc 69 break;
AhmedPlaymaker 25:e827f1a8fadc 70 }
AhmedPlaymaker 25:e827f1a8fadc 71
AhmedPlaymaker 25:e827f1a8fadc 72 }
AhmedPlaymaker 1:32e312688a65 73 }
AhmedPlaymaker 1:32e312688a65 74 }
AhmedPlaymaker 1:32e312688a65 75
AhmedPlaymaker 1:32e312688a65 76 void init()
AhmedPlaymaker 1:32e312688a65 77 {
AhmedPlaymaker 1:32e312688a65 78 // need to initialise LCD and Gamepad
AhmedPlaymaker 1:32e312688a65 79 lcd.init();
AhmedPlaymaker 1:32e312688a65 80 pad.init();
AhmedPlaymaker 6:3ffab44ed49c 81 pad.tone(1000.0,0.1);
AhmedPlaymaker 7:48ba87cd79b5 82 game.init();
AhmedPlaymaker 11:d6ceff1ff6d7 83 srand(100000*noisy.read());
AhmedPlaymaker 1:32e312688a65 84 }
AhmedPlaymaker 1:32e312688a65 85
AhmedPlaymaker 1:32e312688a65 86 void refresh_game()
AhmedPlaymaker 1:32e312688a65 87 {
AhmedPlaymaker 3:fbb1fa853f09 88 lcd.clear();
AhmedPlaymaker 7:48ba87cd79b5 89 game.draw(lcd, pad);
AhmedPlaymaker 7:48ba87cd79b5 90 game.get_pos();
AhmedPlaymaker 1:32e312688a65 91 lcd.refresh();
AhmedPlaymaker 0:4b15c2d4aa58 92 }