Snake vs Block Game to be run upon K64F.

Dependencies:   mbed

Committer:
AhmedPlaymaker
Date:
Thu May 09 14:52:19 2019 +0000
Revision:
104:17040265b7b4
Parent:
87:871d9fecb593
Final Submission. I have read and agreed with Statement of Academic Integrity.

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 83:329da564799a 26 _start.titleScreen();
AhmedPlaymaker 32:3a3bdeffdf62 27 while(1) { //This loop is created for Play/Continue configuration
AhmedPlaymaker 84:9950d561fdf8 28
AhmedPlaymaker 70:7caab8069b9b 29 menu(); //pops up the menu by calling the StartScreen Classes.
AhmedPlaymaker 79:35cb65c52d25 30
AhmedPlaymaker 62:ebf6ecf8a6d5 31 // start the game
AhmedPlaymaker 83:329da564799a 32 _start.credits(); // 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 33 wait(1.0f/fps);
AhmedPlaymaker 69:55e309da7efd 34 // snakeVSblock game loop - detect input respect to the menu options, and update data and refresh screen
AhmedPlaymaker 69:55e309da7efd 35 gameLoop();
AhmedPlaymaker 1:32e312688a65 36 }
AhmedPlaymaker 1:32e312688a65 37 }
AhmedPlaymaker 1:32e312688a65 38
AhmedPlaymaker 70:7caab8069b9b 39 //MAIN_FUNCTIONS
AhmedPlaymaker 70:7caab8069b9b 40
AhmedPlaymaker 70:7caab8069b9b 41 //INIT
AhmedPlaymaker 1:32e312688a65 42 void init()
AhmedPlaymaker 1:32e312688a65 43 {
AhmedPlaymaker 62:ebf6ecf8a6d5 44 // need to initialise LCD and Gamepad
AhmedPlaymaker 56:142e9fdb77a8 45 lcd.init(); //init for the N5110 Library.
AhmedPlaymaker 56:142e9fdb77a8 46 device.init(); //init for the FXOS8700CQ Library.
AhmedPlaymaker 56:142e9fdb77a8 47 pad.init(); //init for the Gamepad Library.
AhmedPlaymaker 83:329da564799a 48 _start.init(&lcd, &pad); //init for the Menu Class --> StartScreen.
AhmedPlaymaker 83:329da564799a 49 _game.init(&lcd, &pad); //init for the SnakeVSBlock Class.
AhmedPlaymaker 56:142e9fdb77a8 50 srand(100000*noisy.read_u16()); //seeds the random number generator with a random noise from the K64F.
AhmedPlaymaker 1:32e312688a65 51 }
AhmedPlaymaker 1:32e312688a65 52
AhmedPlaymaker 70:7caab8069b9b 53 //MENU
AhmedPlaymaker 69:55e309da7efd 54 void menu()
AhmedPlaymaker 69:55e309da7efd 55 {
AhmedPlaymaker 69:55e309da7efd 56 read_write_stats(); //inside menu because used in a menu function for displaying the highest level saved.
AhmedPlaymaker 83:329da564799a 57 _start.menu(); // this takes us to main menu inside startscreen, and connects automatically to all other menu functions.
AhmedPlaymaker 69:55e309da7efd 58 _set_mode_speed(); //takes all the data collected from the menu to configure the game.
AhmedPlaymaker 69:55e309da7efd 59 }
AhmedPlaymaker 69:55e309da7efd 60
AhmedPlaymaker 70:7caab8069b9b 61 //READ AND WRITE STATS.
AhmedPlaymaker 67:39b9ba6019b0 62 void read_write_stats()
AhmedPlaymaker 67:39b9ba6019b0 63 {
AhmedPlaymaker 67:39b9ba6019b0 64 _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 65 _stats.write(1, sd); //this tells the stats class that the game has started from level 1;
AhmedPlaymaker 67:39b9ba6019b0 66 }
AhmedPlaymaker 67:39b9ba6019b0 67
AhmedPlaymaker 70:7caab8069b9b 68 //SET MODE AND SPEED.
AhmedPlaymaker 67:39b9ba6019b0 69 void _set_mode_speed()
AhmedPlaymaker 67:39b9ba6019b0 70 {
AhmedPlaymaker 67:39b9ba6019b0 71 fps = _start.fps; // sets the frames per second required, selected from the game speed menu.
AhmedPlaymaker 67:39b9ba6019b0 72 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 73
AhmedPlaymaker 67:39b9ba6019b0 74 if (g_mode == 2) { //show instructions to handle motion control.
AhmedPlaymaker 83:329da564799a 75 _start.motionControlInstructions(); //this only comes up on the screen is the user selects motion control from menu options.
AhmedPlaymaker 67:39b9ba6019b0 76 }
AhmedPlaymaker 68:b9cfd27987ac 77 }
AhmedPlaymaker 69:55e309da7efd 78
AhmedPlaymaker 70:7caab8069b9b 79 //GAME LOOP.
AhmedPlaymaker 69:55e309da7efd 80 void gameLoop()
AhmedPlaymaker 69:55e309da7efd 81 {
AhmedPlaymaker 69:55e309da7efd 82 while (1) {
AhmedPlaymaker 70:7caab8069b9b 83 refresh_game();
AhmedPlaymaker 83:329da564799a 84 _game.read_input(device, g_mode); //this reads the angle or joystick direction, on the condition of either of them being selected.
AhmedPlaymaker 79:35cb65c52d25 85 _game.get_pos(); //takes the game object coordinates and saves it privately to use later for implementing collisions.
AhmedPlaymaker 83:329da564799a 86 _game.update(); //updates the game screen and checks for any collisions.
AhmedPlaymaker 69:55e309da7efd 87
AhmedPlaymaker 69:55e309da7efd 88 //the int back stores the value 1 if back is pressed inside the update function of snakevsblock,
AhmedPlaymaker 69:55e309da7efd 89 //This function also handles level progression and level failure operations by using the class WinLoose.
AhmedPlaymaker 83:329da564799a 90 back = _game.CheckGameProgression(sd); //and also sends relevant data to the sd card to implement stats functionality.
AhmedPlaymaker 69:55e309da7efd 91
AhmedPlaymaker 69:55e309da7efd 92 if(back) {
AhmedPlaymaker 69:55e309da7efd 93 break; //and this allows us to return to main menu by using the keyword break.
AhmedPlaymaker 69:55e309da7efd 94 }
AhmedPlaymaker 69:55e309da7efd 95
AhmedPlaymaker 69:55e309da7efd 96 wait(1.0f/fps);
AhmedPlaymaker 69:55e309da7efd 97 }
AhmedPlaymaker 69:55e309da7efd 98 }
AhmedPlaymaker 69:55e309da7efd 99
AhmedPlaymaker 84:9950d561fdf8 100 //REFRESH GAME.
AhmedPlaymaker 84:9950d561fdf8 101 void refresh_game()
AhmedPlaymaker 84:9950d561fdf8 102 {
AhmedPlaymaker 84:9950d561fdf8 103 lcd.clear(); //clears the N5110 screen for the next frame
AhmedPlaymaker 84:9950d561fdf8 104 _game.updateSnakeLengthAndMovement(); //updates snake length data and makes motion free by default.
AhmedPlaymaker 84:9950d561fdf8 105 _game.draw(); //draws the next game frame
AhmedPlaymaker 84:9950d561fdf8 106 lcd.refresh(); //refreshes the N5110 screen to display the frame.
AhmedPlaymaker 84:9950d561fdf8 107 }
AhmedPlaymaker 84:9950d561fdf8 108
AhmedPlaymaker 87:871d9fecb593 109
AhmedPlaymaker 68:b9cfd27987ac 110 /*
AhmedPlaymaker 68:b9cfd27987ac 111 void deinit()
AhmedPlaymaker 68:b9cfd27987ac 112 {
AhmedPlaymaker 68:b9cfd27987ac 113 // need to deinitialise to quit.
AhmedPlaymaker 68:b9cfd27987ac 114 lcd.~N5110(); //deinit for the N5110 Library.
AhmedPlaymaker 68:b9cfd27987ac 115 pad.~Gamepad(); //deinit for the Gamepad Library.
AhmedPlaymaker 68:b9cfd27987ac 116 device.~FXOS8700CQ(); //deinit for the FXOS8700CQ Library.
AhmedPlaymaker 68:b9cfd27987ac 117
AhmedPlaymaker 68:b9cfd27987ac 118 }
AhmedPlaymaker 68:b9cfd27987ac 119 */