2023-04-06 19:27:10 -05:00
|
|
|
#include "playerinterface.hpp"
|
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
namespace snakeplusplus
|
|
|
|
{
|
2023-04-06 22:22:06 -05:00
|
|
|
PlayerInput::PlayerInput(void)
|
2023-04-06 19:27:10 -05:00
|
|
|
{
|
|
|
|
lastPlayerInput = kNone;
|
|
|
|
}
|
|
|
|
|
2023-04-06 22:22:06 -05:00
|
|
|
PlayerDirection PlayerInput::GetPlayerInput(void)
|
2023-04-06 19:27:10 -05:00
|
|
|
{
|
|
|
|
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
|
|
|
|
lastPlayerInput = kLeft;
|
|
|
|
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
|
|
|
|
lastPlayerInput = kUp;
|
|
|
|
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
|
|
|
|
lastPlayerInput = kDown;
|
|
|
|
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
|
|
|
|
lastPlayerInput = kRight;
|
|
|
|
return lastPlayerInput;
|
|
|
|
}
|
|
|
|
|
2023-04-06 22:22:06 -05:00
|
|
|
bool PlayerOutput::IsOpen(void)
|
2023-04-06 19:27:10 -05:00
|
|
|
{
|
|
|
|
return isWindowAlive;
|
|
|
|
}
|
|
|
|
|
2023-04-06 22:22:06 -05:00
|
|
|
PlayerOutput::PlayerOutput(void)
|
2023-04-06 19:27:10 -05:00
|
|
|
{
|
|
|
|
float kWidth = 1025 / kGridSize;
|
|
|
|
float kHeight = 725 / kGridSize;
|
|
|
|
gameBoundaries = sf::Vector2f(kWidth, kHeight);
|
|
|
|
gameVideoSettings = sf::VideoMode(1025, 725);
|
|
|
|
drawObject.setSize(sf::Vector2f(kGridSize, kGridSize));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-04-06 22:22:06 -05:00
|
|
|
void PlayerOutput::CheckContinue(void)
|
2023-04-06 19:27:10 -05:00
|
|
|
{
|
|
|
|
sf::Event event;
|
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
while (gameWindow.pollEvent(event))
|
|
|
|
{
|
|
|
|
if ((event.type == sf::Event::Closed) ||
|
|
|
|
(sf::Keyboard::isKeyPressed(sf::Keyboard::Escape)))
|
|
|
|
gameWindow.close();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Enter))
|
|
|
|
return;
|
|
|
|
sf::sleep(delay);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-06 22:22:06 -05:00
|
|
|
void PlayerOutput::DisplayEndScreen(void)
|
2023-04-06 19:27:10 -05:00
|
|
|
{
|
|
|
|
gameWindow.clear();
|
|
|
|
sf::Vector2f textPosition(gameBoundaries);
|
|
|
|
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 = Snake();
|
|
|
|
// playerFood.GenerateNewFood(GetGameBoundaries());
|
|
|
|
// gameWindow.clear();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-04-06 22:22:06 -05:00
|
|
|
void PlayerOutput::DisplayGameState(std::vector< std::vector<char> >* gameBoard)
|
2023-04-06 19:27:10 -05:00
|
|
|
{
|
|
|
|
CheckWindowEvents();
|
|
|
|
sf::Vector2i location(0,0);
|
|
|
|
char letterOnBoard;
|
|
|
|
for (; location.y < gameBoundaries.y; location.y++)
|
|
|
|
{
|
|
|
|
for (; location.x < gameBoundaries.x; location.x++)
|
|
|
|
{
|
|
|
|
letterOnBoard = gameBoard->at(location.y).at(location.y);
|
|
|
|
if (letterOnBoard == 'o')
|
|
|
|
DrawSnake(static_cast<sf::Vector2f>(location));
|
|
|
|
else if (letterOnBoard == 'x')
|
|
|
|
DrawFood(static_cast<sf::Vector2f>(location));
|
|
|
|
else
|
|
|
|
DrawEmpty(static_cast<sf::Vector2f>(location));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
gameWindow.display();
|
|
|
|
sf::sleep(delay);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-04-06 22:22:06 -05:00
|
|
|
void PlayerOutput::StartGameWindow(void)
|
2023-04-06 19:27:10 -05:00
|
|
|
{
|
|
|
|
gameWindow.create(gameVideoSettings, "SnakePlusPlus");
|
|
|
|
isWindowAlive = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-04-06 22:22:06 -05:00
|
|
|
void PlayerOutput::CheckWindowEvents(void)
|
2023-04-06 19:27:10 -05:00
|
|
|
{
|
|
|
|
sf::Event event;
|
|
|
|
while (gameWindow.pollEvent(event))
|
|
|
|
{
|
|
|
|
if ((event.type == sf::Event::Closed) ||
|
|
|
|
(sf::Keyboard::isKeyPressed(sf::Keyboard::Escape)))
|
|
|
|
gameWindow.close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-06 22:22:06 -05:00
|
|
|
void PlayerOutput::DrawEmpty(sf::Vector2f location)
|
2023-04-06 19:27:10 -05:00
|
|
|
{
|
|
|
|
location *= static_cast<float>(kGridSize);
|
|
|
|
drawObject.setPosition(location);
|
|
|
|
drawObject.setFillColor(sf::Color::Black);
|
|
|
|
gameWindow.draw(drawObject);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-04-06 22:22:06 -05:00
|
|
|
void PlayerOutput::DrawFood(sf::Vector2f location)
|
2023-04-06 19:27:10 -05:00
|
|
|
{
|
|
|
|
location *= static_cast<float>(kGridSize);
|
|
|
|
drawObject.setPosition(location);
|
|
|
|
drawObject.setFillColor(sf::Color::Red);
|
|
|
|
gameWindow.draw(drawObject);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-04-06 22:22:06 -05:00
|
|
|
void PlayerOutput::DrawSnake(sf::Vector2f location)
|
2023-04-06 19:27:10 -05:00
|
|
|
{
|
|
|
|
location *= static_cast<float>(kGridSize);
|
|
|
|
drawObject.setPosition(location);
|
|
|
|
drawObject.setFillColor(sf::Color::Green);
|
|
|
|
gameWindow.draw(drawObject);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|