From 2c4537eda7393936f6acad3d1ef111d832509f79 Mon Sep 17 00:00:00 2001 From: TriantaTV <56975502+TriantaTV@users.noreply.github.com> Date: Sun, 21 Aug 2022 02:51:33 -0500 Subject: [PATCH] Fixed generation issue with snakeFood Fixed generation issue with snakeFood and game should be fully playable without an official end screen implemented. So when the snake gets stuck or the game finishes, there is nothing to close the game automatically. --- src/GameState.cpp | 1 - src/GeneralFunctions.cpp | 41 ---------------------------------------- src/GeneralFunctions.h | 10 ---------- src/Snake.cpp | 29 +++++++++++++++++++++++++++- src/Snake.h | 4 ++-- src/SnakeFood.cpp | 12 +++++++++++- src/SnakeFood.h | 3 +++ 7 files changed, 44 insertions(+), 56 deletions(-) delete mode 100644 src/GeneralFunctions.cpp delete mode 100644 src/GeneralFunctions.h diff --git a/src/GameState.cpp b/src/GameState.cpp index 4e9fc16..f9a89a1 100644 --- a/src/GameState.cpp +++ b/src/GameState.cpp @@ -2,7 +2,6 @@ #include #include #include "Snake.h" -#include "SnakeFood.h" #include "GameState.h" GameState::GameState() diff --git a/src/GeneralFunctions.cpp b/src/GeneralFunctions.cpp deleted file mode 100644 index 0c9dd1a..0000000 --- a/src/GeneralFunctions.cpp +++ /dev/null @@ -1,41 +0,0 @@ -#include -#include -#include "GeneralFunctions.h" - - -// Get a new coordinate position based on snake direction -sf::Vector2f CalculateNewPosition(int direction, sf::Vector2f position) -{ - if (direction == 0) - return position; - if (direction == 1) - position.x -= 25; - if (direction == 2) - position.y -= 25; - if (direction == 3) - position.y += 25; - if (direction == 4) - position.x += 25; - 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; -} - -int GenerateRandomNumber(int generationLimit) -{ - int generatedNumber; - std::default_random_engine generator; - std::uniform_int_distribution<> distribution(0, generationLimit); - generatedNumber = distribution(generator); - generatedNumber -= (generatedNumber % 25); - return generatedNumber; -} \ No newline at end of file diff --git a/src/GeneralFunctions.h b/src/GeneralFunctions.h deleted file mode 100644 index 404ceb6..0000000 --- a/src/GeneralFunctions.h +++ /dev/null @@ -1,10 +0,0 @@ -#ifndef GENERALFUNCTIONS_H -#define GENERALFUNCTIONS_H -#include - -sf::Vector2f CalculateNewPosition(int direction, sf::Vector2f position); -bool GlobalCollision(sf::Vector2f object1Position, sf::Vector2f object2Position); -int GenerateRandomNumber(int generationLimit); - - -#endif \ No newline at end of file diff --git a/src/Snake.cpp b/src/Snake.cpp index b93a9c1..d1216cf 100644 --- a/src/Snake.cpp +++ b/src/Snake.cpp @@ -1,10 +1,37 @@ // Snake.cpp #include +#include #include -#include "GeneralFunctions.h" #include "Snake.h" #include "SnakeFood.h" +// Get a new coordinate position based on snake direction +sf::Vector2f CalculateNewPosition(int direction, sf::Vector2f position) +{ + if (direction == 0) + return position; + if (direction == 1) + position.x -= 25; + if (direction == 2) + position.y -= 25; + if (direction == 3) + position.y += 25; + if (direction == 4) + position.x += 25; + 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() { diff --git a/src/Snake.h b/src/Snake.h index 0752cbb..b7f7a70 100644 --- a/src/Snake.h +++ b/src/Snake.h @@ -1,9 +1,9 @@ // Snake.h #ifndef SNAKE_H #define SNAKE_H -#include -#include "SnakeFood.h" +sf::Vector2f CalculateNewPosition(int direction, sf::Vector2f position); +bool GlobalCollision(sf::Vector2f object1Position, sf::Vector2f object2Position); class Snake { diff --git a/src/SnakeFood.cpp b/src/SnakeFood.cpp index f14d188..4a1dd0f 100644 --- a/src/SnakeFood.cpp +++ b/src/SnakeFood.cpp @@ -1,8 +1,8 @@ // SnakeFood.cpp #include #include -#include "GeneralFunctions.h" #include "SnakeFood.h" +#include "Snake.h" SnakeFood::SnakeFood() { @@ -29,3 +29,13 @@ void SnakeFood::GenerateNewLocation(int horizontalLocation, int verticalLocation snakeFoodObject.setPosition(newPosition); return; } + + +int SnakeFood::GenerateRandomNumber(int generationLimit) +{ + int generatedNumber; + std::uniform_int_distribution<> distribution(0, generationLimit); + generatedNumber = distribution(generator); + generatedNumber -= (generatedNumber % 25); + return generatedNumber; +} diff --git a/src/SnakeFood.h b/src/SnakeFood.h index d39f52f..67f603a 100644 --- a/src/SnakeFood.h +++ b/src/SnakeFood.h @@ -1,6 +1,7 @@ // SnakeFood.h #ifndef SNAKEFOOD_H #define SNAKEFOOD_H +#include #include class SnakeFood @@ -8,9 +9,11 @@ class SnakeFood private: public: sf::RectangleShape snakeFoodObject; + std::default_random_engine generator; SnakeFood(); SnakeFood(sf::Vector2f snakeFoodSize); void GenerateNewLocation(int horizontalLocation, int verticalLocation); + int GenerateRandomNumber(int generationLimit); }; #endif