diff --git a/include/display.h b/include/display.h index 9a2cbbe..261b216 100755 --- a/include/display.h +++ b/include/display.h @@ -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; diff --git a/include/snakefood.h b/include/snakefood.h index 9e3d4f3..bb48960 100755 --- a/include/snakefood.h +++ b/include/snakefood.h @@ -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: diff --git a/src/display.cpp b/src/display.cpp index b1a0c80..a4d4248 100755 --- a/src/display.cpp +++ b/src/display.cpp @@ -33,15 +33,16 @@ void CommandLine::DisplayEndScreen(void) return; } -// TODO: Use cout for printing game to screen void CommandLine::DisplayGameState(std::vector< std::vector >* 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 >* gameBoard) } } gameWindow.display(); + sf::sleep(delay); return; } diff --git a/src/gamestate.cpp b/src/gamestate.cpp index 0f54bc7..e49006f 100755 --- a/src/gamestate.cpp +++ b/src/gamestate.cpp @@ -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 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; } diff --git a/src/snake.cpp b/src/snake.cpp index c10bfa1..510e71f 100755 --- a/src/snake.cpp +++ b/src/snake.cpp @@ -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) diff --git a/src/snakefood.cpp b/src/snakefood.cpp index b21be6b..f252067 100755 --- a/src/snakefood.cpp +++ b/src/snakefood.cpp @@ -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; }