Ahmed Adamjee
/
SnakeVSBlock
Snake vs Block Game to be run upon K64F.
GameEngine/SnakevsBlock/SnakevsBlock.h@81:4c1641e10dcd, 2019-05-06 (annotated)
- Committer:
- AhmedPlaymaker
- Date:
- Mon May 06 09:05:09 2019 +0000
- Revision:
- 81:4c1641e10dcd
- Parent:
- 79:35cb65c52d25
- Child:
- 82:c51ae8a501d1
Made Some Barriers, not sure about them tough
Who changed what in which revision?
User | Revision | Line number | New 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 | 7:48ba87cd79b5 | 18 | class SnakevsBlock |
AhmedPlaymaker | 7:48ba87cd79b5 | 19 | { |
AhmedPlaymaker | 62:ebf6ecf8a6d5 | 20 | public: |
AhmedPlaymaker | 7:48ba87cd79b5 | 21 | SnakevsBlock(); |
AhmedPlaymaker | 7:48ba87cd79b5 | 22 | ~SnakevsBlock(); |
AhmedPlaymaker | 62:ebf6ecf8a6d5 | 23 | |
AhmedPlaymaker | 7:48ba87cd79b5 | 24 | /** Initialise Game Machine |
AhmedPlaymaker | 7:48ba87cd79b5 | 25 | * |
AhmedPlaymaker | 7:48ba87cd79b5 | 26 | * This function initialises the game machine. |
AhmedPlaymaker | 7:48ba87cd79b5 | 27 | */ |
AhmedPlaymaker | 49:441c32f6603e | 28 | void init(); |
AhmedPlaymaker | 62:ebf6ecf8a6d5 | 29 | |
AhmedPlaymaker | 41:4edac50f010d | 30 | /** Reset Game Machine |
AhmedPlaymaker | 41:4edac50f010d | 31 | * |
AhmedPlaymaker | 41:4edac50f010d | 32 | * This function prepares the game machine for the next level. |
AhmedPlaymaker | 41:4edac50f010d | 33 | */ |
AhmedPlaymaker | 41:4edac50f010d | 34 | void reset( ); |
AhmedPlaymaker | 62:ebf6ecf8a6d5 | 35 | |
AhmedPlaymaker | 49:441c32f6603e | 36 | /** Initialise objects |
AhmedPlaymaker | 49:441c32f6603e | 37 | * |
AhmedPlaymaker | 49:441c32f6603e | 38 | * This function initialises the objects that are used to functionalise the game. |
AhmedPlaymaker | 49:441c32f6603e | 39 | */ |
AhmedPlaymaker | 49:441c32f6603e | 40 | void object_initialisations(); |
AhmedPlaymaker | 62:ebf6ecf8a6d5 | 41 | |
AhmedPlaymaker | 7:48ba87cd79b5 | 42 | /** Read Input |
AhmedPlaymaker | 7:48ba87cd79b5 | 43 | * |
AhmedPlaymaker | 7:48ba87cd79b5 | 44 | * This function obtains numeric data from the gamepads joystick. |
AhmedPlaymaker | 7:48ba87cd79b5 | 45 | */ |
AhmedPlaymaker | 38:30e4e6191762 | 46 | void read_input(Gamepad &pad, FXOS8700CQ &device, int gm); |
AhmedPlaymaker | 64:540aa1602372 | 47 | |
AhmedPlaymaker | 63:205f0ca48473 | 48 | /** Calculate Tilt |
AhmedPlaymaker | 63:205f0ca48473 | 49 | * |
AhmedPlaymaker | 63:205f0ca48473 | 50 | * 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 | 63:205f0ca48473 | 51 | */ |
AhmedPlaymaker | 63:205f0ca48473 | 52 | void calculateTilt(Gamepad &pad, FXOS8700CQ &device); |
AhmedPlaymaker | 64:540aa1602372 | 53 | |
AhmedPlaymaker | 63:205f0ca48473 | 54 | /** Light The LEDS |
AhmedPlaymaker | 63:205f0ca48473 | 55 | * |
AhmedPlaymaker | 63:205f0ca48473 | 56 | * This function ligths the LEDS dependent on the direction of travel. |
AhmedPlaymaker | 63:205f0ca48473 | 57 | */ |
AhmedPlaymaker | 63:205f0ca48473 | 58 | void lightTheLEDS(Gamepad &pad); |
AhmedPlaymaker | 62:ebf6ecf8a6d5 | 59 | |
AhmedPlaymaker | 62:ebf6ecf8a6d5 | 60 | /** Draw |
AhmedPlaymaker | 7:48ba87cd79b5 | 61 | * |
AhmedPlaymaker | 7:48ba87cd79b5 | 62 | * This function contains the draw functions of the other libraries used in the game. |
AhmedPlaymaker | 7:48ba87cd79b5 | 63 | */ |
AhmedPlaymaker | 49:441c32f6603e | 64 | void draw(N5110 &lcd, Gamepad &pad); |
AhmedPlaymaker | 62:ebf6ecf8a6d5 | 65 | |
AhmedPlaymaker | 56:142e9fdb77a8 | 66 | /** Update |
AhmedPlaymaker | 56:142e9fdb77a8 | 67 | * |
AhmedPlaymaker | 56:142e9fdb77a8 | 68 | * This function contains the update functions of the other libraries used in the game. |
AhmedPlaymaker | 56:142e9fdb77a8 | 69 | */ |
AhmedPlaymaker | 63:205f0ca48473 | 70 | void update(N5110 &lcd, Gamepad &pad); |
AhmedPlaymaker | 64:540aa1602372 | 71 | |
AhmedPlaymaker | 63:205f0ca48473 | 72 | /** Check Game Progression |
AhmedPlaymaker | 63:205f0ca48473 | 73 | * |
AhmedPlaymaker | 63:205f0ca48473 | 74 | * Function handles level progression and level failure operations by using the class WinLoose. |
AhmedPlaymaker | 63:205f0ca48473 | 75 | */ |
AhmedPlaymaker | 63:205f0ca48473 | 76 | int CheckGameProgression(N5110 &lcd, Gamepad &pad, SDFileSystem &sd); |
AhmedPlaymaker | 62:ebf6ecf8a6d5 | 77 | |
AhmedPlaymaker | 7:48ba87cd79b5 | 78 | /** Get Position |
AhmedPlaymaker | 7:48ba87cd79b5 | 79 | * |
AhmedPlaymaker | 7:48ba87cd79b5 | 80 | * This function contains the Get Position functions of the otehr libraries used in the game. |
AhmedPlaymaker | 7:48ba87cd79b5 | 81 | */ |
AhmedPlaymaker | 7:48ba87cd79b5 | 82 | void get_pos(); |
AhmedPlaymaker | 62:ebf6ecf8a6d5 | 83 | |
AhmedPlaymaker | 62:ebf6ecf8a6d5 | 84 | //Gamepad @enginePad |
AhmedPlaymaker | 62:ebf6ecf8a6d5 | 85 | |
AhmedPlaymaker | 62:ebf6ecf8a6d5 | 86 | private: |
AhmedPlaymaker | 62:ebf6ecf8a6d5 | 87 | |
AhmedPlaymaker | 9:d1d79d4ee673 | 88 | /** Check for Snake and Food collision |
AhmedPlaymaker | 9:d1d79d4ee673 | 89 | * |
AhmedPlaymaker | 9:d1d79d4ee673 | 90 | * This function checks if the Snake has come into contact with it's food. |
AhmedPlaymaker | 9:d1d79d4ee673 | 91 | */ |
AhmedPlaymaker | 9:d1d79d4ee673 | 92 | void CheckSnakeFoodCollision(Gamepad &pad); |
AhmedPlaymaker | 62:ebf6ecf8a6d5 | 93 | |
AhmedPlaymaker | 65:2872ca289b49 | 94 | /** Check Snake Food Y Collision |
AhmedPlaymaker | 65:2872ca289b49 | 95 | * |
AhmedPlaymaker | 65:2872ca289b49 | 96 | * This function automatically detects each combination of collision in the y postion. |
AhmedPlaymaker | 65:2872ca289b49 | 97 | */ |
AhmedPlaymaker | 79:35cb65c52d25 | 98 | void CheckSnakeFoodYCollision(Gamepad &pad, int food_sr); |
AhmedPlaymaker | 65:2872ca289b49 | 99 | |
AhmedPlaymaker | 65:2872ca289b49 | 100 | /** Check Snake Food X Collision |
AhmedPlaymaker | 65:2872ca289b49 | 101 | * |
AhmedPlaymaker | 65:2872ca289b49 | 102 | * This function automatically detects each combination of collision in the x postion. |
AhmedPlaymaker | 65:2872ca289b49 | 103 | */ |
AhmedPlaymaker | 79:35cb65c52d25 | 104 | void CheckSnakeFoodXCollision(Gamepad &pad, int food_sr); |
AhmedPlaymaker | 65:2872ca289b49 | 105 | |
AhmedPlaymaker | 65:2872ca289b49 | 106 | /** Implement Snake Food Collision |
AhmedPlaymaker | 65:2872ca289b49 | 107 | * |
AhmedPlaymaker | 65:2872ca289b49 | 108 | * This function automatically detects which food we are interacting with and increases the snake length accordingly. |
AhmedPlaymaker | 65:2872ca289b49 | 109 | */ |
AhmedPlaymaker | 79:35cb65c52d25 | 110 | void ImplementSnakeFoodCollision(Gamepad &pad, int food_sr); |
AhmedPlaymaker | 65:2872ca289b49 | 111 | |
AhmedPlaymaker | 12:1e601b176437 | 112 | /** Check for Snake and Block collision |
AhmedPlaymaker | 12:1e601b176437 | 113 | * |
AhmedPlaymaker | 12:1e601b176437 | 114 | * This function checks if the Snake has come into contact with any Block. |
AhmedPlaymaker | 12:1e601b176437 | 115 | */ |
AhmedPlaymaker | 12:1e601b176437 | 116 | void CheckSnakeBlockCollision(Gamepad &pad); |
AhmedPlaymaker | 62:ebf6ecf8a6d5 | 117 | |
AhmedPlaymaker | 39:210ac915e0a0 | 118 | /** Implement velocity alteration. |
AhmedPlaymaker | 39:210ac915e0a0 | 119 | * |
AhmedPlaymaker | 39:210ac915e0a0 | 120 | * this is to stop/move the background (food and blocks), when collision occurs at a length greater than 10.lock. |
AhmedPlaymaker | 39:210ac915e0a0 | 121 | */ |
AhmedPlaymaker | 65:2872ca289b49 | 122 | void _setVelocity(int srn); |
AhmedPlaymaker | 65:2872ca289b49 | 123 | |
AhmedPlaymaker | 65:2872ca289b49 | 124 | /** Check Block |
AhmedPlaymaker | 65:2872ca289b49 | 125 | * |
AhmedPlaymaker | 65:2872ca289b49 | 126 | * This function returns the srn of the block we are colliding with; |
AhmedPlaymaker | 65:2872ca289b49 | 127 | */ |
AhmedPlaymaker | 65:2872ca289b49 | 128 | int CheckBlock(int Block); |
AhmedPlaymaker | 65:2872ca289b49 | 129 | |
AhmedPlaymaker | 65:2872ca289b49 | 130 | /** Implement Collision |
AhmedPlaymaker | 65:2872ca289b49 | 131 | * |
AhmedPlaymaker | 65:2872ca289b49 | 132 | * This function allows the appropriate maths to take place after every collision. |
AhmedPlaymaker | 65:2872ca289b49 | 133 | */ |
AhmedPlaymaker | 65:2872ca289b49 | 134 | void ImplementSnakeBlockCollision(Gamepad &pad); |
AhmedPlaymaker | 62:ebf6ecf8a6d5 | 135 | |
AhmedPlaymaker | 13:9785f2404045 | 136 | /** Check for Snake and Block Sides collision |
AhmedPlaymaker | 13:9785f2404045 | 137 | * |
AhmedPlaymaker | 13:9785f2404045 | 138 | * This function checks if the Snake has come into contact with any the sides of the block and stops it moving. |
AhmedPlaymaker | 13:9785f2404045 | 139 | */ |
AhmedPlaymaker | 38:30e4e6191762 | 140 | void CheckSnakeBlockSidesCollision(Gamepad &pad); |
AhmedPlaymaker | 62:ebf6ecf8a6d5 | 141 | |
AhmedPlaymaker | 63:205f0ca48473 | 142 | /** Make Default Motion Free |
AhmedPlaymaker | 63:205f0ca48473 | 143 | * |
AhmedPlaymaker | 63:205f0ca48473 | 144 | * This function makes the default motion of the snake freemoving before a collision is checked for, to forget the previous collision. |
AhmedPlaymaker | 63:205f0ca48473 | 145 | */ |
AhmedPlaymaker | 63:205f0ca48473 | 146 | void MakeDefaultMotionFree(); |
AhmedPlaymaker | 63:205f0ca48473 | 147 | |
AhmedPlaymaker | 62:ebf6ecf8a6d5 | 148 | /** Check Collision in Y axis |
AhmedPlaymaker | 62:ebf6ecf8a6d5 | 149 | * |
AhmedPlaymaker | 62:ebf6ecf8a6d5 | 150 | * This function checks if the Snake and blocks collide anywhere in the Y axis. |
AhmedPlaymaker | 62:ebf6ecf8a6d5 | 151 | */ |
AhmedPlaymaker | 65:2872ca289b49 | 152 | void CheckSnakeBlockSidesYCollision(int i); |
AhmedPlaymaker | 62:ebf6ecf8a6d5 | 153 | |
AhmedPlaymaker | 62:ebf6ecf8a6d5 | 154 | /** Check Collision in X axis |
AhmedPlaymaker | 62:ebf6ecf8a6d5 | 155 | * |
AhmedPlaymaker | 62:ebf6ecf8a6d5 | 156 | * This function checks if the Snake and blocks collide anywhere in the X axis. |
AhmedPlaymaker | 62:ebf6ecf8a6d5 | 157 | */ |
AhmedPlaymaker | 65:2872ca289b49 | 158 | void CheckSnakeBlockSidesXCollision(int i); |
AhmedPlaymaker | 62:ebf6ecf8a6d5 | 159 | |
AhmedPlaymaker | 62:ebf6ecf8a6d5 | 160 | /** Check Collision East or West |
AhmedPlaymaker | 62:ebf6ecf8a6d5 | 161 | * |
AhmedPlaymaker | 62:ebf6ecf8a6d5 | 162 | * checks if the colliding wall is on east side or west side and then calls StopX_AxisMotion(length,i) to carry out the required task. |
AhmedPlaymaker | 62:ebf6ecf8a6d5 | 163 | */ |
AhmedPlaymaker | 79:35cb65c52d25 | 164 | void CheckSnakeBlockSidesEastWestCollision(int X, int i); |
AhmedPlaymaker | 62:ebf6ecf8a6d5 | 165 | |
AhmedPlaymaker | 64:540aa1602372 | 166 | /** Make Virtual Length Max Ten |
AhmedPlaymaker | 64:540aa1602372 | 167 | * |
AhmedPlaymaker | 64:540aa1602372 | 168 | * 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 |
AhmedPlaymaker | 64:540aa1602372 | 169 | * on screen. |
AhmedPlaymaker | 64:540aa1602372 | 170 | */ |
AhmedPlaymaker | 64:540aa1602372 | 171 | void makeVirtualLengthMaxTen(); |
AhmedPlaymaker | 64:540aa1602372 | 172 | |
AhmedPlaymaker | 62:ebf6ecf8a6d5 | 173 | /** Stop X Axis Motion |
AhmedPlaymaker | 62:ebf6ecf8a6d5 | 174 | * |
AhmedPlaymaker | 62:ebf6ecf8a6d5 | 175 | * This function implements the sides collision by making the x axis speed of the particular snake beed 0. |
AhmedPlaymaker | 62:ebf6ecf8a6d5 | 176 | */ |
AhmedPlaymaker | 65:2872ca289b49 | 177 | void ImplementSnakeBlockSidesCollision(int i); |
AhmedPlaymaker | 64:540aa1602372 | 178 | |
AhmedPlaymaker | 64:540aa1602372 | 179 | //OBJECT DECLARATIONS. |
AhmedPlaymaker | 64:540aa1602372 | 180 | Snake _s; |
AhmedPlaymaker | 79:35cb65c52d25 | 181 | LengthManager _l; |
AhmedPlaymaker | 64:540aa1602372 | 182 | WinLoose _wl; |
AhmedPlaymaker | 64:540aa1602372 | 183 | SnakeFood _f; |
AhmedPlaymaker | 64:540aa1602372 | 184 | SnakeFood _ff; |
AhmedPlaymaker | 64:540aa1602372 | 185 | SnakeFood _fff; |
AhmedPlaymaker | 64:540aa1602372 | 186 | Blocks _b; |
AhmedPlaymaker | 81:4c1641e10dcd | 187 | Barriers _barA; |
AhmedPlaymaker | 81:4c1641e10dcd | 188 | Barriers _barB; |
AhmedPlaymaker | 64:540aa1602372 | 189 | Stats _Setstats; |
AhmedPlaymaker | 64:540aa1602372 | 190 | |
AhmedPlaymaker | 64:540aa1602372 | 191 | //OBJECT POSITIONS ON SCREEN. |
AhmedPlaymaker | 64:540aa1602372 | 192 | int snakex; //x position of top beed |
AhmedPlaymaker | 64:540aa1602372 | 193 | int snakey; //y position of top beed |
AhmedPlaymaker | 71:4bd2b27693f3 | 194 | Vector2D snake_pos[10]; //saves the position of all the snake beeds in an array for ease of collision processing. |
AhmedPlaymaker | 64:540aa1602372 | 195 | 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 | 196 | Vector2D b_pos; //saves the origin of the blocks on the screen (this is the top left corner of the block row). |
AhmedPlaymaker | 81:4c1641e10dcd | 197 | Vector2D bar_pos; //saves the origin of the barrier on the screen (this is the top left corner of the barrier). |
AhmedPlaymaker | 64:540aa1602372 | 198 | |
AhmedPlaymaker | 64:540aa1602372 | 199 | //LEVEL PROGRESSION, FAILURE AND STATISTICS PARAMETERS. |
AhmedPlaymaker | 64:540aa1602372 | 200 | 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 | 201 | char bufferlevel[14]; //this helps me print the level on screen. |
AhmedPlaymaker | 64:540aa1602372 | 202 | int back; //enables the player to go back on main menu if back is pressed. |
AhmedPlaymaker | 64:540aa1602372 | 203 | int _maxLength; // this makes us go to the next level if if maxLength is achieved; |
AhmedPlaymaker | 64:540aa1602372 | 204 | |
AhmedPlaymaker | 81:4c1641e10dcd | 205 | //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 | 206 | int velocity; //this is to stop/move the background (food and blocks), when collision occurs at a length greater than 10. |
AhmedPlaymaker | 64:540aa1602372 | 207 | 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 | 208 | int _virtualLength; //saves the length of the snake, for collision detection relative to it's Max screen length. |
AhmedPlaymaker | 70:7caab8069b9b | 209 | int b[10]; //this saves the beed number of the colliding snake, if beed 3 from top was colliding with any obstruction, b[2] will be 1. |
AhmedPlaymaker | 64:540aa1602372 | 210 | |
AhmedPlaymaker | 64:540aa1602372 | 211 | //TURN DIRECTION, TILT ANGLE AND ANGLE RESET BUFFER (GARBAGE). |
AhmedPlaymaker | 64:540aa1602372 | 212 | Direction _d; |
AhmedPlaymaker | 64:540aa1602372 | 213 | float _tiltAngle; //saves the angle of tilt. |
AhmedPlaymaker | 64:540aa1602372 | 214 | int garbage; //to save the angle at the point button A is pressed. |
AhmedPlaymaker | 64:540aa1602372 | 215 | |
AhmedPlaymaker | 64:540aa1602372 | 216 | //BLOCK NUMBERS, BLOCK DROP GAP, BLOCK DETECTION SRN AND VARIABLE TO SEND/NOT BLOCK NUMBER TO BLOCK CLASS |
AhmedPlaymaker | 64:540aa1602372 | 217 | int *b_number; //pointer to save the numbers inside the block. |
AhmedPlaymaker | 64:540aa1602372 | 218 | int blocknum; // saves the number inside the specific colliding block, for calculations. |
AhmedPlaymaker | 64:540aa1602372 | 219 | int blockgap; //to change frequency of fall |
AhmedPlaymaker | 64:540aa1602372 | 220 | int srn; //sr number of the block we are colliding with (1 to 5) |
AhmedPlaymaker | 64:540aa1602372 | 221 | int send_block_number; //makes sure that the block number is only updated when send is activated. |
AhmedPlaymaker | 64:540aa1602372 | 222 | |
AhmedPlaymaker | 64:540aa1602372 | 223 | //INITIAL FOOD AND BLOCK DROP GAP. |
AhmedPlaymaker | 64:540aa1602372 | 224 | int _dropbuff; //this makes food 1,2,and 3 come at seperate times |
AhmedPlaymaker | 7:48ba87cd79b5 | 225 | }; |
AhmedPlaymaker | 7:48ba87cd79b5 | 226 | #endif |