2022-08-15 23:51:18 -05:00
|
|
|
// GameState.cpp
|
2022-07-28 15:18:24 -05:00
|
|
|
#include <SFML\Graphics.hpp>
|
|
|
|
#include <SFML\System.hpp>
|
2023-03-12 21:57:46 -05:00
|
|
|
#include "Common.h"
|
2022-08-02 21:17:23 -05:00
|
|
|
#include "Snake.h"
|
2022-07-28 15:18:24 -05:00
|
|
|
#include "GameState.h"
|
|
|
|
|
|
|
|
GameState::GameState()
|
|
|
|
{
|
2023-03-12 21:57:46 -05:00
|
|
|
delay = sf::milliseconds(75);
|
|
|
|
gameVideoSettings = sf::VideoMode(1025, 725);
|
2023-03-12 08:50:50 -05:00
|
|
|
gameWindow.create(gameVideoSettings, "SnakePlusPlus");
|
2022-07-28 15:18:24 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-03-12 08:50:50 -05:00
|
|
|
GameState::GameState(int maxHorizontal, int maxVertical)
|
2022-07-28 15:18:24 -05:00
|
|
|
{
|
2023-03-12 21:57:46 -05:00
|
|
|
delay = sf::milliseconds(75);
|
2023-03-12 08:50:50 -05:00
|
|
|
gameVideoSettings = sf::VideoMode(maxHorizontal, maxVertical);
|
|
|
|
gameWindow.create(gameVideoSettings, "SnakePlusPlus");
|
2022-07-28 15:18:24 -05:00
|
|
|
return;
|
|
|
|
}
|
2022-08-02 21:17:23 -05:00
|
|
|
|
2023-03-12 08:50:50 -05:00
|
|
|
void GameState::StartGame()
|
2022-08-02 21:17:23 -05:00
|
|
|
{
|
2023-03-12 08:50:50 -05:00
|
|
|
gameWindow.create(gameVideoSettings, "SnakePlusPlus");
|
|
|
|
Snake player(sf::Vector2f(kGridSize,kGridSize));
|
|
|
|
SnakeFood playerFood(sf::Vector2f(kGridSize,kGridSize));
|
|
|
|
RunGameLoop();
|
2023-03-12 21:57:46 -05:00
|
|
|
return;
|
2023-03-12 08:50:50 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
sf::Vector2f GameState::GetGameBoundaries(void)
|
|
|
|
{
|
|
|
|
sf::Vector2f boundaries;
|
|
|
|
boundaries.x = gameVideoSettings.width;
|
|
|
|
boundaries.y = gameVideoSettings.height;
|
|
|
|
return boundaries;
|
|
|
|
}
|
|
|
|
|
2023-03-12 21:57:46 -05:00
|
|
|
void GameState::GetKeyboardInput(void)
|
|
|
|
{
|
|
|
|
sf::Event event;
|
|
|
|
while (gameWindow.pollEvent(event))
|
|
|
|
{
|
|
|
|
if ((event.type == sf::Event::Closed) ||
|
|
|
|
(sf::Keyboard::isKeyPressed(sf::Keyboard::Escape)))
|
|
|
|
gameWindow.close();
|
|
|
|
}
|
|
|
|
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
|
|
|
|
player.UpdateDirection(kLeft);
|
|
|
|
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
|
|
|
|
player.UpdateDirection(kUp);
|
|
|
|
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
|
|
|
|
player.UpdateDirection(kDown);
|
|
|
|
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
|
|
|
|
player.UpdateDirection(kRight);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-03-12 08:50:50 -05:00
|
|
|
// Generates new food until not colliding with player
|
|
|
|
void GameState::RegenerateFood(void)
|
|
|
|
{
|
|
|
|
// Keep making new food until generating a valid spot
|
|
|
|
while (player.IsTouchingObject(playerFood.GetFoodObject()))
|
|
|
|
playerFood.GenerateNewFood(GetGameBoundaries());
|
|
|
|
return;
|
|
|
|
}
|
2022-08-02 21:17:23 -05:00
|
|
|
|
2023-03-12 08:50:50 -05:00
|
|
|
void GameState::RunGameLoop(void)
|
|
|
|
{
|
2022-08-02 21:17:23 -05:00
|
|
|
while (gameWindow.isOpen())
|
|
|
|
{
|
2023-03-12 21:57:46 -05:00
|
|
|
GetKeyboardInput();
|
|
|
|
player.MoveSnake(&playerFood);
|
2023-03-12 08:50:50 -05:00
|
|
|
RegenerateFood();
|
|
|
|
RenderWindow();
|
2022-08-02 21:17:23 -05:00
|
|
|
sf::sleep(delay);
|
|
|
|
}
|
2023-03-12 21:57:46 -05:00
|
|
|
return;
|
2022-08-02 21:17:23 -05:00
|
|
|
}
|
2023-03-12 08:50:50 -05:00
|
|
|
|
|
|
|
void GameState::RenderWindow(void)
|
|
|
|
{
|
|
|
|
gameWindow.clear();
|
|
|
|
player.DisplaySnake(gameWindow);
|
|
|
|
gameWindow.draw(playerFood.GetFoodObject());
|
|
|
|
gameWindow.display();
|
2023-03-12 21:57:46 -05:00
|
|
|
return;
|
2023-03-12 08:50:50 -05:00
|
|
|
}
|