Snake vs Block Game to be run upon K64F.

Dependencies:   mbed

Committer:
AhmedPlaymaker
Date:
Sun May 05 10:58:33 2019 +0000
Revision:
73:80556a279962
Parent:
72:4d49d2544cef
Child:
79:35cb65c52d25
Added a header file for main.cpp , called main.h, this is used to define all the function prototypes and reference pre-processor directives.

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 72:4d49d2544cef 8 Date: 07/03/2019
AhmedPlaymaker 0:4b15c2d4aa58 9 */
AhmedPlaymaker 0:4b15c2d4aa58 10
AhmedPlaymaker 73:80556a279962 11 #include "main.h"
AhmedPlaymaker 73:80556a279962 12 /** @file main.cpp
AhmedPlaymaker 73:80556a279962 13 * @brief This file is designated to start the program by calling all the necessary functions in a sequential manner.
AhmedPlaymaker 73:80556a279962 14 */
AhmedPlaymaker 68:b9cfd27987ac 15
AhmedPlaymaker 73:80556a279962 16 ///////////// MAIN.CPP ////////////////
AhmedPlaymaker 1:32e312688a65 17 int main()
AhmedPlaymaker 1:32e312688a65 18 {
AhmedPlaymaker 69:55e309da7efd 19 #ifdef TEST_SNAKE
AhmedPlaymaker 68:b9cfd27987ac 20 int number_of_failures = run_all_tests();
AhmedPlaymaker 68:b9cfd27987ac 21
AhmedPlaymaker 68:b9cfd27987ac 22 if(number_of_failures > 0) return number_of_failures;
AhmedPlaymaker 68:b9cfd27987ac 23 #endif
AhmedPlaymaker 68:b9cfd27987ac 24
AhmedPlaymaker 1:32e312688a65 25 init();
AhmedPlaymaker 66:e47333ffc6ca 26 _start.titleScreen(lcd, pad);
AhmedPlaymaker 32:3a3bdeffdf62 27 while(1) { //This loop is created for Play/Continue configuration
AhmedPlaymaker 70:7caab8069b9b 28 menu(); //pops up the menu by calling the StartScreen Classes.
AhmedPlaymaker 70:7caab8069b9b 29
AhmedPlaymaker 62:ebf6ecf8a6d5 30 // start the game
AhmedPlaymaker 67:39b9ba6019b0 31 _start.credits(lcd); // this is after the menu to allow us to hide credits if we want to play the game without wasting any time.
AhmedPlaymaker 62:ebf6ecf8a6d5 32 wait(1.0f/fps);
AhmedPlaymaker 69:55e309da7efd 33 // snakeVSblock game loop - detect input respect to the menu options, and update data and refresh screen
AhmedPlaymaker 69:55e309da7efd 34 gameLoop();
AhmedPlaymaker 1:32e312688a65 35 }
AhmedPlaymaker 1:32e312688a65 36 }
AhmedPlaymaker 1:32e312688a65 37
AhmedPlaymaker 70:7caab8069b9b 38 //MAIN_FUNCTIONS
AhmedPlaymaker 70:7caab8069b9b 39
AhmedPlaymaker 70:7caab8069b9b 40 //INIT
AhmedPlaymaker 1:32e312688a65 41 void init()
AhmedPlaymaker 1:32e312688a65 42 {
AhmedPlaymaker 62:ebf6ecf8a6d5 43 // need to initialise LCD and Gamepad
AhmedPlaymaker 56:142e9fdb77a8 44 lcd.init(); //init for the N5110 Library.
AhmedPlaymaker 56:142e9fdb77a8 45 device.init(); //init for the FXOS8700CQ Library.
AhmedPlaymaker 56:142e9fdb77a8 46 pad.init(); //init for the Gamepad Library.
AhmedPlaymaker 66:e47333ffc6ca 47 _game.init(); //init for the SnakeVSBlock Class.
AhmedPlaymaker 56:142e9fdb77a8 48 _start.init(); //init for the Menu Class --> StartScreen.
AhmedPlaymaker 56:142e9fdb77a8 49 srand(100000*noisy.read_u16()); //seeds the random number generator with a random noise from the K64F.
AhmedPlaymaker 1:32e312688a65 50 }
AhmedPlaymaker 1:32e312688a65 51
AhmedPlaymaker 70:7caab8069b9b 52 //MENU
AhmedPlaymaker 69:55e309da7efd 53 void menu()
AhmedPlaymaker 69:55e309da7efd 54 {
AhmedPlaymaker 69:55e309da7efd 55 read_write_stats(); //inside menu because used in a menu function for displaying the highest level saved.
AhmedPlaymaker 69:55e309da7efd 56 _start.menu(lcd, pad); // this takes us to main menu inside startscreen, and connects automatically to all other menu functions.
AhmedPlaymaker 69:55e309da7efd 57 _set_mode_speed(); //takes all the data collected from the menu to configure the game.
AhmedPlaymaker 69:55e309da7efd 58 }
AhmedPlaymaker 69:55e309da7efd 59
AhmedPlaymaker 70:7caab8069b9b 60 //REFRESH GAME.
AhmedPlaymaker 1:32e312688a65 61 void refresh_game()
AhmedPlaymaker 1:32e312688a65 62 {
AhmedPlaymaker 56:142e9fdb77a8 63 lcd.clear(); //clears the N5110 screen for the next frame
AhmedPlaymaker 66:e47333ffc6ca 64 _game.draw(lcd, pad); //draws the next game frame
AhmedPlaymaker 66:e47333ffc6ca 65 _game.get_pos(); //takes the game object coordinates and saves it privately to use later for implementing collisions.
AhmedPlaymaker 56:142e9fdb77a8 66 lcd.refresh(); //refreshes the N5110 screen to display the frame.
AhmedPlaymaker 67:39b9ba6019b0 67 }
AhmedPlaymaker 67:39b9ba6019b0 68
AhmedPlaymaker 70:7caab8069b9b 69 //READ AND WRITE STATS.
AhmedPlaymaker 67:39b9ba6019b0 70 void read_write_stats()
AhmedPlaymaker 67:39b9ba6019b0 71 {
AhmedPlaymaker 67:39b9ba6019b0 72 _start.read_stats(sd); //this is to save the current highest level in the stats class that can be used in menu.
AhmedPlaymaker 67:39b9ba6019b0 73 _stats.write(1, sd); //this tells the stats class that the game has started from level 1;
AhmedPlaymaker 67:39b9ba6019b0 74 }
AhmedPlaymaker 67:39b9ba6019b0 75
AhmedPlaymaker 70:7caab8069b9b 76 //SET MODE AND SPEED.
AhmedPlaymaker 67:39b9ba6019b0 77 void _set_mode_speed()
AhmedPlaymaker 67:39b9ba6019b0 78 {
AhmedPlaymaker 67:39b9ba6019b0 79 fps = _start.fps; // sets the frames per second required, selected from the game speed menu.
AhmedPlaymaker 67:39b9ba6019b0 80 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 68:b9cfd27987ac 81
AhmedPlaymaker 67:39b9ba6019b0 82 if (g_mode == 2) { //show instructions to handle motion control.
AhmedPlaymaker 67:39b9ba6019b0 83 _start.motionControlInstructions(lcd); //this only comes up on the screen is the user selects motion control from menu options.
AhmedPlaymaker 67:39b9ba6019b0 84 }
AhmedPlaymaker 68:b9cfd27987ac 85 }
AhmedPlaymaker 69:55e309da7efd 86
AhmedPlaymaker 70:7caab8069b9b 87 //GAME LOOP.
AhmedPlaymaker 69:55e309da7efd 88 void gameLoop()
AhmedPlaymaker 69:55e309da7efd 89 {
AhmedPlaymaker 69:55e309da7efd 90 while (1) {
AhmedPlaymaker 70:7caab8069b9b 91 refresh_game();
AhmedPlaymaker 69:55e309da7efd 92 _game.read_input(pad, device, g_mode); //this reads the angle or joystick direction, on the condition of either of them being selected.
AhmedPlaymaker 69:55e309da7efd 93 _game.update(lcd, pad); //updates the game screen and checks for any collisions.
AhmedPlaymaker 69:55e309da7efd 94
AhmedPlaymaker 69:55e309da7efd 95 //the int back stores the value 1 if back is pressed inside the update function of snakevsblock,
AhmedPlaymaker 69:55e309da7efd 96 //This function also handles level progression and level failure operations by using the class WinLoose.
AhmedPlaymaker 69:55e309da7efd 97 back = _game.CheckGameProgression(lcd, pad, sd); //and also sends relevant data to the sd card to implement stats functionality.
AhmedPlaymaker 69:55e309da7efd 98
AhmedPlaymaker 69:55e309da7efd 99 if(back) {
AhmedPlaymaker 69:55e309da7efd 100 break; //and this allows us to return to main menu by using the keyword break.
AhmedPlaymaker 69:55e309da7efd 101 }
AhmedPlaymaker 69:55e309da7efd 102
AhmedPlaymaker 69:55e309da7efd 103 wait(1.0f/fps);
AhmedPlaymaker 69:55e309da7efd 104 }
AhmedPlaymaker 69:55e309da7efd 105 }
AhmedPlaymaker 69:55e309da7efd 106
AhmedPlaymaker 68:b9cfd27987ac 107 /*
AhmedPlaymaker 68:b9cfd27987ac 108 void deinit()
AhmedPlaymaker 68:b9cfd27987ac 109 {
AhmedPlaymaker 68:b9cfd27987ac 110 // need to deinitialise to quit.
AhmedPlaymaker 68:b9cfd27987ac 111 lcd.~N5110(); //deinit for the N5110 Library.
AhmedPlaymaker 68:b9cfd27987ac 112 pad.~Gamepad(); //deinit for the Gamepad Library.
AhmedPlaymaker 68:b9cfd27987ac 113 device.~FXOS8700CQ(); //deinit for the FXOS8700CQ Library.
AhmedPlaymaker 68:b9cfd27987ac 114
AhmedPlaymaker 68:b9cfd27987ac 115 }
AhmedPlaymaker 68:b9cfd27987ac 116 */