Game finishes, but is buggy. Proper grid needed
This commit is contained in:
+39
-2
@@ -24,12 +24,29 @@ GameState::GameState(int maxHorizontal, int maxVertical)
|
||||
void GameState::StartGame()
|
||||
{
|
||||
gameWindow.create(gameVideoSettings, "SnakePlusPlus");
|
||||
Snake player(sf::Vector2f(kGridSize,kGridSize));
|
||||
SnakeFood playerFood(sf::Vector2f(kGridSize,kGridSize));
|
||||
RunGameLoop();
|
||||
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 boundaries;
|
||||
@@ -58,6 +75,24 @@ void GameState::GetKeyboardInput(void)
|
||||
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
|
||||
void GameState::RegenerateFood(void)
|
||||
{
|
||||
@@ -86,5 +121,7 @@ void GameState::RenderWindow(void)
|
||||
player.DisplaySnake(gameWindow);
|
||||
gameWindow.draw(playerFood.GetFoodObject());
|
||||
gameWindow.display();
|
||||
if (player.gameFinished)
|
||||
DisplayEndScreen();
|
||||
return;
|
||||
}
|
||||
|
||||
+33
-15
@@ -9,18 +9,8 @@
|
||||
// General constructor for snake class
|
||||
Snake::Snake(void)
|
||||
{
|
||||
sf::RectangleShape newBodyPart(sf::Vector2f(kGridSize, kGridSize));
|
||||
newBodyPart.setFillColor(sf::Color::Green);
|
||||
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);
|
||||
bodyPartSize = sf::Vector2f(kGridSize, kGridSize);
|
||||
CreateHead();
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -67,27 +57,48 @@ void Snake::MoveSnake(SnakeFood* snakeFood)
|
||||
{
|
||||
// TODO: Add losing on wall collision
|
||||
if (CheckBoundaries()) // Wall collision
|
||||
{
|
||||
gameFinished = true;
|
||||
return;
|
||||
}
|
||||
sf::Vector2f newHeadPosition;
|
||||
newHeadPosition = CalculateNewPosition(GetSnakeHeadPosition());
|
||||
sf::RectangleShape newBodyPart(sf::Vector2f(kGridSize, kGridSize));
|
||||
newBodyPart.setPosition(newHeadPosition);
|
||||
// TODO: Add losing on self collision
|
||||
if (IsSelfCollision(newBodyPart)) // Snake collision
|
||||
if (IsSelfCollision(newBodyPart) && (snakeBody.size() > 1)) // Snake collision
|
||||
{
|
||||
gameFinished = true;
|
||||
return;
|
||||
newBodyPart.setFillColor(sf::Color::Green);
|
||||
snakeBody.push_front(newBodyPart);
|
||||
}
|
||||
if (IsSelfCollision(newBodyPart))
|
||||
return;
|
||||
AddBodyPart(newBodyPart);
|
||||
if (!GlobalCollision(GetSnakeHeadPosition(), snakeFood->GetFoodObjectPosition()))
|
||||
snakeBody.pop_back();
|
||||
return;
|
||||
}
|
||||
|
||||
void Snake::Reset(void)
|
||||
{
|
||||
snakeBody.clear();
|
||||
gameFinished = false;
|
||||
CreateHead();
|
||||
snakeDirection = kRight;
|
||||
}
|
||||
|
||||
void Snake::UpdateDirection(int newDirection)
|
||||
{
|
||||
snakeDirection = newDirection;
|
||||
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
|
||||
sf::Vector2f Snake::CalculateNewPosition(sf::Vector2f position)
|
||||
{
|
||||
@@ -119,6 +130,13 @@ bool Snake::CheckBoundaries(void)
|
||||
return false;
|
||||
}
|
||||
|
||||
void Snake::CreateHead(void)
|
||||
{
|
||||
sf::RectangleShape newBodyPart(bodyPartSize);
|
||||
newBodyPart.setFillColor(sf::Color::Green);
|
||||
snakeBody.push_front(newBodyPart);
|
||||
}
|
||||
|
||||
// Test for snake self collision
|
||||
bool Snake::IsSelfCollision(sf::RectangleShape testRectangle)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user