snakeplusplus/src/playerinterface.cpp

145 lines
4.1 KiB
C++
Raw Normal View History

#include "playerinterface.hpp"
2023-04-15 05:15:11 -05:00
#include <SFML/System/Vector2.hpp>
namespace snakeplusplus
{
2023-08-14 17:39:04 -05:00
PlayerDirection GetPlayerInput(void)
{
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
2023-08-14 17:39:04 -05:00
return kLeft;
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
2023-08-14 17:39:04 -05:00
return kUp;
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
2023-08-14 17:39:04 -05:00
return kDown;
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
2023-08-14 17:39:04 -05:00
return kRight;
return kNone;
}
bool PlayerOutput::IsOpen(void)
{
2023-04-15 05:15:11 -05:00
return gameWindow.isOpen();
}
PlayerOutput::PlayerOutput(void)
{
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);
drawObject.setSize(sf::Vector2f(kGridSize, kGridSize));
return;
}
void PlayerOutput::CheckContinue(void)
{
sf::Event event;
2023-05-17 20:20:01 -05:00
DisplayEndScreen();
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-05-17 20:20:01 -05:00
gameWindow.close();
return;
}
2023-05-17 20:20:01 -05:00
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Enter)) { return; }
sf::sleep(delay);
}
}
void PlayerOutput::DisplayEndScreen(void)
{
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);
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)
{
CheckWindowEvents();
2023-04-15 05:15:11 -05:00
sf::Vector2f location;
char* letterOnBoard;
for (float y = 0; y < gameBoundaries.y; y++)
{
2023-04-15 05:15:11 -05:00
for (float x = 0; x < gameBoundaries.x; x++)
{
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;
}
}
}
gameWindow.display();
sf::sleep(delay);
return;
}
void PlayerOutput::StartGameWindow(void)
{
gameWindow.create(gameVideoSettings, "SnakePlusPlus");
isWindowAlive = true;
return;
}
void PlayerOutput::CheckWindowEvents(void)
{
sf::Event event;
while (gameWindow.pollEvent(event))
{
if ((event.type == sf::Event::Closed) ||
(sf::Keyboard::isKeyPressed(sf::Keyboard::Escape)))
gameWindow.close();
}
}
void PlayerOutput::DrawEmpty(sf::Vector2f location)
{
location *= static_cast<float>(kGridSize);
drawObject.setPosition(location);
drawObject.setFillColor(sf::Color::Black);
gameWindow.draw(drawObject);
return;
}
void PlayerOutput::DrawFood(sf::Vector2f location)
{
location *= static_cast<float>(kGridSize);
drawObject.setPosition(location);
drawObject.setFillColor(sf::Color::Red);
gameWindow.draw(drawObject);
return;
}
void PlayerOutput::DrawSnake(sf::Vector2f location)
{
location *= static_cast<float>(kGridSize);
drawObject.setPosition(location);
drawObject.setFillColor(sf::Color::Green);
gameWindow.draw(drawObject);
return;
}
}