Added snake collision with itself
Added snake collision with itself and also changed color to green.
This commit is contained in:
parent
69423bf9d0
commit
e812588200
@ -37,15 +37,15 @@ void Snake::CheckDirection()
|
|||||||
bool Snake::CheckBoundaries()
|
bool Snake::CheckBoundaries()
|
||||||
{
|
{
|
||||||
if (snakeBody.front().getPosition().x == 0 && snakeDirection == 1)
|
if (snakeBody.front().getPosition().x == 0 && snakeDirection == 1)
|
||||||
return 1;
|
return true;
|
||||||
if (snakeBody.front().getPosition().y == 0 && snakeDirection == 2)
|
if (snakeBody.front().getPosition().y == 0 && snakeDirection == 2)
|
||||||
return 1;
|
return true;
|
||||||
// TODO: Change boundaries to not be hard-coded
|
// TODO: Change boundaries to not be hard-coded
|
||||||
if (snakeBody.front().getPosition().y > 675 && snakeDirection == 3)
|
if (snakeBody.front().getPosition().y > 675 && snakeDirection == 3)
|
||||||
return 1;
|
return true;
|
||||||
if (snakeBody.front().getPosition().x > 975 && snakeDirection == 4)
|
if (snakeBody.front().getPosition().x > 975 && snakeDirection == 4)
|
||||||
return 1;
|
return true;
|
||||||
return 0;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get a new coordinate position based on snake direction
|
// Get a new coordinate position based on snake direction
|
||||||
@ -76,6 +76,12 @@ void Snake::MoveSnake(sf::RectangleShape& snakeFood)
|
|||||||
newHeadPosition = CalculateNewPosition(snakeDirection, newHeadPosition);
|
newHeadPosition = CalculateNewPosition(snakeDirection, newHeadPosition);
|
||||||
sf::RectangleShape newBodyPart(sf::Vector2f(25,25));
|
sf::RectangleShape newBodyPart(sf::Vector2f(25,25));
|
||||||
newBodyPart.setPosition(newHeadPosition);
|
newBodyPart.setPosition(newHeadPosition);
|
||||||
|
if (IsSelfCollision(newBodyPart))
|
||||||
|
{
|
||||||
|
// Do nothing if self collision
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
newBodyPart.setFillColor(sf::Color::Green);
|
||||||
snakeBody.push_front(newBodyPart);
|
snakeBody.push_front(newBodyPart);
|
||||||
if (!SnakeCollision(GetSnakeHead(), snakeFood))
|
if (!SnakeCollision(GetSnakeHead(), snakeFood))
|
||||||
snakeBody.pop_back();
|
snakeBody.pop_back();
|
||||||
@ -115,6 +121,19 @@ void Snake::DisplaySnake(sf::RenderWindow& window)
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Test for snake self collision
|
||||||
|
bool Snake::IsSelfCollision(sf::RectangleShape testRectangle)
|
||||||
|
{
|
||||||
|
for (auto it = snakeBody.cbegin(); it != snakeBody.cend(); ++it)
|
||||||
|
{
|
||||||
|
if (SnakeCollision(testRectangle, *it))
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
// General constructor for snake class
|
// General constructor for snake class
|
||||||
Snake::Snake()
|
Snake::Snake()
|
||||||
{
|
{
|
||||||
|
@ -21,6 +21,7 @@ public:
|
|||||||
void MoveSnake(sf::RectangleShape& snakeFood);
|
void MoveSnake(sf::RectangleShape& snakeFood);
|
||||||
void CheckDirection();
|
void CheckDirection();
|
||||||
bool CheckBoundaries();
|
bool CheckBoundaries();
|
||||||
|
bool IsSelfCollision(sf::RectangleShape testRectangle);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user