snakeplusplus/src/playerinterface.cpp

147 lines
4.3 KiB
C++
Raw Normal View History

#include "playerinterface.hpp"
2023-04-15 05:15:11 -05:00
#include <SFML/System/Vector2.hpp>
#include <SFML/Window/Keyboard.hpp>
namespace snakeplusplus
{
2023-08-14 17:39:04 -05:00
PlayerDirection GetPlayerInput(void)
{
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left)
|| sf::Keyboard::isKeyPressed(sf::Keyboard::A))
2023-08-14 17:39:04 -05:00
return kLeft;
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up)
|| sf::Keyboard::isKeyPressed(sf::Keyboard::W))
2023-08-14 17:39:04 -05:00
return kUp;
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down)
|| sf::Keyboard::isKeyPressed(sf::Keyboard::S))
2023-08-14 17:39:04 -05:00
return kDown;
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;
}
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);
2023-08-18 18:09:09 -05:00
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
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-08-18 22:36:47 -05:00
letterOnBoard = &gameBoard.at(y).at(x);
2023-04-15 05:15:11 -05:00
switch (*letterOnBoard)
{
case 'O':
2023-08-18 22:36:47 -05:00
DrawSnake(sf::Vector2f(x, y));
2023-04-15 05:15:11 -05:00
break;
case 'X':
2023-08-18 22:36:47 -05:00
DrawFood(sf::Vector2f(x,y));
2023-04-15 05:15:11 -05:00
break;
default:
2023-08-18 22:36:47 -05:00
DrawEmpty(sf::Vector2f(x,y));
2023-04-15 05:15:11 -05:00
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))
{
2023-08-18 18:09:09 -05:00
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;
}
}