Snake vs Block Game to be run upon K64F.

Dependencies:   mbed

Committer:
AhmedPlaymaker
Date:
Fri Apr 26 18:04:19 2019 +0000
Revision:
50:3cf9a94a264e
Parent:
49:441c32f6603e
Child:
54:20abd16c7d74
Solved a problem caused by the previous update of level not progressing, the problem of show credits not working still persists.

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