From c3342a14c7be9144f8647cd6af1cc08d00ad49b9 Mon Sep 17 00:00:00 2001 From: TriantaTV <56975502+TriantaTV@users.noreply.github.com> Date: Tue, 2 Aug 2022 21:58:58 -0500 Subject: [PATCH] Moved snakeFood to be a separate class Moved snakeFood to be a separate class and a temporary unused function was added for randomly generating the new location. --- src/GameState.cpp | 14 ++++++++------ src/SnakeFood.cpp | 24 ++++++++++++++++++++++++ src/SnakeFood.h | 16 ++++++++++++++++ 3 files changed, 48 insertions(+), 6 deletions(-) create mode 100644 src/SnakeFood.cpp create mode 100644 src/SnakeFood.h diff --git a/src/GameState.cpp b/src/GameState.cpp index 2aef0b8..c293cf6 100644 --- a/src/GameState.cpp +++ b/src/GameState.cpp @@ -1,7 +1,8 @@ -#include +// #include #include #include #include "Snake.h" +#include "SnakeFood.h" #include "GameState.h" GameState::GameState() @@ -25,8 +26,9 @@ void GameState::startNewGame() gameWindow.create(gameVideoMode, "SnakePlusPlus"); sf::Time delay = sf::milliseconds(25); int snakeDirection = 0; - Snake Player(sf::Vector2f(25,25)); - sf::RectangleShape snakeHead(sf::Vector2f(25,25)); + Snake player(sf::Vector2f(25,25)); + SnakeFood playerFood(sf::Vector2f(25,25)); + // sf::RectangleShape snakeHead(sf::Vector2f(25,25)); sf::RectangleShape snakeFood(sf::Vector2f(25,25)); snakeFood.setFillColor(sf::Color::Red); snakeFood.setPosition(25,25); @@ -47,10 +49,10 @@ void GameState::startNewGame() snakeDirection = 3; if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right)) snakeDirection = 4; - Player.MoveSnake(snakeFood); + player.MoveSnake(playerFood.snakeFoodObject); gameWindow.clear(); - gameWindow.draw(snakeFood); - Player.DisplaySnake(gameWindow); + player.DisplaySnake(gameWindow); + gameWindow.draw(playerFood.snakeFoodObject); gameWindow.display(); sf::sleep(delay); } diff --git a/src/SnakeFood.cpp b/src/SnakeFood.cpp new file mode 100644 index 0000000..464ea25 --- /dev/null +++ b/src/SnakeFood.cpp @@ -0,0 +1,24 @@ +#include +#include "SnakeFood.h" + +SnakeFood::SnakeFood() +{ + snakeFoodObject.setSize(sf::Vector2f(25,25)); + snakeFoodObject.setFillColor(sf::Color::Red); +} + +SnakeFood::SnakeFood(sf::Vector2f snakeFoodSize) +{ + snakeFoodObject.setSize(snakeFoodSize); + snakeFoodObject.setFillColor(sf::Color::Red); +} + +void SnakeFood::GenerateNewLocation(int maxLocation) +{ + sf::Vector2f newPosition; + std::default_random_engine generator; + std::uniform_int_distribution distribution(0, maxLocation); + newPosition.x = distribution(generator); + newPosition.y = distribution(generator); + snakeFoodObject.setPosition(newPosition); +} diff --git a/src/SnakeFood.h b/src/SnakeFood.h new file mode 100644 index 0000000..4c25630 --- /dev/null +++ b/src/SnakeFood.h @@ -0,0 +1,16 @@ +// SnakeFood.h +#ifndef SNAKEFOOD_H +#define SNAKEFOOD_H +#include + +class SnakeFood +{ +private: +public: + sf::RectangleShape snakeFoodObject; + SnakeFood(); + SnakeFood(sf::Vector2f snakeFoodSize); + void GenerateNewLocation(int maxLocation); +}; + +#endif