More cleaning for readabillity, added common file
This commit is contained in:
		
							parent
							
								
									de189301d4
								
							
						
					
					
						commit
						6589dabc09
					
				
							
								
								
									
										7
									
								
								Makefile
									
									
									
									
									
								
							
							
						
						
									
										7
									
								
								Makefile
									
									
									
									
									
								
							| @ -6,15 +6,16 @@ all: compile link | |||||||
| 
 | 
 | ||||||
| fresh: dirs compile link | fresh: dirs compile link | ||||||
| 
 | 
 | ||||||
| dirs: |  | ||||||
| 	mkdir bin build |  | ||||||
| 
 |  | ||||||
| compile: | compile: | ||||||
| 	g++ $(INC) $(STD) -c -o build/main.o src/main.cpp | 	g++ $(INC) $(STD) -c -o build/main.o src/main.cpp | ||||||
|  | 	g++ $(INC) $(STD) -c -o build/Common.o src/Common.cpp | ||||||
| 	g++ $(INC) $(STD) -c -o build/GameState.o src/GameState.cpp | 	g++ $(INC) $(STD) -c -o build/GameState.o src/GameState.cpp | ||||||
| 	g++ $(INC) $(STD) -c -o build/Snake.o src/Snake.cpp | 	g++ $(INC) $(STD) -c -o build/Snake.o src/Snake.cpp | ||||||
| 	g++ $(INC) $(STD) -c -o build/SnakeFood.o src/SnakeFood.cpp | 	g++ $(INC) $(STD) -c -o build/SnakeFood.o src/SnakeFood.cpp | ||||||
| 
 | 
 | ||||||
|  | dirs: | ||||||
|  | 	mkdir bin build | ||||||
|  | 
 | ||||||
| link: | link: | ||||||
| 	g++ build/*.o -o bin/SnakePlusPlus $(SFML) | 	g++ build/*.o -o bin/SnakePlusPlus $(SFML) | ||||||
| 
 | 
 | ||||||
|  | |||||||
							
								
								
									
										18
									
								
								include/Common.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										18
									
								
								include/Common.h
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,18 @@ | |||||||
|  | #ifndef COMMON_H | ||||||
|  | #define COMMON_H | ||||||
|  | 
 | ||||||
|  | #include <SFML\Graphics.hpp> | ||||||
|  | 
 | ||||||
|  | const int kGridSize = 25; | ||||||
|  | 
 | ||||||
|  | enum PlayerDirection | ||||||
|  | { | ||||||
|  |     kLeft = 1, | ||||||
|  |     kUp = 2, | ||||||
|  |     kDown = 3, | ||||||
|  |     kRight = 4 | ||||||
|  | }; | ||||||
|  | 
 | ||||||
|  | bool GlobalCollision(sf::Vector2f object1Position, sf::Vector2f object2Position); | ||||||
|  | 
 | ||||||
|  | #endif | ||||||
| @ -12,10 +12,6 @@ public: | |||||||
|     GameState(int newHorizontal, int newVertical); |     GameState(int newHorizontal, int newVertical); | ||||||
|     void StartGame(void); |     void StartGame(void); | ||||||
|     sf::Vector2f GetGameBoundaries(void); |     sf::Vector2f GetGameBoundaries(void); | ||||||
|     /*
 |  | ||||||
|     gameGridHorizontal = (videoSizeHorizontal // 25) * 25;
 |  | ||||||
|     gameGridVertical = (videoSizeVertical // 25) * 25;
 |  | ||||||
|     */ |  | ||||||
| protected: | protected: | ||||||
|     ; |     ; | ||||||
| private: | private: | ||||||
| @ -24,6 +20,7 @@ private: | |||||||
|     SnakeFood playerFood; |     SnakeFood playerFood; | ||||||
|     Snake player; |     Snake player; | ||||||
|     sf::Time delay; |     sf::Time delay; | ||||||
|  |     void GetKeyboardInput(void); | ||||||
|     void RegenerateFood(void); |     void RegenerateFood(void); | ||||||
|     void RunGameLoop(void); |     void RunGameLoop(void); | ||||||
|     void RenderWindow(void); |     void RenderWindow(void); | ||||||
|  | |||||||
| @ -6,27 +6,26 @@ | |||||||
| #include <SFML\Graphics.hpp> | #include <SFML\Graphics.hpp> | ||||||
| #include "SnakeFood.h" | #include "SnakeFood.h" | ||||||
| 
 | 
 | ||||||
| sf::Vector2f CalculateNewPosition(int direction, sf::Vector2f position); |  | ||||||
| bool GlobalCollision(sf::Vector2f object1Position, sf::Vector2f object2Position); |  | ||||||
| 
 | 
 | ||||||
| class Snake | class Snake | ||||||
| { | { | ||||||
| public: | public: | ||||||
|     Snake(); |     Snake(void); | ||||||
|     Snake(sf::Vector2f head); |     Snake(sf::Vector2f head); | ||||||
|     sf::Vector2f GetSnakeHeadPosition(); |  | ||||||
|     sf::RectangleShape GetSnakeHead(); |  | ||||||
|     void DisplaySnake(sf::RenderWindow& window); |     void DisplaySnake(sf::RenderWindow& window); | ||||||
|     void MoveSnake(SnakeFood& playerFood, sf::VideoMode gameVideoMode); |     sf::RectangleShape GetSnakeHead(void); | ||||||
|     void CheckDirection(); |     sf::Vector2f GetSnakeHeadPosition(void); | ||||||
|     bool CheckBoundaries(); |  | ||||||
|     bool IsSelfCollision(sf::RectangleShape testRectangle); |  | ||||||
|     bool IsTouchingObject(sf::RectangleShape object); |     bool IsTouchingObject(sf::RectangleShape object); | ||||||
|  |     void MoveSnake(SnakeFood* playerFood); | ||||||
|  |     void UpdateDirection(int newDirection); | ||||||
| protected: | protected: | ||||||
|     ; |     ; | ||||||
| private: | private: | ||||||
|     std::deque<sf::RectangleShape> snakeBody; |     std::deque<sf::RectangleShape> snakeBody; | ||||||
|     int snakeDirection = 0; |     int snakeDirection = 0; | ||||||
|  |     sf::Vector2f CalculateNewPosition(sf::Vector2f position); | ||||||
|  |     bool CheckBoundaries(void); | ||||||
|  |     bool IsSelfCollision(sf::RectangleShape testRectangle); | ||||||
| }; | }; | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
|  | |||||||
| @ -2,10 +2,8 @@ | |||||||
| #ifndef SNAKEFOOD_H | #ifndef SNAKEFOOD_H | ||||||
| #define SNAKEFOOD_H | #define SNAKEFOOD_H | ||||||
| 
 | 
 | ||||||
| #include <random> |  | ||||||
| #include <SFML\Graphics.hpp> | #include <SFML\Graphics.hpp> | ||||||
| 
 | #include <random> | ||||||
| extern const int kGridSize; |  | ||||||
| 
 | 
 | ||||||
| class SnakeFood | class SnakeFood | ||||||
| { | { | ||||||
| @ -14,6 +12,7 @@ public: | |||||||
|     SnakeFood(sf::Vector2f snakeFoodSize); |     SnakeFood(sf::Vector2f snakeFoodSize); | ||||||
|     void GenerateNewFood(sf::Vector2f windowSize); |     void GenerateNewFood(sf::Vector2f windowSize); | ||||||
|     sf::RectangleShape GetFoodObject(void); |     sf::RectangleShape GetFoodObject(void); | ||||||
|  |     sf::Vector2f GetFoodObjectPosition(void); | ||||||
| protected: | protected: | ||||||
|     ; |     ; | ||||||
| private: | private: | ||||||
|  | |||||||
							
								
								
									
										11
									
								
								src/Common.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										11
									
								
								src/Common.cpp
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,11 @@ | |||||||
|  | #include "Common.h" | ||||||
|  | 
 | ||||||
|  | // Test for collision between two object positions
 | ||||||
|  | bool GlobalCollision(sf::Vector2f object1Position, sf::Vector2f object2Position) | ||||||
|  | { | ||||||
|  |     if (object1Position.x != object2Position.x) | ||||||
|  |         return false; | ||||||
|  |     if (object1Position.y != object2Position.y) | ||||||
|  |         return false; | ||||||
|  |     return true; | ||||||
|  | } | ||||||
| @ -1,20 +1,21 @@ | |||||||
| // GameState.cpp
 | // GameState.cpp
 | ||||||
| #include <SFML\Graphics.hpp> | #include <SFML\Graphics.hpp> | ||||||
| #include <SFML\System.hpp> | #include <SFML\System.hpp> | ||||||
|  | #include "Common.h" | ||||||
| #include "Snake.h" | #include "Snake.h" | ||||||
| #include "GameState.h" | #include "GameState.h" | ||||||
| 
 | 
 | ||||||
| GameState::GameState() | GameState::GameState() | ||||||
| { | { | ||||||
|     delay = sf::milliseconds(100); |     delay = sf::milliseconds(75); | ||||||
|     gameVideoSettings = sf::VideoMode(1024, 725); |     gameVideoSettings = sf::VideoMode(1025, 725); | ||||||
|     gameWindow.create(gameVideoSettings, "SnakePlusPlus"); |     gameWindow.create(gameVideoSettings, "SnakePlusPlus"); | ||||||
|     return; |     return; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| GameState::GameState(int maxHorizontal, int maxVertical) | GameState::GameState(int maxHorizontal, int maxVertical) | ||||||
| { | { | ||||||
|     delay = sf::milliseconds(100); |     delay = sf::milliseconds(75); | ||||||
|     gameVideoSettings = sf::VideoMode(maxHorizontal, maxVertical); |     gameVideoSettings = sf::VideoMode(maxHorizontal, maxVertical); | ||||||
|     gameWindow.create(gameVideoSettings, "SnakePlusPlus"); |     gameWindow.create(gameVideoSettings, "SnakePlusPlus"); | ||||||
|     return; |     return; | ||||||
| @ -26,6 +27,7 @@ void GameState::StartGame() | |||||||
|     Snake player(sf::Vector2f(kGridSize,kGridSize)); |     Snake player(sf::Vector2f(kGridSize,kGridSize)); | ||||||
|     SnakeFood playerFood(sf::Vector2f(kGridSize,kGridSize)); |     SnakeFood playerFood(sf::Vector2f(kGridSize,kGridSize)); | ||||||
|     RunGameLoop(); |     RunGameLoop(); | ||||||
|  |     return; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| sf::Vector2f GameState::GetGameBoundaries(void) | sf::Vector2f GameState::GetGameBoundaries(void) | ||||||
| @ -36,6 +38,26 @@ sf::Vector2f GameState::GetGameBoundaries(void) | |||||||
|     return boundaries; |     return boundaries; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | void GameState::GetKeyboardInput(void) | ||||||
|  | { | ||||||
|  |     sf::Event event; | ||||||
|  |     while (gameWindow.pollEvent(event)) | ||||||
|  |     { | ||||||
|  |         if ((event.type == sf::Event::Closed) ||  | ||||||
|  |             (sf::Keyboard::isKeyPressed(sf::Keyboard::Escape))) | ||||||
|  |             gameWindow.close(); | ||||||
|  |     } | ||||||
|  |     if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left)) | ||||||
|  |         player.UpdateDirection(kLeft); | ||||||
|  |     if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up)) | ||||||
|  |         player.UpdateDirection(kUp); | ||||||
|  |     if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down)) | ||||||
|  |         player.UpdateDirection(kDown); | ||||||
|  |     if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right)) | ||||||
|  |         player.UpdateDirection(kRight); | ||||||
|  |     return; | ||||||
|  | } | ||||||
|  | 
 | ||||||
| // Generates new food until not colliding with player
 | // Generates new food until not colliding with player
 | ||||||
| void GameState::RegenerateFood(void) | void GameState::RegenerateFood(void) | ||||||
| { | { | ||||||
| @ -47,21 +69,15 @@ void GameState::RegenerateFood(void) | |||||||
| 
 | 
 | ||||||
| void GameState::RunGameLoop(void) | void GameState::RunGameLoop(void) | ||||||
| { | { | ||||||
|     sf::Event event; |  | ||||||
|     while (gameWindow.isOpen()) |     while (gameWindow.isOpen()) | ||||||
|     { |     { | ||||||
|         while (gameWindow.pollEvent(event)) |         GetKeyboardInput(); | ||||||
|         { |         player.MoveSnake(&playerFood); | ||||||
|             if ((event.type == sf::Event::Closed) ||  |  | ||||||
|                 (sf::Keyboard::isKeyPressed(sf::Keyboard::Escape))) |  | ||||||
|                 gameWindow.close(); |  | ||||||
|         } |  | ||||||
|         player.CheckDirection(); |  | ||||||
|         player.MoveSnake(playerFood, gameVideoSettings); |  | ||||||
|         RegenerateFood(); |         RegenerateFood(); | ||||||
|         RenderWindow(); |         RenderWindow(); | ||||||
|         sf::sleep(delay); |         sf::sleep(delay); | ||||||
|     } |     } | ||||||
|  |     return; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| void GameState::RenderWindow(void) | void GameState::RenderWindow(void) | ||||||
| @ -70,4 +86,5 @@ void GameState::RenderWindow(void) | |||||||
|     player.DisplaySnake(gameWindow); |     player.DisplaySnake(gameWindow); | ||||||
|     gameWindow.draw(playerFood.GetFoodObject()); |     gameWindow.draw(playerFood.GetFoodObject()); | ||||||
|     gameWindow.display(); |     gameWindow.display(); | ||||||
|  |     return; | ||||||
| } | } | ||||||
|  | |||||||
							
								
								
									
										182
									
								
								src/Snake.cpp
									
									
									
									
									
								
							
							
						
						
									
										182
									
								
								src/Snake.cpp
									
									
									
									
									
								
							| @ -2,11 +2,12 @@ | |||||||
| #include <iostream> | #include <iostream> | ||||||
| #include <queue> | #include <queue> | ||||||
| #include <SFML\Graphics.hpp> | #include <SFML\Graphics.hpp> | ||||||
|  | #include "Common.h" | ||||||
| #include "Snake.h" | #include "Snake.h" | ||||||
| #include "SnakeFood.h" | #include "SnakeFood.h" | ||||||
| 
 | 
 | ||||||
| // General constructor for snake class
 | // General constructor for snake class
 | ||||||
| Snake::Snake() | Snake::Snake(void) | ||||||
| { | { | ||||||
|     sf::RectangleShape newBodyPart(sf::Vector2f(kGridSize, kGridSize)); |     sf::RectangleShape newBodyPart(sf::Vector2f(kGridSize, kGridSize)); | ||||||
|     newBodyPart.setFillColor(sf::Color::Green); |     newBodyPart.setFillColor(sf::Color::Green); | ||||||
| @ -23,121 +24,28 @@ Snake::Snake(sf::Vector2f headSize) | |||||||
|     return; |     return; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| // Get a new coordinate position based on snake direction
 | // Iterate through snake deque and draw to window
 | ||||||
| sf::Vector2f CalculateNewPosition(int direction, sf::Vector2f position) | void Snake::DisplaySnake(sf::RenderWindow& window) | ||||||
| { | { | ||||||
|     if (direction == 0) |     for (auto snakeBodyPart = snakeBody.cbegin(); snakeBodyPart != snakeBody.cend(); ++snakeBodyPart) | ||||||
|         return position; |         window.draw(*snakeBodyPart); | ||||||
|     if (direction == 1) |  | ||||||
|         position.x -= kGridSize; |  | ||||||
|     if (direction == 2) |  | ||||||
|         position.y -= kGridSize; |  | ||||||
|     if (direction == 3) |  | ||||||
|         position.y += kGridSize; |  | ||||||
|     if (direction == 4) |  | ||||||
|         position.x += kGridSize; |  | ||||||
|     return position; |  | ||||||
| } |  | ||||||
| 
 |  | ||||||
| // Test for collision between two object positions
 |  | ||||||
| bool GlobalCollision(sf::Vector2f object1Position, sf::Vector2f object2Position) |  | ||||||
| { |  | ||||||
|     if (object1Position.x != object2Position.x) |  | ||||||
|         return 0; |  | ||||||
|     if (object1Position.y != object2Position.y) |  | ||||||
|         return 0; |  | ||||||
| 
 |  | ||||||
|     return 1; |  | ||||||
| } |  | ||||||
| 
 |  | ||||||
| // Check keyboard for new direction of snake
 |  | ||||||
| void Snake::CheckDirection() |  | ||||||
| { |  | ||||||
|     if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left)) |  | ||||||
|         snakeDirection = 1; |  | ||||||
|     if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up)) |  | ||||||
|         snakeDirection = 2; |  | ||||||
|     if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down)) |  | ||||||
|         snakeDirection = 3; |  | ||||||
|     if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right)) |  | ||||||
|         snakeDirection = 4; |  | ||||||
|     return; |     return; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| // Check snake head for running into boundaries
 |  | ||||||
| bool Snake::CheckBoundaries() |  | ||||||
| { |  | ||||||
|     if (snakeBody.front().getPosition().x == 0 && snakeDirection == 1) |  | ||||||
|         return true; |  | ||||||
|     if (snakeBody.front().getPosition().y == 0 && snakeDirection == 2) |  | ||||||
|         return true; |  | ||||||
|     // TODO: Change boundaries to not be hard-coded
 |  | ||||||
|     if (snakeBody.front().getPosition().y > 675 && snakeDirection == 3) |  | ||||||
|         return true; |  | ||||||
|     if (snakeBody.front().getPosition().x > 975 && snakeDirection == 4) |  | ||||||
|         return true; |  | ||||||
|     return false; |  | ||||||
| } |  | ||||||
| 
 |  | ||||||
| // Move snake based on direction and test for eating food
 |  | ||||||
| void Snake::MoveSnake(SnakeFood& snakeFood, sf::VideoMode gameVideoMode) |  | ||||||
| { |  | ||||||
|     // CheckDirection();
 |  | ||||||
|     sf::Vector2f newHeadPosition; |  | ||||||
|     newHeadPosition = GetSnakeHeadPosition(); |  | ||||||
|     if (CheckBoundaries()) |  | ||||||
|         return; |  | ||||||
|     newHeadPosition = CalculateNewPosition(snakeDirection, newHeadPosition); |  | ||||||
|     sf::RectangleShape newBodyPart(sf::Vector2f(kGridSize, kGridSize)); |  | ||||||
|     newBodyPart.setPosition(newHeadPosition); |  | ||||||
|     if (IsSelfCollision(newBodyPart)) // Do nothing if self collision
 |  | ||||||
|     { |  | ||||||
|         return; |  | ||||||
|     } |  | ||||||
|     newBodyPart.setFillColor(sf::Color::Green); |  | ||||||
|     snakeBody.push_front(newBodyPart); |  | ||||||
|     if (!GlobalCollision(GetSnakeHead().getPosition(), snakeFood.GetFoodObject().getPosition())) |  | ||||||
|         snakeBody.pop_back(); |  | ||||||
|     return; |  | ||||||
| } |  | ||||||
| 
 |  | ||||||
| // Return the Vector2f head of snake
 |  | ||||||
| sf::Vector2f Snake::GetSnakeHeadPosition() |  | ||||||
| { |  | ||||||
|     sf::Vector2f position; |  | ||||||
|     position = snakeBody.front().getPosition(); |  | ||||||
|     return position; |  | ||||||
| } |  | ||||||
| 
 |  | ||||||
| // Return the RectangleShape head of snake
 | // Return the RectangleShape head of snake
 | ||||||
| sf::RectangleShape Snake::GetSnakeHead() | sf::RectangleShape Snake::GetSnakeHead(void) | ||||||
| { | { | ||||||
|     sf::RectangleShape head; |     sf::RectangleShape head; | ||||||
|     head = snakeBody.front(); |     head = snakeBody.front(); | ||||||
|     return head; |     return head; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| // Iterate through snake deque and draw to window
 | // Return the Vector2f head of snake
 | ||||||
| void Snake::DisplaySnake(sf::RenderWindow& window) | sf::Vector2f Snake::GetSnakeHeadPosition(void) | ||||||
| { | { | ||||||
|     for (auto it = snakeBody.cbegin(); it != snakeBody.cend(); ++it) |     sf::Vector2f position; | ||||||
|     { |     position = snakeBody.front().getPosition(); | ||||||
|         window.draw(*it); |     return position; | ||||||
|     } |  | ||||||
|     return; |  | ||||||
| } |  | ||||||
| 
 |  | ||||||
| // Test for snake self collision
 |  | ||||||
| bool Snake::IsSelfCollision(sf::RectangleShape testRectangle) |  | ||||||
| { |  | ||||||
|     for (auto it = snakeBody.cbegin(); it != snakeBody.cend(); ++it) |  | ||||||
|     { |  | ||||||
|         if (GlobalCollision(testRectangle.getPosition(), (*it).getPosition())) |  | ||||||
|         { |  | ||||||
|             return true; |  | ||||||
|         } |  | ||||||
|     } |  | ||||||
|     return false; |  | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| // Checks if snake position matches object position
 | // Checks if snake position matches object position
 | ||||||
| @ -153,3 +61,69 @@ bool Snake::IsTouchingObject(sf::RectangleShape object) | |||||||
|     } |     } | ||||||
|     return false; |     return false; | ||||||
| } | } | ||||||
|  | 
 | ||||||
|  | // Move snake based on direction and check for collision
 | ||||||
|  | void Snake::MoveSnake(SnakeFood* snakeFood) | ||||||
|  | { | ||||||
|  |     // TODO: Add losing on wall collision
 | ||||||
|  |     if (CheckBoundaries()) // Wall collision
 | ||||||
|  |         return; | ||||||
|  |     sf::Vector2f newHeadPosition; | ||||||
|  |     newHeadPosition = CalculateNewPosition(GetSnakeHeadPosition()); | ||||||
|  |     sf::RectangleShape newBodyPart(sf::Vector2f(kGridSize, kGridSize)); | ||||||
|  |     newBodyPart.setPosition(newHeadPosition); | ||||||
|  |     // TODO: Add losing on self collision
 | ||||||
|  |     if (IsSelfCollision(newBodyPart)) // Snake collision
 | ||||||
|  |         return; | ||||||
|  |     newBodyPart.setFillColor(sf::Color::Green); | ||||||
|  |     snakeBody.push_front(newBodyPart); | ||||||
|  |     if (!GlobalCollision(GetSnakeHeadPosition(), snakeFood->GetFoodObjectPosition())) | ||||||
|  |         snakeBody.pop_back(); | ||||||
|  |     return; | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | void Snake::UpdateDirection(int newDirection) | ||||||
|  | { | ||||||
|  |     snakeDirection = newDirection; | ||||||
|  |     return; | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | // Get a new coordinate position based on snake direction
 | ||||||
|  | sf::Vector2f Snake::CalculateNewPosition(sf::Vector2f position) | ||||||
|  | { | ||||||
|  |     if (snakeDirection == 0) | ||||||
|  |         return position; | ||||||
|  |     if (snakeDirection == kLeft) | ||||||
|  |         position.x -= kGridSize; | ||||||
|  |     if (snakeDirection == kUp) | ||||||
|  |         position.y -= kGridSize; | ||||||
|  |     if (snakeDirection == kDown) | ||||||
|  |         position.y += kGridSize; | ||||||
|  |     if (snakeDirection == kRight) | ||||||
|  |         position.x += kGridSize; | ||||||
|  |     return position; | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | // Check snake head for running into boundaries
 | ||||||
|  | bool Snake::CheckBoundaries(void) | ||||||
|  | { | ||||||
|  |     if (snakeBody.front().getPosition().x == 0 && snakeDirection == kLeft) | ||||||
|  |         return true; | ||||||
|  |     if (snakeBody.front().getPosition().y == 0 && snakeDirection == kUp) | ||||||
|  |         return true; | ||||||
|  |     // TODO: Change boundaries to not be hard-coded
 | ||||||
|  |     if (snakeBody.front().getPosition().y > 675 && snakeDirection == kDown) | ||||||
|  |         return true; | ||||||
|  |     if (snakeBody.front().getPosition().x > 975 && snakeDirection == kRight) | ||||||
|  |         return true; | ||||||
|  |     return false; | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | // Test for snake self collision
 | ||||||
|  | bool Snake::IsSelfCollision(sf::RectangleShape testRectangle) | ||||||
|  | { | ||||||
|  |     for (auto snakeBodyPart = snakeBody.cbegin(); snakeBodyPart != snakeBody.cend(); ++snakeBodyPart) | ||||||
|  |         if (GlobalCollision(testRectangle.getPosition(), (*snakeBodyPart).getPosition())) | ||||||
|  |             return true; | ||||||
|  |     return false; | ||||||
|  | } | ||||||
|  | |||||||
| @ -1,20 +1,22 @@ | |||||||
| // SnakeFood.cpp
 | // SnakeFood.cpp
 | ||||||
| #include <iostream> | #include <iostream> | ||||||
| #include <SFML\Graphics.hpp> | #include <SFML\Graphics.hpp> | ||||||
|  | #include "Common.h" | ||||||
| #include "SnakeFood.h" | #include "SnakeFood.h" | ||||||
| 
 | 
 | ||||||
| const int kGridSize = 25; |  | ||||||
| 
 | 
 | ||||||
| SnakeFood::SnakeFood() | SnakeFood::SnakeFood() | ||||||
| { | { | ||||||
|     snakeFoodObject.setSize(sf::Vector2f(kGridSize,kGridSize)); |     snakeFoodObject.setSize(sf::Vector2f(kGridSize,  kGridSize)); | ||||||
|     snakeFoodObject.setFillColor(sf::Color::Red); |     snakeFoodObject.setFillColor(sf::Color::Red); | ||||||
|  |     return; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| SnakeFood::SnakeFood(sf::Vector2f snakeFoodSize) | SnakeFood::SnakeFood(sf::Vector2f snakeFoodSize) | ||||||
| { | { | ||||||
|     snakeFoodObject.setSize(snakeFoodSize); |     snakeFoodObject.setSize(snakeFoodSize); | ||||||
|     snakeFoodObject.setFillColor(sf::Color::Red); |     snakeFoodObject.setFillColor(sf::Color::Red); | ||||||
|  |     return; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| // Returns a new food object for the snakeFood
 | // Returns a new food object for the snakeFood
 | ||||||
| @ -24,11 +26,7 @@ void SnakeFood::GenerateNewFood(sf::Vector2f windowSize) | |||||||
|     newPosition.x = GenerateRandomNumber(windowSize.x); |     newPosition.x = GenerateRandomNumber(windowSize.x); | ||||||
|     newPosition.y = GenerateRandomNumber(windowSize.y); |     newPosition.y = GenerateRandomNumber(windowSize.y); | ||||||
|     snakeFoodObject.setPosition(newPosition); |     snakeFoodObject.setPosition(newPosition); | ||||||
|     // if (GlobalCollision(snakeFoodObject.getPosition(), newPosition))
 |     return; | ||||||
|     // {
 |  | ||||||
|     //     std::cout << "Location error: " << newPosition.x << " " << newPosition.y << '\n';
 |  | ||||||
|     //     throw std::runtime_error("Error! New generation on same location");
 |  | ||||||
|     // }
 |  | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| sf::RectangleShape SnakeFood::GetFoodObject(void) | sf::RectangleShape SnakeFood::GetFoodObject(void) | ||||||
| @ -36,6 +34,11 @@ sf::RectangleShape SnakeFood::GetFoodObject(void) | |||||||
|     return snakeFoodObject; |     return snakeFoodObject; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | sf::Vector2f SnakeFood::GetFoodObjectPosition(void) | ||||||
|  | { | ||||||
|  |     return snakeFoodObject.getPosition(); | ||||||
|  | } | ||||||
|  | 
 | ||||||
| // Returns a newly generated number
 | // Returns a newly generated number
 | ||||||
| int SnakeFood::GenerateRandomNumber(int generationLimit) | int SnakeFood::GenerateRandomNumber(int generationLimit) | ||||||
| { | { | ||||||
|  | |||||||
		Loading…
	
		Reference in New Issue
	
	Block a user