Fixed class organization

This commit is contained in:
TriantaTV 2023-03-17 20:13:50 -05:00
parent f317dc7a8c
commit 9082d7fdfd
14 changed files with 178 additions and 241 deletions

View File

@ -8,9 +8,7 @@ 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/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/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
@ -19,7 +17,7 @@ dirs:
mkdir bin build mkdir bin build
link: link:
g++ build/*.o -o bin/SnakePlusPlus $(SFML) g++ build/*.o -o bin/SnakePlusPlus.out $(SFML)
clean: clean:
rm bin/*.o build/*.out rm bin/*.out build/*.o

View File

@ -1,8 +1,6 @@
#ifndef COMMON_H #ifndef COMMON_H
#define COMMON_H #define COMMON_H
#include <SFML/Graphics.hpp>
enum PlayerDirection enum PlayerDirection
{ {
kLeft = 1, kLeft = 1,
@ -11,6 +9,4 @@ enum PlayerDirection
kRight = 4 kRight = 4
}; };
bool GlobalCollision(sf::Vector2f object1Position, sf::Vector2f object2Position);
#endif #endif

View File

@ -3,6 +3,8 @@
#include <SFML/Graphics.hpp> #include <SFML/Graphics.hpp>
const int kGridSize = 25;
class DisplayInterface class DisplayInterface
{ {
public: public:
@ -10,10 +12,10 @@ public:
DisplayInterface(void); DisplayInterface(void);
bool IsOpen(void); bool IsOpen(void);
protected: protected:
bool isGameStillRunning; bool isWindowAlive;
virtual void DisplayGameState(void) = 0; virtual void DisplayGameState(std::vector< std::vector<char> >* gameBoard) = 0;
virtual void DisplayEndScreen(void) = 0; virtual void DisplayEndScreen(void) = 0;
virtual void StartGame(void) = 0; virtual void StartGameWindow(void) = 0;
private: private:
; ;
}; };
@ -22,29 +24,34 @@ class CommandLine : public DisplayInterface
{ {
public: public:
CommandLine(void); CommandLine(void);
void DisplayGameState(void); void DisplayGameState(std::vector< std::vector<char> >* gameBoard);
void DisplayEndScreen(void); void DisplayEndScreen(void);
void StartGame(void); void StartGameWindow(void);
protected: protected:
; ;
private: private:
const int kGridSize = 25; ;
}; };
class SFML : public DisplayInterface class SFML : public DisplayInterface
{ {
public: public:
SFML(void); SFML(void);
void DisplayGameState(void); void DisplayGameState(std::vector< std::vector<char> >* gameBoard);
void DisplayEndScreen(void); void DisplayEndScreen(void);
void StartGame(void); void StartGameWindow(void);
void UpdateResolution(sf::Vector2i newResolution); void UpdateResolution(sf::Vector2i newResolution);
protected: protected:
; ;
private: private:
void CheckWindowEvents(void);
void DrawEmpty(sf::Vector2f location);
void DrawFood(sf::Vector2f location);
void DrawSnake(sf::Vector2f location);
sf::Time delay; sf::Time delay;
sf::RenderWindow gameWindow; sf::RenderWindow gameWindow;
sf::VideoMode gameVideoSettings; sf::VideoMode gameVideoSettings;
sf::RectangleShape drawObject;
}; };
#endif #endif

View File

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

View File

@ -5,22 +5,25 @@
#include <memory> #include <memory>
#include <SFML/Graphics.hpp> #include <SFML/Graphics.hpp>
#include "snake.h" #include "snake.h"
#include "snakefood.h"
#include "display.h" #include "display.h"
class GameState class GameState
{ {
public: public:
std::vector< std::vector<char> > gameBoard;
bool useSFML = 1;
GameState(); GameState();
void SetGameSettings(int argc, char* argv[]);
void StartGame(void); void StartGame(void);
sf::Vector2f GetGameBoundaries(void); sf::Vector2f GetGameBoundaries(void);
protected: protected:
; ;
private: private:
std::vector< std::vector<char> > gameBoard;
std::unique_ptr<DisplayInterface> graphics; std::unique_ptr<DisplayInterface> graphics;
SnakeFood playerFood;
Snake player; Snake player;
SnakeFood playerFood;
bool useSFML = 1;
void ApplySettings(void);
void DisplayEndScreen(void); void DisplayEndScreen(void);
void GetKeyboardInput(void); void GetKeyboardInput(void);
bool PlayerWantsToContinue(void); bool PlayerWantsToContinue(void);

View File

@ -2,33 +2,24 @@
#ifndef SNAKE_H #ifndef SNAKE_H
#define SNAKE_H #define SNAKE_H
#include <deque> #include <queue>
#include <SFML/Graphics.hpp> #include <SFML/Graphics.hpp>
#include "snakefood.h"
class Snake class Snake
{ {
public: public:
bool gameFinished = false;
Snake(void); Snake(void);
void DisplaySnake(sf::RenderWindow& window); sf::Vector2f MoveSnake(void);
sf::RectangleShape GetSnakeHead(void); sf::Vector2f Pop(void);
sf::Vector2f GetSnakeHeadPosition(void);
bool IsTouchingObject(sf::RectangleShape object);
void MoveSnake(SnakeFood* playerFood);
void UpdateDirection(int newDirection); void UpdateDirection(int newDirection);
protected: protected:
; ;
private: private:
std::deque<sf::RectangleShape> snakeBody; std::queue<sf::Vector2f> snakeBody;
sf::Vector2f bodyPartSize;
int snakeDirection = 0; int snakeDirection = 0;
void AddBodyPart(sf::RectangleShape newBodyPart); sf::Vector2f CalculateNewHead();
sf::Vector2f CalculateNewPosition(sf::Vector2f position); void CreateNewHead(sf::Vector2f);
bool CheckBoundaries(void);
void CreateHead(void);
bool IsSelfCollision(sf::RectangleShape testRectangle);
}; };

View File

@ -9,14 +9,11 @@ class SnakeFood
{ {
public: public:
SnakeFood(); SnakeFood();
SnakeFood(sf::Vector2f snakeFoodSize); sf::Vector2f GenerateNewFood(sf::Vector2f boundaries);
void GenerateNewFood(sf::Vector2f windowSize);
sf::RectangleShape GetFoodObject(void);
sf::Vector2f GetFoodObjectPosition(void);
protected: protected:
; ;
private: private:
sf::RectangleShape snakeFoodObject; sf::Vector2f location;
std::default_random_engine generator; std::default_random_engine generator;
int GenerateRandomNumber(int generationLimit); int GenerateRandomNumber(int generationLimit);
}; };

View File

@ -1,11 +0,0 @@
#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;
}

View File

@ -1,5 +1,5 @@
#include "common.h"
#include "display.h" #include "display.h"
#include <iostream>
//#include <SFML/System.hpp> //#include <SFML/System.hpp>
DisplayInterface::DisplayInterface(void) DisplayInterface::DisplayInterface(void)
@ -9,7 +9,7 @@ DisplayInterface::DisplayInterface(void)
bool DisplayInterface::IsOpen(void) bool DisplayInterface::IsOpen(void)
{ {
return isGameStillRunning; return isWindowAlive;
} }
CommandLine::CommandLine(void) CommandLine::CommandLine(void)
@ -19,28 +19,34 @@ CommandLine::CommandLine(void)
return; return;
} }
// TODO: Use cout for printing game to screen void CommandLine::DisplayEndScreen(void)
void CommandLine::DisplayGameState(void)
{ {
; std::cout << "Game Over!" << std::endl;
} return;
}
void CommandLine::StartGame(void)
{ // TODO: Use cout for printing game to screen
isGameStillRunning = true; void CommandLine::DisplayGameState(std::vector< std::vector<char> >* gameBoard)
{
for (int i = 0; i < gameBoundaries.y; i++)
{
for (int j = 0; j < gameBoundaries.x; j++)
std::cout << gameBoard->at(i).at(j);
std::cout << std::endl;
}
}
void CommandLine::StartGameWindow(void)
{
isWindowAlive = true;
return; return;
} }
// TODO: Setup making window
SFML::SFML(void) SFML::SFML(void)
{ {
delay = sf::milliseconds(75); delay = sf::milliseconds(75);
gameVideoSettings = sf::VideoMode(1025, 725); gameVideoSettings = sf::VideoMode(1025, 725);
return; drawObject.setSize(sf::Vector2f(kGridSize, kGridSize));
}
void SFML::DisplayGameState(void)
{
return; return;
} }
@ -64,10 +70,32 @@ void SFML::DisplayEndScreen(void)
return; return;
} }
void SFML::StartGame(void) void SFML::DisplayGameState(std::vector< std::vector<char> >* gameBoard)
{
CheckWindowEvents();
sf::Vector2i location(0,0);
char letterOnBoard;
for (; location.y < gameBoundaries.y; location.y++)
{
for (; location.x < gameBoundaries.x; location.x++)
{
letterOnBoard = gameBoard->at(location.y).at(location.y);
if (letterOnBoard == 'o')
DrawSnake(static_cast<sf::Vector2f>(location));
else if (letterOnBoard == 'x')
DrawFood(static_cast<sf::Vector2f>(location));
else
DrawEmpty(static_cast<sf::Vector2f>(location));
}
}
gameWindow.display();
return;
}
void SFML::StartGameWindow(void)
{ {
gameWindow.create(gameVideoSettings, "SnakePlusPlus"); gameWindow.create(gameVideoSettings, "SnakePlusPlus");
isGameStillRunning = true; isWindowAlive = true;
return; return;
} }
@ -80,3 +108,41 @@ void SFML::UpdateResolution(sf::Vector2i newResolution)
gameWindow.create(gameVideoSettings, "SnakePlusPlus"); gameWindow.create(gameVideoSettings, "SnakePlusPlus");
return; return;
} }
void SFML::CheckWindowEvents(void)
{
sf::Event event;
while (gameWindow.pollEvent(event))
{
if ((event.type == sf::Event::Closed) ||
(sf::Keyboard::isKeyPressed(sf::Keyboard::Escape)))
gameWindow.close();
}
}
void SFML::DrawEmpty(sf::Vector2f location)
{
location *= static_cast<float>(kGridSize);
drawObject.setPosition(location);
drawObject.setFillColor(sf::Color::Black);
gameWindow.draw(drawObject);
return;
}
void SFML::DrawFood(sf::Vector2f location)
{
location *= static_cast<float>(kGridSize);
drawObject.setPosition(location);
drawObject.setFillColor(sf::Color::Red);
gameWindow.draw(drawObject);
return;
}
void SFML::DrawSnake(sf::Vector2f location)
{
location *= static_cast<float>(kGridSize);
drawObject.setPosition(location);
drawObject.setFillColor(sf::Color::Green);
gameWindow.draw(drawObject);
return;
}

View File

@ -1 +0,0 @@
#include "game.h"

View File

@ -1,4 +1,5 @@
// GameState.cpp // GameState.cpp
#include <string>
#include <SFML/Graphics.hpp> #include <SFML/Graphics.hpp>
#include "common.h" #include "common.h"
#include "display.h" #include "display.h"
@ -6,20 +7,36 @@
GameState::GameState() GameState::GameState()
{ {
if (useSFML)
graphics.reset(new SFML());
else
graphics.reset(new CommandLine());
return; return;
} }
void GameState::SetGameSettings(int argc, char* argv[])
{
std::string convertedString;
for (int i = 0; i < argc; i++)
{
convertedString = argv[i];
if (convertedString == "--no-sfml")
useSFML = false;
}
}
void GameState::StartGame() void GameState::StartGame()
{ {
ApplySettings();
ResetGameBoard(); ResetGameBoard();
RunGameLoop(); RunGameLoop();
return; return;
} }
void GameState::ApplySettings(void)
{
if (useSFML)
graphics.reset(new SFML());
else
graphics.reset(new CommandLine());
}
// TODO: Reimplement for DisplayInterface // TODO: Reimplement for DisplayInterface
void GameState::DisplayEndScreen(void) void GameState::DisplayEndScreen(void)
{ {
@ -32,24 +49,16 @@ sf::Vector2f GameState::GetGameBoundaries(void)
return graphics->gameBoundaries; return graphics->gameBoundaries;
} }
// TODO: Reimplement for DisplayInterface
void GameState::GetKeyboardInput(void) void GameState::GetKeyboardInput(void)
{ {
// sf::Event event; if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
// while (gameWindow.pollEvent(event)) player.UpdateDirection(kLeft);
// { if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
// if ((event.type == SFML:Event::Closed) || player.UpdateDirection(kUp);
// (sf::Keyboard::isKeyPressed(sf::Keyboard::Escape))) if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
// gameWindow.close(); player.UpdateDirection(kDown);
// } if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
// if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left)) player.UpdateDirection(kRight);
// 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; return;
} }
@ -75,9 +84,12 @@ bool GameState::PlayerWantsToContinue(void)
// Generates new food until not colliding with player // Generates new food until not colliding with player
void GameState::RegenerateFood(void) void GameState::RegenerateFood(void)
{ {
sf::Vector2f newLocation;
playerFood.GenerateNewFood(GetGameBoundaries());
// Keep making new food until generating a valid spot // Keep making new food until generating a valid spot
while (player.IsTouchingObject(playerFood.GetFoodObject())) while (gameBoard.at(newLocation.y).at(newLocation.x) == 'o')
playerFood.GenerateNewFood(GetGameBoundaries()); playerFood.GenerateNewFood(GetGameBoundaries());
gameBoard.at(newLocation.y).at(newLocation.x) = 'x';
return; return;
} }
@ -97,7 +109,7 @@ void GameState::RunGameLoop(void)
while (graphics->IsOpen()) while (graphics->IsOpen())
{ {
GetKeyboardInput(); GetKeyboardInput();
player.MoveSnake(&playerFood); player.MoveSnake();
RegenerateFood(); RegenerateFood();
RenderWindow(); RenderWindow();
} }

View File

@ -1,7 +1,8 @@
#include "gamestate.h" #include "gamestate.h"
int main() int main(int argc, char *argv[])
{ {
GameState game; GameState game;
game.SetGameSettings(argc, argv);
game.StartGame(); game.StartGame();
} }

View File

@ -1,82 +1,32 @@
// Snake.cpp // Snake.cpp
#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"
// General constructor for snake class // General constructor for snake class
Snake::Snake(void) Snake::Snake(void)
{ {
bodyPartSize = sf::Vector2f(kGridSize, kGridSize); CreateNewHead(sf::Vector2f(4,5));
CreateHead();
return; return;
} }
// Iterate through snake deque and draw to window
void Snake::DisplaySnake(sf::RenderWindow& window)
{
for (auto snakeBodyPart = snakeBody.cbegin(); snakeBodyPart != snakeBody.cend(); ++snakeBodyPart)
window.draw(*snakeBodyPart);
return;
}
// Return the RectangleShape head of snake
sf::RectangleShape Snake::GetSnakeHead(void)
{
sf::RectangleShape head;
head = snakeBody.front();
return head;
}
// Return the Vector2f head of snake
sf::Vector2f Snake::GetSnakeHeadPosition(void)
{
sf::Vector2f position;
position = snakeBody.front().getPosition();
return position;
}
// Checks if snake position matches object position
bool Snake::IsTouchingObject(sf::RectangleShape object)
{
for (auto snakeBodyPart = snakeBody.cbegin(); snakeBodyPart != snakeBody.cend(); ++snakeBodyPart)
{
if ((*snakeBodyPart).getPosition().x != object.getPosition().x)
continue;
if ((*snakeBodyPart).getPosition().y != object.getPosition().y)
continue;
return true;
}
return false;
}
// Move snake based on direction and check for collision // Move snake based on direction and check for collision
void Snake::MoveSnake(SnakeFood* snakeFood) sf::Vector2f Snake::MoveSnake(void)
{ {
// TODO: Add losing on wall collision
if (CheckBoundaries()) // Wall collision
{
gameFinished = true;
return;
}
sf::Vector2f newHeadPosition; sf::Vector2f newHeadPosition;
newHeadPosition = CalculateNewPosition(GetSnakeHeadPosition()); newHeadPosition = CalculateNewHead();
sf::RectangleShape newBodyPart(sf::Vector2f(kGridSize, kGridSize)); CreateNewHead(newHeadPosition);
newBodyPart.setPosition(newHeadPosition); return newHeadPosition;
// TODO: Add losing on self collision }
if (IsSelfCollision(newBodyPart) && (snakeBody.size() > 1)) // Snake collision
{ // Removes tail of snake
gameFinished = true; // Returns the location of the tail
return; sf::Vector2f Snake::Pop(void)
} {
if (IsSelfCollision(newBodyPart)) sf::Vector2f tailLocation = snakeBody.front();
return; snakeBody.pop();
AddBodyPart(newBodyPart); return tailLocation;
if (!GlobalCollision(GetSnakeHeadPosition(), snakeFood->GetFoodObjectPosition()))
snakeBody.pop_back();
return;
} }
void Snake::UpdateDirection(int newDirection) void Snake::UpdateDirection(int newDirection)
@ -85,55 +35,22 @@ void Snake::UpdateDirection(int newDirection)
return; return;
} }
void Snake::AddBodyPart(sf::RectangleShape newBodyPart)
{
newBodyPart.setFillColor(sf::Color::Green);
snakeBody.push_front(newBodyPart);
}
// Get a new coordinate position based on snake direction // Get a new coordinate position based on snake direction
sf::Vector2f Snake::CalculateNewPosition(sf::Vector2f position) sf::Vector2f Snake::CalculateNewHead(void)
{ {
if (snakeDirection == 0) sf::Vector2f position = snakeBody.back();
return position;
if (snakeDirection == kLeft) if (snakeDirection == kLeft)
position.x -= kGridSize; position.x -= 1;
if (snakeDirection == kUp) if (snakeDirection == kUp)
position.y -= kGridSize; position.y -= 1;
if (snakeDirection == kDown) if (snakeDirection == kDown)
position.y += kGridSize; position.y += 1;
if (snakeDirection == kRight) if (snakeDirection == kRight)
position.x += kGridSize; position.x += 1;
return position;
} }
// Check snake head for running into boundaries void Snake::CreateNewHead(sf::Vector2f headLocation)
bool Snake::CheckBoundaries(void)
{ {
if (snakeBody.front().getPosition().x == 0 && snakeDirection == kLeft) snakeBody.push(headLocation);
return true; return;
if (snakeBody.front().getPosition().y == 0 && snakeDirection == kUp)
return true;
// TODO: Change boundaries to not be hard-coded
if (snakeBody.front().getPosition().y > 675 && snakeDirection == kDown)
return true;
if (snakeBody.front().getPosition().x > 975 && snakeDirection == kRight)
return true;
return false;
}
void Snake::CreateHead(void)
{
sf::RectangleShape newBodyPart(bodyPartSize);
newBodyPart.setFillColor(sf::Color::Green);
snakeBody.push_front(newBodyPart);
}
// Test for snake self collision
bool Snake::IsSelfCollision(sf::RectangleShape testRectangle)
{
for (auto snakeBodyPart = snakeBody.cbegin(); snakeBodyPart != snakeBody.cend(); ++snakeBodyPart)
if (GlobalCollision(testRectangle.getPosition(), (*snakeBodyPart).getPosition()))
return true;
return false;
} }

View File

@ -1,42 +1,18 @@
// SnakeFood.cpp // SnakeFood.cpp
#include <iostream>
#include <SFML/Graphics.hpp>
#include "common.h"
#include "snakefood.h" #include "snakefood.h"
SnakeFood::SnakeFood() SnakeFood::SnakeFood()
{ {
snakeFoodObject.setSize(sf::Vector2f(kGridSize, kGridSize));
snakeFoodObject.setFillColor(sf::Color::Red);
return;
}
SnakeFood::SnakeFood(sf::Vector2f snakeFoodSize)
{
snakeFoodObject.setSize(snakeFoodSize);
snakeFoodObject.setFillColor(sf::Color::Red);
return; return;
} }
// Returns a new food object for the snakeFood // Returns a new food object for the snakeFood
void SnakeFood::GenerateNewFood(sf::Vector2f windowSize) sf::Vector2f SnakeFood::GenerateNewFood(sf::Vector2f boundaries)
{ {
sf::Vector2f newPosition; location.x = GenerateRandomNumber(boundaries.x);
newPosition.x = GenerateRandomNumber(windowSize.x); location.y = GenerateRandomNumber(boundaries.y);
newPosition.y = GenerateRandomNumber(windowSize.y); return location;
snakeFoodObject.setPosition(newPosition);
return;
}
sf::RectangleShape SnakeFood::GetFoodObject(void)
{
return snakeFoodObject;
}
sf::Vector2f SnakeFood::GetFoodObjectPosition(void)
{
return snakeFoodObject.getPosition();
} }
// Returns a newly generated number // Returns a newly generated number
@ -45,6 +21,5 @@ int SnakeFood::GenerateRandomNumber(int generationLimit)
int generatedNumber; int generatedNumber;
std::uniform_int_distribution<> distribution(0, generationLimit); std::uniform_int_distribution<> distribution(0, generationLimit);
generatedNumber = distribution(generator); generatedNumber = distribution(generator);
generatedNumber -= (generatedNumber % kGridSize);
return generatedNumber; return generatedNumber;
} }