Added ability to play again

This commit is contained in:
TriantaTV 2023-08-10 18:47:40 -05:00
parent b7b42fa038
commit c71b1d6982
4 changed files with 9 additions and 2 deletions

View File

@ -31,6 +31,8 @@ namespace snakeplusplus
if (isGameOver) if (isGameOver)
{ {
graphics.CheckContinue(); graphics.CheckContinue();
player.Reset();
PrepareGameBoard();
isGameOver = 0; isGameOver = 0;
} }
UpdatePlayerSpeed(); UpdatePlayerSpeed();
@ -69,7 +71,6 @@ namespace snakeplusplus
player.Pop(); player.Pop();
} catch (const std::out_of_range& error) { } catch (const std::out_of_range& error) {
isGameOver = true; // Snake ran into edge isGameOver = true; // Snake ran into edge
exit(0);
} }
} }

View File

@ -66,7 +66,7 @@ namespace snakeplusplus
textPosition.y = textPosition.y / 2; textPosition.y = textPosition.y / 2;
sf::Font font; sf::Font font;
font.loadFromFile("Arial.ttf"); font.loadFromFile("Arial.ttf");
sf::Text gameOverText("Game Over", font); sf::Text gameOverText("Game Over\nPress 'Enter' to play again", font);
gameOverText.setPosition(textPosition); gameOverText.setPosition(textPosition);
gameWindow.draw(gameOverText); gameWindow.draw(gameOverText);
gameWindow.display(); gameWindow.display();

View File

@ -14,6 +14,11 @@ namespace snakeplusplus
body.pop(); body.pop();
} }
void Snake::Reset(void)
{
while (!body.empty()) Pop();
}
Food::Food(void) Food::Food(void)
{ {
generator.seed(std::random_device{}()); generator.seed(std::random_device{}());

View File

@ -17,6 +17,7 @@ namespace snakeplusplus
sf::Vector2f speed; sf::Vector2f speed;
std::queue<char*> body; std::queue<char*> body;
void Pop(void); void Pop(void);
void Reset(void);
}; };
struct Food struct Food