Ahmed Adamjee
/
SnakeVSBlock
Snake vs Block Game to be run upon K64F.
GameObjects/Snake/Snake.h@104:17040265b7b4, 2019-05-09 (annotated)
- Committer:
- AhmedPlaymaker
- Date:
- Thu May 09 14:52:19 2019 +0000
- Revision:
- 104:17040265b7b4
- Parent:
- 96:1ab67b3e6898
Final Submission. I have read and agreed with Statement of Academic Integrity.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
AhmedPlaymaker | 87:871d9fecb593 | 1 | #ifndef SNAKE_H |
AhmedPlaymaker | 87:871d9fecb593 | 2 | #define SNAKE_H |
AhmedPlaymaker | 7:48ba87cd79b5 | 3 | |
AhmedPlaymaker | 7:48ba87cd79b5 | 4 | #include "mbed.h" |
AhmedPlaymaker | 7:48ba87cd79b5 | 5 | #include "N5110.h" |
AhmedPlaymaker | 7:48ba87cd79b5 | 6 | #include "Gamepad.h" |
AhmedPlaymaker | 7:48ba87cd79b5 | 7 | |
AhmedPlaymaker | 96:1ab67b3e6898 | 8 | /** Snake Class |
AhmedPlaymaker | 96:1ab67b3e6898 | 9 | @brief This class draws and updates snake after every collision, and creates perfect animations of the snake moving. |
AhmedPlaymaker | 96:1ab67b3e6898 | 10 | @author Ahmed N.Adamjee |
AhmedPlaymaker | 96:1ab67b3e6898 | 11 | @date 9th May 2019 |
AhmedPlaymaker | 96:1ab67b3e6898 | 12 | */ |
AhmedPlaymaker | 7:48ba87cd79b5 | 13 | class Snake |
AhmedPlaymaker | 7:48ba87cd79b5 | 14 | { |
AhmedPlaymaker | 69:55e309da7efd | 15 | public: |
AhmedPlaymaker | 96:1ab67b3e6898 | 16 | /** Constructor */ |
AhmedPlaymaker | 7:48ba87cd79b5 | 17 | Snake(); |
AhmedPlaymaker | 96:1ab67b3e6898 | 18 | /** Destructor */ |
AhmedPlaymaker | 7:48ba87cd79b5 | 19 | ~Snake(); |
AhmedPlaymaker | 69:55e309da7efd | 20 | |
AhmedPlaymaker | 96:1ab67b3e6898 | 21 | /** |
AhmedPlaymaker | 96:1ab67b3e6898 | 22 | * @brief Initialises the snakes position struct and reset variable and gets pointers of lcd from int main() to be used privately in the entire class. |
AhmedPlaymaker | 96:1ab67b3e6898 | 23 | * @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 | 7:48ba87cd79b5 | 24 | */ |
AhmedPlaymaker | 83:329da564799a | 25 | void init(N5110 *lcd); |
AhmedPlaymaker | 96:1ab67b3e6898 | 26 | |
AhmedPlaymaker | 96:1ab67b3e6898 | 27 | /** |
AhmedPlaymaker | 96:1ab67b3e6898 | 28 | * @brief This mutator sets the length of the snake directly when called. |
AhmedPlaymaker | 96:1ab67b3e6898 | 29 | * @param length @details This is the length of the snake, which is recieved from the SnakevsBlocks class or test files to draw snake with relation to it's length. |
AhmedPlaymaker | 96:1ab67b3e6898 | 30 | */ |
AhmedPlaymaker | 96:1ab67b3e6898 | 31 | void _setLength(int length); |
AhmedPlaymaker | 83:329da564799a | 32 | |
AhmedPlaymaker | 96:1ab67b3e6898 | 33 | /** |
AhmedPlaymaker | 96:1ab67b3e6898 | 34 | * @brief This mutator sets the speed of the snake directly when called. |
AhmedPlaymaker | 96:1ab67b3e6898 | 35 | * @param speed @details This is the x axis speed of the snake, which is recieved from the SnakevsBlocks class or test files to move snake with respect to joystick or motion control input. |
AhmedPlaymaker | 96:1ab67b3e6898 | 36 | */ |
AhmedPlaymaker | 83:329da564799a | 37 | void _setSpeed(int speed); |
AhmedPlaymaker | 69:55e309da7efd | 38 | |
AhmedPlaymaker | 96:1ab67b3e6898 | 39 | /** |
AhmedPlaymaker | 96:1ab67b3e6898 | 40 | * @brief This function draws the Snake sprite onto the screen at the specified coordinates. |
AhmedPlaymaker | 7:48ba87cd79b5 | 41 | */ |
AhmedPlaymaker | 83:329da564799a | 42 | void draw(); |
AhmedPlaymaker | 69:55e309da7efd | 43 | |
AhmedPlaymaker | 96:1ab67b3e6898 | 44 | /** |
AhmedPlaymaker | 96:1ab67b3e6898 | 45 | * @brief This function sets a position for where the snake should spawn. |
AhmedPlaymaker | 96:1ab67b3e6898 | 46 | * @param Vector2D p @details This is a struct that saves the x and y position one of the snake beads during the time of initialisation and sets other snake bead coordinates with respect to this. |
AhmedPlaymaker | 68:b9cfd27987ac | 47 | */ |
AhmedPlaymaker | 68:b9cfd27987ac | 48 | void set_pos(Vector2D p); |
AhmedPlaymaker | 69:55e309da7efd | 49 | |
AhmedPlaymaker | 96:1ab67b3e6898 | 50 | /** |
AhmedPlaymaker | 96:1ab67b3e6898 | 51 | * @brief This function updates the Snake sprite position on screen. |
AhmedPlaymaker | 96:1ab67b3e6898 | 52 | * @param Direction d @details This struct saves the direction of snake movement. |
AhmedPlaymaker | 96:1ab67b3e6898 | 53 | * @param immobile_bead_n[10] @details this array is indexed by the beed number of the colliding snake, if immobile_bead_n[2] = 1 the particular bead can move freely. |
AhmedPlaymaker | 7:48ba87cd79b5 | 54 | */ |
AhmedPlaymaker | 87:871d9fecb593 | 55 | void update(Direction d, int* immobile_bead_n); |
AhmedPlaymaker | 96:1ab67b3e6898 | 56 | |
AhmedPlaymaker | 96:1ab67b3e6898 | 57 | /** |
AhmedPlaymaker | 96:1ab67b3e6898 | 58 | * @brief This function makes all of the snake beeds chained together by making the lower ones drag towards where the top one was in the previous loop. |
AhmedPlaymaker | 96:1ab67b3e6898 | 59 | * @param immobile_bead_n[10] @details this array is indexed by the beed number of the colliding snake, if immobile_bead_n[2] = 1 the particular bead can move freely. |
AhmedPlaymaker | 71:4bd2b27693f3 | 60 | */ |
AhmedPlaymaker | 87:871d9fecb593 | 61 | void chainSnakeTogether(int* immobile_bead_n); |
AhmedPlaymaker | 71:4bd2b27693f3 | 62 | |
AhmedPlaymaker | 96:1ab67b3e6898 | 63 | /** |
AhmedPlaymaker | 96:1ab67b3e6898 | 64 | * @brief This function makes the controls of W/E directions only exclusive to the top beed in the snake. |
AhmedPlaymaker | 96:1ab67b3e6898 | 65 | * @param immobile_bead_n[10] @details this array is indexed by the beed number of the colliding snake, if immobile_bead_n[2] = 1 the particular bead can move freely. |
AhmedPlaymaker | 71:4bd2b27693f3 | 66 | */ |
AhmedPlaymaker | 87:871d9fecb593 | 67 | void moveSnake(int* immobile_bead_n); |
AhmedPlaymaker | 71:4bd2b27693f3 | 68 | |
AhmedPlaymaker | 96:1ab67b3e6898 | 69 | /** |
AhmedPlaymaker | 96:1ab67b3e6898 | 70 | * @brief This function makes sure that the snake stays where it is when it ate food and does not travel off the screen. |
AhmedPlaymaker | 71:4bd2b27693f3 | 71 | */ |
AhmedPlaymaker | 71:4bd2b27693f3 | 72 | void _setSnakeLimits(); |
AhmedPlaymaker | 69:55e309da7efd | 73 | |
AhmedPlaymaker | 96:1ab67b3e6898 | 74 | /** |
AhmedPlaymaker | 96:1ab67b3e6898 | 75 | * @brief This function returns the coordinate of the top-left pixel in the Snake sprite of each bead which is reffered to by snakeIndex. |
AhmedPlaymaker | 96:1ab67b3e6898 | 76 | * @param int snakeIndex @details This is the index of the snake bead of which the position needs to be returned. |
AhmedPlaymaker | 96:1ab67b3e6898 | 77 | * @returns Vector2D snakepos @details This is a struct that returns the x and y position of the snake bead that is being referred to by the snakeIndex. |
AhmedPlaymaker | 7:48ba87cd79b5 | 78 | */ |
AhmedPlaymaker | 70:7caab8069b9b | 79 | Vector2D get_pos(int snakeIndex); |
AhmedPlaymaker | 70:7caab8069b9b | 80 | |
AhmedPlaymaker | 96:1ab67b3e6898 | 81 | int reset; //reset is used to re position the snake after each level.. |
AhmedPlaymaker | 87:871d9fecb593 | 82 | int immobile_bead_n[10]; //each element in this array represents the beed number in the snake. |
AhmedPlaymaker | 69:55e309da7efd | 83 | |
AhmedPlaymaker | 69:55e309da7efd | 84 | private: |
AhmedPlaymaker | 25:e827f1a8fadc | 85 | int _speed; //this is the speed of the snake flowing in the x axis. |
AhmedPlaymaker | 79:35cb65c52d25 | 86 | int _x[10]; //each element in this array represents the x position of each beed in the snake. |
AhmedPlaymaker | 79:35cb65c52d25 | 87 | int _y[10]; //each element in this array represents the y position of each beed in the snake. |
AhmedPlaymaker | 87:871d9fecb593 | 88 | int _length; //stores thee length of snake to be drawn or managed in terms of structure. |
AhmedPlaymaker | 87:871d9fecb593 | 89 | Direction _d; //Stores the direction the snake moves in. |
AhmedPlaymaker | 96:1ab67b3e6898 | 90 | |
AhmedPlaymaker | 83:329da564799a | 91 | //Pointer to the game pad object pad. |
AhmedPlaymaker | 83:329da564799a | 92 | Gamepad *_pad; |
AhmedPlaymaker | 83:329da564799a | 93 | //Pointer to the N5110 object lcd. |
AhmedPlaymaker | 83:329da564799a | 94 | N5110 *_lcd; |
AhmedPlaymaker | 7:48ba87cd79b5 | 95 | |
AhmedPlaymaker | 7:48ba87cd79b5 | 96 | }; |
AhmedPlaymaker | 7:48ba87cd79b5 | 97 | #endif |