Merge pull request #5 from TriantaTV/Development

Added eating food being able to extend snake
This commit is contained in:
TriantaTV 2022-07-27 15:09:24 -05:00 committed by GitHub
commit 7589e17d6b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 18 additions and 23 deletions

View File

@ -48,6 +48,7 @@ bool Snake::CheckBoundaries()
return 0;
}
// Get a new coordinate position based on snake direction
sf::Vector2f CalculateNewPosition(int direction, sf::Vector2f position)
{
if (direction == 0)
@ -69,7 +70,7 @@ void Snake::ExtendSnake()
return;
}
void Snake::MoveSnake()
void Snake::MoveSnake(sf::RectangleShape& snakeFood)
{
// Create a new deque RectangleShape and pop old
// Todo: Depreciate ExtendSnake and just add a collision test
@ -80,8 +81,16 @@ void Snake::MoveSnake()
newHeadPosition = CalculateNewPosition(snakeDirection, newHeadPosition);
sf::RectangleShape newBodyPart(sf::Vector2f(25,25));
newBodyPart.setPosition(newHeadPosition);
snakeBody.push_back(newBodyPart);
snakeBody.pop_front();
snakeBody.push_front(newBodyPart);
if (!SnakeCollision(GetSnakeHead(), snakeFood))
snakeBody.pop_back();
else if (SnakeCollision(GetSnakeHead(), snakeFood))
{
sf::Vector2f snakeFoodPosition = snakeFood.getPosition();
snakeFoodPosition.x += 25;
snakeFoodPosition.y += 25;
snakeFood.setPosition(snakeFoodPosition.x, snakeFoodPosition.y);
}
return;
}

View File

@ -26,13 +26,11 @@ private:
public:
Snake();
Snake(sf::Vector2f head);
// Instead of popping like in MoveSnake()
// Simply add to deque
sf::Vector2f GetSnakeHeadPosition();
sf::RectangleShape GetSnakeHead();
void DisplaySnake(sf::RenderWindow& window);
void ExtendSnake();
void MoveSnake(); // Move only head body piece
void MoveSnake(sf::RectangleShape& snakeFood);
void CheckDirection();
bool CheckBoundaries();
};

View File

@ -6,10 +6,8 @@
/*
TODO: Work on snake body
TODO:
Add ability for body to extend when eating food
Turn snake body into queue
Each piece of queue has coordinates
If head touches food, just add to queue, don't pop
@ -27,13 +25,12 @@ int main()
gameGridVertical = (videoSizeVertical // 25) * 25;
*/
sf::RenderWindow window(sf::VideoMode(videoSizeHorizontal, videoSizeVertical), "SnakePlusPlus");
sf::Time delay = sf::milliseconds(100);
sf::Time delay = sf::milliseconds(50);
int snakeDirection = 0;
Snake Player(sf::Vector2f(25,25));
sf::RectangleShape snakeHead(sf::Vector2f(25,25));
sf::RectangleShape snakeFood(sf::Vector2f(25,25));
// snakeHead.setFillColor(sf::Color::Green);
snakeFood.setFillColor(sf::Color::Red);
snakeFood.setPosition(25,25);
@ -46,11 +43,7 @@ int main()
if ((event.type == sf::Event::Closed) || (sf::Keyboard::isKeyPressed(sf::Keyboard::Escape)))
window.close();
}
// sf::Vector2f snakeHeadPosition = Player.GetSnakeHeadPosition();
sf::Vector2f snakeFoodPosition = snakeFood.getPosition();
// TODO: Split Movement into separate function
// Add boundaries
// Add movement until boundaries
// sf::Vector2f snakeFoodPosition = snakeFood.getPosition();
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
snakeDirection = 1;
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
@ -59,16 +52,11 @@ int main()
snakeDirection = 3;
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
snakeDirection = 4;
Player.MoveSnake();
// if (SnakeCollision(snakeHead, snakeFood))
// {
// snakeFoodPosition.x += 25;
// snakeFoodPosition.y += 25;
// snakeFood.setPosition(snakeFoodPosition.x, snakeFoodPosition.y);
Player.MoveSnake(snakeFood);
window.clear();
window.draw(snakeFood);
Player.DisplaySnake(window);
window.display();
// sf::sleep(delay);
sf::sleep(delay);
}
}