Many issues restructuring the program, needs cleaning

This commit is contained in:
TriantaTV 2023-03-17 17:27:02 -05:00
parent a76d9ccfb5
commit f317dc7a8c
16 changed files with 284 additions and 194 deletions

0
.gitignore vendored Normal file → Executable file
View File

48
Makefile Normal file → Executable file
View File

@ -1,25 +1,25 @@
INC := -I include INC := -I include
STD := -std=c++11 STD := -std=c++11
SFML := -lsfml-graphics -lsfml-window -lsfml-system SFML := -lsfml-graphics -lsfml-window -lsfml-system
all: compile link all: compile link
fresh: dirs compile link fresh: dirs compile link
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/common.o src/common.cpp
g++ $(INC) $(STD) -c -o build/display.o src/display.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/game.o src/game.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: dirs:
mkdir bin build mkdir bin build
link: link:
g++ build/*.o -o bin/SnakePlusPlus $(SFML) g++ build/*.o -o bin/SnakePlusPlus $(SFML)
clean: clean:
rm bin/*.o build/*.out rm bin/*.o build/*.out

42
README.md Normal file → Executable file
View File

@ -1,21 +1,21 @@
# SnakePlusPlus # SnakePlusPlus
A version of Snake made in C++ and using SFML A version of Snake made in C++ and using SFML
## Controls ## Controls
----------- -----------
- Use the arrow keys to move the Snake - Use the arrow keys to move the Snake
- Use the escape key to close the game - Use the escape key to close the game
## Compiling the game ## Compiling the game
------------------ ------------------
Prerequisites Prerequisites
- C++11 - C++11
- [SFML](https://github.com/SFML/SFML) - [SFML](https://github.com/SFML/SFML)
Clone the repository and compile it using: Clone the repository and compile it using:
git clone https://github.com/TriantaTV/snakeplusplus.git git clone https://github.com/TriantaTV/snakeplusplus.git
make make
The game is output into the `bin` folder, simply run the game and enjoy! The game is output into the `bin` folder, simply run the game and enjoy!

34
include/common.h Normal file → Executable file
View File

@ -1,18 +1,16 @@
#ifndef COMMON_H #ifndef COMMON_H
#define COMMON_H #define COMMON_H
#include <SFML\Graphics.hpp> #include <SFML/Graphics.hpp>
const int kGridSize = 25; enum PlayerDirection
{
enum PlayerDirection kLeft = 1,
{ kUp = 2,
kLeft = 1, kDown = 3,
kUp = 2, kRight = 4
kDown = 3, };
kRight = 4
}; bool GlobalCollision(sf::Vector2f object1Position, sf::Vector2f object2Position);
bool GlobalCollision(sf::Vector2f object1Position, sf::Vector2f object2Position); #endif
#endif

86
include/display.h Normal file → Executable file
View File

@ -1,36 +1,50 @@
#ifndef DISPLAY_H #ifndef DISPLAY_H
#define DISPLAY_H #define DISPLAY_H
class DisplayInterface #include <SFML/Graphics.hpp>
{
public: class DisplayInterface
DisplayInterface(void); {
protected: public:
virtual void DisplayGameState(void) = 0; sf::Vector2f gameBoundaries;
private: DisplayInterface(void);
; bool IsOpen(void);
}; protected:
bool isGameStillRunning;
class CommandLine : public DisplayInterface virtual void DisplayGameState(void) = 0;
{ virtual void DisplayEndScreen(void) = 0;
public: virtual void StartGame(void) = 0;
CommandLine(void) = default; private:
void DisplayGameState(void); ;
protected: };
;
private: class CommandLine : public DisplayInterface
; {
}; public:
CommandLine(void);
class GameWindow : public DisplayInterface void DisplayGameState(void);
{ void DisplayEndScreen(void);
public: void StartGame(void);
GameWindow(void) = default; protected:
void DisplayGameState(void); ;
protected: private:
; const int kGridSize = 25;
private: };
;
}; class SFML : public DisplayInterface
{
#endif 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

14
include/game.h Normal file → Executable file
View File

@ -0,0 +1,14 @@
#ifndef GAME_H
#define GAME_H
class Game
{
public:
;
protected:
;
private:
;
};
#endif

7
include/gamestate.h Normal file → Executable file
View File

@ -3,7 +3,7 @@
#define GAMESTATE_H #define GAMESTATE_H
#include <memory> #include <memory>
#include <SFML\Graphics.hpp> #include <SFML/Graphics.hpp>
#include "snake.h" #include "snake.h"
#include "display.h" #include "display.h"
@ -13,22 +13,19 @@ public:
std::vector< std::vector<char> > gameBoard; std::vector< std::vector<char> > gameBoard;
bool useSFML = 1; bool useSFML = 1;
GameState(); GameState();
GameState(int newHorizontal, int newVertical);
void StartGame(void); void StartGame(void);
sf::Vector2f GetGameBoundaries(void); sf::Vector2f GetGameBoundaries(void);
protected: protected:
; ;
private: private:
std::unique_ptr<DisplayInterface> graphics; std::unique_ptr<DisplayInterface> graphics;
sf::RenderWindow gameWindow;
sf::VideoMode gameVideoSettings;
SnakeFood playerFood; SnakeFood playerFood;
Snake player; Snake player;
sf::Time delay;
void DisplayEndScreen(void); void DisplayEndScreen(void);
void GetKeyboardInput(void); void GetKeyboardInput(void);
bool PlayerWantsToContinue(void); bool PlayerWantsToContinue(void);
void RegenerateFood(void); void RegenerateFood(void);
void ResetGameBoard(void);
void RunGameLoop(void); void RunGameLoop(void);
void RenderWindow(void); void RenderWindow(void);
}; };

2
include/snake.h Normal file → Executable file
View File

@ -3,7 +3,7 @@
#define SNAKE_H #define SNAKE_H
#include <deque> #include <deque>
#include <SFML\Graphics.hpp> #include <SFML/Graphics.hpp>
#include "snakefood.h" #include "snakefood.h"

2
include/snakefood.h Normal file → Executable file
View File

@ -2,7 +2,7 @@
#ifndef SNAKEFOOD_H #ifndef SNAKEFOOD_H
#define SNAKEFOOD_H #define SNAKEFOOD_H
#include <SFML\Graphics.hpp> #include <SFML/Graphics.hpp>
#include <random> #include <random>
class SnakeFood class SnakeFood

20
src/common.cpp Normal file → Executable file
View File

@ -1,11 +1,11 @@
#include "common.h" #include "common.h"
// Test for collision between two object positions // Test for collision between two object positions
bool GlobalCollision(sf::Vector2f object1Position, sf::Vector2f object2Position) bool GlobalCollision(sf::Vector2f object1Position, sf::Vector2f object2Position)
{ {
if (object1Position.x != object2Position.x) if (object1Position.x != object2Position.x)
return false; return false;
if (object1Position.y != object2Position.y) if (object1Position.y != object2Position.y)
return false; return false;
return true; return true;
} }

82
src/display.cpp Normal file → Executable file
View File

@ -0,0 +1,82 @@
#include "common.h"
#include "display.h"
//#include <SFML/System.hpp>
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;
}

2
src/game.cpp Normal file → Executable file
View File

@ -1 +1 @@
#include "game.cpp" #include "game.h"

129
src/gamestate.cpp Normal file → Executable file
View File

@ -1,101 +1,75 @@
// GameState.cpp // GameState.cpp
#include <SFML\Graphics.hpp> #include <SFML/Graphics.hpp>
#include <SFML\System.hpp>
#include "common.h" #include "common.h"
#include "display.h"
#include "gamestate.h" #include "gamestate.h"
GameState::GameState() GameState::GameState()
{ {
delay = sf::milliseconds(75);
gameVideoSettings = sf::VideoMode(1025, 725);
gameWindow.create(gameVideoSettings, "SnakePlusPlus");
if (useSFML) if (useSFML)
graphics.reset(new GameWindow()); graphics.reset(new SFML());
else else
graphics.reset(new CommandLine()); 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; return;
} }
void GameState::StartGame() void GameState::StartGame()
{ {
gameWindow.create(gameVideoSettings, "SnakePlusPlus"); ResetGameBoard();
RunGameLoop(); RunGameLoop();
return; return;
} }
// TODO: Reimplement for DisplayInterface
void GameState::DisplayEndScreen(void) void GameState::DisplayEndScreen(void)
{ {
gameWindow.clear(); // graphics->DisplayEndScreen();
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();
return; return;
} }
sf::Vector2f GameState::GetGameBoundaries(void) sf::Vector2f GameState::GetGameBoundaries(void)
{ {
sf::Vector2f boundaries; return graphics->gameBoundaries;
boundaries.x = gameVideoSettings.width;
boundaries.y = gameVideoSettings.height;
return boundaries;
} }
// TODO: Reimplement for DisplayInterface
void GameState::GetKeyboardInput(void) void GameState::GetKeyboardInput(void)
{ {
sf::Event event; // sf::Event event;
while (gameWindow.pollEvent(event)) // while (gameWindow.pollEvent(event))
{ // {
if ((event.type == sf::Event::Closed) || // if ((event.type == SFML:Event::Closed) ||
(sf::Keyboard::isKeyPressed(sf::Keyboard::Escape))) // (sf::Keyboard::isKeyPressed(sf::Keyboard::Escape)))
gameWindow.close(); // gameWindow.close();
} // }
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left)) // if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
player.UpdateDirection(kLeft); // player.UpdateDirection(kLeft);
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up)) // if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
player.UpdateDirection(kUp); // player.UpdateDirection(kUp);
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down)) // if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
player.UpdateDirection(kDown); // player.UpdateDirection(kDown);
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right)) // if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
player.UpdateDirection(kRight); // player.UpdateDirection(kRight);
return; return;
} }
// TODO: Reimplement for DisplayInterface
bool GameState::PlayerWantsToContinue(void) bool GameState::PlayerWantsToContinue(void)
{ {
sf::Event event; // sf::Event event;
while (true) // while (true)
{ // {
while (gameWindow.pollEvent(event)) // while (gameWindow.pollEvent(event))
{ // {
if ((event.type == sf::Event::Closed) || // if ((event.type == sf::Event::Closed) ||
(sf::Keyboard::isKeyPressed(sf::Keyboard::Escape))) // (sf::Keyboard::isKeyPressed(sf::Keyboard::Escape)))
gameWindow.close(); // gameWindow.close();
return false; // return false;
} // }
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Enter)) // if (sf::Keyboard::isKeyPressed(sf::Keyboard::Enter))
return true; // return true;
sf::sleep(delay); // sf::sleep(delay);
} // }
} }
// Generates new food until not colliding with player // Generates new food until not colliding with player
@ -107,26 +81,37 @@ void GameState::RegenerateFood(void)
return; 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) void GameState::RunGameLoop(void)
{ {
while (gameWindow.isOpen()) while (graphics->IsOpen())
{ {
GetKeyboardInput(); GetKeyboardInput();
player.MoveSnake(&playerFood); player.MoveSnake(&playerFood);
RegenerateFood(); RegenerateFood();
RenderWindow(); RenderWindow();
sf::sleep(delay);
} }
return; return;
} }
// TODO: Reimplement for DisplayInterface
void GameState::RenderWindow(void) void GameState::RenderWindow(void)
{ {
gameWindow.clear(); // gameWindow.clear();
player.DisplaySnake(gameWindow); // player.DisplaySnake(gameWindow);
gameWindow.draw(playerFood.GetFoodObject()); // gameWindow.draw(playerFood.GetFoodObject());
gameWindow.display(); // gameWindow.display();
if (player.gameFinished) // if (player.gameFinished)
DisplayEndScreen(); // DisplayEndScreen();
return; return;
} }

2
src/main.cpp Normal file → Executable file
View File

@ -1,4 +1,4 @@
#include "GameState.h" #include "gamestate.h"
int main() int main()
{ {

4
src/snake.cpp Normal file → Executable file
View File

@ -1,10 +1,10 @@
// Snake.cpp // Snake.cpp
#include <iostream> #include <iostream>
#include <queue> #include <queue>
#include <SFML\Graphics.hpp> #include <SFML/Graphics.hpp>
#include "common.h" #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(void) Snake::Snake(void)

4
src/snakefood.cpp Normal file → Executable file
View File

@ -1,8 +1,8 @@
// SnakeFood.cpp // SnakeFood.cpp
#include <iostream> #include <iostream>
#include <SFML\Graphics.hpp> #include <SFML/Graphics.hpp>
#include "common.h" #include "common.h"
#include "snakeFood.h" #include "snakefood.h"
SnakeFood::SnakeFood() SnakeFood::SnakeFood()