2023-04-06 19:27:10 -05:00
|
|
|
#include "playerinterface.hpp"
|
2023-04-15 05:15:11 -05:00
|
|
|
#include <SFML/System/Vector2.hpp>
|
2023-08-18 17:51:58 -05:00
|
|
|
#include <SFML/Window/Keyboard.hpp>
|
2023-04-06 19:27:10 -05:00
|
|
|
|
|
|
|
namespace snakeplusplus
|
|
|
|
{
|
2023-08-14 17:39:04 -05:00
|
|
|
PlayerDirection GetPlayerInput(void)
|
2023-04-06 19:27:10 -05:00
|
|
|
{
|
2023-08-18 17:51:58 -05:00
|
|
|
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left)
|
|
|
|
|| sf::Keyboard::isKeyPressed(sf::Keyboard::A))
|
2023-08-14 17:39:04 -05:00
|
|
|
return kLeft;
|
2023-08-18 17:51:58 -05:00
|
|
|
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up)
|
|
|
|
|| sf::Keyboard::isKeyPressed(sf::Keyboard::W))
|
2023-08-14 17:39:04 -05:00
|
|
|
return kUp;
|
2023-08-18 17:51:58 -05:00
|
|
|
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down)
|
|
|
|
|| sf::Keyboard::isKeyPressed(sf::Keyboard::S))
|
2023-08-14 17:39:04 -05:00
|
|
|
return kDown;
|
2023-08-18 17:51:58 -05:00
|
|
|
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right)
|
|
|
|
|| sf::Keyboard::isKeyPressed(sf::Keyboard::D))
|
2023-08-14 17:39:04 -05:00
|
|
|
return kRight;
|
|
|
|
return kNone;
|
2023-04-06 19:27:10 -05:00
|
|
|
}
|
|
|
|
|
2023-04-06 22:22:06 -05:00
|
|
|
bool PlayerOutput::IsOpen(void)
|
2023-04-06 19:27:10 -05:00
|
|
|
{
|
2023-04-15 05:15:11 -05:00
|
|
|
return gameWindow.isOpen();
|
2023-04-06 19:27:10 -05:00
|
|
|
}
|
|
|
|
|
2023-04-06 22:22:06 -05:00
|
|
|
PlayerOutput::PlayerOutput(void)
|
2023-04-06 19:27:10 -05:00
|
|
|
{
|
2023-04-15 05:15:11 -05:00
|
|
|
float kWidth = 1025;
|
|
|
|
float kHeight = 725;
|
|
|
|
float kBoardWidth = kWidth / kGridSize;
|
|
|
|
float kBoardHeight = kHeight / kGridSize;
|
|
|
|
gameBoundaries = sf::Vector2f(kBoardWidth, kBoardHeight);
|
|
|
|
gameVideoSettings = sf::VideoMode(kWidth, kHeight);
|
2023-04-06 19:27:10 -05:00
|
|
|
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;
|
2023-05-17 20:20:01 -05:00
|
|
|
DisplayEndScreen();
|
2023-04-06 19:27:10 -05:00
|
|
|
while (true)
|
|
|
|
{
|
2023-05-17 20:20:01 -05:00
|
|
|
gameWindow.pollEvent(event);
|
|
|
|
if ((event.type == sf::Event::Closed) ||
|
|
|
|
(sf::Keyboard::isKeyPressed(sf::Keyboard::Escape)))
|
2023-04-06 19:27:10 -05:00
|
|
|
{
|
2023-05-17 20:20:01 -05:00
|
|
|
gameWindow.close();
|
2023-04-06 19:27:10 -05:00
|
|
|
return;
|
|
|
|
}
|
2023-05-17 20:20:01 -05:00
|
|
|
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Enter)) { return; }
|
2023-04-06 19:27:10 -05:00
|
|
|
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;
|
2023-05-17 20:20:01 -05:00
|
|
|
sf::Font font;
|
|
|
|
font.loadFromFile("Arial.ttf");
|
2023-08-10 18:47:40 -05:00
|
|
|
sf::Text gameOverText("Game Over\nPress 'Enter' to play again", font);
|
2023-04-06 19:27:10 -05:00
|
|
|
gameOverText.setPosition(textPosition);
|
|
|
|
gameWindow.draw(gameOverText);
|
|
|
|
gameWindow.display();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-04-15 05:15:11 -05:00
|
|
|
void PlayerOutput::DisplayGameState(std::vector< std::vector<char> >& gameBoard)
|
2023-04-06 19:27:10 -05:00
|
|
|
{
|
|
|
|
CheckWindowEvents();
|
2023-04-15 05:15:11 -05:00
|
|
|
sf::Vector2f location;
|
|
|
|
char* letterOnBoard;
|
|
|
|
for (float y = 0; y < gameBoundaries.y; y++)
|
2023-04-06 19:27:10 -05:00
|
|
|
{
|
2023-04-15 05:15:11 -05:00
|
|
|
for (float x = 0; x < gameBoundaries.x; x++)
|
2023-04-06 19:27:10 -05:00
|
|
|
{
|
2023-04-15 05:15:11 -05:00
|
|
|
location.x = x;
|
|
|
|
location.y = y;
|
|
|
|
letterOnBoard = &gameBoard.at(location.y).at(location.x);
|
|
|
|
switch (*letterOnBoard)
|
|
|
|
{
|
|
|
|
case 'O':
|
|
|
|
DrawSnake(location);
|
|
|
|
break;
|
|
|
|
case 'X':
|
|
|
|
DrawFood(location);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
DrawEmpty(location);
|
|
|
|
break;
|
|
|
|
}
|
2023-04-06 19:27:10 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|