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; return 0;
} }
// Get a new coordinate position based on snake direction
sf::Vector2f CalculateNewPosition(int direction, sf::Vector2f position) sf::Vector2f CalculateNewPosition(int direction, sf::Vector2f position)
{ {
if (direction == 0) if (direction == 0)
@ -69,7 +70,7 @@ void Snake::ExtendSnake()
return; return;
} }
void Snake::MoveSnake() void Snake::MoveSnake(sf::RectangleShape& snakeFood)
{ {
// 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
@ -80,8 +81,16 @@ void Snake::MoveSnake()
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);
snakeBody.push_back(newBodyPart); snakeBody.push_front(newBodyPart);
snakeBody.pop_front(); 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; return;
} }

View File

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

View File

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