diff --git a/include/gamestate.hpp b/include/gamestate.hpp index 48f799c..238aa4b 100755 --- a/include/gamestate.hpp +++ b/include/gamestate.hpp @@ -15,8 +15,6 @@ namespace snakeplusplus GameEngine(); void StartGame(void); sf::Vector2f GetGameBoundaries(void); - protected: - ; private: std::vector< std::vector > gameBoard; PlayerInput controls; diff --git a/include/playerinterface.hpp b/include/playerinterface.hpp index 88665fb..f4267ca 100755 --- a/include/playerinterface.hpp +++ b/include/playerinterface.hpp @@ -13,8 +13,6 @@ namespace snakeplusplus public: PlayerInput(void); PlayerDirection GetPlayerInput(void); - protected: - ; private: PlayerDirection lastPlayerInput; }; @@ -26,10 +24,8 @@ namespace snakeplusplus PlayerOutput(void); bool IsOpen(void); void CheckContinue(void); - void DisplayGameState(std::vector< std::vector >* gameBoard); + void DisplayGameState(std::vector< std::vector >& gameBoard); void StartGameWindow(void); - protected: - ; private: void CheckWindowEvents(void); void DisplayEndScreen(void); @@ -40,7 +36,7 @@ namespace snakeplusplus sf::VideoMode gameVideoSettings; sf::RectangleShape drawObject; bool isWindowAlive; - sf::Time delay = sf::milliseconds(120); + sf::Time delay = sf::milliseconds(60); }; } diff --git a/include/snake.hpp b/include/snake.hpp index 01b4365..3ed81ea 100755 --- a/include/snake.hpp +++ b/include/snake.hpp @@ -13,15 +13,10 @@ namespace snakeplusplus struct Snake { public: - Snake(void); sf::Vector2f headLocation; sf::Vector2f speed; - std::queue< std::shared_ptr > body; + std::queue body; void Pop(void); - protected: - ; - private: - ; }; struct Food @@ -29,10 +24,8 @@ namespace snakeplusplus public: Food(void); sf::Vector2f location; - std::shared_ptr food; + char* food; void GenerateNewFood(sf::Vector2f boundaries); - protected: - ; private: std::default_random_engine generator; int GenerateRandomNumber(int generationLimit); diff --git a/src/gamestate.cpp b/src/gamestate.cpp index 6cc60d8..3e1354c 100755 --- a/src/gamestate.cpp +++ b/src/gamestate.cpp @@ -1,5 +1,5 @@ // GameState.cpp -#include +#include #include #include #include @@ -31,7 +31,8 @@ namespace snakeplusplus { UpdatePlayerSpeed(); PlaceNewSnakePart(MovePlayer()); - graphics.DisplayGameState(&gameBoard); + RegenerateFood(); + graphics.DisplayGameState(gameBoard); } return; } @@ -50,20 +51,21 @@ namespace snakeplusplus void GameEngine::PlaceNewSnakePart(sf::Vector2f location) { - char locationState; try { - locationState = gameBoard.at(location.y).at(location.x); - if (locationState == 'O') + char* locationState; + locationState = &gameBoard.at(location.y).at(location.x); + if (*locationState == 'O' && player.body.size() > 1) isGameOver = true; // Game should end (Snake touching snake) + *locationState = 'O'; + player.body.push(locationState); + player.headLocation = location; if (playerFood.location != location) player.Pop(); - gameBoard.at(location.y).at(location.x) = 'O'; } catch (const std::out_of_range& error) { isGameOver = true; // Snake ran into edge - return; + exit(0); } - return; } // Generates new food until not colliding with player @@ -87,15 +89,16 @@ namespace snakeplusplus { gameBoard.clear(); sf::Vector2f boardDimensions = GetGameBoundaries(); - std::vector tempBoard; - tempBoard.resize(boardDimensions.x, ' '); - gameBoard.resize(boardDimensions.y, tempBoard); - sf::Vector2f playerLocation(4,5); - char* headLocation = &gameBoard.at(playerLocation.y).at(playerLocation.x); - *headLocation = 'O'; - player.body.push(std::shared_ptr(headLocation)); - sf::Vector2f foodLocation(2,2); - gameBoard.at(foodLocation.y).at(foodLocation.x) = 'X'; + gameBoard.resize(boardDimensions.y, std::vector (boardDimensions.x, ' ')); + player.headLocation.x = 4; + player.headLocation.y = 5; + char* locationState = &gameBoard.at(player.headLocation.y).at(player.headLocation.x); + player.body.push(locationState); + *player.body.front() = 'O'; + playerFood.location.x = 2; + playerFood.location.y = 2; + playerFood.food = &gameBoard.at(2).at(2); + *playerFood.food = 'X'; return; } @@ -105,16 +108,20 @@ namespace snakeplusplus switch (input) { case kUp: player.speed.x = 0; - player.speed.y = 1; + player.speed.y = -1; + break; case kLeft: player.speed.x = -1; player.speed.y = 0; + break; case kRight: player.speed.x = 1; player.speed.y = 0; + break; case kDown: player.speed.x = 0; - player.speed.y = -1; + player.speed.y = 1; + break; default: break; } diff --git a/src/playerinterface.cpp b/src/playerinterface.cpp index f4a5b78..903e059 100755 --- a/src/playerinterface.cpp +++ b/src/playerinterface.cpp @@ -1,4 +1,5 @@ #include "playerinterface.hpp" +#include #include namespace snakeplusplus @@ -23,15 +24,17 @@ namespace snakeplusplus bool PlayerOutput::IsOpen(void) { - return isWindowAlive; + return gameWindow.isOpen(); } PlayerOutput::PlayerOutput(void) { - float kWidth = 1025 / kGridSize; - float kHeight = 725 / kGridSize; - gameBoundaries = sf::Vector2f(kWidth, kHeight); - gameVideoSettings = sf::VideoMode(1025, 725); + float kWidth = 1025; + float kHeight = 725; + float kBoardWidth = kWidth / kGridSize; + float kBoardHeight = kHeight / kGridSize; + gameBoundaries = sf::Vector2f(kBoardWidth, kBoardHeight); + gameVideoSettings = sf::VideoMode(kWidth, kHeight); drawObject.setSize(sf::Vector2f(kGridSize, kGridSize)); return; } @@ -74,22 +77,30 @@ namespace snakeplusplus return; } - void PlayerOutput::DisplayGameState(std::vector< std::vector >* gameBoard) + void PlayerOutput::DisplayGameState(std::vector< std::vector >& gameBoard) { CheckWindowEvents(); - sf::Vector2i location(0,0); - char letterOnBoard; - for (; location.y < gameBoundaries.y; location.y++) + sf::Vector2f location; + char* letterOnBoard; + for (float y = 0; y < gameBoundaries.y; y++) { - for (; location.x < gameBoundaries.x; location.x++) + for (float x = 0; x < gameBoundaries.x; x++) { - letterOnBoard = gameBoard->at(location.y).at(location.y); - if (letterOnBoard == 'o') - DrawSnake(static_cast(location)); - else if (letterOnBoard == 'x') - DrawFood(static_cast(location)); - else - DrawEmpty(static_cast(location)); + location.x = x; + location.y = y; + letterOnBoard = &gameBoard.at(location.y).at(location.x); + switch (*letterOnBoard) + { + case 'O': + DrawSnake(location); + break; + case 'X': + DrawFood(location); + break; + default: + DrawEmpty(location); + break; + } } } gameWindow.display(); diff --git a/src/snake.cpp b/src/snake.cpp index b4f3ce5..f239e6e 100755 --- a/src/snake.cpp +++ b/src/snake.cpp @@ -1,4 +1,5 @@ // Snake.cpp +#include #include #include #include @@ -7,21 +8,15 @@ namespace snakeplusplus { - // General constructor for snake class - Snake::Snake(void) - { - return; - } - void Snake::Pop(void) { - *(body.front().get()) = ' '; + *(body.front()) = ' '; body.pop(); } - Food::Food() + Food::Food(void) { - return; + generator.seed(std::random_device{}()); } // Returns a new food object for the snakeFood