Snake vs Block Game to be run upon K64F.

Dependencies:   mbed

Committer:
AhmedPlaymaker
Date:
Sun Apr 28 16:56:08 2019 +0000
Revision:
56:142e9fdb77a8
Parent:
54:20abd16c7d74
Child:
62:ebf6ecf8a6d5
Removed some old feature that seems to never make a difference and also added some logos for joystick and motion control, along with changing some code style

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 56:142e9fdb77a8 6 Username: el17ana
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 1:32e312688a65 20 /////////////// objects ///////////////
AhmedPlaymaker 1:32e312688a65 21 N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11);
AhmedPlaymaker 1:32e312688a65 22 Gamepad pad;
AhmedPlaymaker 1:32e312688a65 23 FXOS8700CQ device(I2C_SDA,I2C_SCL);
AhmedPlaymaker 3:fbb1fa853f09 24 StartScreen _start;
AhmedPlaymaker 49:441c32f6603e 25 Stats stats;
AhmedPlaymaker 7:48ba87cd79b5 26 SnakevsBlock game;
AhmedPlaymaker 32:3a3bdeffdf62 27 AnalogIn noisy(PTB0); //This creates a random noise which I can use to seed the random numbers.
AhmedPlaymaker 33:249cf423fb18 28 // Connections to SD card holder on K64F (SPI interface)
AhmedPlaymaker 33:249cf423fb18 29 SDFileSystem sd(PTE3, PTE1, PTE2, PTE4, "sd"); // MOSI, MISO, SCK, CS
AhmedPlaymaker 33:249cf423fb18 30 //Serial serial(USBTX, USBRX); // for PC debug
AhmedPlaymaker 1:32e312688a65 31
AhmedPlaymaker 7:48ba87cd79b5 32 ///////////// prototypes //////////////
AhmedPlaymaker 1:32e312688a65 33 void init();
AhmedPlaymaker 1:32e312688a65 34 void refresh_game();
AhmedPlaymaker 1:32e312688a65 35
AhmedPlaymaker 2:83e85dea3c89 36 //Constants//
AhmedPlaymaker 46:dc7dccae9f9e 37 int fps = 40; // frames per second, this will be changed in menu.
AhmedPlaymaker 39:210ac915e0a0 38 int g_mode = 1; //selects between joystick and motion control.
AhmedPlaymaker 54:20abd16c7d74 39 int back; //this allows us to use the back key to exit the game loop;
AhmedPlaymaker 2:83e85dea3c89 40
AhmedPlaymaker 1:32e312688a65 41 ///////////// MAIN ////////////////
AhmedPlaymaker 1:32e312688a65 42 int main()
AhmedPlaymaker 1:32e312688a65 43 {
AhmedPlaymaker 1:32e312688a65 44 init();
AhmedPlaymaker 3:fbb1fa853f09 45 _start.screen_saver(lcd, pad);
AhmedPlaymaker 32:3a3bdeffdf62 46 while(1) { //This loop is created for Play/Continue configuration
AhmedPlaymaker 49:441c32f6603e 47 _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 48 stats.write(1, sd); //this tells the stats class that the game has started from level 1;
AhmedPlaymaker 56:142e9fdb77a8 49 _start.main_menu(lcd, pad); // this takes us to main menu inside startscreen, and connects automatically to all other menu functions.
AhmedPlaymaker 49:441c32f6603e 50 _start.credits(lcd); // this is after the menu to allow us to hide credits if we want to play the game fast.
AhmedPlaymaker 56:142e9fdb77a8 51 fps = _start.fps; // sets the frames per second required, selected from the game speed menu.
AhmedPlaymaker 56:142e9fdb77a8 52 g_mode = _start.g_mode;// allows us to pass this information on to the snakevsblock class, to set the controls to either joystick or motion control.
AhmedPlaymaker 25:e827f1a8fadc 53 // start the game
AhmedPlaymaker 1:32e312688a65 54 refresh_game();
AhmedPlaymaker 25:e827f1a8fadc 55 wait(1.0f/fps);
AhmedPlaymaker 25:e827f1a8fadc 56
AhmedPlaymaker 25:e827f1a8fadc 57 // snakeVSblock - detect input respect to the menu options, and update data and refresh screen
AhmedPlaymaker 25:e827f1a8fadc 58 while (1) {
AhmedPlaymaker 25:e827f1a8fadc 59
AhmedPlaymaker 56:142e9fdb77a8 60 game.read_input(pad, device, g_mode); //this reads the angle or joystick direction, on the condition of either of them being selected.
AhmedPlaymaker 56:142e9fdb77a8 61 back = game.update(lcd, pad, sd); //the int back stores the value 1 if back is pressed inside the update function of snakevsblock
AhmedPlaymaker 56:142e9fdb77a8 62 if(back) { break; } //and this allows us to return to main menu by using the keyword break.
AhmedPlaymaker 56:142e9fdb77a8 63
AhmedPlaymaker 25:e827f1a8fadc 64 refresh_game();
AhmedPlaymaker 25:e827f1a8fadc 65 wait(1.0f/fps);
AhmedPlaymaker 25:e827f1a8fadc 66 }
AhmedPlaymaker 1:32e312688a65 67 }
AhmedPlaymaker 1:32e312688a65 68 }
AhmedPlaymaker 1:32e312688a65 69
AhmedPlaymaker 1:32e312688a65 70 void init()
AhmedPlaymaker 1:32e312688a65 71 {
AhmedPlaymaker 1:32e312688a65 72 // need to initialise LCD and Gamepad
AhmedPlaymaker 56:142e9fdb77a8 73 lcd.init(); //init for the N5110 Library.
AhmedPlaymaker 56:142e9fdb77a8 74 device.init(); //init for the FXOS8700CQ Library.
AhmedPlaymaker 56:142e9fdb77a8 75 pad.init(); //init for the Gamepad Library.
AhmedPlaymaker 56:142e9fdb77a8 76 game.init(); //init for the SnakeVSBlock Class.
AhmedPlaymaker 56:142e9fdb77a8 77 _start.init(); //init for the Menu Class --> StartScreen.
AhmedPlaymaker 56:142e9fdb77a8 78 srand(100000*noisy.read_u16()); //seeds the random number generator with a random noise from the K64F.
AhmedPlaymaker 1:32e312688a65 79 }
AhmedPlaymaker 1:32e312688a65 80
AhmedPlaymaker 1:32e312688a65 81 void refresh_game()
AhmedPlaymaker 1:32e312688a65 82 {
AhmedPlaymaker 56:142e9fdb77a8 83 lcd.clear(); //clears the N5110 screen for the next frame
AhmedPlaymaker 56:142e9fdb77a8 84 game.draw(lcd, pad); //draws the next game frame
AhmedPlaymaker 56:142e9fdb77a8 85 game.get_pos(); //takes the game object coordinates and saves it privately to use later for implementing collisions.
AhmedPlaymaker 56:142e9fdb77a8 86 lcd.refresh(); //refreshes the N5110 screen to display the frame.
AhmedPlaymaker 0:4b15c2d4aa58 87 }