Fixed going wrong way with snake movement

This commit is contained in:
Trimutex 2023-08-19 00:19:04 -05:00
parent 71dfa043a9
commit 197311831f

View File

@ -1,7 +1,6 @@
// GameState.cpp // GameState.cpp
#include <stdexcept> #include <stdexcept>
#include <SFML/Graphics.hpp> #include <SFML/Graphics.hpp>
#include <tuple>
#include "common.hpp" #include "common.hpp"
#include "playerinterface.hpp" #include "playerinterface.hpp"
#include "gamestate.hpp" #include "gamestate.hpp"
@ -99,19 +98,16 @@ namespace snakeplusplus
sf::Vector2f boardDimensions = GetGameBoundaries(); sf::Vector2f boardDimensions = GetGameBoundaries();
gameBoard.resize(boardDimensions.y, std::vector<char> (boardDimensions.x, ' ')); gameBoard.resize(boardDimensions.y, std::vector<char> (boardDimensions.x, ' '));
// Snake setup // Snake setup
player.headLocation.x = GenerateRandomNumber(boardDimensions.x);
player.headLocation.y = GenerateRandomNumber(boardDimensions.y);
{ {
player.headLocation.x = GenerateRandomNumber(boardDimensions.x);
player.headLocation.y = GenerateRandomNumber(boardDimensions.y);
char* locationState = &gameBoard.at(player.headLocation.y).at(player.headLocation.x); char* locationState = &gameBoard.at(player.headLocation.y).at(player.headLocation.x);
player.body.push(locationState); player.body.push(locationState);
*locationState = 'O'; *locationState = 'O';
} }
// Food setup // Food setup
{ playerFood.GenerateNewFood(boardDimensions);
playerFood.GenerateNewFood(boardDimensions); gameBoard.at(playerFood.location.y).at(playerFood.location.x) = 'X';
sf::Vector2f newLocation = playerFood.location;
gameBoard.at(newLocation.y).at(newLocation.x) = 'X';
}
return; return;
} }
@ -119,18 +115,22 @@ namespace snakeplusplus
{ {
switch (GetPlayerInput()) { switch (GetPlayerInput()) {
case kUp: case kUp:
if (player.speed.y == 1) { break; }
player.speed.x = 0; player.speed.x = 0;
player.speed.y = -1; player.speed.y = -1;
break; break;
case kLeft: case kLeft:
if (player.speed.x == 1) { break; }
player.speed.x = -1; player.speed.x = -1;
player.speed.y = 0; player.speed.y = 0;
break; break;
case kRight: case kRight:
if (player.speed.x == -1) { break; }
player.speed.x = 1; player.speed.x = 1;
player.speed.y = 0; player.speed.y = 0;
break; break;
case kDown: case kDown:
if (player.speed.y == -1) { break; }
player.speed.x = 0; player.speed.x = 0;
player.speed.y = 1; player.speed.y = 1;
break; break;