From f317dc7a8cf1410d28dc62c3e6e9764411604b59 Mon Sep 17 00:00:00 2001 From: TriantaTV Date: Fri, 17 Mar 2023 17:27:02 -0500 Subject: [PATCH] Many issues restructuring the program, needs cleaning --- .gitignore | 0 Makefile | 48 ++++++++--------- README.md | 42 +++++++-------- include/common.h | 34 ++++++------ include/display.h | 86 ++++++++++++++++------------- include/game.h | 14 +++++ include/gamestate.h | 7 +-- include/snake.h | 2 +- include/snakefood.h | 2 +- src/common.cpp | 20 +++---- src/display.cpp | 82 ++++++++++++++++++++++++++++ src/game.cpp | 2 +- src/gamestate.cpp | 129 ++++++++++++++++++++------------------------ src/main.cpp | 2 +- src/snake.cpp | 4 +- src/snakefood.cpp | 4 +- 16 files changed, 284 insertions(+), 194 deletions(-) mode change 100644 => 100755 .gitignore mode change 100644 => 100755 Makefile mode change 100644 => 100755 README.md mode change 100644 => 100755 include/common.h mode change 100644 => 100755 include/display.h mode change 100644 => 100755 include/game.h mode change 100644 => 100755 include/gamestate.h mode change 100644 => 100755 include/snake.h mode change 100644 => 100755 include/snakefood.h mode change 100644 => 100755 src/common.cpp mode change 100644 => 100755 src/display.cpp mode change 100644 => 100755 src/game.cpp mode change 100644 => 100755 src/gamestate.cpp mode change 100644 => 100755 src/main.cpp mode change 100644 => 100755 src/snake.cpp mode change 100644 => 100755 src/snakefood.cpp diff --git a/.gitignore b/.gitignore old mode 100644 new mode 100755 diff --git a/Makefile b/Makefile old mode 100644 new mode 100755 index ca5a19b..cff45ec --- a/Makefile +++ b/Makefile @@ -1,25 +1,25 @@ -INC := -I include -STD := -std=c++11 -SFML := -lsfml-graphics -lsfml-window -lsfml-system - -all: compile link - -fresh: dirs compile link - -compile: - 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/display.o src/display.cpp - g++ $(INC) $(STD) -c -o build/game.o src/game.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/snakefood.o src/snakefood.cpp - -dirs: - mkdir bin build - -link: - g++ build/*.o -o bin/SnakePlusPlus $(SFML) - -clean: +INC := -I include +STD := -std=c++11 +SFML := -lsfml-graphics -lsfml-window -lsfml-system + +all: compile link + +fresh: dirs compile link + +compile: + 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/display.o src/display.cpp + g++ $(INC) $(STD) -c -o build/game.o src/game.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/snakefood.o src/snakefood.cpp + +dirs: + mkdir bin build + +link: + g++ build/*.o -o bin/SnakePlusPlus $(SFML) + +clean: rm bin/*.o build/*.out \ No newline at end of file diff --git a/README.md b/README.md old mode 100644 new mode 100755 index ee5777f..cf82603 --- a/README.md +++ b/README.md @@ -1,21 +1,21 @@ -# SnakePlusPlus -A version of Snake made in C++ and using SFML - -## Controls ------------ - - Use the arrow keys to move the Snake - - Use the escape key to close the game - -## Compiling the game ------------------- - -Prerequisites - - C++11 - - [SFML](https://github.com/SFML/SFML) - -Clone the repository and compile it using: - - git clone https://github.com/TriantaTV/snakeplusplus.git - make - -The game is output into the `bin` folder, simply run the game and enjoy! +# SnakePlusPlus +A version of Snake made in C++ and using SFML + +## Controls +----------- + - Use the arrow keys to move the Snake + - Use the escape key to close the game + +## Compiling the game +------------------ + +Prerequisites + - C++11 + - [SFML](https://github.com/SFML/SFML) + +Clone the repository and compile it using: + + git clone https://github.com/TriantaTV/snakeplusplus.git + make + +The game is output into the `bin` folder, simply run the game and enjoy! diff --git a/include/common.h b/include/common.h old mode 100644 new mode 100755 index 22c2353..62b9dbb --- a/include/common.h +++ b/include/common.h @@ -1,18 +1,16 @@ -#ifndef COMMON_H -#define COMMON_H - -#include - -const int kGridSize = 25; - -enum PlayerDirection -{ - kLeft = 1, - kUp = 2, - kDown = 3, - kRight = 4 -}; - -bool GlobalCollision(sf::Vector2f object1Position, sf::Vector2f object2Position); - -#endif \ No newline at end of file +#ifndef COMMON_H +#define COMMON_H + +#include + +enum PlayerDirection +{ + kLeft = 1, + kUp = 2, + kDown = 3, + kRight = 4 +}; + +bool GlobalCollision(sf::Vector2f object1Position, sf::Vector2f object2Position); + +#endif diff --git a/include/display.h b/include/display.h old mode 100644 new mode 100755 index ff43170..c90ff04 --- a/include/display.h +++ b/include/display.h @@ -1,36 +1,50 @@ -#ifndef DISPLAY_H -#define DISPLAY_H - -class DisplayInterface -{ -public: - DisplayInterface(void); -protected: - virtual void DisplayGameState(void) = 0; -private: - ; -}; - -class CommandLine : public DisplayInterface -{ -public: - CommandLine(void) = default; - void DisplayGameState(void); -protected: - ; -private: - ; -}; - -class GameWindow : public DisplayInterface -{ -public: - GameWindow(void) = default; - void DisplayGameState(void); -protected: - ; -private: - ; -}; - -#endif \ No newline at end of file +#ifndef DISPLAY_H +#define DISPLAY_H + +#include + +class DisplayInterface +{ +public: + sf::Vector2f gameBoundaries; + DisplayInterface(void); + bool IsOpen(void); +protected: + bool isGameStillRunning; + virtual void DisplayGameState(void) = 0; + virtual void DisplayEndScreen(void) = 0; + virtual void StartGame(void) = 0; +private: + ; +}; + +class CommandLine : public DisplayInterface +{ +public: + CommandLine(void); + void DisplayGameState(void); + void DisplayEndScreen(void); + void StartGame(void); +protected: + ; +private: + const int kGridSize = 25; +}; + +class SFML : public DisplayInterface +{ +public: + SFML(void); + void DisplayGameState(void); + void DisplayEndScreen(void); + void StartGame(void); + void UpdateResolution(sf::Vector2i newResolution); +protected: + ; +private: + sf::Time delay; + sf::RenderWindow gameWindow; + sf::VideoMode gameVideoSettings; +}; + +#endif diff --git a/include/game.h b/include/game.h old mode 100644 new mode 100755 index e69de29..fd17d79 --- a/include/game.h +++ b/include/game.h @@ -0,0 +1,14 @@ +#ifndef GAME_H +#define GAME_H + +class Game +{ +public: + ; +protected: + ; +private: + ; +}; + +#endif \ No newline at end of file diff --git a/include/gamestate.h b/include/gamestate.h old mode 100644 new mode 100755 index bf072c8..7551bda --- a/include/gamestate.h +++ b/include/gamestate.h @@ -3,7 +3,7 @@ #define GAMESTATE_H #include -#include +#include #include "snake.h" #include "display.h" @@ -13,22 +13,19 @@ public: std::vector< std::vector > gameBoard; bool useSFML = 1; GameState(); - GameState(int newHorizontal, int newVertical); void StartGame(void); sf::Vector2f GetGameBoundaries(void); protected: ; private: std::unique_ptr graphics; - sf::RenderWindow gameWindow; - sf::VideoMode gameVideoSettings; SnakeFood playerFood; Snake player; - sf::Time delay; void DisplayEndScreen(void); void GetKeyboardInput(void); bool PlayerWantsToContinue(void); void RegenerateFood(void); + void ResetGameBoard(void); void RunGameLoop(void); void RenderWindow(void); }; diff --git a/include/snake.h b/include/snake.h old mode 100644 new mode 100755 index e510292..b42a913 --- a/include/snake.h +++ b/include/snake.h @@ -3,7 +3,7 @@ #define SNAKE_H #include -#include +#include #include "snakefood.h" diff --git a/include/snakefood.h b/include/snakefood.h old mode 100644 new mode 100755 index 06b21cb..adf7dc3 --- a/include/snakefood.h +++ b/include/snakefood.h @@ -2,7 +2,7 @@ #ifndef SNAKEFOOD_H #define SNAKEFOOD_H -#include +#include #include class SnakeFood diff --git a/src/common.cpp b/src/common.cpp old mode 100644 new mode 100755 index e66adcf..f163cbc --- a/src/common.cpp +++ b/src/common.cpp @@ -1,11 +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; +#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; } \ No newline at end of file diff --git a/src/display.cpp b/src/display.cpp old mode 100644 new mode 100755 index e69de29..d613eb7 --- a/src/display.cpp +++ b/src/display.cpp @@ -0,0 +1,82 @@ +#include "common.h" +#include "display.h" +//#include + +DisplayInterface::DisplayInterface(void) +{ + ; +} + +bool DisplayInterface::IsOpen(void) +{ + return isGameStillRunning; +} + +CommandLine::CommandLine(void) +{ + gameBoundaries.x = 1025 / kGridSize; + gameBoundaries.y = 725 / kGridSize; + return; +} + +// TODO: Use cout for printing game to screen +void CommandLine::DisplayGameState(void) +{ + ; +} + +void CommandLine::StartGame(void) +{ + isGameStillRunning = true; + return; +} + +// TODO: Setup making window +SFML::SFML(void) +{ + delay = sf::milliseconds(75); + gameVideoSettings = sf::VideoMode(1025, 725); + return; +} + +void SFML::DisplayGameState(void) +{ + return; +} + +void SFML::DisplayEndScreen(void) +{ + gameWindow.clear(); + sf::Vector2f textPosition(gameBoundaries); + textPosition.x = textPosition.x / 2; + textPosition.y = textPosition.y / 2; + sf::Text gameOverText; + gameOverText.setString("Game Over"); + gameOverText.setCharacterSize(30); + gameOverText.setPosition(textPosition); + gameWindow.draw(gameOverText); + gameWindow.display(); + // if (!PlayerWantsToContinue()) + // return; + // player = Snake(); + // playerFood.GenerateNewFood(GetGameBoundaries()); + // gameWindow.clear(); + return; +} + +void SFML::StartGame(void) +{ + gameWindow.create(gameVideoSettings, "SnakePlusPlus"); + isGameStillRunning = true; + return; +} + +void SFML::UpdateResolution(sf::Vector2i newResolution) +{ + gameVideoSettings.width = newResolution.x; + gameVideoSettings.height = newResolution.y; + gameBoundaries.x = gameVideoSettings.width / kGridSize; + gameBoundaries.y = gameVideoSettings.height / kGridSize; + gameWindow.create(gameVideoSettings, "SnakePlusPlus"); + return; +} diff --git a/src/game.cpp b/src/game.cpp old mode 100644 new mode 100755 index b529604..7bb61ba --- a/src/game.cpp +++ b/src/game.cpp @@ -1 +1 @@ -#include "game.cpp" \ No newline at end of file +#include "game.h" diff --git a/src/gamestate.cpp b/src/gamestate.cpp old mode 100644 new mode 100755 index a9f1893..e818d6f --- a/src/gamestate.cpp +++ b/src/gamestate.cpp @@ -1,101 +1,75 @@ // GameState.cpp -#include -#include +#include #include "common.h" +#include "display.h" #include "gamestate.h" GameState::GameState() { - delay = sf::milliseconds(75); - gameVideoSettings = sf::VideoMode(1025, 725); - gameWindow.create(gameVideoSettings, "SnakePlusPlus"); if (useSFML) - graphics.reset(new GameWindow()); + graphics.reset(new SFML()); else graphics.reset(new CommandLine()); - - return; -} - -GameState::GameState(int maxHorizontal, int maxVertical) -{ - delay = sf::milliseconds(75); - gameVideoSettings = sf::VideoMode(maxHorizontal, maxVertical); - gameWindow.create(gameVideoSettings, "SnakePlusPlus"); return; } void GameState::StartGame() { - gameWindow.create(gameVideoSettings, "SnakePlusPlus"); + ResetGameBoard(); RunGameLoop(); return; } +// TODO: Reimplement for DisplayInterface void GameState::DisplayEndScreen(void) { - gameWindow.clear(); - sf::Vector2f textPosition(GetGameBoundaries()); - textPosition.x = textPosition.x / 2; - textPosition.y = textPosition.y / 2; - sf::Text gameOverText; - gameOverText.setString("Game Over"); - gameOverText.setCharacterSize(30); - gameOverText.setPosition(textPosition); - gameWindow.draw(gameOverText); - gameWindow.display(); - if (!PlayerWantsToContinue()) - return; - player = Snake(); - playerFood.GenerateNewFood(GetGameBoundaries()); - gameWindow.clear(); + // graphics->DisplayEndScreen(); return; } sf::Vector2f GameState::GetGameBoundaries(void) { - sf::Vector2f boundaries; - boundaries.x = gameVideoSettings.width; - boundaries.y = gameVideoSettings.height; - return boundaries; + return graphics->gameBoundaries; } +// TODO: Reimplement for DisplayInterface 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); + // sf::Event event; + // while (gameWindow.pollEvent(event)) + // { + // if ((event.type == SFML: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; } +// TODO: Reimplement for DisplayInterface bool GameState::PlayerWantsToContinue(void) { - sf::Event event; - while (true) - { - while (gameWindow.pollEvent(event)) - { - if ((event.type == sf::Event::Closed) || - (sf::Keyboard::isKeyPressed(sf::Keyboard::Escape))) - gameWindow.close(); - return false; - } - if (sf::Keyboard::isKeyPressed(sf::Keyboard::Enter)) - return true; - sf::sleep(delay); - } + // sf::Event event; + // while (true) + // { + // while (gameWindow.pollEvent(event)) + // { + // if ((event.type == sf::Event::Closed) || + // (sf::Keyboard::isKeyPressed(sf::Keyboard::Escape))) + // gameWindow.close(); + // return false; + // } + // if (sf::Keyboard::isKeyPressed(sf::Keyboard::Enter)) + // return true; + // sf::sleep(delay); + // } } // Generates new food until not colliding with player @@ -107,26 +81,37 @@ void GameState::RegenerateFood(void) return; } +void GameState::ResetGameBoard(void) +{ + gameBoard.clear(); + sf::Vector2f boardDimensions = GetGameBoundaries(); + gameBoard.resize(boardDimensions.y); + for (int i = 0; i < boardDimensions.y; i++) + for (int j = 0; j < boardDimensions.x; j++) + gameBoard[i].push_back(' '); + return; +} + void GameState::RunGameLoop(void) { - while (gameWindow.isOpen()) + while (graphics->IsOpen()) { GetKeyboardInput(); player.MoveSnake(&playerFood); RegenerateFood(); RenderWindow(); - sf::sleep(delay); } return; } +// TODO: Reimplement for DisplayInterface void GameState::RenderWindow(void) { - gameWindow.clear(); - player.DisplaySnake(gameWindow); - gameWindow.draw(playerFood.GetFoodObject()); - gameWindow.display(); - if (player.gameFinished) - DisplayEndScreen(); + // gameWindow.clear(); + // player.DisplaySnake(gameWindow); + // gameWindow.draw(playerFood.GetFoodObject()); + // gameWindow.display(); + // if (player.gameFinished) + // DisplayEndScreen(); return; } diff --git a/src/main.cpp b/src/main.cpp old mode 100644 new mode 100755 index 7a1a501..354ebd1 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,4 +1,4 @@ -#include "GameState.h" +#include "gamestate.h" int main() { diff --git a/src/snake.cpp b/src/snake.cpp old mode 100644 new mode 100755 index 4466d88..a65e343 --- a/src/snake.cpp +++ b/src/snake.cpp @@ -1,10 +1,10 @@ // Snake.cpp #include #include -#include +#include #include "common.h" #include "snake.h" -#include "snakeFood.h" +#include "snakefood.h" // General constructor for snake class Snake::Snake(void) diff --git a/src/snakefood.cpp b/src/snakefood.cpp old mode 100644 new mode 100755 index 1f71fb4..55ecbbf --- a/src/snakefood.cpp +++ b/src/snakefood.cpp @@ -1,8 +1,8 @@ // SnakeFood.cpp #include -#include +#include #include "common.h" -#include "snakeFood.h" +#include "snakefood.h" SnakeFood::SnakeFood()