Fixed snake
This commit is contained in:
		
							parent
							
								
									498ac65dbc
								
							
						
					
					
						commit
						a9d194c90c
					
				| @ -15,8 +15,6 @@ namespace snakeplusplus | ||||
|         GameEngine(); | ||||
|         void StartGame(void); | ||||
|         sf::Vector2f GetGameBoundaries(void); | ||||
|     protected: | ||||
|         ; | ||||
|     private: | ||||
|         std::vector< std::vector<char> > gameBoard; | ||||
|         PlayerInput controls; | ||||
|  | ||||
| @ -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<char> >* gameBoard); | ||||
|         void DisplayGameState(std::vector< std::vector<char> >& 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); | ||||
|     }; | ||||
| } | ||||
| 
 | ||||
|  | ||||
| @ -13,15 +13,10 @@ namespace snakeplusplus | ||||
|     struct Snake | ||||
|     { | ||||
|     public: | ||||
|         Snake(void); | ||||
|         sf::Vector2f headLocation; | ||||
|         sf::Vector2f speed; | ||||
|         std::queue< std::shared_ptr<char> > body; | ||||
|         std::queue<char*> body; | ||||
|         void Pop(void); | ||||
|     protected: | ||||
|         ; | ||||
|     private: | ||||
|         ; | ||||
|     }; | ||||
| 
 | ||||
|     struct Food | ||||
| @ -29,10 +24,8 @@ namespace snakeplusplus | ||||
|     public: | ||||
|         Food(void); | ||||
|         sf::Vector2f location; | ||||
|         std::shared_ptr<char> food; | ||||
|         char* food; | ||||
|         void GenerateNewFood(sf::Vector2f boundaries); | ||||
|     protected: | ||||
|         ; | ||||
|     private: | ||||
|         std::default_random_engine generator; | ||||
|         int GenerateRandomNumber(int generationLimit); | ||||
|  | ||||
| @ -1,5 +1,5 @@ | ||||
| // GameState.cpp
 | ||||
| #include <SFML/System/Vector2.hpp> | ||||
| #include <iostream> | ||||
| #include <memory> | ||||
| #include <stdexcept> | ||||
| #include <string> | ||||
| @ -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<char> 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<char>(headLocation)); | ||||
|         sf::Vector2f foodLocation(2,2);  | ||||
|         gameBoard.at(foodLocation.y).at(foodLocation.x) = 'X'; | ||||
|         gameBoard.resize(boardDimensions.y, std::vector<char> (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; | ||||
|         } | ||||
|  | ||||
| @ -1,4 +1,5 @@ | ||||
| #include "playerinterface.hpp" | ||||
| #include <SFML/System/Vector2.hpp> | ||||
| #include <iostream> | ||||
| 
 | ||||
| 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<char> >* gameBoard) | ||||
|     void PlayerOutput::DisplayGameState(std::vector< std::vector<char> >& 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<sf::Vector2f>(location)); | ||||
|                 else if (letterOnBoard == 'x') | ||||
|                     DrawFood(static_cast<sf::Vector2f>(location)); | ||||
|                 else | ||||
|                     DrawEmpty(static_cast<sf::Vector2f>(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(); | ||||
|  | ||||
| @ -1,4 +1,5 @@ | ||||
| // Snake.cpp
 | ||||
| #include <algorithm> | ||||
| #include <memory> | ||||
| #include <queue> | ||||
| #include <random> | ||||
| @ -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
 | ||||
|  | ||||
		Loading…
	
		Reference in New Issue
	
	Block a user