This was overengineered, lots of trimming to do
This commit is contained in:
parent
c01f687ec7
commit
498ac65dbc
@ -13,27 +13,25 @@ namespace snakeplusplus
|
|||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
GameEngine();
|
GameEngine();
|
||||||
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::vector< std::vector<char> > gameBoard;
|
||||||
std::unique_ptr<DisplayInterface> graphics;
|
PlayerInput controls;
|
||||||
|
PlayerOutput graphics;
|
||||||
Snake player;
|
Snake player;
|
||||||
Food playerFood;
|
Food playerFood;
|
||||||
Input playerInput;
|
|
||||||
bool useSFML = 1;
|
bool useSFML = 1;
|
||||||
bool isGameOver = 0;
|
bool isGameOver = 0;
|
||||||
void ApplySettings(void);
|
|
||||||
void DisplayEndScreen(void);
|
void DisplayEndScreen(void);
|
||||||
void GameLoop(void);
|
void GameLoop(void);
|
||||||
|
sf::Vector2f MovePlayer(void);
|
||||||
void PlaceNewSnakePart(sf::Vector2f location);
|
void PlaceNewSnakePart(sf::Vector2f location);
|
||||||
void PlayerWantsToContinue(void);
|
|
||||||
void RegenerateFood(void);
|
void RegenerateFood(void);
|
||||||
void ResetGameBoard(void);
|
void PrepareGameBoard(void);
|
||||||
void RestartGame(void);
|
void UpdatePlayerSpeed();
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -8,10 +8,10 @@ const int kGridSize = 25;
|
|||||||
|
|
||||||
namespace snakeplusplus
|
namespace snakeplusplus
|
||||||
{
|
{
|
||||||
class Input
|
class PlayerInput
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
Input(void);
|
PlayerInput(void);
|
||||||
PlayerDirection GetPlayerInput(void);
|
PlayerDirection GetPlayerInput(void);
|
||||||
protected:
|
protected:
|
||||||
;
|
;
|
||||||
@ -19,56 +19,28 @@ namespace snakeplusplus
|
|||||||
PlayerDirection lastPlayerInput;
|
PlayerDirection lastPlayerInput;
|
||||||
};
|
};
|
||||||
|
|
||||||
class DisplayInterface
|
class PlayerOutput
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
sf::Vector2f gameBoundaries;
|
sf::Vector2f gameBoundaries;
|
||||||
DisplayInterface(void);
|
PlayerOutput(void);
|
||||||
bool IsOpen(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 CheckContinue(void);
|
||||||
void DisplayGameState(std::vector< std::vector<char> >* gameBoard);
|
void DisplayGameState(std::vector< std::vector<char> >* gameBoard);
|
||||||
void DisplayEndScreen(void);
|
|
||||||
void StartGameWindow(void);
|
void StartGameWindow(void);
|
||||||
protected:
|
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:
|
private:
|
||||||
void CheckWindowEvents(void);
|
void CheckWindowEvents(void);
|
||||||
|
void DisplayEndScreen(void);
|
||||||
void DrawEmpty(sf::Vector2f location);
|
void DrawEmpty(sf::Vector2f location);
|
||||||
void DrawFood(sf::Vector2f location);
|
void DrawFood(sf::Vector2f location);
|
||||||
void DrawSnake(sf::Vector2f location);
|
void DrawSnake(sf::Vector2f location);
|
||||||
sf::RenderWindow gameWindow;
|
sf::RenderWindow gameWindow;
|
||||||
sf::VideoMode gameVideoSettings;
|
sf::VideoMode gameVideoSettings;
|
||||||
sf::RectangleShape drawObject;
|
sf::RectangleShape drawObject;
|
||||||
|
bool isWindowAlive;
|
||||||
|
sf::Time delay = sf::milliseconds(120);
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -2,47 +2,38 @@
|
|||||||
#ifndef SNAKE_HPP
|
#ifndef SNAKE_HPP
|
||||||
#define SNAKE_HPP
|
#define SNAKE_HPP
|
||||||
|
|
||||||
|
#include <SFML/System/Vector2.hpp>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <queue>
|
#include <queue>
|
||||||
#include <random>
|
#include <random>
|
||||||
#include <SFML/Graphics.hpp>
|
#include <SFML/Graphics.hpp>
|
||||||
#include "common.hpp"
|
|
||||||
|
|
||||||
namespace snakeplusplus
|
namespace snakeplusplus
|
||||||
{
|
{
|
||||||
class Snake
|
struct Snake
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
Snake(void);
|
Snake(void);
|
||||||
PlayerDirection GetDirection(void);
|
sf::Vector2f headLocation;
|
||||||
sf::Vector2f GetHeadLocation(void);
|
sf::Vector2f speed;
|
||||||
sf::Vector2f Move(void);
|
std::queue< std::shared_ptr<char> > body;
|
||||||
void Pop(void);
|
void Pop(void);
|
||||||
sf::Vector2f Reset(void);
|
|
||||||
void UpdateDirection(PlayerDirection newDirection);
|
|
||||||
protected:
|
protected:
|
||||||
;
|
;
|
||||||
private:
|
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
|
struct Food
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
Food();
|
Food(void);
|
||||||
|
sf::Vector2f location;
|
||||||
|
std::shared_ptr<char> food;
|
||||||
void GenerateNewFood(sf::Vector2f boundaries);
|
void GenerateNewFood(sf::Vector2f boundaries);
|
||||||
sf::Vector2f GetFoodLocation(void);
|
|
||||||
protected:
|
protected:
|
||||||
;
|
;
|
||||||
private:
|
private:
|
||||||
std::shared_ptr<char> food;
|
|
||||||
sf::Vector2f location;
|
|
||||||
std::default_random_engine generator;
|
std::default_random_engine generator;
|
||||||
int GenerateRandomNumber(int generationLimit);
|
int GenerateRandomNumber(int generationLimit);
|
||||||
};
|
};
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
// GameState.cpp
|
// GameState.cpp
|
||||||
|
#include <SFML/System/Vector2.hpp>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
#include <string>
|
#include <string>
|
||||||
@ -14,62 +15,37 @@ namespace snakeplusplus
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
void GameEngine::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 GameEngine::StartGame()
|
void GameEngine::StartGame()
|
||||||
{
|
{
|
||||||
ApplySettings();
|
//ApplySettings();
|
||||||
ResetGameBoard();
|
PrepareGameBoard();
|
||||||
graphics->StartGameWindow();
|
graphics.StartGameWindow();
|
||||||
GameLoop();
|
GameLoop();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
void GameEngine::ApplySettings(void)
|
|
||||||
{
|
|
||||||
if (useSFML)
|
|
||||||
graphics.reset(new SFML());
|
|
||||||
else
|
|
||||||
graphics.reset(new CommandLine());
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO: Reimplement for DisplayInterface
|
|
||||||
void GameEngine::DisplayEndScreen(void)
|
|
||||||
{
|
|
||||||
graphics->DisplayEndScreen();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
void GameEngine::GameLoop(void)
|
void GameEngine::GameLoop(void)
|
||||||
{
|
{
|
||||||
sf::Vector2f newHeadPosition;
|
sf::Vector2f newHeadPosition;
|
||||||
while (graphics->IsOpen())
|
while (graphics.IsOpen())
|
||||||
{
|
{
|
||||||
// player.UpdateDirection(playerInput.GetPlayerInput());
|
UpdatePlayerSpeed();
|
||||||
// newHeadPosition = player.Move();
|
PlaceNewSnakePart(MovePlayer());
|
||||||
// if (playerFood.GetFoodLocation() != newHeadPosition)
|
graphics.DisplayGameState(&gameBoard);
|
||||||
// player.Pop();
|
|
||||||
// PlaceNewSnakePart(newHeadPosition);
|
|
||||||
graphics->DisplayGameState(&gameBoard);
|
|
||||||
// if (isGameOver)
|
|
||||||
// PlayerWantsToContinue();
|
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
sf::Vector2f GameEngine::MovePlayer(void)
|
||||||
|
{
|
||||||
|
sf::Vector2f newHeadPosition;
|
||||||
|
newHeadPosition.x = player.headLocation.x + player.speed.x;
|
||||||
|
newHeadPosition.y = player.headLocation.y + player.speed.y;
|
||||||
|
return newHeadPosition;
|
||||||
|
}
|
||||||
|
|
||||||
sf::Vector2f GameEngine::GetGameBoundaries(void)
|
sf::Vector2f GameEngine::GetGameBoundaries(void)
|
||||||
{
|
{
|
||||||
return graphics->gameBoundaries;
|
return graphics.gameBoundaries;
|
||||||
}
|
}
|
||||||
|
|
||||||
void GameEngine::PlaceNewSnakePart(sf::Vector2f location)
|
void GameEngine::PlaceNewSnakePart(sf::Vector2f location)
|
||||||
@ -80,8 +56,8 @@ namespace snakeplusplus
|
|||||||
locationState = gameBoard.at(location.y).at(location.x);
|
locationState = gameBoard.at(location.y).at(location.x);
|
||||||
if (locationState == 'O')
|
if (locationState == 'O')
|
||||||
isGameOver = true; // Game should end (Snake touching snake)
|
isGameOver = true; // Game should end (Snake touching snake)
|
||||||
if (locationState == ' ')
|
if (playerFood.location != location)
|
||||||
player.Pop(); // Snake shouldn't extend
|
player.Pop();
|
||||||
gameBoard.at(location.y).at(location.x) = 'O';
|
gameBoard.at(location.y).at(location.x) = 'O';
|
||||||
} catch (const std::out_of_range& error) {
|
} catch (const std::out_of_range& error) {
|
||||||
isGameOver = true; // Snake ran into edge
|
isGameOver = true; // Snake ran into edge
|
||||||
@ -90,40 +66,57 @@ namespace snakeplusplus
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
void GameEngine::PlayerWantsToContinue(void)
|
|
||||||
{
|
|
||||||
graphics->CheckContinue();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Generates new food until not colliding with player
|
// Generates new food until not colliding with player
|
||||||
void GameEngine::RegenerateFood(void)
|
void GameEngine::RegenerateFood(void)
|
||||||
{
|
{
|
||||||
sf::Vector2f newLocation = playerFood.GetFoodLocation();
|
sf::Vector2f newLocation = playerFood.location;
|
||||||
bool isUpdated = false;
|
bool isUpdated = false;
|
||||||
// Keep making new food until generating a valid spot
|
// 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;
|
isUpdated = true;
|
||||||
playerFood.GenerateNewFood(GetGameBoundaries());
|
playerFood.GenerateNewFood(GetGameBoundaries());
|
||||||
newLocation = playerFood.GetFoodLocation();
|
newLocation = playerFood.location;
|
||||||
}
|
}
|
||||||
if (isUpdated)
|
if (isUpdated)
|
||||||
gameBoard.at(newLocation.y).at(newLocation.x) = 'X';
|
gameBoard.at(newLocation.y).at(newLocation.x) = 'X';
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
void GameEngine::ResetGameBoard(void)
|
void GameEngine::PrepareGameBoard(void)
|
||||||
{
|
{
|
||||||
gameBoard.clear();
|
gameBoard.clear();
|
||||||
sf::Vector2f boardDimensions = GetGameBoundaries();
|
sf::Vector2f boardDimensions = GetGameBoundaries();
|
||||||
std::vector<char> tempBoard;
|
std::vector<char> tempBoard;
|
||||||
tempBoard.resize(boardDimensions.x, ' ');
|
tempBoard.resize(boardDimensions.x, ' ');
|
||||||
gameBoard.resize(boardDimensions.y, tempBoard);
|
gameBoard.resize(boardDimensions.y, tempBoard);
|
||||||
PlaceNewSnakePart(player.Reset());
|
sf::Vector2f playerLocation(4,5);
|
||||||
// playerFood.GenerateNewFood(boardDimensions);
|
char* headLocation = &gameBoard.at(playerLocation.y).at(playerLocation.x);
|
||||||
// sf::Vector2f foodStartLocation = playerFood.GetFoodLocation();
|
*headLocation = 'O';
|
||||||
// gameBoard.at(foodStartLocation.y).at(foodStartLocation.x) = 'X';
|
player.body.push(std::shared_ptr<char>(headLocation));
|
||||||
// return;
|
sf::Vector2f foodLocation(2,2);
|
||||||
|
gameBoard.at(foodLocation.y).at(foodLocation.x) = 'X';
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
void GameEngine::UpdatePlayerSpeed(void)
|
||||||
|
{
|
||||||
|
PlayerDirection input = controls.GetPlayerInput();
|
||||||
|
switch (input) {
|
||||||
|
case kUp:
|
||||||
|
player.speed.x = 0;
|
||||||
|
player.speed.y = 1;
|
||||||
|
case kLeft:
|
||||||
|
player.speed.x = -1;
|
||||||
|
player.speed.y = 0;
|
||||||
|
case kRight:
|
||||||
|
player.speed.x = 1;
|
||||||
|
player.speed.y = 0;
|
||||||
|
case kDown:
|
||||||
|
player.speed.x = 0;
|
||||||
|
player.speed.y = -1;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,8 +1,7 @@
|
|||||||
#include "gamestate.hpp"
|
#include "gamestate.hpp"
|
||||||
|
|
||||||
int main(int argc, char *argv[])
|
int main(void)
|
||||||
{
|
{
|
||||||
snakeplusplus::GameEngine game;
|
snakeplusplus::GameEngine game;
|
||||||
game.SetGameSettings(argc, argv);
|
|
||||||
game.StartGame();
|
game.StartGame();
|
||||||
}
|
}
|
||||||
|
@ -3,12 +3,12 @@
|
|||||||
|
|
||||||
namespace snakeplusplus
|
namespace snakeplusplus
|
||||||
{
|
{
|
||||||
Input::Input(void)
|
PlayerInput::PlayerInput(void)
|
||||||
{
|
{
|
||||||
lastPlayerInput = kNone;
|
lastPlayerInput = kNone;
|
||||||
}
|
}
|
||||||
|
|
||||||
PlayerDirection Input::GetPlayerInput(void)
|
PlayerDirection PlayerInput::GetPlayerInput(void)
|
||||||
{
|
{
|
||||||
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
|
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
|
||||||
lastPlayerInput = kLeft;
|
lastPlayerInput = kLeft;
|
||||||
@ -21,69 +21,12 @@ namespace snakeplusplus
|
|||||||
return lastPlayerInput;
|
return lastPlayerInput;
|
||||||
}
|
}
|
||||||
|
|
||||||
DisplayInterface::DisplayInterface(void)
|
bool PlayerOutput::IsOpen(void)
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool DisplayInterface::IsOpen(void)
|
|
||||||
{
|
{
|
||||||
return isWindowAlive;
|
return isWindowAlive;
|
||||||
}
|
}
|
||||||
|
|
||||||
CommandLine::CommandLine(void)
|
PlayerOutput::PlayerOutput(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 kWidth = 1025 / kGridSize;
|
||||||
float kHeight = 725 / kGridSize;
|
float kHeight = 725 / kGridSize;
|
||||||
@ -93,7 +36,7 @@ namespace snakeplusplus
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
void SFML::CheckContinue(void)
|
void PlayerOutput::CheckContinue(void)
|
||||||
{
|
{
|
||||||
sf::Event event;
|
sf::Event event;
|
||||||
while (true)
|
while (true)
|
||||||
@ -111,7 +54,7 @@ namespace snakeplusplus
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void SFML::DisplayEndScreen(void)
|
void PlayerOutput::DisplayEndScreen(void)
|
||||||
{
|
{
|
||||||
gameWindow.clear();
|
gameWindow.clear();
|
||||||
sf::Vector2f textPosition(gameBoundaries);
|
sf::Vector2f textPosition(gameBoundaries);
|
||||||
@ -131,7 +74,7 @@ namespace snakeplusplus
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
void SFML::DisplayGameState(std::vector< std::vector<char> >* gameBoard)
|
void PlayerOutput::DisplayGameState(std::vector< std::vector<char> >* gameBoard)
|
||||||
{
|
{
|
||||||
CheckWindowEvents();
|
CheckWindowEvents();
|
||||||
sf::Vector2i location(0,0);
|
sf::Vector2i location(0,0);
|
||||||
@ -154,24 +97,14 @@ namespace snakeplusplus
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
void SFML::StartGameWindow(void)
|
void PlayerOutput::StartGameWindow(void)
|
||||||
{
|
{
|
||||||
gameWindow.create(gameVideoSettings, "SnakePlusPlus");
|
gameWindow.create(gameVideoSettings, "SnakePlusPlus");
|
||||||
isWindowAlive = true;
|
isWindowAlive = true;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
void SFML::UpdateResolution(sf::Vector2i newResolution)
|
void PlayerOutput::CheckWindowEvents(void)
|
||||||
{
|
|
||||||
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;
|
sf::Event event;
|
||||||
while (gameWindow.pollEvent(event))
|
while (gameWindow.pollEvent(event))
|
||||||
@ -182,7 +115,7 @@ namespace snakeplusplus
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void SFML::DrawEmpty(sf::Vector2f location)
|
void PlayerOutput::DrawEmpty(sf::Vector2f location)
|
||||||
{
|
{
|
||||||
location *= static_cast<float>(kGridSize);
|
location *= static_cast<float>(kGridSize);
|
||||||
drawObject.setPosition(location);
|
drawObject.setPosition(location);
|
||||||
@ -191,7 +124,7 @@ namespace snakeplusplus
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
void SFML::DrawFood(sf::Vector2f location)
|
void PlayerOutput::DrawFood(sf::Vector2f location)
|
||||||
{
|
{
|
||||||
location *= static_cast<float>(kGridSize);
|
location *= static_cast<float>(kGridSize);
|
||||||
drawObject.setPosition(location);
|
drawObject.setPosition(location);
|
||||||
@ -200,7 +133,7 @@ namespace snakeplusplus
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
void SFML::DrawSnake(sf::Vector2f location)
|
void PlayerOutput::DrawSnake(sf::Vector2f location)
|
||||||
{
|
{
|
||||||
location *= static_cast<float>(kGridSize);
|
location *= static_cast<float>(kGridSize);
|
||||||
drawObject.setPosition(location);
|
drawObject.setPosition(location);
|
||||||
|
@ -1,8 +1,8 @@
|
|||||||
// Snake.cpp
|
// Snake.cpp
|
||||||
|
#include <memory>
|
||||||
#include <queue>
|
#include <queue>
|
||||||
#include <random>
|
#include <random>
|
||||||
#include <SFML/Graphics.hpp>
|
#include <SFML/Graphics.hpp>
|
||||||
#include "common.hpp"
|
|
||||||
#include "snake.hpp"
|
#include "snake.hpp"
|
||||||
|
|
||||||
namespace snakeplusplus
|
namespace snakeplusplus
|
||||||
@ -13,67 +13,10 @@ namespace snakeplusplus
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
PlayerDirection Snake::GetDirection(void)
|
|
||||||
{
|
|
||||||
return direction;
|
|
||||||
}
|
|
||||||
|
|
||||||
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)
|
void Snake::Pop(void)
|
||||||
{
|
{
|
||||||
*(snakeBody.front().get()) = ' ';
|
*(body.front().get()) = ' ';
|
||||||
snakeBody.pop();
|
body.pop();
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
sf::Vector2f Snake::Reset(void)
|
|
||||||
{
|
|
||||||
return sf::Vector2f(4,5);
|
|
||||||
}
|
|
||||||
|
|
||||||
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()
|
Food::Food()
|
||||||
@ -88,11 +31,6 @@ namespace snakeplusplus
|
|||||||
location.y = GenerateRandomNumber(boundaries.y);
|
location.y = GenerateRandomNumber(boundaries.y);
|
||||||
}
|
}
|
||||||
|
|
||||||
sf::Vector2f Food::GetFoodLocation(void)
|
|
||||||
{
|
|
||||||
return location;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Returns a newly generated number
|
// Returns a newly generated number
|
||||||
int Food::GenerateRandomNumber(int generationLimit)
|
int Food::GenerateRandomNumber(int generationLimit)
|
||||||
{
|
{
|
||||||
|
Loading…
Reference in New Issue
Block a user