snakeplusplus/src/GameState.cpp

74 lines
1.9 KiB
C++
Raw Normal View History

// GameState.cpp
#include <SFML\Graphics.hpp>
#include <SFML\System.hpp>
#include "Snake.h"
#include "GameState.h"
GameState::GameState()
{
2023-03-12 08:50:50 -05:00
delay = sf::milliseconds(100);
gameVideoSettings = sf::VideoMode(1024, 725);
gameWindow.create(gameVideoSettings, "SnakePlusPlus");
return;
}
2023-03-12 08:50:50 -05:00
GameState::GameState(int maxHorizontal, int maxVertical)
{
2023-03-12 08:50:50 -05:00
delay = sf::milliseconds(100);
gameVideoSettings = sf::VideoMode(maxHorizontal, maxVertical);
gameWindow.create(gameVideoSettings, "SnakePlusPlus");
return;
}
2023-03-12 08:50:50 -05:00
void GameState::StartGame()
{
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();
}
sf::Vector2f GameState::GetGameBoundaries(void)
{
sf::Vector2f boundaries;
boundaries.x = gameVideoSettings.width;
boundaries.y = gameVideoSettings.height;
return boundaries;
}
// 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;
}
2023-03-12 08:50:50 -05:00
void GameState::RunGameLoop(void)
{
sf::Event event;
while (gameWindow.isOpen())
{
while (gameWindow.pollEvent(event))
{
2023-03-12 08:50:50 -05:00
if ((event.type == sf::Event::Closed) ||
(sf::Keyboard::isKeyPressed(sf::Keyboard::Escape)))
gameWindow.close();
}
player.CheckDirection();
2023-03-12 08:50:50 -05:00
player.MoveSnake(playerFood, gameVideoSettings);
RegenerateFood();
RenderWindow();
sf::sleep(delay);
}
}
2023-03-12 08:50:50 -05:00
void GameState::RenderWindow(void)
{
gameWindow.clear();
player.DisplaySnake(gameWindow);
gameWindow.draw(playerFood.GetFoodObject());
gameWindow.display();
}