Returns true if fruitsNumber is greater or equal than Data::maxFruitNumber
void CheckCollisions()
Call all the checking methods below
bool checkWallCollision() const
Returns true whenever snake hits a wall(window border)
bool checkSnakeCollision() const
Returns true if snake eats itself
void checkItemCollision() const
Checks if snakesnake's eaten a fruit
Slots
Slot signature
Description
void spawnFruit()
Spawns a fruit if checkFruitsNumber() return false
void moveSnake()
If elapsed time is less than gameTime, snake is moved
Snake class
Snake is a moving set of points. Its shape is a set of circles painted on the points coordinates. The biggest circle is the head. The snake grows when a fruit is eaten. Is controlled with arrow keys, can move in any direction except diagonal. Inherits from QGraphicsItem.
Fields
Variable
Description
QPointF head
Point containing coordinates of snake's head
QVector<QPointF> tail
Vector containing the coordinates of the tail points
QGraphicsScene* scene
Pointer to Board's scene. Used to remove eaten fruits from scene
Data::Direction direction
Enum containing info about the direction snake's heading to. Initial direction is set to Data::Direction::Right
int velocity
Current snake's speed
int xDirection
How far can snake go in left or right, set by velocity. Initialized with Data::velocity
int yDirection
How far can snake go in up or down, set by velocity. Initialized with 0
int snakeLength
Snake's length
int toGrow
How many QPointF coordinates to add to tail
Methods
Method signature
Description
Snake(QGraphicsScene* gameScene, int snakeLength)
Set head coordinates to the middle of the gameScene
QPainterPath shape() const
Sets snake's shape to a set of circles based on head and tail points
QRectF boundingRect() const
Sets a rectangular shape to the body.
void paint()
Paints the shape
void move()
Updates head and tail positions
bool wallHit()
Returns true if head coordinates met a frame of scene
void goThroughWall()
Checks which wall was hit by head and makes it appear on the opposite side of the scene
void eatFruit()
Removes the eaten fruit, increases Controller::score, makes the snake grow
void checkCollision()
Eats a fruit if any was hit by head
void moveLeft()
Sets xDirection to -velocity and yDirection to 0. Thus direciton is set to Data::Direction::Left
void moveRight()
Sets xDirection to velocity and yDirection to 0. Thus direciton is set to Data::Direction::Right
void moveUp()
Sets xDirection to 0 and yDirection to -velocity. Thus direciton is set to Data::Direction::Up
void moveDown()
Sets xDirection to 0 and yDirection to velocity. Thus direciton is set to Data::Direction::Down
bool intersects() const
When tail contains head it means that snake's eaten itself. Returns true so
void updateHead()
Sets head.x() to head.x() + xDirection and head.y() to head.y() + yDirection
void updateTail()
Adds a new QPointF to tail.
Board class
Board is responsible for displaying snake, its movement, and furit. Inherits from QGraphicsView.
Fields
Variable
Description
QGraphicsScene* scene
Surface to manage 2D objects
Controller* controller
Initialized at the same time as the Board
Methods
Method signature
Description
Board(int, int)
Initializes controller and scene, sets the background
Fruit class
A 2D object inheriting from QGraphicsItem. When eaten, enlengthens the snake.