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
4
include/common.h
Normal file → Executable file
4
include/common.h
Normal file → Executable file
@ -1,9 +1,7 @@
|
|||||||
#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
|
||||||
{
|
{
|
||||||
|
24
include/display.h
Normal file → Executable file
24
include/display.h
Normal file → Executable file
@ -1,12 +1,19 @@
|
|||||||
#ifndef DISPLAY_H
|
#ifndef DISPLAY_H
|
||||||
#define DISPLAY_H
|
#define DISPLAY_H
|
||||||
|
|
||||||
|
#include <SFML/Graphics.hpp>
|
||||||
|
|
||||||
class DisplayInterface
|
class DisplayInterface
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
sf::Vector2f gameBoundaries;
|
||||||
DisplayInterface(void);
|
DisplayInterface(void);
|
||||||
|
bool IsOpen(void);
|
||||||
protected:
|
protected:
|
||||||
|
bool isGameStillRunning;
|
||||||
virtual void DisplayGameState(void) = 0;
|
virtual void DisplayGameState(void) = 0;
|
||||||
|
virtual void DisplayEndScreen(void) = 0;
|
||||||
|
virtual void StartGame(void) = 0;
|
||||||
private:
|
private:
|
||||||
;
|
;
|
||||||
};
|
};
|
||||||
@ -14,23 +21,30 @@ private:
|
|||||||
class CommandLine : public DisplayInterface
|
class CommandLine : public DisplayInterface
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
CommandLine(void) = default;
|
CommandLine(void);
|
||||||
void DisplayGameState(void);
|
void DisplayGameState(void);
|
||||||
|
void DisplayEndScreen(void);
|
||||||
|
void StartGame(void);
|
||||||
protected:
|
protected:
|
||||||
;
|
;
|
||||||
private:
|
private:
|
||||||
;
|
const int kGridSize = 25;
|
||||||
};
|
};
|
||||||
|
|
||||||
class GameWindow : public DisplayInterface
|
class SFML : public DisplayInterface
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
GameWindow(void) = default;
|
SFML(void);
|
||||||
void DisplayGameState(void);
|
void DisplayGameState(void);
|
||||||
|
void DisplayEndScreen(void);
|
||||||
|
void StartGame(void);
|
||||||
|
void UpdateResolution(sf::Vector2i newResolution);
|
||||||
protected:
|
protected:
|
||||||
;
|
;
|
||||||
private:
|
private:
|
||||||
;
|
sf::Time delay;
|
||||||
|
sf::RenderWindow gameWindow;
|
||||||
|
sf::VideoMode gameVideoSettings;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#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
|
#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
2
include/snake.h
Normal file → Executable 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
2
include/snakefood.h
Normal file → Executable 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
|
||||||
|
0
src/common.cpp
Normal file → Executable file
0
src/common.cpp
Normal file → Executable file
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
|
// 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
2
src/main.cpp
Normal file → Executable file
@ -1,4 +1,4 @@
|
|||||||
#include "GameState.h"
|
#include "gamestate.h"
|
||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
|
4
src/snake.cpp
Normal file → Executable file
4
src/snake.cpp
Normal file → Executable 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
4
src/snakefood.cpp
Normal file → Executable 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()
|
||||||
|
Loading…
Reference in New Issue
Block a user