Added boundaries for snake movement
Snake is now limited to a hard-coded boundary. Boundary is planned to be changed to no longer be hard-coded.
This commit is contained in:
parent
97085dd314
commit
9f90e6fb2f
@ -20,10 +20,8 @@ bool SnakeCollision(sf::RectangleShape object1, sf::RectangleShape object2)
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Move snake head piece
|
// Move snake head piece
|
||||||
int SnakeMovement()
|
void Snake::CheckDirection()
|
||||||
{
|
{
|
||||||
std::cout << "Test text \n";
|
|
||||||
int snakeDirection = 0;
|
|
||||||
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
|
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
|
||||||
snakeDirection = 1;
|
snakeDirection = 1;
|
||||||
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
|
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
|
||||||
@ -32,7 +30,22 @@ int SnakeMovement()
|
|||||||
snakeDirection = 3;
|
snakeDirection = 3;
|
||||||
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
|
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
|
||||||
snakeDirection = 4;
|
snakeDirection = 4;
|
||||||
return snakeDirection;
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check snake head for running into boundaries
|
||||||
|
bool Snake::CheckBoundaries()
|
||||||
|
{
|
||||||
|
if (snakeBody.front().getPosition().x == 0 && snakeDirection == 1)
|
||||||
|
return 1;
|
||||||
|
if (snakeBody.front().getPosition().y == 0 && snakeDirection == 2)
|
||||||
|
return 1;
|
||||||
|
// TODO: Change boundaries to not be hard-coded
|
||||||
|
if (snakeBody.front().getPosition().y > 675 && snakeDirection == 3)
|
||||||
|
return 1;
|
||||||
|
if (snakeBody.front().getPosition().x > 975 && snakeDirection == 4)
|
||||||
|
return 1;
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
sf::Vector2f CalculateNewPosition(int direction, sf::Vector2f position)
|
sf::Vector2f CalculateNewPosition(int direction, sf::Vector2f position)
|
||||||
@ -56,12 +69,14 @@ void Snake::ExtendSnake()
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Snake::MoveSnake(int snakeDirection)
|
void Snake::MoveSnake()
|
||||||
{
|
{
|
||||||
// Create a new deque RectangleShape and pop old
|
// Create a new deque RectangleShape and pop old
|
||||||
// Todo: Depreciate ExtendSnake and just add a collision test
|
// Todo: Depreciate ExtendSnake and just add a collision test
|
||||||
|
CheckDirection();
|
||||||
sf::Vector2f newHeadPosition;
|
sf::Vector2f newHeadPosition;
|
||||||
newHeadPosition = GetSnakeHeadPosition();
|
newHeadPosition = GetSnakeHeadPosition();
|
||||||
|
if (!CheckBoundaries())
|
||||||
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);
|
||||||
|
@ -22,6 +22,7 @@ class Snake
|
|||||||
{
|
{
|
||||||
private:
|
private:
|
||||||
std::deque<sf::RectangleShape> snakeBody;
|
std::deque<sf::RectangleShape> snakeBody;
|
||||||
|
int snakeDirection = 0;
|
||||||
public:
|
public:
|
||||||
Snake();
|
Snake();
|
||||||
Snake(sf::Vector2f head);
|
Snake(sf::Vector2f head);
|
||||||
@ -31,7 +32,9 @@ public:
|
|||||||
sf::RectangleShape GetSnakeHead();
|
sf::RectangleShape GetSnakeHead();
|
||||||
void DisplaySnake(sf::RenderWindow& window);
|
void DisplaySnake(sf::RenderWindow& window);
|
||||||
void ExtendSnake();
|
void ExtendSnake();
|
||||||
void MoveSnake(int snakeDirection); // Move only head body piece
|
void MoveSnake(); // Move only head body piece
|
||||||
|
void CheckDirection();
|
||||||
|
bool CheckBoundaries();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
@ -27,7 +27,7 @@ int main()
|
|||||||
gameGridVertical = (videoSizeVertical // 25) * 25;
|
gameGridVertical = (videoSizeVertical // 25) * 25;
|
||||||
*/
|
*/
|
||||||
sf::RenderWindow window(sf::VideoMode(videoSizeHorizontal, videoSizeVertical), "SnakePlusPlus");
|
sf::RenderWindow window(sf::VideoMode(videoSizeHorizontal, videoSizeVertical), "SnakePlusPlus");
|
||||||
sf::Time delay = sf::milliseconds(5);
|
sf::Time delay = sf::milliseconds(100);
|
||||||
|
|
||||||
int snakeDirection = 0;
|
int snakeDirection = 0;
|
||||||
Snake Player(sf::Vector2f(25,25));
|
Snake Player(sf::Vector2f(25,25));
|
||||||
@ -59,7 +59,7 @@ int main()
|
|||||||
snakeDirection = 3;
|
snakeDirection = 3;
|
||||||
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
|
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
|
||||||
snakeDirection = 4;
|
snakeDirection = 4;
|
||||||
Player.MoveSnake(snakeDirection);
|
Player.MoveSnake();
|
||||||
// if (SnakeCollision(snakeHead, snakeFood))
|
// if (SnakeCollision(snakeHead, snakeFood))
|
||||||
// {
|
// {
|
||||||
// snakeFoodPosition.x += 25;
|
// snakeFoodPosition.x += 25;
|
||||||
@ -69,6 +69,6 @@ int main()
|
|||||||
window.draw(snakeFood);
|
window.draw(snakeFood);
|
||||||
Player.DisplaySnake(window);
|
Player.DisplaySnake(window);
|
||||||
window.display();
|
window.display();
|
||||||
sf::sleep(delay);
|
// sf::sleep(delay);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user