Issues to fix: sfml window and bounds checking

This commit is contained in:
TriantaTV 2023-03-17 21:40:49 -05:00
parent c4d40d152b
commit 67e56bcb3c
6 changed files with 38 additions and 9 deletions

View File

@ -17,6 +17,7 @@ public:
virtual void StartGameWindow(void) = 0; virtual void StartGameWindow(void) = 0;
protected: protected:
bool isWindowAlive; bool isWindowAlive;
sf::Time delay = sf::milliseconds(75);
private: private:
; ;
}; };
@ -32,7 +33,7 @@ public:
protected: protected:
; ;
private: private:
; void Clear(void);
}; };
class SFML : public DisplayInterface class SFML : public DisplayInterface
@ -51,7 +52,6 @@ private:
void DrawEmpty(sf::Vector2f location); void DrawEmpty(sf::Vector2f location);
void DrawFood(sf::Vector2f location); void DrawFood(sf::Vector2f location);
void DrawSnake(sf::Vector2f location); void DrawSnake(sf::Vector2f location);
sf::Time delay;
sf::RenderWindow gameWindow; sf::RenderWindow gameWindow;
sf::VideoMode gameVideoSettings; sf::VideoMode gameVideoSettings;
sf::RectangleShape drawObject; sf::RectangleShape drawObject;

View File

@ -9,7 +9,8 @@ class SnakeFood
{ {
public: public:
SnakeFood(); SnakeFood();
sf::Vector2f GenerateNewFood(sf::Vector2f boundaries); void GenerateNewFood(sf::Vector2f boundaries);
sf::Vector2f GetFoodLocation(void);
protected: protected:
; ;
private: private:

View File

@ -33,15 +33,16 @@ void CommandLine::DisplayEndScreen(void)
return; return;
} }
// TODO: Use cout for printing game to screen
void CommandLine::DisplayGameState(std::vector< std::vector<char> >* gameBoard) void CommandLine::DisplayGameState(std::vector< std::vector<char> >* gameBoard)
{ {
Clear();
for (int i = 0; i < gameBoundaries.y; i++) for (int i = 0; i < gameBoundaries.y; i++)
{ {
for (int j = 0; j < gameBoundaries.x; j++) for (int j = 0; j < gameBoundaries.x; j++)
std::cout << gameBoard->at(i).at(j); std::cout << gameBoard->at(i).at(j);
std::cout << std::endl; std::cout << std::endl;
} }
sf::sleep(delay);
} }
void CommandLine::StartGameWindow(void) void CommandLine::StartGameWindow(void)
@ -50,9 +51,21 @@ void CommandLine::StartGameWindow(void)
return; return;
} }
void CommandLine::Clear(void)
{
#if defined _WIN32
system("cls");
//clrscr(); // including header file : conio.h
#elif defined (__LINUX__) || defined(__gnu_linux__) || defined(__linux__)
system("clear");
//std::cout<< u8"\033[2J\033[1;1H"; //Using ANSI Escape Sequences
#elif defined (__APPLE__)
system("clear");
#endif
}
SFML::SFML(void) SFML::SFML(void)
{ {
delay = sf::milliseconds(75);
gameVideoSettings = sf::VideoMode(1025, 725); gameVideoSettings = sf::VideoMode(1025, 725);
drawObject.setSize(sf::Vector2f(kGridSize, kGridSize)); drawObject.setSize(sf::Vector2f(kGridSize, kGridSize));
return; return;
@ -115,6 +128,7 @@ void SFML::DisplayGameState(std::vector< std::vector<char> >* gameBoard)
} }
} }
gameWindow.display(); gameWindow.display();
sf::sleep(delay);
return; return;
} }

View File

@ -79,11 +79,16 @@ void GameState::PlayerWantsToContinue(void)
// Generates new food until not colliding with player // Generates new food until not colliding with player
void GameState::RegenerateFood(void) void GameState::RegenerateFood(void)
{ {
sf::Vector2f newLocation; sf::Vector2f newLocation = playerFood.GetFoodLocation();
playerFood.GenerateNewFood(GetGameBoundaries()); bool isUpdated = false;
// Keep making new food until generating a valid spot // Keep making new food until generating a valid spot
while (gameBoard.at(newLocation.y).at(newLocation.x) == 'o') while (gameBoard.at(newLocation.y).at(newLocation.x) == 'o')
{
isUpdated = true;
playerFood.GenerateNewFood(GetGameBoundaries()); playerFood.GenerateNewFood(GetGameBoundaries());
newLocation = playerFood.GetFoodLocation();
}
if (isUpdated)
gameBoard.at(newLocation.y).at(newLocation.x) = 'x'; gameBoard.at(newLocation.y).at(newLocation.x) = 'x';
return; return;
} }
@ -95,6 +100,10 @@ void GameState::ResetGameBoard(void)
std::vector<char> tempBoard; std::vector<char> tempBoard;
tempBoard.resize(boardDimensions.x, ' '); tempBoard.resize(boardDimensions.x, ' ');
gameBoard.resize(boardDimensions.y, tempBoard); gameBoard.resize(boardDimensions.y, tempBoard);
playerFood.GenerateNewFood(boardDimensions);
sf::Vector2f foodStartLocation = playerFood.GetFoodLocation();
gameBoard.at(foodStartLocation.y).at(foodStartLocation.x) = 'x';
return; return;
} }

View File

@ -47,6 +47,7 @@ sf::Vector2f Snake::CalculateNewHead(void)
position.y += 1; position.y += 1;
if (snakeDirection == kRight) if (snakeDirection == kRight)
position.x += 1; position.x += 1;
return position;
} }
void Snake::CreateNewHead(sf::Vector2f headLocation) void Snake::CreateNewHead(sf::Vector2f headLocation)

View File

@ -8,10 +8,14 @@ SnakeFood::SnakeFood()
} }
// Returns a new food object for the snakeFood // Returns a new food object for the snakeFood
sf::Vector2f SnakeFood::GenerateNewFood(sf::Vector2f boundaries) void SnakeFood::GenerateNewFood(sf::Vector2f boundaries)
{ {
location.x = GenerateRandomNumber(boundaries.x); location.x = GenerateRandomNumber(boundaries.x);
location.y = GenerateRandomNumber(boundaries.y); location.y = GenerateRandomNumber(boundaries.y);
}
sf::Vector2f SnakeFood::GetFoodLocation(void)
{
return location; return location;
} }