Fixed snake keeping movement on reset

This commit is contained in:
TriantaTV 2023-08-14 17:39:04 -05:00
parent c71b1d6982
commit d83f271adf
5 changed files with 10 additions and 22 deletions

View File

@ -110,8 +110,7 @@ namespace snakeplusplus
void GameEngine::UpdatePlayerSpeed(void) void GameEngine::UpdatePlayerSpeed(void)
{ {
PlayerDirection input = controls.GetPlayerInput(); switch (GetPlayerInput()) {
switch (input) {
case kUp: case kUp:
player.speed.x = 0; player.speed.x = 0;
player.speed.y = -1; player.speed.y = -1;

View File

@ -17,7 +17,6 @@ namespace snakeplusplus
sf::Vector2f GetGameBoundaries(void); sf::Vector2f GetGameBoundaries(void);
private: private:
std::vector< std::vector<char> > gameBoard; std::vector< std::vector<char> > gameBoard;
PlayerInput controls;
PlayerOutput graphics; PlayerOutput graphics;
Snake player; Snake player;
Food playerFood; Food playerFood;

View File

@ -5,22 +5,17 @@
namespace snakeplusplus namespace snakeplusplus
{ {
PlayerInput::PlayerInput(void) PlayerDirection GetPlayerInput(void)
{
lastPlayerInput = kNone;
}
PlayerDirection PlayerInput::GetPlayerInput(void)
{ {
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left)) if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
lastPlayerInput = kLeft; return kLeft;
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up)) if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
lastPlayerInput = kUp; return kUp;
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down)) if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
lastPlayerInput = kDown; return kDown;
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right)) if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
lastPlayerInput = kRight; return kRight;
return lastPlayerInput; return kNone;
} }
bool PlayerOutput::IsOpen(void) bool PlayerOutput::IsOpen(void)

View File

@ -8,14 +8,7 @@ const int kGridSize = 25;
namespace snakeplusplus namespace snakeplusplus
{ {
class PlayerInput PlayerDirection GetPlayerInput(void);
{
public:
PlayerInput(void);
PlayerDirection GetPlayerInput(void);
private:
PlayerDirection lastPlayerInput;
};
class PlayerOutput class PlayerOutput
{ {

View File

@ -17,6 +17,8 @@ namespace snakeplusplus
void Snake::Reset(void) void Snake::Reset(void)
{ {
while (!body.empty()) Pop(); while (!body.empty()) Pop();
speed.x = 0;
speed.y = 0;
} }
Food::Food(void) Food::Food(void)