From 88902fed6ae2d85d1949a2455d8b2dbe32bc06c1 Mon Sep 17 00:00:00 2001 From: TriantaTV <56975502+TriantaTV@users.noreply.github.com> Date: Wed, 17 Aug 2022 00:41:29 -0500 Subject: [PATCH] Moved general functions to a general file Moved general functions to a general file and added a statement to catch if the newly generated location is the same. The snake food is not currently generating to a new random location like expected and is broken. --- src/GameState.cpp | 2 +- src/GeneralFunctions.cpp | 41 ++++++++++++++++++++++++++++++++++++++++ src/GeneralFunctions.h | 10 ++++++++++ src/Snake.cpp | 37 +++--------------------------------- src/Snake.h | 3 --- src/SnakeFood.cpp | 20 ++++++++++---------- 6 files changed, 65 insertions(+), 48 deletions(-) create mode 100644 src/GeneralFunctions.cpp create mode 100644 src/GeneralFunctions.h diff --git a/src/GameState.cpp b/src/GameState.cpp index 4700de2..4e9fc16 100644 --- a/src/GameState.cpp +++ b/src/GameState.cpp @@ -24,7 +24,7 @@ GameState::GameState(int newHorizontal, int newVertical) void GameState::startNewGame() { gameWindow.create(gameVideoMode, "SnakePlusPlus"); - sf::Time delay = sf::milliseconds(50); + sf::Time delay = sf::milliseconds(100); int snakeDirection = 0; Snake player(sf::Vector2f(25,25)); SnakeFood playerFood(sf::Vector2f(25,25)); diff --git a/src/GeneralFunctions.cpp b/src/GeneralFunctions.cpp new file mode 100644 index 0000000..0c9dd1a --- /dev/null +++ b/src/GeneralFunctions.cpp @@ -0,0 +1,41 @@ +#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 new file mode 100644 index 0000000..404ceb6 --- /dev/null +++ b/src/GeneralFunctions.h @@ -0,0 +1,10 @@ +#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 1c9603f..b93a9c1 100644 --- a/src/Snake.cpp +++ b/src/Snake.cpp @@ -1,25 +1,10 @@ // Snake.cpp #include #include +#include "GeneralFunctions.h" #include "Snake.h" #include "SnakeFood.h" -// Test for collision between two objects -bool SnakeCollision(sf::RectangleShape object1, sf::RectangleShape object2) -{ - // Hack for getting a temporary collision - // Collision only tested for origin corrordinate - sf::Vector2f object1Position = object1.getPosition(); - sf::Vector2f object2Position = object2.getPosition(); - 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() { @@ -49,22 +34,6 @@ bool Snake::CheckBoundaries() return false; } -// 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; -} - // Move snake based on direction and test for eating food void Snake::MoveSnake(SnakeFood& snakeFood, sf::VideoMode gameVideoMode) { @@ -82,7 +51,7 @@ void Snake::MoveSnake(SnakeFood& snakeFood, sf::VideoMode gameVideoMode) } newBodyPart.setFillColor(sf::Color::Green); snakeBody.push_front(newBodyPart); - if (!SnakeCollision(GetSnakeHead(), snakeFood.snakeFoodObject)) + if (!GlobalCollision(GetSnakeHead().getPosition(), snakeFood.snakeFoodObject.getPosition())) snakeBody.pop_back(); SnakeFoodCollision(snakeFood, gameVideoMode); return; @@ -119,7 +88,7 @@ bool Snake::IsSelfCollision(sf::RectangleShape testRectangle) { for (auto it = snakeBody.cbegin(); it != snakeBody.cend(); ++it) { - if (SnakeCollision(testRectangle, *it)) + if (GlobalCollision(testRectangle.getPosition(), (*it).getPosition())) { return true; } diff --git a/src/Snake.h b/src/Snake.h index b6575e5..0752cbb 100644 --- a/src/Snake.h +++ b/src/Snake.h @@ -4,9 +4,6 @@ #include #include "SnakeFood.h" -bool SnakeCollision(sf::RectangleShape object1, sf::RectangleShape object2); -int SnakeMovement(); -sf::Vector2f CalculateNewPosition(int direction, sf::Vector2f position); class Snake { diff --git a/src/SnakeFood.cpp b/src/SnakeFood.cpp index 9ae847f..f14d188 100644 --- a/src/SnakeFood.cpp +++ b/src/SnakeFood.cpp @@ -1,5 +1,7 @@ // SnakeFood.cpp -#include +#include +#include +#include "GeneralFunctions.h" #include "SnakeFood.h" SnakeFood::SnakeFood() @@ -17,15 +19,13 @@ SnakeFood::SnakeFood(sf::Vector2f snakeFoodSize) void SnakeFood::GenerateNewLocation(int horizontalLocation, int verticalLocation) { sf::Vector2f newPosition; - std::default_random_engine generator(time(NULL)); - std::uniform_int_distribution distributionX(0, horizontalLocation); - std::uniform_int_distribution distributionY(0, verticalLocation); - int newX = distributionX(generator); - int newY = distributionY(generator); - newX = newX - (newX % 25) - 25; - newY = newY - (newY % 25) - 25; - newPosition.x = newX; - newPosition.y = newY; + newPosition.x = GenerateRandomNumber(horizontalLocation); + newPosition.y = GenerateRandomNumber(verticalLocation); + 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); return; }