From c57fbc714c6904eeafbf0a9a46b1c615700c716b Mon Sep 17 00:00:00 2001 From: Trimutex Date: Fri, 18 Aug 2023 17:51:58 -0500 Subject: [PATCH] Cleaned up some naming and added WASD controls --- src/gamestate.cpp | 23 ++++++++++++----------- src/gamestate.hpp | 6 +++--- src/main.cpp | 2 +- src/playerinterface.cpp | 13 +++++++++---- 4 files changed, 25 insertions(+), 19 deletions(-) diff --git a/src/gamestate.cpp b/src/gamestate.cpp index 74aa232..e53b81c 100755 --- a/src/gamestate.cpp +++ b/src/gamestate.cpp @@ -12,26 +12,27 @@ namespace snakeplusplus return; } - void GameEngine::StartGame() + void GameEngine::Start() { - //ApplySettings(); PrepareGameBoard(); graphics.StartGameWindow(); - GameLoop(); + Loop(); return; } - void GameEngine::GameLoop(void) + void GameEngine::Reset() + { + graphics.CheckContinue(); + player.Reset(); + PrepareGameBoard(); + isGameOver = 0; + } + + void GameEngine::Loop(void) { while (graphics.IsOpen()) { - if (isGameOver) - { - graphics.CheckContinue(); - player.Reset(); - PrepareGameBoard(); - isGameOver = 0; - } + if (isGameOver) {Reset();} UpdatePlayerSpeed(); PlaceNewSnakePart(MovePlayer()); RegenerateFood(); diff --git a/src/gamestate.hpp b/src/gamestate.hpp index 91b2af1..29763a4 100755 --- a/src/gamestate.hpp +++ b/src/gamestate.hpp @@ -12,17 +12,17 @@ namespace snakeplusplus { public: GameEngine(); - void StartGame(void); + void Start(void); + void Reset(void); sf::Vector2f GetGameBoundaries(void); private: std::vector< std::vector > gameBoard; PlayerOutput graphics; Snake player; Food playerFood; - bool useSFML = 1; bool isGameOver = 0; void DisplayEndScreen(void); - void GameLoop(void); + void Loop(void); sf::Vector2f MovePlayer(void); void PlaceNewSnakePart(sf::Vector2f location); void RegenerateFood(void); diff --git a/src/main.cpp b/src/main.cpp index 854534f..0bf1900 100755 --- a/src/main.cpp +++ b/src/main.cpp @@ -3,5 +3,5 @@ int main(void) { snakeplusplus::GameEngine game; - game.StartGame(); + game.Start(); } diff --git a/src/playerinterface.cpp b/src/playerinterface.cpp index 2c95059..d981553 100755 --- a/src/playerinterface.cpp +++ b/src/playerinterface.cpp @@ -1,17 +1,22 @@ #include "playerinterface.hpp" #include +#include namespace snakeplusplus { 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; - if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up)) + if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up) + || sf::Keyboard::isKeyPressed(sf::Keyboard::W)) return kUp; - if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down)) + if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down) + || sf::Keyboard::isKeyPressed(sf::Keyboard::S)) 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 kNone; }