Cleaned up some naming and added WASD controls

This commit is contained in:
Trimutex 2023-08-18 17:51:58 -05:00
parent 35244daa02
commit c57fbc714c
4 changed files with 25 additions and 19 deletions

View File

@ -12,26 +12,27 @@ namespace snakeplusplus
return; return;
} }
void GameEngine::StartGame() void GameEngine::Start()
{ {
//ApplySettings();
PrepareGameBoard(); PrepareGameBoard();
graphics.StartGameWindow(); graphics.StartGameWindow();
GameLoop(); Loop();
return; return;
} }
void GameEngine::GameLoop(void) void GameEngine::Reset()
{
while (graphics.IsOpen())
{
if (isGameOver)
{ {
graphics.CheckContinue(); graphics.CheckContinue();
player.Reset(); player.Reset();
PrepareGameBoard(); PrepareGameBoard();
isGameOver = 0; isGameOver = 0;
} }
void GameEngine::Loop(void)
{
while (graphics.IsOpen())
{
if (isGameOver) {Reset();}
UpdatePlayerSpeed(); UpdatePlayerSpeed();
PlaceNewSnakePart(MovePlayer()); PlaceNewSnakePart(MovePlayer());
RegenerateFood(); RegenerateFood();

View File

@ -12,17 +12,17 @@ namespace snakeplusplus
{ {
public: public:
GameEngine(); GameEngine();
void StartGame(void); void Start(void);
void Reset(void);
sf::Vector2f GetGameBoundaries(void); sf::Vector2f GetGameBoundaries(void);
private: private:
std::vector< std::vector<char> > gameBoard; std::vector< std::vector<char> > gameBoard;
PlayerOutput graphics; PlayerOutput graphics;
Snake player; Snake player;
Food playerFood; Food playerFood;
bool useSFML = 1;
bool isGameOver = 0; bool isGameOver = 0;
void DisplayEndScreen(void); void DisplayEndScreen(void);
void GameLoop(void); void Loop(void);
sf::Vector2f MovePlayer(void); sf::Vector2f MovePlayer(void);
void PlaceNewSnakePart(sf::Vector2f location); void PlaceNewSnakePart(sf::Vector2f location);
void RegenerateFood(void); void RegenerateFood(void);

View File

@ -3,5 +3,5 @@
int main(void) int main(void)
{ {
snakeplusplus::GameEngine game; snakeplusplus::GameEngine game;
game.StartGame(); game.Start();
} }

View File

@ -1,17 +1,22 @@
#include "playerinterface.hpp" #include "playerinterface.hpp"
#include <SFML/System/Vector2.hpp> #include <SFML/System/Vector2.hpp>
#include <SFML/Window/Keyboard.hpp>
namespace snakeplusplus namespace snakeplusplus
{ {
PlayerDirection GetPlayerInput(void) PlayerDirection GetPlayerInput(void)
{ {
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left)) if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left)
|| sf::Keyboard::isKeyPressed(sf::Keyboard::A))
return kLeft; return kLeft;
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up)) if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up)
|| sf::Keyboard::isKeyPressed(sf::Keyboard::W))
return kUp; return kUp;
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down)) if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down)
|| sf::Keyboard::isKeyPressed(sf::Keyboard::S))
return kDown; return kDown;
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right)) if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right)
|| sf::Keyboard::isKeyPressed(sf::Keyboard::D))
return kRight; return kRight;
return kNone; return kNone;
} }