Snake vs Block Game to be run upon K64F.

Dependencies:   mbed

Committer:
AhmedPlaymaker
Date:
Thu May 09 09:59:05 2019 +0000
Revision:
98:3061aa318fa2
Parent:
96:1ab67b3e6898
Child:
100:6403c1705a08
Updated some part of the SnakevsBlocks documentation

Who changed what in which revision?

UserRevisionLine numberNew contents of line
AhmedPlaymaker 7:48ba87cd79b5 1 #ifndef SNAKEVSBLOCK_H
AhmedPlaymaker 7:48ba87cd79b5 2 #define SNAKEVSBLOCK_H
AhmedPlaymaker 7:48ba87cd79b5 3
AhmedPlaymaker 7:48ba87cd79b5 4 #include "mbed.h"
AhmedPlaymaker 7:48ba87cd79b5 5 #include "N5110.h"
AhmedPlaymaker 38:30e4e6191762 6 #include "FXOS8700CQ.h"
AhmedPlaymaker 7:48ba87cd79b5 7 #include "Gamepad.h"
AhmedPlaymaker 7:48ba87cd79b5 8 #include "Snake.h"
AhmedPlaymaker 79:35cb65c52d25 9 #include "LengthManager.h"
AhmedPlaymaker 44:cd10d07ea1e5 10 #include "WinLoose.h"
AhmedPlaymaker 9:d1d79d4ee673 11 #include "SnakeFood.h"
AhmedPlaymaker 10:751bd953fa27 12 #include "Blocks.h"
AhmedPlaymaker 81:4c1641e10dcd 13 #include "Barriers.h"
AhmedPlaymaker 33:249cf423fb18 14 #include "StartScreen.h"
AhmedPlaymaker 33:249cf423fb18 15 #include "Stats.h"
AhmedPlaymaker 33:249cf423fb18 16 #include "SDFileSystem.h"
AhmedPlaymaker 7:48ba87cd79b5 17
AhmedPlaymaker 92:693a6ae0ff8e 18 /** SnakevsBlock Class
AhmedPlaymaker 92:693a6ae0ff8e 19 @brief This class controls the game by detecting collisions, drawing objects and updating game parameters.
AhmedPlaymaker 92:693a6ae0ff8e 20 @author Ahmed N.Adamjee
AhmedPlaymaker 92:693a6ae0ff8e 21 @date 8th May 2019
AhmedPlaymaker 92:693a6ae0ff8e 22 */
AhmedPlaymaker 7:48ba87cd79b5 23 class SnakevsBlock
AhmedPlaymaker 7:48ba87cd79b5 24 {
AhmedPlaymaker 62:ebf6ecf8a6d5 25 public:
AhmedPlaymaker 92:693a6ae0ff8e 26 /** Constructor */
AhmedPlaymaker 7:48ba87cd79b5 27 SnakevsBlock();
AhmedPlaymaker 92:693a6ae0ff8e 28 /** Destructor */
AhmedPlaymaker 7:48ba87cd79b5 29 ~SnakevsBlock();
AhmedPlaymaker 62:ebf6ecf8a6d5 30
AhmedPlaymaker 7:48ba87cd79b5 31 /** Initialise Game Machine
AhmedPlaymaker 7:48ba87cd79b5 32 *
AhmedPlaymaker 7:48ba87cd79b5 33 * This function initialises the game machine.
AhmedPlaymaker 7:48ba87cd79b5 34 */
AhmedPlaymaker 92:693a6ae0ff8e 35 /**
AhmedPlaymaker 92:693a6ae0ff8e 36 * @brief Initialises all game parameters and objects and therefore configures the start state of the game.
AhmedPlaymaker 92:693a6ae0ff8e 37 * @param N5110 *lcd @details pointer to the N5110 object in main, address of this pointer is saved to make availability to the entire class, without passing address to each function.
AhmedPlaymaker 92:693a6ae0ff8e 38 * @param Gamepad *pad @details pointer to the gamepad object in main, address of this pointer is saved to make availability to the entire class, without passing address to each function.
AhmedPlaymaker 92:693a6ae0ff8e 39 */
AhmedPlaymaker 83:329da564799a 40 void init(N5110 *lcd, Gamepad *pad);
AhmedPlaymaker 62:ebf6ecf8a6d5 41
AhmedPlaymaker 92:693a6ae0ff8e 42 /**
AhmedPlaymaker 92:693a6ae0ff8e 43 * @brief This function prepares the game machine for the next level.
AhmedPlaymaker 41:4edac50f010d 44 */
AhmedPlaymaker 41:4edac50f010d 45 void reset( );
AhmedPlaymaker 62:ebf6ecf8a6d5 46
AhmedPlaymaker 92:693a6ae0ff8e 47 /**
AhmedPlaymaker 92:693a6ae0ff8e 48 * @brief This function initialises the objects that are used to functionalise the game.
AhmedPlaymaker 49:441c32f6603e 49 */
AhmedPlaymaker 49:441c32f6603e 50 void object_initialisations();
AhmedPlaymaker 62:ebf6ecf8a6d5 51
AhmedPlaymaker 92:693a6ae0ff8e 52 /**
AhmedPlaymaker 92:693a6ae0ff8e 53 * @brief This function obtains numeric data from the gamepads joystick.
AhmedPlaymaker 92:693a6ae0ff8e 54 * @param FXOS8700CQ &device @details The FXOS8700CQ library.
AhmedPlaymaker 92:693a6ae0ff8e 55 * @parm g_mode @details Contains a value that is used to choose between setting the controls based on joystick or motion control.
AhmedPlaymaker 7:48ba87cd79b5 56 */
AhmedPlaymaker 92:693a6ae0ff8e 57 void read_input(FXOS8700CQ &device, int g_mode);
AhmedPlaymaker 64:540aa1602372 58
AhmedPlaymaker 92:693a6ae0ff8e 59 /**
AhmedPlaymaker 92:693a6ae0ff8e 60 * @brief This function reads the angle of tilt required for motion contol and also processes it by reseting the angle buffer if A is pressed.
AhmedPlaymaker 92:693a6ae0ff8e 61 * @param FXOS8700CQ &device @details The FXOS8700CQ library.
AhmedPlaymaker 63:205f0ca48473 62 */
AhmedPlaymaker 83:329da564799a 63 void calculateTilt(FXOS8700CQ &device);
AhmedPlaymaker 64:540aa1602372 64
AhmedPlaymaker 92:693a6ae0ff8e 65 /**
AhmedPlaymaker 92:693a6ae0ff8e 66 * @brief This function ligths the LEDS dependent on the direction of travel.
AhmedPlaymaker 63:205f0ca48473 67 */
AhmedPlaymaker 83:329da564799a 68 void lightTheLEDS();
AhmedPlaymaker 62:ebf6ecf8a6d5 69
AhmedPlaymaker 92:693a6ae0ff8e 70 /**
AhmedPlaymaker 92:693a6ae0ff8e 71 * @brief This function updates length and motion data inside the snake class and also uses the length manager class to update length.
AhmedPlaymaker 84:9950d561fdf8 72 */
AhmedPlaymaker 84:9950d561fdf8 73 void updateSnakeLengthAndMovement();
AhmedPlaymaker 84:9950d561fdf8 74
AhmedPlaymaker 92:693a6ae0ff8e 75 /**
AhmedPlaymaker 92:693a6ae0ff8e 76 * @brief This function contains the draw functions of the other libraries used in the game.
AhmedPlaymaker 7:48ba87cd79b5 77 */
AhmedPlaymaker 83:329da564799a 78 void draw();
AhmedPlaymaker 62:ebf6ecf8a6d5 79
AhmedPlaymaker 92:693a6ae0ff8e 80 /**
AhmedPlaymaker 92:693a6ae0ff8e 81 * @brief This function contains the update functions of the other libraries used in the game.
AhmedPlaymaker 56:142e9fdb77a8 82 */
AhmedPlaymaker 83:329da564799a 83 void update();
AhmedPlaymaker 64:540aa1602372 84
AhmedPlaymaker 92:693a6ae0ff8e 85 /**
AhmedPlaymaker 92:693a6ae0ff8e 86 * @brief Function handles level progression and level failure operations by using the class WinLoose.
AhmedPlaymaker 92:693a6ae0ff8e 87 * @param SDFileSystem &sd @details The SDFileSystem library.
AhmedPlaymaker 63:205f0ca48473 88 */
AhmedPlaymaker 83:329da564799a 89 int CheckGameProgression(SDFileSystem &sd);
AhmedPlaymaker 62:ebf6ecf8a6d5 90
AhmedPlaymaker 92:693a6ae0ff8e 91 /**
AhmedPlaymaker 92:693a6ae0ff8e 92 * @brief This function contains the Get Position functions of all the class objects used in the game and saves them to use in an array throughout the class.
AhmedPlaymaker 7:48ba87cd79b5 93 */
AhmedPlaymaker 7:48ba87cd79b5 94 void get_pos();
AhmedPlaymaker 92:693a6ae0ff8e 95
AhmedPlaymaker 92:693a6ae0ff8e 96 /**
AhmedPlaymaker 92:693a6ae0ff8e 97 * @brief this is to stop/move the background (food and blocks), when collision occurs at a length greater than 10.lock.
AhmedPlaymaker 92:693a6ae0ff8e 98 * @param srn @details this is the serial number of blocks and tells us which of the 5 blocks we are colliding with
AhmedPlaymaker 39:210ac915e0a0 99 */
AhmedPlaymaker 65:2872ca289b49 100 void _setVelocity(int srn);
AhmedPlaymaker 98:3061aa318fa2 101
AhmedPlaymaker 92:693a6ae0ff8e 102 /**
AhmedPlaymaker 92:693a6ae0ff8e 103 * @brief This function makes the default motion of the snake freemoving before a collision is checked for, to forget the previous collision.
AhmedPlaymaker 63:205f0ca48473 104 */
AhmedPlaymaker 63:205f0ca48473 105 void MakeDefaultMotionFree();
AhmedPlaymaker 84:9950d561fdf8 106
AhmedPlaymaker 92:693a6ae0ff8e 107 /**
AhmedPlaymaker 92:693a6ae0ff8e 108 * @brief This makes the virtual length -> 10 for the side collision implementation because if the length is fifteen and the last beed collides, it still is the 10th beed on screen.
AhmedPlaymaker 64:540aa1602372 109 */
AhmedPlaymaker 64:540aa1602372 110 void makeVirtualLengthMaxTen();
AhmedPlaymaker 64:540aa1602372 111
AhmedPlaymaker 98:3061aa318fa2 112 private:
AhmedPlaymaker 98:3061aa318fa2 113
AhmedPlaymaker 98:3061aa318fa2 114 /*
AhmedPlaymaker 98:3061aa318fa2 115 This function checks if the Snake has come into contact with it's food.
AhmedPlaymaker 98:3061aa318fa2 116 */
AhmedPlaymaker 98:3061aa318fa2 117 void CheckSnakeFoodCollision();
AhmedPlaymaker 98:3061aa318fa2 118
AhmedPlaymaker 98:3061aa318fa2 119 /*
AhmedPlaymaker 98:3061aa318fa2 120 This function automatically detects each combination of collision in the y postion.
AhmedPlaymaker 98:3061aa318fa2 121 food_sr -> creted in a loop that runs 3 times to detect collision with all the three food objects.
AhmedPlaymaker 98:3061aa318fa2 122 */
AhmedPlaymaker 98:3061aa318fa2 123 void CheckSnakeFoodYCollision(int food_sr);
AhmedPlaymaker 98:3061aa318fa2 124
AhmedPlaymaker 98:3061aa318fa2 125 /*
AhmedPlaymaker 98:3061aa318fa2 126 This function automatically detects each combination of collision in the x postion.
AhmedPlaymaker 98:3061aa318fa2 127 food_sr -> creted in a loop that runs 3 times to detect collision with all the three food objects.
AhmedPlaymaker 98:3061aa318fa2 128 */
AhmedPlaymaker 98:3061aa318fa2 129 void CheckSnakeFoodXCollision(int food_sr);
AhmedPlaymaker 98:3061aa318fa2 130
AhmedPlaymaker 98:3061aa318fa2 131 /*
AhmedPlaymaker 98:3061aa318fa2 132 This function automatically detects which food we are interacting with and increases the snake length accordingly.
AhmedPlaymaker 98:3061aa318fa2 133 food_sr -> creted in a loop that runs 3 times to detect collision with all the three food objects.
AhmedPlaymaker 98:3061aa318fa2 134 */
AhmedPlaymaker 98:3061aa318fa2 135 void ImplementSnakeFoodCollision(int food_sr);
AhmedPlaymaker 98:3061aa318fa2 136
AhmedPlaymaker 98:3061aa318fa2 137 /*
AhmedPlaymaker 98:3061aa318fa2 138 * @brief This function checks if the Snake has come into contact with any Block.
AhmedPlaymaker 98:3061aa318fa2 139 */
AhmedPlaymaker 98:3061aa318fa2 140 void CheckSnakeBlockCollision();
AhmedPlaymaker 98:3061aa318fa2 141
AhmedPlaymaker 98:3061aa318fa2 142 /*
AhmedPlaymaker 98:3061aa318fa2 143 This function returns the srn of the block we are colliding with.
AhmedPlaymaker 98:3061aa318fa2 144 Block -> value of int block which is compared to a preset ranges, to allocate a serial number to the colliding block.
AhmedPlaymaker 98:3061aa318fa2 145 */
AhmedPlaymaker 98:3061aa318fa2 146 int CheckCollidingBlock(int Block);
AhmedPlaymaker 98:3061aa318fa2 147
AhmedPlaymaker 98:3061aa318fa2 148 /*
AhmedPlaymaker 98:3061aa318fa2 149 This function allows the appropriate maths to take place after every collision.
AhmedPlaymaker 98:3061aa318fa2 150 */
AhmedPlaymaker 98:3061aa318fa2 151 void ImplementSnakeBlockCollision();
AhmedPlaymaker 98:3061aa318fa2 152
AhmedPlaymaker 98:3061aa318fa2 153 /*
AhmedPlaymaker 98:3061aa318fa2 154 This function checks if the Snake has come into contact with any the sides of the block and stops it moving.
AhmedPlaymaker 98:3061aa318fa2 155 */
AhmedPlaymaker 98:3061aa318fa2 156 void CheckSnakeBlockSidesCollision();
AhmedPlaymaker 98:3061aa318fa2 157
AhmedPlaymaker 98:3061aa318fa2 158 /*
AhmedPlaymaker 98:3061aa318fa2 159 This function checks if the Snake and blocks collide anywhere in the Y axis.
AhmedPlaymaker 98:3061aa318fa2 160 i -> is the index of the snake beed and allows to check for all possible collisions with the snake respective to it's length.
AhmedPlaymaker 98:3061aa318fa2 161 */
AhmedPlaymaker 98:3061aa318fa2 162 void CheckSnakeBlockSidesYCollision(int i);
AhmedPlaymaker 98:3061aa318fa2 163
AhmedPlaymaker 98:3061aa318fa2 164 /*
AhmedPlaymaker 98:3061aa318fa2 165 This function checks if the Snake and blocks collide anywhere in the X axis.
AhmedPlaymaker 98:3061aa318fa2 166 i -> is the index of the snake beed and allows to check for all possible collisions with the snake respective to it's length.
AhmedPlaymaker 98:3061aa318fa2 167 */
AhmedPlaymaker 98:3061aa318fa2 168 void CheckSnakeBlockSidesXCollision(int i);
AhmedPlaymaker 98:3061aa318fa2 169
AhmedPlaymaker 98:3061aa318fa2 170 /*
AhmedPlaymaker 98:3061aa318fa2 171 Checks if the colliding wall is on east side or west side and then calls ImplementBarrierCollision(i) to carry out the required task.
AhmedPlaymaker 98:3061aa318fa2 172 X -> is just the X axis coordinate of the nokia screen.
AhmedPlaymaker 98:3061aa318fa2 173 i -> is the index of the snake beed and allows to check for all possible collisions with the snake respective to it's length.
AhmedPlaymaker 98:3061aa318fa2 174 */
AhmedPlaymaker 98:3061aa318fa2 175 void CheckSnakeBlockSidesEastWestCollision(int X, int i);
AhmedPlaymaker 98:3061aa318fa2 176
AhmedPlaymaker 98:3061aa318fa2 177 /*
AhmedPlaymaker 98:3061aa318fa2 178 This function checks if the Snake has come into contact with any the sides of the barriers and stops it moving in the X axis if true.
AhmedPlaymaker 98:3061aa318fa2 179 bar_sr_no -> is the index of which barrier collision we are detecting.
AhmedPlaymaker 98:3061aa318fa2 180 */
AhmedPlaymaker 98:3061aa318fa2 181 void CheckSnakeBarrierCollision(int bar_sr_no);
AhmedPlaymaker 98:3061aa318fa2 182
AhmedPlaymaker 98:3061aa318fa2 183 /*
AhmedPlaymaker 98:3061aa318fa2 184 This function checks if the Snake and blocks collide anywhere in the Y axis.
AhmedPlaymaker 98:3061aa318fa2 185 i -> is the index of the snake beed and allows to check for all possible collisions with the snake respective to it's length.
AhmedPlaymaker 98:3061aa318fa2 186 bar_sr_no -> is the index of which barrier collision we are detecting.
AhmedPlaymaker 98:3061aa318fa2 187 */
AhmedPlaymaker 98:3061aa318fa2 188 void CheckSnakeBarrierYCollision(int i, int bar_sr_no); //i is the index of the snake beed and checks for all possible collisions with the snake respective to it's length.
AhmedPlaymaker 98:3061aa318fa2 189
AhmedPlaymaker 98:3061aa318fa2 190 /*
AhmedPlaymaker 98:3061aa318fa2 191 * Checks if the colliding barrier's side is on east side or west side of snake and then calls ImplementBarrierCollision(i) to carry out the required task.
AhmedPlaymaker 98:3061aa318fa2 192 * i -> is the index of the snake beed and allows to check for all possible collisions with the snake respective to it's length.
AhmedPlaymaker 98:3061aa318fa2 193 * bar_sr_no -> is the index of which barrier collision we are detecting.
AhmedPlaymaker 98:3061aa318fa2 194 */
AhmedPlaymaker 98:3061aa318fa2 195 void CheckSnakeBarrierEastWestCollision(int i, int bar_sr_no); //i checks for all possible collisions with the snake respective to it's length.
AhmedPlaymaker 98:3061aa318fa2 196
AhmedPlaymaker 98:3061aa318fa2 197 /*
AhmedPlaymaker 98:3061aa318fa2 198 This function implements the sides collision by making the x axis speed of the particular snake beed 0.
AhmedPlaymaker 98:3061aa318fa2 199 i -> is the index of the snake beed and allows to check for all possible collisions with the snake respective to it's length.
AhmedPlaymaker 62:ebf6ecf8a6d5 200 */
AhmedPlaymaker 82:c51ae8a501d1 201 void ImplementBarrierCollision(int i);
AhmedPlaymaker 64:540aa1602372 202
AhmedPlaymaker 64:540aa1602372 203 //OBJECT DECLARATIONS.
AhmedPlaymaker 64:540aa1602372 204 Snake _s;
AhmedPlaymaker 79:35cb65c52d25 205 LengthManager _l;
AhmedPlaymaker 64:540aa1602372 206 WinLoose _wl;
AhmedPlaymaker 64:540aa1602372 207 SnakeFood _f;
AhmedPlaymaker 64:540aa1602372 208 SnakeFood _ff;
AhmedPlaymaker 64:540aa1602372 209 SnakeFood _fff;
AhmedPlaymaker 64:540aa1602372 210 Blocks _b;
AhmedPlaymaker 81:4c1641e10dcd 211 Barriers _barA;
AhmedPlaymaker 81:4c1641e10dcd 212 Barriers _barB;
AhmedPlaymaker 64:540aa1602372 213 Stats _Setstats;
AhmedPlaymaker 84:9950d561fdf8 214
AhmedPlaymaker 83:329da564799a 215 //Pointer to the game pad object pad.
AhmedPlaymaker 83:329da564799a 216 Gamepad *_pad;
AhmedPlaymaker 83:329da564799a 217 //Pointer to the N5110 object lcd.
AhmedPlaymaker 83:329da564799a 218 N5110 *_lcd;
AhmedPlaymaker 64:540aa1602372 219
AhmedPlaymaker 64:540aa1602372 220 //OBJECT POSITIONS ON SCREEN.
AhmedPlaymaker 64:540aa1602372 221 int snakex; //x position of top beed
AhmedPlaymaker 64:540aa1602372 222 int snakey; //y position of top beed
AhmedPlaymaker 71:4bd2b27693f3 223 Vector2D snake_pos[10]; //saves the position of all the snake beeds in an array for ease of collision processing.
AhmedPlaymaker 64:540aa1602372 224 Vector2D food_pos[3]; //saves the position of all 3 snake food on the screen in a single array for ease of collision processing.
AhmedPlaymaker 64:540aa1602372 225 Vector2D b_pos; //saves the origin of the blocks on the screen (this is the top left corner of the block row).
AhmedPlaymaker 82:c51ae8a501d1 226 Vector2D bar_pos[2]; //saves the origin of the barrier on the screen (this is the top left corner of the barrier).
AhmedPlaymaker 64:540aa1602372 227
AhmedPlaymaker 64:540aa1602372 228 //LEVEL PROGRESSION, FAILURE AND STATISTICS PARAMETERS.
AhmedPlaymaker 64:540aa1602372 229 int level; // this is diffrent to int _length as this stops at 10 to not complicate collisions as the snake doesn't grow longer than 10 visually.
AhmedPlaymaker 64:540aa1602372 230 char bufferlevel[14]; //this helps me print the level on screen.
AhmedPlaymaker 64:540aa1602372 231 int back; //enables the player to go back on main menu if back is pressed.
AhmedPlaymaker 64:540aa1602372 232 int _maxLength; // this makes us go to the next level if if maxLength is achieved;
AhmedPlaymaker 64:540aa1602372 233
AhmedPlaymaker 81:4c1641e10dcd 234 //Block, Food and Barrier Y AXIS VELOCITY, SNAKE LENGTH ,MAX VIRTUAL LENGTH AND VARABLE THAT ALLOWS FREE MOTION OF A SPECIFIC BEED NUMBER OF THE SNAKE.
AhmedPlaymaker 64:540aa1602372 235 int velocity; //this is to stop/move the background (food and blocks), when collision occurs at a length greater than 10.
AhmedPlaymaker 64:540aa1602372 236 int _length; //this is diffrent to the _virtualLength as this saves the length of the snake, for collision detection relative to it's length and calculations.
AhmedPlaymaker 64:540aa1602372 237 int _virtualLength; //saves the length of the snake, for collision detection relative to it's Max screen length.
AhmedPlaymaker 96:1ab67b3e6898 238 int immobile_bead_n[10]; //this array is indexed by the beed number of the colliding snake, if beed 3 from top was colliding with any obstruction, immobile_bead_n[2] will be 1.
AhmedPlaymaker 64:540aa1602372 239
AhmedPlaymaker 64:540aa1602372 240 //TURN DIRECTION, TILT ANGLE AND ANGLE RESET BUFFER (GARBAGE).
AhmedPlaymaker 96:1ab67b3e6898 241 Direction _d; //This struct saves the direction of snake movement.
AhmedPlaymaker 64:540aa1602372 242 float _tiltAngle; //saves the angle of tilt.
AhmedPlaymaker 64:540aa1602372 243 int garbage; //to save the angle at the point button A is pressed.
AhmedPlaymaker 64:540aa1602372 244
AhmedPlaymaker 64:540aa1602372 245 //BLOCK NUMBERS, BLOCK DROP GAP, BLOCK DETECTION SRN AND VARIABLE TO SEND/NOT BLOCK NUMBER TO BLOCK CLASS
AhmedPlaymaker 64:540aa1602372 246 int *b_number; //pointer to save the numbers inside the block.
AhmedPlaymaker 64:540aa1602372 247 int blocknum; // saves the number inside the specific colliding block, for calculations.
AhmedPlaymaker 64:540aa1602372 248 int blockgap; //to change frequency of fall
AhmedPlaymaker 64:540aa1602372 249 int srn; //sr number of the block we are colliding with (1 to 5)
AhmedPlaymaker 95:b068b0735f45 250 bool send_block_number; //makes sure that the block number is only updated when send is activated.
AhmedPlaymaker 64:540aa1602372 251
AhmedPlaymaker 64:540aa1602372 252 //INITIAL FOOD AND BLOCK DROP GAP.
AhmedPlaymaker 64:540aa1602372 253 int _dropbuff; //this makes food 1,2,and 3 come at seperate times
AhmedPlaymaker 7:48ba87cd79b5 254 };
AhmedPlaymaker 7:48ba87cd79b5 255 #endif