diff --git a/Makefile b/Makefile index 8eda9c0..bd9ffcf 100755 --- a/Makefile +++ b/Makefile @@ -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 diff --git a/include/common.h b/include/common.hpp similarity index 58% rename from include/common.h rename to include/common.hpp index 0ac55ec..ef440c9 100755 --- a/include/common.h +++ b/include/common.hpp @@ -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, diff --git a/include/display.h b/include/display.h deleted file mode 100755 index 261b216..0000000 --- a/include/display.h +++ /dev/null @@ -1,60 +0,0 @@ -#ifndef DISPLAY_H -#define DISPLAY_H - -#include - -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 >* 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 >* 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 >* 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 diff --git a/include/gamestate.h b/include/gamestate.h deleted file mode 100755 index dc6e4f3..0000000 --- a/include/gamestate.h +++ /dev/null @@ -1,37 +0,0 @@ -// GameState.h -#ifndef GAMESTATE_H -#define GAMESTATE_H - -#include -#include -#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 > gameBoard; - std::unique_ptr 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 diff --git a/include/gamestate.hpp b/include/gamestate.hpp new file mode 100755 index 0000000..13c6765 --- /dev/null +++ b/include/gamestate.hpp @@ -0,0 +1,40 @@ +// GameState.h +#ifndef GAMESTATE_HPP +#define GAMESTATE_HPP + +#include +#include +#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 > gameBoard; + std::unique_ptr 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 diff --git a/include/playerinterface.hpp b/include/playerinterface.hpp new file mode 100755 index 0000000..a168c4f --- /dev/null +++ b/include/playerinterface.hpp @@ -0,0 +1,75 @@ +#ifndef PLAYERINTERFACE_HPP +#define PLAYERINTERFACE_HPP + +#include "common.hpp" +#include + +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 >* 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 >* 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 >* 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 diff --git a/include/snake.h b/include/snake.h deleted file mode 100755 index 85941b8..0000000 --- a/include/snake.h +++ /dev/null @@ -1,26 +0,0 @@ -// Snake.h -#ifndef SNAKE_H -#define SNAKE_H - -#include -#include - - -class Snake -{ -public: - Snake(void); - sf::Vector2f MoveSnake(void); - sf::Vector2f Pop(void); - void UpdateDirection(int newDirection); -protected: - ; -private: - std::queue snakeBody; - int snakeDirection = 0; - sf::Vector2f CalculateNewHead(); - void CreateNewHead(sf::Vector2f); -}; - - -#endif diff --git a/include/snake.hpp b/include/snake.hpp new file mode 100755 index 0000000..9a58fb2 --- /dev/null +++ b/include/snake.hpp @@ -0,0 +1,51 @@ +// Snake.h +#ifndef SNAKE_HPP +#define SNAKE_HPP + +#include +#include +#include +#include +#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 > 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 food; + sf::Vector2f location; + std::default_random_engine generator; + int GenerateRandomNumber(int generationLimit); + }; +} + +#endif diff --git a/include/snakefood.h b/include/snakefood.h deleted file mode 100755 index bb48960..0000000 --- a/include/snakefood.h +++ /dev/null @@ -1,22 +0,0 @@ -// SnakeFood.h -#ifndef SNAKEFOOD_H -#define SNAKEFOOD_H - -#include -#include - -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 diff --git a/src/display.cpp b/src/display.cpp deleted file mode 100755 index a4d4248..0000000 --- a/src/display.cpp +++ /dev/null @@ -1,188 +0,0 @@ -#include "display.h" -#include -//#include - -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 >* 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 >* 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(location)); - else if (letterOnBoard == 'x') - DrawFood(static_cast(location)); - else - DrawEmpty(static_cast(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(kGridSize); - drawObject.setPosition(location); - drawObject.setFillColor(sf::Color::Black); - gameWindow.draw(drawObject); - return; -} - -void SFML::DrawFood(sf::Vector2f location) -{ - location *= static_cast(kGridSize); - drawObject.setPosition(location); - drawObject.setFillColor(sf::Color::Red); - gameWindow.draw(drawObject); - return; -} - -void SFML::DrawSnake(sf::Vector2f location) -{ - location *= static_cast(kGridSize); - drawObject.setPosition(location); - drawObject.setFillColor(sf::Color::Green); - gameWindow.draw(drawObject); - return; -} diff --git a/src/gamestate.cpp b/src/gamestate.cpp index e49006f..26fa424 100755 --- a/src/gamestate.cpp +++ b/src/gamestate.cpp @@ -1,123 +1,129 @@ // GameState.cpp +#include +#include #include #include -#include "common.h" -#include "display.h" -#include "gamestate.h" +#include "common.hpp" +#include "playerinterface.hpp" +#include "gamestate.hpp" -GameState::GameState() +namespace snakeplusplus { - return; -} - -void GameState::SetGameSettings(int argc, char* argv[]) -{ - std::string convertedString; - for (int i = 0; i < argc; i++) + GameEngine::GameEngine() { - convertedString = argv[i]; - if (convertedString == "--no-sfml") - useSFML = false; + 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() + { + ApplySettings(); + ResetGameBoard(); + graphics->StartGameWindow(); + GameLoop(); + 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) + { + 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 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; + } + return; + } + + void GameEngine::PlayerWantsToContinue(void) + { + graphics->CheckContinue(); + return; + } + + // 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') + { + isUpdated = true; + playerFood.GenerateNewFood(GetGameBoundaries()); + newLocation = playerFood.GetFoodLocation(); + } + if (isUpdated) + gameBoard.at(newLocation.y).at(newLocation.x) = 'X'; + return; + } + + void GameEngine::ResetGameBoard(void) + { + gameBoard.clear(); + sf::Vector2f boardDimensions = GetGameBoundaries(); + std::vector tempBoard; + tempBoard.resize(boardDimensions.x, ' '); + gameBoard.resize(boardDimensions.y, tempBoard); + PlaceNewSnakePart(player.Reset()); + // playerFood.GenerateNewFood(boardDimensions); + // sf::Vector2f foodStartLocation = playerFood.GetFoodLocation(); + // gameBoard.at(foodStartLocation.y).at(foodStartLocation.x) = 'X'; + // return; } } - -void GameState::StartGame() -{ - ApplySettings(); - ResetGameBoard(); - graphics->StartGameWindow(); - RunGameLoop(); - return; -} - -void GameState::ApplySettings(void) -{ - if (useSFML) - graphics.reset(new SFML()); - else - graphics.reset(new CommandLine()); - return; -} - -// TODO: Reimplement for DisplayInterface -void GameState::DisplayEndScreen(void) -{ - graphics->DisplayEndScreen(); - return; -} - -sf::Vector2f GameState::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); - return; -} - -void GameState::PlaceNewSnakePart(sf::Vector2f location) -{ - gameBoard.at(location.y).at(location.x) = 'o'; - return; -} - -void GameState::PlayerWantsToContinue(void) -{ - graphics->CheckContinue(); - return; -} - -// Generates new food until not colliding with player -void GameState::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') - { - isUpdated = true; - playerFood.GenerateNewFood(GetGameBoundaries()); - newLocation = playerFood.GetFoodLocation(); - } - if (isUpdated) - gameBoard.at(newLocation.y).at(newLocation.x) = 'x'; - return; -} - -void GameState::ResetGameBoard(void) -{ - gameBoard.clear(); - sf::Vector2f boardDimensions = GetGameBoundaries(); - std::vector 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(); - } - return; -} - diff --git a/src/main.cpp b/src/main.cpp index d1b266b..74082b6 100755 --- a/src/main.cpp +++ b/src/main.cpp @@ -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(); } diff --git a/src/playerinterface.cpp b/src/playerinterface.cpp new file mode 100755 index 0000000..00bc9d5 --- /dev/null +++ b/src/playerinterface.cpp @@ -0,0 +1,211 @@ +#include "playerinterface.hpp" +#include + +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 >* 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 >* 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(location)); + else if (letterOnBoard == 'x') + DrawFood(static_cast(location)); + else + DrawEmpty(static_cast(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(kGridSize); + drawObject.setPosition(location); + drawObject.setFillColor(sf::Color::Black); + gameWindow.draw(drawObject); + return; + } + + void SFML::DrawFood(sf::Vector2f location) + { + location *= static_cast(kGridSize); + drawObject.setPosition(location); + drawObject.setFillColor(sf::Color::Red); + gameWindow.draw(drawObject); + return; + } + + void SFML::DrawSnake(sf::Vector2f location) + { + location *= static_cast(kGridSize); + drawObject.setPosition(location); + drawObject.setFillColor(sf::Color::Green); + gameWindow.draw(drawObject); + return; + } +} diff --git a/src/snake.cpp b/src/snake.cpp index 510e71f..a53a8af 100755 --- a/src/snake.cpp +++ b/src/snake.cpp @@ -1,57 +1,104 @@ // Snake.cpp #include +#include #include -#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)); - return; -} + // 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(); - snakeBody.pop(); - return tailLocation; -} + sf::Vector2f Snake::GetHeadLocation(void) + { + return headLocation; + } -void Snake::UpdateDirection(int newDirection) -{ - snakeDirection = newDirection; - return; -} + // 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; + } -// 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; -} + // Removes tail of snake + void Snake::Pop(void) + { + *(snakeBody.front().get()) = ' '; + snakeBody.pop(); + return; + } -void Snake::CreateNewHead(sf::Vector2f headLocation) -{ - snakeBody.push(headLocation); - 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() + { + 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; + } } diff --git a/src/snakefood.cpp b/src/snakefood.cpp deleted file mode 100755 index f252067..0000000 --- a/src/snakefood.cpp +++ /dev/null @@ -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; -}