Cleaned up naming of files; setup more structure

This commit is contained in:
TriantaTV 2023-03-13 21:10:52 -05:00
parent 3ee01d936b
commit a76d9ccfb5
13 changed files with 65 additions and 25 deletions

View File

@ -8,10 +8,12 @@ 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/GameState.o src/GameState.cpp g++ $(INC) $(STD) -c -o build/display.o src/display.cpp
g++ $(INC) $(STD) -c -o build/Snake.o src/Snake.cpp g++ $(INC) $(STD) -c -o build/game.o src/game.cpp
g++ $(INC) $(STD) -c -o build/SnakeFood.o src/SnakeFood.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: dirs:
mkdir bin build mkdir bin build

36
include/display.h Normal file
View File

@ -0,0 +1,36 @@
#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

0
include/game.h Normal file
View File

View File

@ -2,12 +2,16 @@
#ifndef GAMESTATE_H #ifndef GAMESTATE_H
#define GAMESTATE_H #define GAMESTATE_H
#include <memory>
#include <SFML\Graphics.hpp> #include <SFML\Graphics.hpp>
#include "Snake.h" #include "snake.h"
#include "display.h"
class GameState class GameState
{ {
public: public:
std::vector< std::vector<char> > gameBoard;
bool useSFML = 1;
GameState(); GameState();
GameState(int newHorizontal, int newVertical); GameState(int newHorizontal, int newVertical);
void StartGame(void); void StartGame(void);
@ -15,6 +19,7 @@ public:
protected: protected:
; ;
private: private:
std::unique_ptr<DisplayInterface> graphics;
sf::RenderWindow gameWindow; sf::RenderWindow gameWindow;
sf::VideoMode gameVideoSettings; sf::VideoMode gameVideoSettings;
SnakeFood playerFood; SnakeFood playerFood;

View File

@ -4,7 +4,7 @@
#include <deque> #include <deque>
#include <SFML\Graphics.hpp> #include <SFML\Graphics.hpp>
#include "SnakeFood.h" #include "snakefood.h"
class Snake class Snake
@ -17,7 +17,6 @@ public:
sf::Vector2f GetSnakeHeadPosition(void); sf::Vector2f GetSnakeHeadPosition(void);
bool IsTouchingObject(sf::RectangleShape object); bool IsTouchingObject(sf::RectangleShape object);
void MoveSnake(SnakeFood* playerFood); void MoveSnake(SnakeFood* playerFood);
void Reset(void);
void UpdateDirection(int newDirection); void UpdateDirection(int newDirection);
protected: protected:
; ;

View File

@ -1,4 +1,4 @@
#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)

0
src/display.cpp Normal file
View File

1
src/game.cpp Normal file
View File

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

View File

@ -1,15 +1,19 @@
// GameState.cpp // GameState.cpp
#include <SFML\Graphics.hpp> #include <SFML\Graphics.hpp>
#include <SFML\System.hpp> #include <SFML\System.hpp>
#include "Common.h" #include "common.h"
#include "Snake.h" #include "gamestate.h"
#include "GameState.h"
GameState::GameState() GameState::GameState()
{ {
delay = sf::milliseconds(75); delay = sf::milliseconds(75);
gameVideoSettings = sf::VideoMode(1025, 725); gameVideoSettings = sf::VideoMode(1025, 725);
gameWindow.create(gameVideoSettings, "SnakePlusPlus"); gameWindow.create(gameVideoSettings, "SnakePlusPlus");
if (useSFML)
graphics.reset(new GameWindow());
else
graphics.reset(new CommandLine());
return; return;
} }
@ -42,7 +46,8 @@ void GameState::DisplayEndScreen(void)
gameWindow.display(); gameWindow.display();
if (!PlayerWantsToContinue()) if (!PlayerWantsToContinue())
return; return;
player.Reset(); player = Snake();
playerFood.GenerateNewFood(GetGameBoundaries());
gameWindow.clear(); gameWindow.clear();
return; return;
} }

View File

@ -2,9 +2,9 @@
#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)
@ -79,14 +79,6 @@ void Snake::MoveSnake(SnakeFood* snakeFood)
return; return;
} }
void Snake::Reset(void)
{
snakeBody.clear();
gameFinished = false;
CreateHead();
snakeDirection = kRight;
}
void Snake::UpdateDirection(int newDirection) void Snake::UpdateDirection(int newDirection)
{ {
snakeDirection = newDirection; snakeDirection = newDirection;

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()