Doing too much at once. Need to fix branch

This commit is contained in:
TriantaTV 2023-04-06 19:27:10 -05:00
parent 67e56bcb3c
commit c01f687ec7
15 changed files with 597 additions and 529 deletions

View File

@ -8,10 +8,9 @@ fresh: dirs compile link
compile:
g++ $(INC) $(STD) -c -o build/main.o src/main.cpp
g++ $(INC) $(STD) -c -o build/display.o src/display.cpp
g++ $(INC) $(STD) -c -o build/playerinterface.o src/playerinterface.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

View File

@ -1,8 +1,9 @@
#ifndef COMMON_H
#define COMMON_H
#ifndef COMMON_HPP
#define COMMON_HPP
enum PlayerDirection
{
kNone = 0,
kLeft = 1,
kUp = 2,
kDown = 3,

View File

@ -1,60 +0,0 @@
#ifndef DISPLAY_H
#define DISPLAY_H
#include <SFML/Graphics.hpp>
const int kGridSize = 25;
class DisplayInterface
{
public:
sf::Vector2f gameBoundaries;
DisplayInterface(void);
bool IsOpen(void);
virtual void CheckContinue(void) = 0;
virtual void DisplayGameState(std::vector< std::vector<char> >* gameBoard) = 0;
virtual void DisplayEndScreen(void) = 0;
virtual void StartGameWindow(void) = 0;
protected:
bool isWindowAlive;
sf::Time delay = sf::milliseconds(75);
private:
;
};
class CommandLine : public DisplayInterface
{
public:
CommandLine(void);
void CheckContinue(void);
void DisplayGameState(std::vector< std::vector<char> >* gameBoard);
void DisplayEndScreen(void);
void StartGameWindow(void);
protected:
;
private:
void Clear(void);
};
class SFML : public DisplayInterface
{
public:
SFML(void);
void CheckContinue(void);
void DisplayGameState(std::vector< std::vector<char> >* gameBoard);
void DisplayEndScreen(void);
void StartGameWindow(void);
void UpdateResolution(sf::Vector2i newResolution);
protected:
;
private:
void CheckWindowEvents(void);
void DrawEmpty(sf::Vector2f location);
void DrawFood(sf::Vector2f location);
void DrawSnake(sf::Vector2f location);
sf::RenderWindow gameWindow;
sf::VideoMode gameVideoSettings;
sf::RectangleShape drawObject;
};
#endif

View File

@ -1,37 +0,0 @@
// GameState.h
#ifndef GAMESTATE_H
#define GAMESTATE_H
#include <memory>
#include <SFML/Graphics.hpp>
#include "snake.h"
#include "snakefood.h"
#include "display.h"
class GameState
{
public:
GameState();
void SetGameSettings(int argc, char* argv[]);
void StartGame(void);
sf::Vector2f GetGameBoundaries(void);
protected:
;
private:
std::vector< std::vector<char> > gameBoard;
std::unique_ptr<DisplayInterface> graphics;
Snake player;
SnakeFood playerFood;
bool useSFML = 1;
bool isGameOver = 0;
void ApplySettings(void);
void DisplayEndScreen(void);
void GetKeyboardInput(void);
void PlaceNewSnakePart(sf::Vector2f location);
void PlayerWantsToContinue(void);
void RegenerateFood(void);
void ResetGameBoard(void);
void RunGameLoop(void);
};
#endif

40
include/gamestate.hpp Executable file
View File

@ -0,0 +1,40 @@
// GameState.h
#ifndef GAMESTATE_HPP
#define GAMESTATE_HPP
#include <memory>
#include <SFML/Graphics.hpp>
#include "snake.hpp"
#include "playerinterface.hpp"
namespace snakeplusplus
{
class GameEngine
{
public:
GameEngine();
void SetGameSettings(int argc, char* argv[]);
void StartGame(void);
sf::Vector2f GetGameBoundaries(void);
protected:
;
private:
std::vector< std::vector<char> > gameBoard;
std::unique_ptr<DisplayInterface> graphics;
Snake player;
Food playerFood;
Input playerInput;
bool useSFML = 1;
bool isGameOver = 0;
void ApplySettings(void);
void DisplayEndScreen(void);
void GameLoop(void);
void PlaceNewSnakePart(sf::Vector2f location);
void PlayerWantsToContinue(void);
void RegenerateFood(void);
void ResetGameBoard(void);
void RestartGame(void);
};
}
#endif

75
include/playerinterface.hpp Executable file
View File

@ -0,0 +1,75 @@
#ifndef PLAYERINTERFACE_HPP
#define PLAYERINTERFACE_HPP
#include "common.hpp"
#include <SFML/Graphics.hpp>
const int kGridSize = 25;
namespace snakeplusplus
{
class Input
{
public:
Input(void);
PlayerDirection GetPlayerInput(void);
protected:
;
private:
PlayerDirection lastPlayerInput;
};
class DisplayInterface
{
public:
sf::Vector2f gameBoundaries;
DisplayInterface(void);
bool IsOpen(void);
virtual void CheckContinue(void) = 0;
virtual void DisplayGameState(std::vector< std::vector<char> >* gameBoard) = 0;
virtual void DisplayEndScreen(void) = 0;
virtual void StartGameWindow(void) = 0;
protected:
bool isWindowAlive;
sf::Time delay = sf::milliseconds(120);
private:
;
};
class CommandLine : public DisplayInterface
{
public:
CommandLine(void);
void CheckContinue(void);
void DisplayGameState(std::vector< std::vector<char> >* gameBoard);
void DisplayEndScreen(void);
void StartGameWindow(void);
protected:
;
private:
void Clear(void);
};
class SFML : public DisplayInterface
{
public:
SFML(void);
void CheckContinue(void);
void DisplayGameState(std::vector< std::vector<char> >* gameBoard);
void DisplayEndScreen(void);
void StartGameWindow(void);
void UpdateResolution(sf::Vector2i newResolution);
protected:
;
private:
void CheckWindowEvents(void);
void DrawEmpty(sf::Vector2f location);
void DrawFood(sf::Vector2f location);
void DrawSnake(sf::Vector2f location);
sf::RenderWindow gameWindow;
sf::VideoMode gameVideoSettings;
sf::RectangleShape drawObject;
};
}
#endif

View File

@ -1,26 +0,0 @@
// Snake.h
#ifndef SNAKE_H
#define SNAKE_H
#include <queue>
#include <SFML/Graphics.hpp>
class Snake
{
public:
Snake(void);
sf::Vector2f MoveSnake(void);
sf::Vector2f Pop(void);
void UpdateDirection(int newDirection);
protected:
;
private:
std::queue<sf::Vector2f> snakeBody;
int snakeDirection = 0;
sf::Vector2f CalculateNewHead();
void CreateNewHead(sf::Vector2f);
};
#endif

51
include/snake.hpp Executable file
View File

@ -0,0 +1,51 @@
// Snake.h
#ifndef SNAKE_HPP
#define SNAKE_HPP
#include <memory>
#include <queue>
#include <random>
#include <SFML/Graphics.hpp>
#include "common.hpp"
namespace snakeplusplus
{
class Snake
{
public:
Snake(void);
PlayerDirection GetDirection(void);
sf::Vector2f GetHeadLocation(void);
sf::Vector2f Move(void);
void Pop(void);
sf::Vector2f Reset(void);
void UpdateDirection(PlayerDirection newDirection);
protected:
;
private:
std::queue< std::shared_ptr<char> > snakeBody;
PlayerDirection direction = kNone;
sf::Vector2f headLocation;
void MoveLeft(void);
void MoveUp(void);
void MoveDown(void);
void MoveRight(void);
};
class Food
{
public:
Food();
void GenerateNewFood(sf::Vector2f boundaries);
sf::Vector2f GetFoodLocation(void);
protected:
;
private:
std::shared_ptr<char> food;
sf::Vector2f location;
std::default_random_engine generator;
int GenerateRandomNumber(int generationLimit);
};
}
#endif

View File

@ -1,22 +0,0 @@
// SnakeFood.h
#ifndef SNAKEFOOD_H
#define SNAKEFOOD_H
#include <SFML/Graphics.hpp>
#include <random>
class SnakeFood
{
public:
SnakeFood();
void GenerateNewFood(sf::Vector2f boundaries);
sf::Vector2f GetFoodLocation(void);
protected:
;
private:
sf::Vector2f location;
std::default_random_engine generator;
int GenerateRandomNumber(int generationLimit);
};
#endif

View File

@ -1,188 +0,0 @@
#include "display.h"
#include <iostream>
//#include <SFML/System.hpp>
DisplayInterface::DisplayInterface(void)
{
;
}
bool DisplayInterface::IsOpen(void)
{
return isWindowAlive;
}
CommandLine::CommandLine(void)
{
gameBoundaries.x = 1025 / kGridSize;
gameBoundaries.y = 725 / kGridSize;
return;
}
void CommandLine::CheckContinue(void)
{
int placeholder;
std::cout << "Press enter to play again.";
std::cin >> placeholder;
return;
}
void CommandLine::DisplayEndScreen(void)
{
std::cout << "Game Over!" << std::endl;
return;
}
void CommandLine::DisplayGameState(std::vector< std::vector<char> >* gameBoard)
{
Clear();
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;
}
sf::sleep(delay);
}
void CommandLine::StartGameWindow(void)
{
isWindowAlive = true;
return;
}
void CommandLine::Clear(void)
{
#if defined _WIN32
system("cls");
//clrscr(); // including header file : conio.h
#elif defined (__LINUX__) || defined(__gnu_linux__) || defined(__linux__)
system("clear");
//std::cout<< u8"\033[2J\033[1;1H"; //Using ANSI Escape Sequences
#elif defined (__APPLE__)
system("clear");
#endif
}
SFML::SFML(void)
{
gameVideoSettings = sf::VideoMode(1025, 725);
drawObject.setSize(sf::Vector2f(kGridSize, kGridSize));
return;
}
void SFML::CheckContinue(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;
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Enter))
return;
sf::sleep(delay);
}
}
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::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();
sf::sleep(delay);
return;
}
void SFML::StartGameWindow(void)
{
gameWindow.create(gameVideoSettings, "SnakePlusPlus");
isWindowAlive = 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;
}
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,17 +1,21 @@
// GameState.cpp
#include <memory>
#include <stdexcept>
#include <string>
#include <SFML/Graphics.hpp>
#include "common.h"
#include "display.h"
#include "gamestate.h"
#include "common.hpp"
#include "playerinterface.hpp"
#include "gamestate.hpp"
GameState::GameState()
namespace snakeplusplus
{
GameEngine::GameEngine()
{
return;
}
}
void GameState::SetGameSettings(int argc, char* argv[])
{
void GameEngine::SetGameSettings(int argc, char* argv[])
{
std::string convertedString;
for (int i = 0; i < argc; i++)
{
@ -19,105 +23,107 @@ void GameState::SetGameSettings(int argc, char* argv[])
if (convertedString == "--no-sfml")
useSFML = false;
}
}
}
void GameState::StartGame()
{
void GameEngine::StartGame()
{
ApplySettings();
ResetGameBoard();
graphics->StartGameWindow();
RunGameLoop();
GameLoop();
return;
}
}
void GameState::ApplySettings(void)
{
void GameEngine::ApplySettings(void)
{
if (useSFML)
graphics.reset(new SFML());
else
graphics.reset(new CommandLine());
return;
}
}
// TODO: Reimplement for DisplayInterface
void GameState::DisplayEndScreen(void)
{
// TODO: Reimplement for DisplayInterface
void GameEngine::DisplayEndScreen(void)
{
graphics->DisplayEndScreen();
return;
}
}
sf::Vector2f GameState::GetGameBoundaries(void)
{
void GameEngine::GameLoop(void)
{
sf::Vector2f newHeadPosition;
while (graphics->IsOpen())
{
// player.UpdateDirection(playerInput.GetPlayerInput());
// newHeadPosition = player.Move();
// if (playerFood.GetFoodLocation() != newHeadPosition)
// player.Pop();
// PlaceNewSnakePart(newHeadPosition);
graphics->DisplayGameState(&gameBoard);
// if (isGameOver)
// PlayerWantsToContinue();
}
return;
}
sf::Vector2f GameEngine::GetGameBoundaries(void)
{
return graphics->gameBoundaries;
}
}
void GameState::GetKeyboardInput(void)
{
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);
void GameEngine::PlaceNewSnakePart(sf::Vector2f location)
{
char locationState;
try
{
locationState = gameBoard.at(location.y).at(location.x);
if (locationState == 'O')
isGameOver = true; // Game should end (Snake touching snake)
if (locationState == ' ')
player.Pop(); // Snake shouldn't extend
gameBoard.at(location.y).at(location.x) = 'O';
} catch (const std::out_of_range& error) {
isGameOver = true; // Snake ran into edge
return;
}
void GameState::PlaceNewSnakePart(sf::Vector2f location)
{
gameBoard.at(location.y).at(location.x) = 'o';
}
return;
}
}
void GameState::PlayerWantsToContinue(void)
{
void GameEngine::PlayerWantsToContinue(void)
{
graphics->CheckContinue();
return;
}
}
// Generates new food until not colliding with player
void GameState::RegenerateFood(void)
{
// Generates new food until not colliding with player
void GameEngine::RegenerateFood(void)
{
sf::Vector2f newLocation = playerFood.GetFoodLocation();
bool isUpdated = false;
// Keep making new food until generating a valid spot
while (gameBoard.at(newLocation.y).at(newLocation.x) == 'o')
while (gameBoard.at(newLocation.y).at(newLocation.x) == 'O')
{
isUpdated = true;
playerFood.GenerateNewFood(GetGameBoundaries());
newLocation = playerFood.GetFoodLocation();
}
if (isUpdated)
gameBoard.at(newLocation.y).at(newLocation.x) = 'x';
gameBoard.at(newLocation.y).at(newLocation.x) = 'X';
return;
}
}
void GameState::ResetGameBoard(void)
{
void GameEngine::ResetGameBoard(void)
{
gameBoard.clear();
sf::Vector2f boardDimensions = GetGameBoundaries();
std::vector<char> tempBoard;
tempBoard.resize(boardDimensions.x, ' ');
gameBoard.resize(boardDimensions.y, tempBoard);
playerFood.GenerateNewFood(boardDimensions);
sf::Vector2f foodStartLocation = playerFood.GetFoodLocation();
gameBoard.at(foodStartLocation.y).at(foodStartLocation.x) = 'x';
return;
}
void GameState::RunGameLoop(void)
{
while (graphics->IsOpen())
{
GetKeyboardInput();
PlaceNewSnakePart(player.MoveSnake());
RegenerateFood();
graphics->DisplayGameState(&gameBoard);
if (isGameOver)
PlayerWantsToContinue();
PlaceNewSnakePart(player.Reset());
// playerFood.GenerateNewFood(boardDimensions);
// sf::Vector2f foodStartLocation = playerFood.GetFoodLocation();
// gameBoard.at(foodStartLocation.y).at(foodStartLocation.x) = 'X';
// return;
}
return;
}

View File

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

211
src/playerinterface.cpp Executable file
View File

@ -0,0 +1,211 @@
#include "playerinterface.hpp"
#include <iostream>
namespace snakeplusplus
{
Input::Input(void)
{
lastPlayerInput = kNone;
}
PlayerDirection Input::GetPlayerInput(void)
{
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
lastPlayerInput = kLeft;
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
lastPlayerInput = kUp;
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
lastPlayerInput = kDown;
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
lastPlayerInput = kRight;
return lastPlayerInput;
}
DisplayInterface::DisplayInterface(void)
{
return;
}
bool DisplayInterface::IsOpen(void)
{
return isWindowAlive;
}
CommandLine::CommandLine(void)
{
gameBoundaries.x = 1025 / kGridSize;
gameBoundaries.y = 725 / kGridSize;
return;
}
void CommandLine::CheckContinue(void)
{
int placeholder;
std::cout << "Press enter to play again.";
std::cin >> placeholder;
return;
}
void CommandLine::DisplayEndScreen(void)
{
std::cout << "Game Over!" << std::endl;
return;
}
void CommandLine::DisplayGameState(std::vector< std::vector<char> >* gameBoard)
{
Clear();
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;
}
sf::sleep(delay);
}
void CommandLine::StartGameWindow(void)
{
isWindowAlive = true;
return;
}
void CommandLine::Clear(void)
{
#if defined _WIN32
system("cls");
//clrscr(); // including header file : conio.h
#elif defined (__LINUX__) || defined(__gnu_linux__) || defined(__linux__)
system("clear");
//std::cout<< u8"\033[2J\033[1;1H"; //Using ANSI Escape Sequences
#elif defined (__APPLE__)
system("clear");
#endif
}
SFML::SFML(void)
{
float kWidth = 1025 / kGridSize;
float kHeight = 725 / kGridSize;
gameBoundaries = sf::Vector2f(kWidth, kHeight);
gameVideoSettings = sf::VideoMode(1025, 725);
drawObject.setSize(sf::Vector2f(kGridSize, kGridSize));
return;
}
void SFML::CheckContinue(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;
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Enter))
return;
sf::sleep(delay);
}
}
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::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();
sf::sleep(delay);
return;
}
void SFML::StartGameWindow(void)
{
gameWindow.create(gameVideoSettings, "SnakePlusPlus");
isWindowAlive = 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;
}
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,57 +1,104 @@
// Snake.cpp
#include <queue>
#include <random>
#include <SFML/Graphics.hpp>
#include "common.h"
#include "snake.h"
#include "common.hpp"
#include "snake.hpp"
// General constructor for snake class
Snake::Snake(void)
namespace snakeplusplus
{
CreateNewHead(sf::Vector2f(4,5));
// General constructor for snake class
Snake::Snake(void)
{
return;
}
}
// Move snake based on direction and check for collision
sf::Vector2f Snake::MoveSnake(void)
{
sf::Vector2f newHeadPosition;
newHeadPosition = CalculateNewHead();
CreateNewHead(newHeadPosition);
return newHeadPosition;
}
PlayerDirection Snake::GetDirection(void)
{
return direction;
}
// Removes tail of snake
// Returns the location of the tail
sf::Vector2f Snake::Pop(void)
{
sf::Vector2f tailLocation = snakeBody.front();
sf::Vector2f Snake::GetHeadLocation(void)
{
return headLocation;
}
// Update snake with location information
sf::Vector2f Snake::Move(void)
{
if (direction == kLeft) MoveLeft();
if (direction == kUp) MoveUp();
if (direction == kDown) MoveDown();
if (direction == kRight) MoveRight();
return headLocation;
}
// Removes tail of snake
void Snake::Pop(void)
{
*(snakeBody.front().get()) = ' ';
snakeBody.pop();
return tailLocation;
}
void Snake::UpdateDirection(int newDirection)
{
snakeDirection = newDirection;
return;
}
}
// Get a new coordinate position based on snake direction
sf::Vector2f Snake::CalculateNewHead(void)
{
sf::Vector2f position = snakeBody.back();
if (snakeDirection == kLeft)
position.x -= 1;
if (snakeDirection == kUp)
position.y -= 1;
if (snakeDirection == kDown)
position.y += 1;
if (snakeDirection == kRight)
position.x += 1;
return position;
}
sf::Vector2f Snake::Reset(void)
{
return sf::Vector2f(4,5);
}
void Snake::CreateNewHead(sf::Vector2f headLocation)
{
snakeBody.push(headLocation);
void Snake::UpdateDirection(PlayerDirection newDirection)
{
direction = newDirection;
return;
}
void Snake::MoveLeft(void)
{
headLocation.x -= 1;
return;
}
void Snake::MoveUp(void)
{
headLocation.y -= 1;
return;
}
void Snake::MoveDown(void)
{
headLocation.y += 1;
return;
}
void Snake::MoveRight(void)
{
headLocation.x += 1;
return;
}
Food::Food()
{
return;
}
// Returns a new food object for the snakeFood
void Food::GenerateNewFood(sf::Vector2f boundaries)
{
location.x = GenerateRandomNumber(boundaries.x);
location.y = GenerateRandomNumber(boundaries.y);
}
sf::Vector2f Food::GetFoodLocation(void)
{
return location;
}
// Returns a newly generated number
int Food::GenerateRandomNumber(int generationLimit)
{
int generatedNumber;
std::uniform_int_distribution<> distribution(0, generationLimit);
generatedNumber = distribution(generator);
return generatedNumber;
}
}

View File

@ -1,29 +0,0 @@
// SnakeFood.cpp
#include "snakefood.h"
SnakeFood::SnakeFood()
{
return;
}
// Returns a new food object for the snakeFood
void SnakeFood::GenerateNewFood(sf::Vector2f boundaries)
{
location.x = GenerateRandomNumber(boundaries.x);
location.y = GenerateRandomNumber(boundaries.y);
}
sf::Vector2f SnakeFood::GetFoodLocation(void)
{
return location;
}
// Returns a newly generated number
int SnakeFood::GenerateRandomNumber(int generationLimit)
{
int generatedNumber;
std::uniform_int_distribution<> distribution(0, generationLimit);
generatedNumber = distribution(generator);
return generatedNumber;
}