Cleaned up code for readability
This commit is contained in:
		
							parent
							
								
									153b4e1367
								
							
						
					
					
						commit
						de189301d4
					
				
							
								
								
									
										4
									
								
								Makefile
									
									
									
									
									
								
							
							
						
						
									
										4
									
								
								Makefile
									
									
									
									
									
								
							| @ -2,7 +2,9 @@ INC := -I include | |||||||
| STD := -std=c++11 | STD := -std=c++11 | ||||||
| SFML := -lsfml-graphics -lsfml-window -lsfml-system | SFML := -lsfml-graphics -lsfml-window -lsfml-system | ||||||
| 
 | 
 | ||||||
| all: dirs compile link | all: compile link | ||||||
|  | 
 | ||||||
|  | fresh: dirs compile link | ||||||
| 
 | 
 | ||||||
| dirs: | dirs: | ||||||
| 	mkdir bin build | 	mkdir bin build | ||||||
|  | |||||||
| @ -1,22 +1,32 @@ | |||||||
| // GameState.h
 | // GameState.h
 | ||||||
| #ifndef GAMESTATE_H | #ifndef GAMESTATE_H | ||||||
| #define GAMESTATE_H | #define GAMESTATE_H | ||||||
|  | 
 | ||||||
| #include <SFML\Graphics.hpp> | #include <SFML\Graphics.hpp> | ||||||
|  | #include "Snake.h" | ||||||
| 
 | 
 | ||||||
| class GameState | class GameState | ||||||
| { | { | ||||||
| private: |  | ||||||
| public: | public: | ||||||
|     sf::VideoMode gameVideoMode; |  | ||||||
|     sf::RenderWindow gameWindow; |  | ||||||
|     GameState(); |     GameState(); | ||||||
|     GameState(int newHorizontal, int newVertical); |     GameState(int newHorizontal, int newVertical); | ||||||
|     void startNewGame(); |     void StartGame(void); | ||||||
|  |     sf::Vector2f GetGameBoundaries(void); | ||||||
|     /*
 |     /*
 | ||||||
|     gameGridHorizontal = (videoSizeHorizontal // 25) * 25;
 |     gameGridHorizontal = (videoSizeHorizontal // 25) * 25;
 | ||||||
|     gameGridVertical = (videoSizeVertical // 25) * 25;
 |     gameGridVertical = (videoSizeVertical // 25) * 25;
 | ||||||
|     */ |     */ | ||||||
|     // sf::Vector2f GetGameBoundaries();
 | protected: | ||||||
|  |     ; | ||||||
|  | private: | ||||||
|  |     sf::RenderWindow gameWindow; | ||||||
|  |     sf::VideoMode gameVideoSettings; | ||||||
|  |     SnakeFood playerFood; | ||||||
|  |     Snake player; | ||||||
|  |     sf::Time delay; | ||||||
|  |     void RegenerateFood(void); | ||||||
|  |     void RunGameLoop(void); | ||||||
|  |     void RenderWindow(void); | ||||||
| }; | }; | ||||||
| 
 | 
 | ||||||
| #endif | #endif | ||||||
|  | |||||||
| @ -11,9 +11,6 @@ bool GlobalCollision(sf::Vector2f object1Position, sf::Vector2f object2Position) | |||||||
| 
 | 
 | ||||||
| class Snake | class Snake | ||||||
| { | { | ||||||
| private: |  | ||||||
|     std::deque<sf::RectangleShape> snakeBody; |  | ||||||
|     int snakeDirection = 0; |  | ||||||
| public: | public: | ||||||
|     Snake(); |     Snake(); | ||||||
|     Snake(sf::Vector2f head); |     Snake(sf::Vector2f head); | ||||||
| @ -21,10 +18,15 @@ public: | |||||||
|     sf::RectangleShape GetSnakeHead(); |     sf::RectangleShape GetSnakeHead(); | ||||||
|     void DisplaySnake(sf::RenderWindow& window); |     void DisplaySnake(sf::RenderWindow& window); | ||||||
|     void MoveSnake(SnakeFood& playerFood, sf::VideoMode gameVideoMode); |     void MoveSnake(SnakeFood& playerFood, sf::VideoMode gameVideoMode); | ||||||
|     void SnakeFoodCollision(SnakeFood& snakeFood, sf::VideoMode gameVideoMode); |  | ||||||
|     void CheckDirection(); |     void CheckDirection(); | ||||||
|     bool CheckBoundaries(); |     bool CheckBoundaries(); | ||||||
|     bool IsSelfCollision(sf::RectangleShape testRectangle); |     bool IsSelfCollision(sf::RectangleShape testRectangle); | ||||||
|  |     bool IsTouchingObject(sf::RectangleShape object); | ||||||
|  | protected: | ||||||
|  |     ; | ||||||
|  | private: | ||||||
|  |     std::deque<sf::RectangleShape> snakeBody; | ||||||
|  |     int snakeDirection = 0; | ||||||
| }; | }; | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
|  | |||||||
| @ -1,18 +1,24 @@ | |||||||
| // SnakeFood.h
 | // SnakeFood.h
 | ||||||
| #ifndef SNAKEFOOD_H | #ifndef SNAKEFOOD_H | ||||||
| #define SNAKEFOOD_H | #define SNAKEFOOD_H | ||||||
|  | 
 | ||||||
| #include <random> | #include <random> | ||||||
| #include <SFML\Graphics.hpp> | #include <SFML\Graphics.hpp> | ||||||
| 
 | 
 | ||||||
|  | extern const int kGridSize; | ||||||
|  | 
 | ||||||
| class SnakeFood | class SnakeFood | ||||||
| { | { | ||||||
| private: |  | ||||||
| public: | public: | ||||||
|     sf::RectangleShape snakeFoodObject; |  | ||||||
|     std::default_random_engine generator; |  | ||||||
|     SnakeFood(); |     SnakeFood(); | ||||||
|     SnakeFood(sf::Vector2f snakeFoodSize); |     SnakeFood(sf::Vector2f snakeFoodSize); | ||||||
|     void GenerateNewLocation(int horizontalLocation, int verticalLocation); |     void GenerateNewFood(sf::Vector2f windowSize); | ||||||
|  |     sf::RectangleShape GetFoodObject(void); | ||||||
|  | protected: | ||||||
|  |     ; | ||||||
|  | private: | ||||||
|  |     sf::RectangleShape snakeFoodObject; | ||||||
|  |     std::default_random_engine generator; | ||||||
|     int GenerateRandomNumber(int generationLimit); |     int GenerateRandomNumber(int generationLimit); | ||||||
| }; | }; | ||||||
| 
 | 
 | ||||||
|  | |||||||
| @ -6,42 +6,68 @@ | |||||||
| 
 | 
 | ||||||
| GameState::GameState() | GameState::GameState() | ||||||
| { | { | ||||||
|     sf::VideoMode tempVideoMode(1024, 725); |     delay = sf::milliseconds(100); | ||||||
|     gameVideoMode = tempVideoMode; |     gameVideoSettings = sf::VideoMode(1024, 725); | ||||||
|     sf::RenderWindow gameWindow(gameVideoMode, "SnakePlusPlus"); |     gameWindow.create(gameVideoSettings, "SnakePlusPlus"); | ||||||
|     return; |     return; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| GameState::GameState(int newHorizontal, int newVertical) | GameState::GameState(int maxHorizontal, int maxVertical) | ||||||
| { | { | ||||||
|     sf::VideoMode tempVideoMode(newHorizontal, newVertical); |     delay = sf::milliseconds(100); | ||||||
|     gameVideoMode = tempVideoMode; |     gameVideoSettings = sf::VideoMode(maxHorizontal, maxVertical); | ||||||
|     sf::RenderWindow tempWindow(gameVideoMode, "SnakePlusPlus"); |     gameWindow.create(gameVideoSettings, "SnakePlusPlus"); | ||||||
|     return; |     return; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| void GameState::startNewGame() | void GameState::StartGame() | ||||||
| { | { | ||||||
|     gameWindow.create(gameVideoMode, "SnakePlusPlus"); |     gameWindow.create(gameVideoSettings, "SnakePlusPlus"); | ||||||
|     sf::Time delay = sf::milliseconds(100); |     Snake player(sf::Vector2f(kGridSize,kGridSize)); | ||||||
|     int snakeDirection = 0; |     SnakeFood playerFood(sf::Vector2f(kGridSize,kGridSize)); | ||||||
|     Snake player(sf::Vector2f(25,25)); |     RunGameLoop(); | ||||||
|     SnakeFood playerFood(sf::Vector2f(25,25)); | } | ||||||
| 
 | 
 | ||||||
|  | sf::Vector2f GameState::GetGameBoundaries(void) | ||||||
|  | { | ||||||
|  |     sf::Vector2f boundaries; | ||||||
|  |     boundaries.x = gameVideoSettings.width; | ||||||
|  |     boundaries.y = gameVideoSettings.height; | ||||||
|  |     return boundaries; | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | // Generates new food until not colliding with player
 | ||||||
|  | void GameState::RegenerateFood(void) | ||||||
|  | { | ||||||
|  |     // Keep making new food until generating a valid spot
 | ||||||
|  |     while (player.IsTouchingObject(playerFood.GetFoodObject())) | ||||||
|  |         playerFood.GenerateNewFood(GetGameBoundaries()); | ||||||
|  |     return; | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | void GameState::RunGameLoop(void) | ||||||
|  | { | ||||||
|  |     sf::Event event; | ||||||
|     while (gameWindow.isOpen()) |     while (gameWindow.isOpen()) | ||||||
|     { |     { | ||||||
|         sf::Event event; |  | ||||||
|         while (gameWindow.pollEvent(event)) |         while (gameWindow.pollEvent(event)) | ||||||
|         { |         { | ||||||
|             if ((event.type == sf::Event::Closed) || (sf::Keyboard::isKeyPressed(sf::Keyboard::Escape))) |             if ((event.type == sf::Event::Closed) ||  | ||||||
|  |                 (sf::Keyboard::isKeyPressed(sf::Keyboard::Escape))) | ||||||
|                 gameWindow.close(); |                 gameWindow.close(); | ||||||
|         } |         } | ||||||
|         player.CheckDirection(); |         player.CheckDirection(); | ||||||
|         player.MoveSnake(playerFood, gameVideoMode); |         player.MoveSnake(playerFood, gameVideoSettings); | ||||||
|         gameWindow.clear(); |         RegenerateFood(); | ||||||
|         player.DisplaySnake(gameWindow); |         RenderWindow(); | ||||||
|         gameWindow.draw(playerFood.snakeFoodObject); |  | ||||||
|         gameWindow.display(); |  | ||||||
|         sf::sleep(delay); |         sf::sleep(delay); | ||||||
|     } |     } | ||||||
| } | } | ||||||
|  | 
 | ||||||
|  | void GameState::RenderWindow(void) | ||||||
|  | { | ||||||
|  |     gameWindow.clear(); | ||||||
|  |     player.DisplaySnake(gameWindow); | ||||||
|  |     gameWindow.draw(playerFood.GetFoodObject()); | ||||||
|  |     gameWindow.display(); | ||||||
|  | } | ||||||
|  | |||||||
| @ -5,19 +5,37 @@ | |||||||
| #include "Snake.h" | #include "Snake.h" | ||||||
| #include "SnakeFood.h" | #include "SnakeFood.h" | ||||||
| 
 | 
 | ||||||
|  | // General constructor for snake class
 | ||||||
|  | Snake::Snake() | ||||||
|  | { | ||||||
|  |     sf::RectangleShape newBodyPart(sf::Vector2f(kGridSize, kGridSize)); | ||||||
|  |     newBodyPart.setFillColor(sf::Color::Green); | ||||||
|  |     snakeBody.push_back(newBodyPart); | ||||||
|  |     return; | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | // Constructor for snake with position
 | ||||||
|  | Snake::Snake(sf::Vector2f headSize) | ||||||
|  | { | ||||||
|  |     sf::RectangleShape newBodyPart(headSize); | ||||||
|  |     newBodyPart.setFillColor(sf::Color::Green); | ||||||
|  |     snakeBody.push_back(newBodyPart); | ||||||
|  |     return; | ||||||
|  | } | ||||||
|  | 
 | ||||||
| // Get a new coordinate position based on snake direction
 | // Get a new coordinate position based on snake direction
 | ||||||
| sf::Vector2f CalculateNewPosition(int direction, sf::Vector2f position) | sf::Vector2f CalculateNewPosition(int direction, sf::Vector2f position) | ||||||
| { | { | ||||||
|     if (direction == 0) |     if (direction == 0) | ||||||
|         return position; |         return position; | ||||||
|     if (direction == 1) |     if (direction == 1) | ||||||
|         position.x -= 25; |         position.x -= kGridSize; | ||||||
|     if (direction == 2) |     if (direction == 2) | ||||||
|         position.y -= 25; |         position.y -= kGridSize; | ||||||
|     if (direction == 3) |     if (direction == 3) | ||||||
|         position.y += 25; |         position.y += kGridSize; | ||||||
|     if (direction == 4) |     if (direction == 4) | ||||||
|         position.x += 25; |         position.x += kGridSize; | ||||||
|     return position; |     return position; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| @ -70,7 +88,7 @@ void Snake::MoveSnake(SnakeFood& snakeFood, sf::VideoMode gameVideoMode) | |||||||
|     if (CheckBoundaries()) |     if (CheckBoundaries()) | ||||||
|         return; |         return; | ||||||
|     newHeadPosition = CalculateNewPosition(snakeDirection, newHeadPosition); |     newHeadPosition = CalculateNewPosition(snakeDirection, newHeadPosition); | ||||||
|     sf::RectangleShape newBodyPart(sf::Vector2f(25,25)); |     sf::RectangleShape newBodyPart(sf::Vector2f(kGridSize, kGridSize)); | ||||||
|     newBodyPart.setPosition(newHeadPosition); |     newBodyPart.setPosition(newHeadPosition); | ||||||
|     if (IsSelfCollision(newBodyPart)) // Do nothing if self collision
 |     if (IsSelfCollision(newBodyPart)) // Do nothing if self collision
 | ||||||
|     { |     { | ||||||
| @ -78,9 +96,8 @@ void Snake::MoveSnake(SnakeFood& snakeFood, sf::VideoMode gameVideoMode) | |||||||
|     } |     } | ||||||
|     newBodyPart.setFillColor(sf::Color::Green); |     newBodyPart.setFillColor(sf::Color::Green); | ||||||
|     snakeBody.push_front(newBodyPart); |     snakeBody.push_front(newBodyPart); | ||||||
|     if (!GlobalCollision(GetSnakeHead().getPosition(), snakeFood.snakeFoodObject.getPosition())) |     if (!GlobalCollision(GetSnakeHead().getPosition(), snakeFood.GetFoodObject().getPosition())) | ||||||
|         snakeBody.pop_back(); |         snakeBody.pop_back(); | ||||||
|     SnakeFoodCollision(snakeFood, gameVideoMode); |  | ||||||
|     return; |     return; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| @ -123,30 +140,16 @@ bool Snake::IsSelfCollision(sf::RectangleShape testRectangle) | |||||||
|     return false; |     return false; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| // If player collides with food then generate until no longer collided with food
 | // Checks if snake position matches object position
 | ||||||
| void Snake::SnakeFoodCollision(SnakeFood& snakeFood, sf::VideoMode gameVideoMode) | bool Snake::IsTouchingObject(sf::RectangleShape object) | ||||||
| { | { | ||||||
|     while(IsSelfCollision(snakeFood.snakeFoodObject)) |     for (auto snakeBodyPart = snakeBody.cbegin(); snakeBodyPart != snakeBody.cend(); ++snakeBodyPart) | ||||||
|     { |     { | ||||||
|         snakeFood.GenerateNewLocation(gameVideoMode.width, gameVideoMode.height); |         if ((*snakeBodyPart).getPosition().x != object.getPosition().x) | ||||||
|  |             continue; | ||||||
|  |         if ((*snakeBodyPart).getPosition().y != object.getPosition().y) | ||||||
|  |             continue; | ||||||
|  |         return true; | ||||||
|     } |     } | ||||||
|     return; |     return false; | ||||||
| } |  | ||||||
| 
 |  | ||||||
| // General constructor for snake class
 |  | ||||||
| Snake::Snake() |  | ||||||
| { |  | ||||||
|     sf::RectangleShape newBodyPart(sf::Vector2f(25,25)); |  | ||||||
|     newBodyPart.setFillColor(sf::Color::Green); |  | ||||||
|     snakeBody.push_back(newBodyPart); |  | ||||||
|     return; |  | ||||||
| } |  | ||||||
| 
 |  | ||||||
| // Constructor for snake with position
 |  | ||||||
| Snake::Snake(sf::Vector2f head) |  | ||||||
| { |  | ||||||
|     sf::RectangleShape newBodyPart(head); |  | ||||||
|     newBodyPart.setFillColor(sf::Color::Green); |  | ||||||
|     snakeBody.push_back(newBodyPart); |  | ||||||
|     return; |  | ||||||
| } | } | ||||||
|  | |||||||
| @ -2,11 +2,12 @@ | |||||||
| #include <iostream> | #include <iostream> | ||||||
| #include <SFML\Graphics.hpp> | #include <SFML\Graphics.hpp> | ||||||
| #include "SnakeFood.h" | #include "SnakeFood.h" | ||||||
| #include "Snake.h" | 
 | ||||||
|  | const int kGridSize = 25; | ||||||
| 
 | 
 | ||||||
| SnakeFood::SnakeFood() | SnakeFood::SnakeFood() | ||||||
| { | { | ||||||
|     snakeFoodObject.setSize(sf::Vector2f(25,25)); |     snakeFoodObject.setSize(sf::Vector2f(kGridSize,kGridSize)); | ||||||
|     snakeFoodObject.setFillColor(sf::Color::Red); |     snakeFoodObject.setFillColor(sf::Color::Red); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| @ -16,26 +17,31 @@ SnakeFood::SnakeFood(sf::Vector2f snakeFoodSize) | |||||||
|     snakeFoodObject.setFillColor(sf::Color::Red); |     snakeFoodObject.setFillColor(sf::Color::Red); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| void SnakeFood::GenerateNewLocation(int horizontalLocation, int verticalLocation) | // Returns a new food object for the snakeFood
 | ||||||
|  | void SnakeFood::GenerateNewFood(sf::Vector2f windowSize) | ||||||
| { | { | ||||||
|     sf::Vector2f newPosition; |     sf::Vector2f newPosition; | ||||||
|     newPosition.x = GenerateRandomNumber(horizontalLocation); |     newPosition.x = GenerateRandomNumber(windowSize.x); | ||||||
|     newPosition.y = GenerateRandomNumber(verticalLocation); |     newPosition.y = GenerateRandomNumber(windowSize.y); | ||||||
|     if (GlobalCollision(snakeFoodObject.getPosition(), newPosition)) |  | ||||||
|         { |  | ||||||
|             std::cout << "Location error: " << newPosition.x << " " << newPosition.y << '\n'; |  | ||||||
|             throw std::runtime_error("Error! New generation on same location"); |  | ||||||
|         } |  | ||||||
|     snakeFoodObject.setPosition(newPosition); |     snakeFoodObject.setPosition(newPosition); | ||||||
|     return; |     // if (GlobalCollision(snakeFoodObject.getPosition(), newPosition))
 | ||||||
|  |     // {
 | ||||||
|  |     //     std::cout << "Location error: " << newPosition.x << " " << newPosition.y << '\n';
 | ||||||
|  |     //     throw std::runtime_error("Error! New generation on same location");
 | ||||||
|  |     // }
 | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | sf::RectangleShape SnakeFood::GetFoodObject(void) | ||||||
|  | { | ||||||
|  |     return snakeFoodObject; | ||||||
|  | } | ||||||
| 
 | 
 | ||||||
|  | // Returns a newly generated number
 | ||||||
| int SnakeFood::GenerateRandomNumber(int generationLimit) | int SnakeFood::GenerateRandomNumber(int generationLimit) | ||||||
| { | { | ||||||
|     int generatedNumber; |     int generatedNumber; | ||||||
|     std::uniform_int_distribution<> distribution(0, generationLimit); |     std::uniform_int_distribution<> distribution(0, generationLimit); | ||||||
|     generatedNumber = distribution(generator); |     generatedNumber = distribution(generator); | ||||||
|     generatedNumber -= (generatedNumber % 25); |     generatedNumber -= (generatedNumber % kGridSize); | ||||||
|     return generatedNumber; |     return generatedNumber; | ||||||
| } | } | ||||||
|  | |||||||
| @ -2,6 +2,6 @@ | |||||||
| 
 | 
 | ||||||
| int main() | int main() | ||||||
| { | { | ||||||
|     GameState newGame; |     GameState game; | ||||||
|     newGame.startNewGame(); |     game.StartGame(); | ||||||
| } | } | ||||||
|  | |||||||
		Loading…
	
		Reference in New Issue
	
	Block a user