Many issues restructuring the program, needs cleaning
This commit is contained in:
parent
a76d9ccfb5
commit
f317dc7a8c
0
.gitignore
vendored
Normal file → Executable file
0
.gitignore
vendored
Normal file → Executable file
48
Makefile
Normal file → Executable file
48
Makefile
Normal file → Executable file
@ -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
|
42
README.md
Normal file → Executable file
42
README.md
Normal file → Executable file
@ -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!
|
||||
|
34
include/common.h
Normal file → Executable file
34
include/common.h
Normal file → Executable file
@ -1,18 +1,16 @@
|
||||
#ifndef COMMON_H
|
||||
#define COMMON_H
|
||||
|
||||
#include <SFML\Graphics.hpp>
|
||||
|
||||
const int kGridSize = 25;
|
||||
|
||||
enum PlayerDirection
|
||||
{
|
||||
kLeft = 1,
|
||||
kUp = 2,
|
||||
kDown = 3,
|
||||
kRight = 4
|
||||
};
|
||||
|
||||
bool GlobalCollision(sf::Vector2f object1Position, sf::Vector2f object2Position);
|
||||
|
||||
#endif
|
||||
#ifndef COMMON_H
|
||||
#define COMMON_H
|
||||
|
||||
#include <SFML/Graphics.hpp>
|
||||
|
||||
enum PlayerDirection
|
||||
{
|
||||
kLeft = 1,
|
||||
kUp = 2,
|
||||
kDown = 3,
|
||||
kRight = 4
|
||||
};
|
||||
|
||||
bool GlobalCollision(sf::Vector2f object1Position, sf::Vector2f object2Position);
|
||||
|
||||
#endif
|
||||
|
86
include/display.h
Normal file → Executable file
86
include/display.h
Normal file → Executable file
@ -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
|
||||
#ifndef DISPLAY_H
|
||||
#define DISPLAY_H
|
||||
|
||||
#include <SFML/Graphics.hpp>
|
||||
|
||||
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
|
||||
|
14
include/game.h
Normal file → Executable file
14
include/game.h
Normal file → Executable 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
7
include/gamestate.h
Normal file → Executable file
@ -3,7 +3,7 @@
|
||||
#define GAMESTATE_H
|
||||
|
||||
#include <memory>
|
||||
#include <SFML\Graphics.hpp>
|
||||
#include <SFML/Graphics.hpp>
|
||||
#include "snake.h"
|
||||
#include "display.h"
|
||||
|
||||
@ -13,22 +13,19 @@ public:
|
||||
std::vector< std::vector<char> > gameBoard;
|
||||
bool useSFML = 1;
|
||||
GameState();
|
||||
GameState(int newHorizontal, int newVertical);
|
||||
void StartGame(void);
|
||||
sf::Vector2f GetGameBoundaries(void);
|
||||
protected:
|
||||
;
|
||||
private:
|
||||
std::unique_ptr<DisplayInterface> 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);
|
||||
};
|
||||
|
2
include/snake.h
Normal file → Executable file
2
include/snake.h
Normal file → Executable file
@ -3,7 +3,7 @@
|
||||
#define SNAKE_H
|
||||
|
||||
#include <deque>
|
||||
#include <SFML\Graphics.hpp>
|
||||
#include <SFML/Graphics.hpp>
|
||||
#include "snakefood.h"
|
||||
|
||||
|
||||
|
2
include/snakefood.h
Normal file → Executable file
2
include/snakefood.h
Normal file → Executable file
@ -2,7 +2,7 @@
|
||||
#ifndef SNAKEFOOD_H
|
||||
#define SNAKEFOOD_H
|
||||
|
||||
#include <SFML\Graphics.hpp>
|
||||
#include <SFML/Graphics.hpp>
|
||||
#include <random>
|
||||
|
||||
class SnakeFood
|
||||
|
20
src/common.cpp
Normal file → Executable file
20
src/common.cpp
Normal file → Executable file
@ -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;
|
||||
}
|
82
src/display.cpp
Normal file → Executable file
82
src/display.cpp
Normal file → Executable 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
2
src/game.cpp
Normal file → Executable file
@ -1 +1 @@
|
||||
#include "game.cpp"
|
||||
#include "game.h"
|
||||
|
129
src/gamestate.cpp
Normal file → Executable file
129
src/gamestate.cpp
Normal file → Executable file
@ -1,101 +1,75 @@
|
||||
// GameState.cpp
|
||||
#include <SFML\Graphics.hpp>
|
||||
#include <SFML\System.hpp>
|
||||
#include <SFML/Graphics.hpp>
|
||||
#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;
|
||||
}
|
||||
|
2
src/main.cpp
Normal file → Executable file
2
src/main.cpp
Normal file → Executable file
@ -1,4 +1,4 @@
|
||||
#include "GameState.h"
|
||||
#include "gamestate.h"
|
||||
|
||||
int main()
|
||||
{
|
||||
|
4
src/snake.cpp
Normal file → Executable file
4
src/snake.cpp
Normal file → Executable file
@ -1,10 +1,10 @@
|
||||
// Snake.cpp
|
||||
#include <iostream>
|
||||
#include <queue>
|
||||
#include <SFML\Graphics.hpp>
|
||||
#include <SFML/Graphics.hpp>
|
||||
#include "common.h"
|
||||
#include "snake.h"
|
||||
#include "snakeFood.h"
|
||||
#include "snakefood.h"
|
||||
|
||||
// General constructor for snake class
|
||||
Snake::Snake(void)
|
||||
|
4
src/snakefood.cpp
Normal file → Executable file
4
src/snakefood.cpp
Normal file → Executable file
@ -1,8 +1,8 @@
|
||||
// SnakeFood.cpp
|
||||
#include <iostream>
|
||||
#include <SFML\Graphics.hpp>
|
||||
#include <SFML/Graphics.hpp>
|
||||
#include "common.h"
|
||||
#include "snakeFood.h"
|
||||
#include "snakefood.h"
|
||||
|
||||
|
||||
SnakeFood::SnakeFood()
|
||||
|
Loading…
Reference in New Issue
Block a user