Game finishes, but is buggy. Proper grid needed

This commit is contained in:
TriantaTV 2023-03-13 08:24:56 -05:00
parent 6589dabc09
commit 3ee01d936b
4 changed files with 79 additions and 18 deletions

View File

@ -20,7 +20,9 @@ private:
SnakeFood playerFood; SnakeFood playerFood;
Snake player; Snake player;
sf::Time delay; sf::Time delay;
void DisplayEndScreen(void);
void GetKeyboardInput(void); void GetKeyboardInput(void);
bool PlayerWantsToContinue(void);
void RegenerateFood(void); void RegenerateFood(void);
void RunGameLoop(void); void RunGameLoop(void);
void RenderWindow(void); void RenderWindow(void);

View File

@ -10,21 +10,25 @@
class Snake class Snake
{ {
public: public:
bool gameFinished = false;
Snake(void); Snake(void);
Snake(sf::Vector2f head);
void DisplaySnake(sf::RenderWindow& window); void DisplaySnake(sf::RenderWindow& window);
sf::RectangleShape GetSnakeHead(void); sf::RectangleShape GetSnakeHead(void);
sf::Vector2f GetSnakeHeadPosition(void); sf::Vector2f GetSnakeHeadPosition(void);
bool IsTouchingObject(sf::RectangleShape object); bool IsTouchingObject(sf::RectangleShape object);
void MoveSnake(SnakeFood* playerFood); void MoveSnake(SnakeFood* playerFood);
void Reset(void);
void UpdateDirection(int newDirection); void UpdateDirection(int newDirection);
protected: protected:
; ;
private: private:
std::deque<sf::RectangleShape> snakeBody; std::deque<sf::RectangleShape> snakeBody;
sf::Vector2f bodyPartSize;
int snakeDirection = 0; int snakeDirection = 0;
void AddBodyPart(sf::RectangleShape newBodyPart);
sf::Vector2f CalculateNewPosition(sf::Vector2f position); sf::Vector2f CalculateNewPosition(sf::Vector2f position);
bool CheckBoundaries(void); bool CheckBoundaries(void);
void CreateHead(void);
bool IsSelfCollision(sf::RectangleShape testRectangle); bool IsSelfCollision(sf::RectangleShape testRectangle);
}; };

View File

@ -24,12 +24,29 @@ GameState::GameState(int maxHorizontal, int maxVertical)
void GameState::StartGame() void GameState::StartGame()
{ {
gameWindow.create(gameVideoSettings, "SnakePlusPlus"); gameWindow.create(gameVideoSettings, "SnakePlusPlus");
Snake player(sf::Vector2f(kGridSize,kGridSize));
SnakeFood playerFood(sf::Vector2f(kGridSize,kGridSize));
RunGameLoop(); RunGameLoop();
return; return;
} }
void GameState::DisplayEndScreen(void)
{
gameWindow.clear();
sf::Vector2f textPosition(GetGameBoundaries());
textPosition.x = textPosition.x / 2;
textPosition.y = textPosition.y / 2;
sf::Text gameOverText;
gameOverText.setString("Game Over");
gameOverText.setCharacterSize(30);
gameOverText.setPosition(textPosition);
gameWindow.draw(gameOverText);
gameWindow.display();
if (!PlayerWantsToContinue())
return;
player.Reset();
gameWindow.clear();
return;
}
sf::Vector2f GameState::GetGameBoundaries(void) sf::Vector2f GameState::GetGameBoundaries(void)
{ {
sf::Vector2f boundaries; sf::Vector2f boundaries;
@ -58,6 +75,24 @@ void GameState::GetKeyboardInput(void)
return; return;
} }
bool GameState::PlayerWantsToContinue(void)
{
sf::Event event;
while (true)
{
while (gameWindow.pollEvent(event))
{
if ((event.type == sf::Event::Closed) ||
(sf::Keyboard::isKeyPressed(sf::Keyboard::Escape)))
gameWindow.close();
return false;
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Enter))
return true;
sf::sleep(delay);
}
}
// Generates new food until not colliding with player // Generates new food until not colliding with player
void GameState::RegenerateFood(void) void GameState::RegenerateFood(void)
{ {
@ -86,5 +121,7 @@ void GameState::RenderWindow(void)
player.DisplaySnake(gameWindow); player.DisplaySnake(gameWindow);
gameWindow.draw(playerFood.GetFoodObject()); gameWindow.draw(playerFood.GetFoodObject());
gameWindow.display(); gameWindow.display();
if (player.gameFinished)
DisplayEndScreen();
return; return;
} }

View File

@ -9,18 +9,8 @@
// General constructor for snake class // General constructor for snake class
Snake::Snake(void) Snake::Snake(void)
{ {
sf::RectangleShape newBodyPart(sf::Vector2f(kGridSize, kGridSize)); bodyPartSize = sf::Vector2f(kGridSize, kGridSize);
newBodyPart.setFillColor(sf::Color::Green); CreateHead();
snakeBody.push_back(newBodyPart);
return;
}
// Constructor for snake with position
Snake::Snake(sf::Vector2f headSize)
{
sf::RectangleShape newBodyPart(headSize);
newBodyPart.setFillColor(sf::Color::Green);
snakeBody.push_back(newBodyPart);
return; return;
} }
@ -67,27 +57,48 @@ void Snake::MoveSnake(SnakeFood* snakeFood)
{ {
// TODO: Add losing on wall collision // TODO: Add losing on wall collision
if (CheckBoundaries()) // Wall collision if (CheckBoundaries()) // Wall collision
{
gameFinished = true;
return; return;
}
sf::Vector2f newHeadPosition; sf::Vector2f newHeadPosition;
newHeadPosition = CalculateNewPosition(GetSnakeHeadPosition()); newHeadPosition = CalculateNewPosition(GetSnakeHeadPosition());
sf::RectangleShape newBodyPart(sf::Vector2f(kGridSize, kGridSize)); sf::RectangleShape newBodyPart(sf::Vector2f(kGridSize, kGridSize));
newBodyPart.setPosition(newHeadPosition); newBodyPart.setPosition(newHeadPosition);
// TODO: Add losing on self collision // TODO: Add losing on self collision
if (IsSelfCollision(newBodyPart)) // Snake collision if (IsSelfCollision(newBodyPart) && (snakeBody.size() > 1)) // Snake collision
{
gameFinished = true;
return; return;
newBodyPart.setFillColor(sf::Color::Green); }
snakeBody.push_front(newBodyPart); if (IsSelfCollision(newBodyPart))
return;
AddBodyPart(newBodyPart);
if (!GlobalCollision(GetSnakeHeadPosition(), snakeFood->GetFoodObjectPosition())) if (!GlobalCollision(GetSnakeHeadPosition(), snakeFood->GetFoodObjectPosition()))
snakeBody.pop_back(); snakeBody.pop_back();
return; return;
} }
void Snake::Reset(void)
{
snakeBody.clear();
gameFinished = false;
CreateHead();
snakeDirection = kRight;
}
void Snake::UpdateDirection(int newDirection) void Snake::UpdateDirection(int newDirection)
{ {
snakeDirection = newDirection; snakeDirection = newDirection;
return; return;
} }
void Snake::AddBodyPart(sf::RectangleShape newBodyPart)
{
newBodyPart.setFillColor(sf::Color::Green);
snakeBody.push_front(newBodyPart);
}
// Get a new coordinate position based on snake direction // Get a new coordinate position based on snake direction
sf::Vector2f Snake::CalculateNewPosition(sf::Vector2f position) sf::Vector2f Snake::CalculateNewPosition(sf::Vector2f position)
{ {
@ -119,6 +130,13 @@ bool Snake::CheckBoundaries(void)
return false; return false;
} }
void Snake::CreateHead(void)
{
sf::RectangleShape newBodyPart(bodyPartSize);
newBodyPart.setFillColor(sf::Color::Green);
snakeBody.push_front(newBodyPart);
}
// Test for snake self collision // Test for snake self collision
bool Snake::IsSelfCollision(sf::RectangleShape testRectangle) bool Snake::IsSelfCollision(sf::RectangleShape testRectangle)
{ {