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)
{
PlayerDirection input = controls.GetPlayerInput();
switch (input) {
switch (GetPlayerInput()) {
case kUp:
player.speed.x = 0;
player.speed.y = -1;

View File

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

View File

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

View File

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

View File

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