Ahmed Adamjee
/
SnakeVSBlock
Snake vs Block Game to be run upon K64F.
GameObjects/Snake/Snake.h@83:329da564799a, 2019-05-06 (annotated)
- Committer:
- AhmedPlaymaker
- Date:
- Mon May 06 14:28:35 2019 +0000
- Revision:
- 83:329da564799a
- Parent:
- 81:4c1641e10dcd
- Child:
- 87:871d9fecb593
Now, I have put gamepad and lcd objects from the main() in a pointer, so that i can declare them in init() in every class i use them in and use them as global objects for each class, without having to send their addresses to all of the functions.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
AhmedPlaymaker | 7:48ba87cd79b5 | 1 | #ifndef Snake_H |
AhmedPlaymaker | 7:48ba87cd79b5 | 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 | 7:48ba87cd79b5 | 8 | |
AhmedPlaymaker | 7:48ba87cd79b5 | 9 | class Snake |
AhmedPlaymaker | 7:48ba87cd79b5 | 10 | { |
AhmedPlaymaker | 69:55e309da7efd | 11 | public: |
AhmedPlaymaker | 69:55e309da7efd | 12 | |
AhmedPlaymaker | 69:55e309da7efd | 13 | |
AhmedPlaymaker | 7:48ba87cd79b5 | 14 | Snake(); |
AhmedPlaymaker | 7:48ba87cd79b5 | 15 | ~Snake(); |
AhmedPlaymaker | 69:55e309da7efd | 16 | |
AhmedPlaymaker | 9:d1d79d4ee673 | 17 | /** Initialise Snake |
AhmedPlaymaker | 7:48ba87cd79b5 | 18 | * |
AhmedPlaymaker | 9:d1d79d4ee673 | 19 | * This function initalises the Snake library. |
AhmedPlaymaker | 7:48ba87cd79b5 | 20 | */ |
AhmedPlaymaker | 83:329da564799a | 21 | void init(N5110 *lcd); |
AhmedPlaymaker | 83:329da564799a | 22 | |
AhmedPlaymaker | 83:329da564799a | 23 | void _setLength(int length); |
AhmedPlaymaker | 83:329da564799a | 24 | void _setSpeed(int speed); |
AhmedPlaymaker | 69:55e309da7efd | 25 | |
AhmedPlaymaker | 7:48ba87cd79b5 | 26 | /** Draw |
AhmedPlaymaker | 7:48ba87cd79b5 | 27 | * |
AhmedPlaymaker | 9:d1d79d4ee673 | 28 | * This function draws the Snake sprite onto the screen at the specified coordinates. |
AhmedPlaymaker | 7:48ba87cd79b5 | 29 | */ |
AhmedPlaymaker | 83:329da564799a | 30 | void draw(); |
AhmedPlaymaker | 69:55e309da7efd | 31 | |
AhmedPlaymaker | 68:b9cfd27987ac | 32 | /** Set Position |
AhmedPlaymaker | 68:b9cfd27987ac | 33 | * |
AhmedPlaymaker | 68:b9cfd27987ac | 34 | * This function sets a position for where the snake should spawn. |
AhmedPlaymaker | 68:b9cfd27987ac | 35 | */ |
AhmedPlaymaker | 68:b9cfd27987ac | 36 | void set_pos(Vector2D p); |
AhmedPlaymaker | 69:55e309da7efd | 37 | |
AhmedPlaymaker | 7:48ba87cd79b5 | 38 | /** Update |
AhmedPlaymaker | 7:48ba87cd79b5 | 39 | * |
AhmedPlaymaker | 9:d1d79d4ee673 | 40 | * This function updates the Snake sprite position on screen. |
AhmedPlaymaker | 7:48ba87cd79b5 | 41 | */ |
AhmedPlaymaker | 41:4edac50f010d | 42 | void update(Direction d, int* b); |
AhmedPlaymaker | 71:4bd2b27693f3 | 43 | |
AhmedPlaymaker | 71:4bd2b27693f3 | 44 | /** Chain Snake Together |
AhmedPlaymaker | 71:4bd2b27693f3 | 45 | * |
AhmedPlaymaker | 71:4bd2b27693f3 | 46 | * 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 | 71:4bd2b27693f3 | 47 | */ |
AhmedPlaymaker | 71:4bd2b27693f3 | 48 | void chainSnakeTogether(int* b); |
AhmedPlaymaker | 71:4bd2b27693f3 | 49 | |
AhmedPlaymaker | 71:4bd2b27693f3 | 50 | /** Moove Snake |
AhmedPlaymaker | 71:4bd2b27693f3 | 51 | * |
AhmedPlaymaker | 71:4bd2b27693f3 | 52 | * This function makes the controls of W/E directions only exclusive to the top beed in the snake |
AhmedPlaymaker | 71:4bd2b27693f3 | 53 | */ |
AhmedPlaymaker | 71:4bd2b27693f3 | 54 | void mooveSnake(Direction d, int* b); |
AhmedPlaymaker | 71:4bd2b27693f3 | 55 | |
AhmedPlaymaker | 71:4bd2b27693f3 | 56 | /** Set Snake Limits |
AhmedPlaymaker | 71:4bd2b27693f3 | 57 | * |
AhmedPlaymaker | 71:4bd2b27693f3 | 58 | * 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 | 59 | */ |
AhmedPlaymaker | 71:4bd2b27693f3 | 60 | void _setSnakeLimits(); |
AhmedPlaymaker | 69:55e309da7efd | 61 | |
AhmedPlaymaker | 7:48ba87cd79b5 | 62 | /** Get Position |
AhmedPlaymaker | 7:48ba87cd79b5 | 63 | * |
AhmedPlaymaker | 9:d1d79d4ee673 | 64 | * This function obtains the coordinate of the top-left pixel in the Snake sprite. |
AhmedPlaymaker | 7:48ba87cd79b5 | 65 | */ |
AhmedPlaymaker | 70:7caab8069b9b | 66 | Vector2D get_pos(int snakeIndex); |
AhmedPlaymaker | 70:7caab8069b9b | 67 | |
AhmedPlaymaker | 81:4c1641e10dcd | 68 | int reset; |
AhmedPlaymaker | 71:4bd2b27693f3 | 69 | int b[10]; //each element in this array represents the beed number in the snake. |
AhmedPlaymaker | 69:55e309da7efd | 70 | |
AhmedPlaymaker | 69:55e309da7efd | 71 | private: |
AhmedPlaymaker | 25:e827f1a8fadc | 72 | int _speed; //this is the speed of the snake flowing in the x axis. |
AhmedPlaymaker | 79:35cb65c52d25 | 73 | int _x[10]; //each element in this array represents the x position of each beed in the snake. |
AhmedPlaymaker | 79:35cb65c52d25 | 74 | int _y[10]; //each element in this array represents the y position of each beed in the snake. |
AhmedPlaymaker | 37:ee47699915b8 | 75 | int _length; |
AhmedPlaymaker | 83:329da564799a | 76 | |
AhmedPlaymaker | 83:329da564799a | 77 | //Pointer to the game pad object pad. |
AhmedPlaymaker | 83:329da564799a | 78 | Gamepad *_pad; |
AhmedPlaymaker | 83:329da564799a | 79 | //Pointer to the N5110 object lcd. |
AhmedPlaymaker | 83:329da564799a | 80 | N5110 *_lcd; |
AhmedPlaymaker | 7:48ba87cd79b5 | 81 | |
AhmedPlaymaker | 7:48ba87cd79b5 | 82 | }; |
AhmedPlaymaker | 7:48ba87cd79b5 | 83 | #endif |