Snake vs Block Game to be run upon K64F.

Dependencies:   mbed

Committer:
AhmedPlaymaker
Date:
Sun May 05 23:48:57 2019 +0000
Revision:
79:35cb65c52d25
Parent:
77:5c6bd659c32d
Child:
81:4c1641e10dcd
Fixed a bug where the snake wouldn't move if its length is 1. Also sorted out Collision functions so that they perform tasks independently.

Who changed what in which revision?

UserRevisionLine numberNew 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 68:b9cfd27987ac 21 void init(int length, int speed);
AhmedPlaymaker 69:55e309da7efd 22
AhmedPlaymaker 7:48ba87cd79b5 23 /** Draw
AhmedPlaymaker 7:48ba87cd79b5 24 *
AhmedPlaymaker 9:d1d79d4ee673 25 * This function draws the Snake sprite onto the screen at the specified coordinates.
AhmedPlaymaker 7:48ba87cd79b5 26 */
AhmedPlaymaker 77:5c6bd659c32d 27 void draw(N5110 &lcd, int length);
AhmedPlaymaker 69:55e309da7efd 28
AhmedPlaymaker 68:b9cfd27987ac 29 /** Set Position
AhmedPlaymaker 68:b9cfd27987ac 30 *
AhmedPlaymaker 68:b9cfd27987ac 31 * This function sets a position for where the snake should spawn.
AhmedPlaymaker 68:b9cfd27987ac 32 */
AhmedPlaymaker 68:b9cfd27987ac 33 void set_pos(Vector2D p);
AhmedPlaymaker 69:55e309da7efd 34
AhmedPlaymaker 7:48ba87cd79b5 35 /** Update
AhmedPlaymaker 7:48ba87cd79b5 36 *
AhmedPlaymaker 9:d1d79d4ee673 37 * This function updates the Snake sprite position on screen.
AhmedPlaymaker 7:48ba87cd79b5 38 */
AhmedPlaymaker 41:4edac50f010d 39 void update(Direction d, int* b);
AhmedPlaymaker 71:4bd2b27693f3 40
AhmedPlaymaker 71:4bd2b27693f3 41 /** Chain Snake Together
AhmedPlaymaker 71:4bd2b27693f3 42 *
AhmedPlaymaker 71:4bd2b27693f3 43 * 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 44 */
AhmedPlaymaker 71:4bd2b27693f3 45 void chainSnakeTogether(int* b);
AhmedPlaymaker 71:4bd2b27693f3 46
AhmedPlaymaker 71:4bd2b27693f3 47 /** Moove Snake
AhmedPlaymaker 71:4bd2b27693f3 48 *
AhmedPlaymaker 71:4bd2b27693f3 49 * This function makes the controls of W/E directions only exclusive to the top beed in the snake
AhmedPlaymaker 71:4bd2b27693f3 50 */
AhmedPlaymaker 71:4bd2b27693f3 51 void mooveSnake(Direction d, int* b);
AhmedPlaymaker 71:4bd2b27693f3 52
AhmedPlaymaker 71:4bd2b27693f3 53 /** Set Snake Limits
AhmedPlaymaker 71:4bd2b27693f3 54 *
AhmedPlaymaker 71:4bd2b27693f3 55 * 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 56 */
AhmedPlaymaker 71:4bd2b27693f3 57 void _setSnakeLimits();
AhmedPlaymaker 69:55e309da7efd 58
AhmedPlaymaker 7:48ba87cd79b5 59 /** Get Position
AhmedPlaymaker 7:48ba87cd79b5 60 *
AhmedPlaymaker 9:d1d79d4ee673 61 * This function obtains the coordinate of the top-left pixel in the Snake sprite.
AhmedPlaymaker 7:48ba87cd79b5 62 */
AhmedPlaymaker 70:7caab8069b9b 63 Vector2D get_pos(int snakeIndex);
AhmedPlaymaker 70:7caab8069b9b 64
AhmedPlaymaker 13:9785f2404045 65 int m;
AhmedPlaymaker 71:4bd2b27693f3 66 int b[10]; //each element in this array represents the beed number in the snake.
AhmedPlaymaker 69:55e309da7efd 67
AhmedPlaymaker 69:55e309da7efd 68 private:
AhmedPlaymaker 25:e827f1a8fadc 69 int _speed; //this is the speed of the snake flowing in the x axis.
AhmedPlaymaker 79:35cb65c52d25 70 int _x[10]; //each element in this array represents the x position of each beed in the snake.
AhmedPlaymaker 79:35cb65c52d25 71 int _y[10]; //each element in this array represents the y position of each beed in the snake.
AhmedPlaymaker 37:ee47699915b8 72 int _length;
AhmedPlaymaker 7:48ba87cd79b5 73
AhmedPlaymaker 7:48ba87cd79b5 74 };
AhmedPlaymaker 7:48ba87cd79b5 75 #endif