Snake vs Block Game to be run upon K64F.

Dependencies:   mbed

Committer:
AhmedPlaymaker
Date:
Thu May 02 11:30:04 2019 +0000
Revision:
64:540aa1602372
Parent:
63:205f0ca48473
Child:
65:2872ca289b49
Made header file more readable

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 41:4edac50f010d 9 #include "LengthCalc.h"
AhmedPlaymaker 44:cd10d07ea1e5 10 #include "WinLoose.h"
AhmedPlaymaker 9:d1d79d4ee673 11 #include "SnakeFood.h"
AhmedPlaymaker 10:751bd953fa27 12 #include "Blocks.h"
AhmedPlaymaker 33:249cf423fb18 13 #include "StartScreen.h"
AhmedPlaymaker 33:249cf423fb18 14 #include "Stats.h"
AhmedPlaymaker 33:249cf423fb18 15 #include "SDFileSystem.h"
AhmedPlaymaker 7:48ba87cd79b5 16
AhmedPlaymaker 7:48ba87cd79b5 17 class SnakevsBlock
AhmedPlaymaker 7:48ba87cd79b5 18 {
AhmedPlaymaker 62:ebf6ecf8a6d5 19 public:
AhmedPlaymaker 7:48ba87cd79b5 20 SnakevsBlock();
AhmedPlaymaker 7:48ba87cd79b5 21 ~SnakevsBlock();
AhmedPlaymaker 62:ebf6ecf8a6d5 22
AhmedPlaymaker 7:48ba87cd79b5 23 /** Initialise Game Machine
AhmedPlaymaker 7:48ba87cd79b5 24 *
AhmedPlaymaker 7:48ba87cd79b5 25 * This function initialises the game machine.
AhmedPlaymaker 7:48ba87cd79b5 26 */
AhmedPlaymaker 49:441c32f6603e 27 void init();
AhmedPlaymaker 62:ebf6ecf8a6d5 28
AhmedPlaymaker 41:4edac50f010d 29 /** Reset Game Machine
AhmedPlaymaker 41:4edac50f010d 30 *
AhmedPlaymaker 41:4edac50f010d 31 * This function prepares the game machine for the next level.
AhmedPlaymaker 41:4edac50f010d 32 */
AhmedPlaymaker 41:4edac50f010d 33 void reset( );
AhmedPlaymaker 62:ebf6ecf8a6d5 34
AhmedPlaymaker 49:441c32f6603e 35 /** Initialise objects
AhmedPlaymaker 49:441c32f6603e 36 *
AhmedPlaymaker 49:441c32f6603e 37 * This function initialises the objects that are used to functionalise the game.
AhmedPlaymaker 49:441c32f6603e 38 */
AhmedPlaymaker 49:441c32f6603e 39 void object_initialisations();
AhmedPlaymaker 62:ebf6ecf8a6d5 40
AhmedPlaymaker 7:48ba87cd79b5 41 /** Read Input
AhmedPlaymaker 7:48ba87cd79b5 42 *
AhmedPlaymaker 7:48ba87cd79b5 43 * This function obtains numeric data from the gamepads joystick.
AhmedPlaymaker 7:48ba87cd79b5 44 */
AhmedPlaymaker 38:30e4e6191762 45 void read_input(Gamepad &pad, FXOS8700CQ &device, int gm);
AhmedPlaymaker 64:540aa1602372 46
AhmedPlaymaker 63:205f0ca48473 47 /** Calculate Tilt
AhmedPlaymaker 63:205f0ca48473 48 *
AhmedPlaymaker 63:205f0ca48473 49 * 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 50 */
AhmedPlaymaker 63:205f0ca48473 51 void calculateTilt(Gamepad &pad, FXOS8700CQ &device);
AhmedPlaymaker 64:540aa1602372 52
AhmedPlaymaker 63:205f0ca48473 53 /** Light The LEDS
AhmedPlaymaker 63:205f0ca48473 54 *
AhmedPlaymaker 63:205f0ca48473 55 * This function ligths the LEDS dependent on the direction of travel.
AhmedPlaymaker 63:205f0ca48473 56 */
AhmedPlaymaker 63:205f0ca48473 57 void lightTheLEDS(Gamepad &pad);
AhmedPlaymaker 62:ebf6ecf8a6d5 58
AhmedPlaymaker 62:ebf6ecf8a6d5 59 /** Draw
AhmedPlaymaker 7:48ba87cd79b5 60 *
AhmedPlaymaker 7:48ba87cd79b5 61 * This function contains the draw functions of the other libraries used in the game.
AhmedPlaymaker 7:48ba87cd79b5 62 */
AhmedPlaymaker 49:441c32f6603e 63 void draw(N5110 &lcd, Gamepad &pad);
AhmedPlaymaker 62:ebf6ecf8a6d5 64
AhmedPlaymaker 56:142e9fdb77a8 65 /** Update
AhmedPlaymaker 56:142e9fdb77a8 66 *
AhmedPlaymaker 56:142e9fdb77a8 67 * This function contains the update functions of the other libraries used in the game.
AhmedPlaymaker 56:142e9fdb77a8 68 */
AhmedPlaymaker 63:205f0ca48473 69 void update(N5110 &lcd, Gamepad &pad);
AhmedPlaymaker 64:540aa1602372 70
AhmedPlaymaker 63:205f0ca48473 71 /** Check Game Progression
AhmedPlaymaker 63:205f0ca48473 72 *
AhmedPlaymaker 63:205f0ca48473 73 * Function handles level progression and level failure operations by using the class WinLoose.
AhmedPlaymaker 63:205f0ca48473 74 */
AhmedPlaymaker 63:205f0ca48473 75 int CheckGameProgression(N5110 &lcd, Gamepad &pad, SDFileSystem &sd);
AhmedPlaymaker 62:ebf6ecf8a6d5 76
AhmedPlaymaker 7:48ba87cd79b5 77 /** Get Position
AhmedPlaymaker 7:48ba87cd79b5 78 *
AhmedPlaymaker 7:48ba87cd79b5 79 * This function contains the Get Position functions of the otehr libraries used in the game.
AhmedPlaymaker 7:48ba87cd79b5 80 */
AhmedPlaymaker 7:48ba87cd79b5 81 void get_pos();
AhmedPlaymaker 62:ebf6ecf8a6d5 82
AhmedPlaymaker 29:c6358c39a70e 83 /** Check Block
AhmedPlaymaker 29:c6358c39a70e 84 *
AhmedPlaymaker 29:c6358c39a70e 85 * This function returns the srn of the block we are colliding with;
AhmedPlaymaker 29:c6358c39a70e 86 */
AhmedPlaymaker 29:c6358c39a70e 87 int CheckBlock(int Block);
AhmedPlaymaker 62:ebf6ecf8a6d5 88
AhmedPlaymaker 27:3b4775a0d0bb 89 /** Implement Collision
AhmedPlaymaker 27:3b4775a0d0bb 90 *
AhmedPlaymaker 27:3b4775a0d0bb 91 * This function allows the appropriate maths to take place after every collision.
AhmedPlaymaker 27:3b4775a0d0bb 92 */
AhmedPlaymaker 27:3b4775a0d0bb 93 void ImplementCollision(Gamepad &pad);
AhmedPlaymaker 62:ebf6ecf8a6d5 94
AhmedPlaymaker 62:ebf6ecf8a6d5 95 //Gamepad @enginePad
AhmedPlaymaker 62:ebf6ecf8a6d5 96
AhmedPlaymaker 62:ebf6ecf8a6d5 97 private:
AhmedPlaymaker 62:ebf6ecf8a6d5 98
AhmedPlaymaker 9:d1d79d4ee673 99 /** Check for Snake and Food collision
AhmedPlaymaker 9:d1d79d4ee673 100 *
AhmedPlaymaker 9:d1d79d4ee673 101 * This function checks if the Snake has come into contact with it's food.
AhmedPlaymaker 9:d1d79d4ee673 102 */
AhmedPlaymaker 9:d1d79d4ee673 103 void CheckSnakeFoodCollision(Gamepad &pad);
AhmedPlaymaker 62:ebf6ecf8a6d5 104
AhmedPlaymaker 12:1e601b176437 105 /** Check for Snake and Block collision
AhmedPlaymaker 12:1e601b176437 106 *
AhmedPlaymaker 12:1e601b176437 107 * This function checks if the Snake has come into contact with any Block.
AhmedPlaymaker 12:1e601b176437 108 */
AhmedPlaymaker 12:1e601b176437 109 void CheckSnakeBlockCollision(Gamepad &pad);
AhmedPlaymaker 62:ebf6ecf8a6d5 110
AhmedPlaymaker 39:210ac915e0a0 111 /** Implement velocity alteration.
AhmedPlaymaker 39:210ac915e0a0 112 *
AhmedPlaymaker 39:210ac915e0a0 113 * this is to stop/move the background (food and blocks), when collision occurs at a length greater than 10.lock.
AhmedPlaymaker 39:210ac915e0a0 114 */
AhmedPlaymaker 39:210ac915e0a0 115 void _set_velocity();
AhmedPlaymaker 62:ebf6ecf8a6d5 116
AhmedPlaymaker 13:9785f2404045 117 /** Check for Snake and Block Sides collision
AhmedPlaymaker 13:9785f2404045 118 *
AhmedPlaymaker 13:9785f2404045 119 * This function checks if the Snake has come into contact with any the sides of the block and stops it moving.
AhmedPlaymaker 13:9785f2404045 120 */
AhmedPlaymaker 38:30e4e6191762 121 void CheckSnakeBlockSidesCollision(Gamepad &pad);
AhmedPlaymaker 62:ebf6ecf8a6d5 122
AhmedPlaymaker 63:205f0ca48473 123 /** Make Default Motion Free
AhmedPlaymaker 63:205f0ca48473 124 *
AhmedPlaymaker 63:205f0ca48473 125 * This function makes the default motion of the snake freemoving before a collision is checked for, to forget the previous collision.
AhmedPlaymaker 63:205f0ca48473 126 */
AhmedPlaymaker 63:205f0ca48473 127 void MakeDefaultMotionFree();
AhmedPlaymaker 63:205f0ca48473 128
AhmedPlaymaker 62:ebf6ecf8a6d5 129 /** Check Collision in Y axis
AhmedPlaymaker 62:ebf6ecf8a6d5 130 *
AhmedPlaymaker 62:ebf6ecf8a6d5 131 * This function checks if the Snake and blocks collide anywhere in the Y axis.
AhmedPlaymaker 62:ebf6ecf8a6d5 132 */
AhmedPlaymaker 62:ebf6ecf8a6d5 133 void check_Collision_Y_axis(int i);
AhmedPlaymaker 62:ebf6ecf8a6d5 134
AhmedPlaymaker 62:ebf6ecf8a6d5 135 /** Check Collision in X axis
AhmedPlaymaker 62:ebf6ecf8a6d5 136 *
AhmedPlaymaker 62:ebf6ecf8a6d5 137 * This function checks if the Snake and blocks collide anywhere in the X axis.
AhmedPlaymaker 62:ebf6ecf8a6d5 138 */
AhmedPlaymaker 62:ebf6ecf8a6d5 139 void check_Collision_X_axis(int i);
AhmedPlaymaker 62:ebf6ecf8a6d5 140
AhmedPlaymaker 62:ebf6ecf8a6d5 141 /** Check Collision East or West
AhmedPlaymaker 62:ebf6ecf8a6d5 142 *
AhmedPlaymaker 62:ebf6ecf8a6d5 143 * 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 144 */
AhmedPlaymaker 62:ebf6ecf8a6d5 145 void checkCollision_EastorWest(int b_x_combination, int i);
AhmedPlaymaker 62:ebf6ecf8a6d5 146
AhmedPlaymaker 64:540aa1602372 147 /** Make Virtual Length Max Ten
AhmedPlaymaker 64:540aa1602372 148 *
AhmedPlaymaker 64:540aa1602372 149 * 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 150 * on screen.
AhmedPlaymaker 64:540aa1602372 151 */
AhmedPlaymaker 64:540aa1602372 152 void makeVirtualLengthMaxTen();
AhmedPlaymaker 64:540aa1602372 153
AhmedPlaymaker 62:ebf6ecf8a6d5 154 /** Stop X Axis Motion
AhmedPlaymaker 62:ebf6ecf8a6d5 155 *
AhmedPlaymaker 62:ebf6ecf8a6d5 156 * This function implements the sides collision by making the x axis speed of the particular snake beed 0.
AhmedPlaymaker 62:ebf6ecf8a6d5 157 */
AhmedPlaymaker 64:540aa1602372 158 void StopX_AxisMotion(int i);
AhmedPlaymaker 64:540aa1602372 159
AhmedPlaymaker 64:540aa1602372 160 //OBJECT DECLARATIONS.
AhmedPlaymaker 64:540aa1602372 161 Snake _s;
AhmedPlaymaker 64:540aa1602372 162 LengthCalc _l;
AhmedPlaymaker 64:540aa1602372 163 WinLoose _wl;
AhmedPlaymaker 64:540aa1602372 164 SnakeFood _f;
AhmedPlaymaker 64:540aa1602372 165 SnakeFood _ff;
AhmedPlaymaker 64:540aa1602372 166 SnakeFood _fff;
AhmedPlaymaker 64:540aa1602372 167 Blocks _b;
AhmedPlaymaker 64:540aa1602372 168 Stats _Setstats;
AhmedPlaymaker 64:540aa1602372 169
AhmedPlaymaker 64:540aa1602372 170 //OBJECT POSITIONS ON SCREEN.
AhmedPlaymaker 64:540aa1602372 171 int snakex; //x position of top beed
AhmedPlaymaker 64:540aa1602372 172 int snakey; //y position of top beed
AhmedPlaymaker 64:540aa1602372 173 Vector2D snake_pos[15]; //saves the position of all the snake beeds in an array for ease of collision processing.
AhmedPlaymaker 64:540aa1602372 174 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 175 Vector2D b_pos; //saves the origin of the blocks on the screen (this is the top left corner of the block row).
AhmedPlaymaker 64:540aa1602372 176
AhmedPlaymaker 64:540aa1602372 177 //LEVEL PROGRESSION, FAILURE AND STATISTICS PARAMETERS.
AhmedPlaymaker 64:540aa1602372 178 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 179 char bufferlevel[14]; //this helps me print the level on screen.
AhmedPlaymaker 64:540aa1602372 180 int back; //enables the player to go back on main menu if back is pressed.
AhmedPlaymaker 64:540aa1602372 181 int _maxLength; // this makes us go to the next level if if maxLength is achieved;
AhmedPlaymaker 64:540aa1602372 182
AhmedPlaymaker 64:540aa1602372 183 //SNAKE X AXIS VELOCITY, SNAKE LENGTH ,MAX VIRTUAL LENGTH AND VARABLE THAT ALLOWS FREE MOTION OF A SPECIFIC BEED NUMBER OF THE SNAKE.
AhmedPlaymaker 64:540aa1602372 184 int velocity; //this is to stop/move the background (food and blocks), when collision occurs at a length greater than 10.
AhmedPlaymaker 64:540aa1602372 185 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 186 int _virtualLength; //saves the length of the snake, for collision detection relative to it's Max screen length.
AhmedPlaymaker 64:540aa1602372 187 int b[15]; //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 188
AhmedPlaymaker 64:540aa1602372 189 //TURN DIRECTION, TILT ANGLE AND ANGLE RESET BUFFER (GARBAGE).
AhmedPlaymaker 64:540aa1602372 190 Direction _d;
AhmedPlaymaker 64:540aa1602372 191 float _tiltAngle; //saves the angle of tilt.
AhmedPlaymaker 64:540aa1602372 192 int garbage; //to save the angle at the point button A is pressed.
AhmedPlaymaker 64:540aa1602372 193
AhmedPlaymaker 64:540aa1602372 194 //BLOCK NUMBERS, BLOCK DROP GAP, BLOCK DETECTION SRN AND VARIABLE TO SEND/NOT BLOCK NUMBER TO BLOCK CLASS
AhmedPlaymaker 64:540aa1602372 195 int *b_number; //pointer to save the numbers inside the block.
AhmedPlaymaker 64:540aa1602372 196 int blocknum; // saves the number inside the specific colliding block, for calculations.
AhmedPlaymaker 64:540aa1602372 197 int blockgap; //to change frequency of fall
AhmedPlaymaker 64:540aa1602372 198 int srn; //sr number of the block we are colliding with (1 to 5)
AhmedPlaymaker 64:540aa1602372 199 int send_block_number; //makes sure that the block number is only updated when send is activated.
AhmedPlaymaker 64:540aa1602372 200
AhmedPlaymaker 64:540aa1602372 201 //INITIAL FOOD AND BLOCK DROP GAP.
AhmedPlaymaker 64:540aa1602372 202 int _dropbuff; //this makes food 1,2,and 3 come at seperate times
AhmedPlaymaker 7:48ba87cd79b5 203 };
AhmedPlaymaker 7:48ba87cd79b5 204 #endif