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

View File

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

View File

@ -33,15 +33,16 @@ void CommandLine::DisplayEndScreen(void)
return;
}
// TODO: Use cout for printing game to screen
void CommandLine::DisplayGameState(std::vector< std::vector<char> >* gameBoard)
{
Clear();
for (int i = 0; i < gameBoundaries.y; i++)
{
for (int j = 0; j < gameBoundaries.x; j++)
std::cout << gameBoard->at(i).at(j);
std::cout << std::endl;
}
sf::sleep(delay);
}
void CommandLine::StartGameWindow(void)
@ -50,9 +51,21 @@ void CommandLine::StartGameWindow(void)
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)
{
delay = sf::milliseconds(75);
gameVideoSettings = sf::VideoMode(1025, 725);
drawObject.setSize(sf::Vector2f(kGridSize, kGridSize));
return;
@ -115,6 +128,7 @@ void SFML::DisplayGameState(std::vector< std::vector<char> >* gameBoard)
}
}
gameWindow.display();
sf::sleep(delay);
return;
}

View File

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

View File

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

View File

@ -8,10 +8,14 @@ SnakeFood::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.y = GenerateRandomNumber(boundaries.y);
}
sf::Vector2f SnakeFood::GetFoodLocation(void)
{
return location;
}