Snake vs Block Game to be run upon K64F.

Dependencies:   mbed

Committer:
AhmedPlaymaker
Date:
Wed May 01 22:35:40 2019 +0000
Revision:
63:205f0ca48473
Parent:
62:ebf6ecf8a6d5
Child:
66:e47333ffc6ca
Sorted out some functions, specifically related to their functionality

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 62:ebf6ecf8a6d5 53 // start the game
AhmedPlaymaker 1:32e312688a65 54 refresh_game();
AhmedPlaymaker 62:ebf6ecf8a6d5 55 wait(1.0f/fps);
AhmedPlaymaker 62:ebf6ecf8a6d5 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 62:ebf6ecf8a6d5 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 63:205f0ca48473 61 game.update(lcd, pad); //updates the game screen and checks for any collisions.
AhmedPlaymaker 63:205f0ca48473 62 back = game.CheckGameProgression(lcd, pad, sd); //the int back stores the value 1 if back is pressed inside the update function of snakevsblock,
AhmedPlaymaker 63:205f0ca48473 63 //This function also handles level progression and level failure operations by using the class WinLoose.
AhmedPlaymaker 63:205f0ca48473 64 //and also sends relevant data to the sd card to implement stats functionality.
AhmedPlaymaker 63:205f0ca48473 65
AhmedPlaymaker 63:205f0ca48473 66
AhmedPlaymaker 62:ebf6ecf8a6d5 67 if(back) {
AhmedPlaymaker 62:ebf6ecf8a6d5 68 break; //and this allows us to return to main menu by using the keyword break.
AhmedPlaymaker 62:ebf6ecf8a6d5 69 }
AhmedPlaymaker 62:ebf6ecf8a6d5 70
AhmedPlaymaker 25:e827f1a8fadc 71 refresh_game();
AhmedPlaymaker 25:e827f1a8fadc 72 wait(1.0f/fps);
AhmedPlaymaker 25:e827f1a8fadc 73 }
AhmedPlaymaker 1:32e312688a65 74 }
AhmedPlaymaker 1:32e312688a65 75 }
AhmedPlaymaker 1:32e312688a65 76
AhmedPlaymaker 1:32e312688a65 77 void init()
AhmedPlaymaker 1:32e312688a65 78 {
AhmedPlaymaker 62:ebf6ecf8a6d5 79 // need to initialise LCD and Gamepad
AhmedPlaymaker 56:142e9fdb77a8 80 lcd.init(); //init for the N5110 Library.
AhmedPlaymaker 56:142e9fdb77a8 81 device.init(); //init for the FXOS8700CQ Library.
AhmedPlaymaker 56:142e9fdb77a8 82 pad.init(); //init for the Gamepad Library.
AhmedPlaymaker 56:142e9fdb77a8 83 game.init(); //init for the SnakeVSBlock Class.
AhmedPlaymaker 56:142e9fdb77a8 84 _start.init(); //init for the Menu Class --> StartScreen.
AhmedPlaymaker 56:142e9fdb77a8 85 srand(100000*noisy.read_u16()); //seeds the random number generator with a random noise from the K64F.
AhmedPlaymaker 1:32e312688a65 86 }
AhmedPlaymaker 1:32e312688a65 87
AhmedPlaymaker 1:32e312688a65 88 void refresh_game()
AhmedPlaymaker 1:32e312688a65 89 {
AhmedPlaymaker 56:142e9fdb77a8 90 lcd.clear(); //clears the N5110 screen for the next frame
AhmedPlaymaker 56:142e9fdb77a8 91 game.draw(lcd, pad); //draws the next game frame
AhmedPlaymaker 56:142e9fdb77a8 92 game.get_pos(); //takes the game object coordinates and saves it privately to use later for implementing collisions.
AhmedPlaymaker 56:142e9fdb77a8 93 lcd.refresh(); //refreshes the N5110 screen to display the frame.
AhmedPlaymaker 0:4b15c2d4aa58 94 }