diff --git a/src/GameState.cpp b/src/GameState.cpp index a81ae0b..c293cf6 100644 --- a/src/GameState.cpp +++ b/src/GameState.cpp @@ -1,6 +1,8 @@ -#include +// #include #include #include +#include "Snake.h" +#include "SnakeFood.h" #include "GameState.h" GameState::GameState() @@ -18,3 +20,40 @@ GameState::GameState(int newHorizontal, int newVertical) sf::RenderWindow tempWindow(gameVideoMode, "SnakePlusPlus"); return; } + +void GameState::startNewGame() +{ + gameWindow.create(gameVideoMode, "SnakePlusPlus"); + sf::Time delay = sf::milliseconds(25); + int snakeDirection = 0; + Snake player(sf::Vector2f(25,25)); + SnakeFood playerFood(sf::Vector2f(25,25)); + // sf::RectangleShape snakeHead(sf::Vector2f(25,25)); + sf::RectangleShape snakeFood(sf::Vector2f(25,25)); + snakeFood.setFillColor(sf::Color::Red); + snakeFood.setPosition(25,25); + + while (gameWindow.isOpen()) + { + sf::Event event; + while (gameWindow.pollEvent(event)) + { + if ((event.type == sf::Event::Closed) || (sf::Keyboard::isKeyPressed(sf::Keyboard::Escape))) + gameWindow.close(); + } + if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left)) + snakeDirection = 1; + if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up)) + snakeDirection = 2; + if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down)) + snakeDirection = 3; + if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right)) + snakeDirection = 4; + player.MoveSnake(playerFood.snakeFoodObject); + gameWindow.clear(); + player.DisplaySnake(gameWindow); + gameWindow.draw(playerFood.snakeFoodObject); + gameWindow.display(); + sf::sleep(delay); + } +} diff --git a/src/GameState.h b/src/GameState.h index eec85ae..ad8c7bd 100644 --- a/src/GameState.h +++ b/src/GameState.h @@ -1,6 +1,8 @@ // GameState.h #ifndef GAMESTATE_H #define GAMESTATE_H +#include +#include class GameState { @@ -8,12 +10,13 @@ private: public: sf::VideoMode gameVideoMode; sf::RenderWindow gameWindow; + GameState(); + GameState(int newHorizontal, int newVertical); + void startNewGame(); /* gameGridHorizontal = (videoSizeHorizontal // 25) * 25; gameGridVertical = (videoSizeVertical // 25) * 25; */ - GameState(); - GameState(int newHorizontal, int newVertical); // sf::Vector2f GetGameBoundaries(); }; diff --git a/src/SnakeFood.cpp b/src/SnakeFood.cpp new file mode 100644 index 0000000..464ea25 --- /dev/null +++ b/src/SnakeFood.cpp @@ -0,0 +1,24 @@ +#include +#include "SnakeFood.h" + +SnakeFood::SnakeFood() +{ + snakeFoodObject.setSize(sf::Vector2f(25,25)); + snakeFoodObject.setFillColor(sf::Color::Red); +} + +SnakeFood::SnakeFood(sf::Vector2f snakeFoodSize) +{ + snakeFoodObject.setSize(snakeFoodSize); + snakeFoodObject.setFillColor(sf::Color::Red); +} + +void SnakeFood::GenerateNewLocation(int maxLocation) +{ + sf::Vector2f newPosition; + std::default_random_engine generator; + std::uniform_int_distribution distribution(0, maxLocation); + newPosition.x = distribution(generator); + newPosition.y = distribution(generator); + snakeFoodObject.setPosition(newPosition); +} diff --git a/src/SnakeFood.h b/src/SnakeFood.h new file mode 100644 index 0000000..4c25630 --- /dev/null +++ b/src/SnakeFood.h @@ -0,0 +1,16 @@ +// SnakeFood.h +#ifndef SNAKEFOOD_H +#define SNAKEFOOD_H +#include + +class SnakeFood +{ +private: +public: + sf::RectangleShape snakeFoodObject; + SnakeFood(); + SnakeFood(sf::Vector2f snakeFoodSize); + void GenerateNewLocation(int maxLocation); +}; + +#endif diff --git a/src/main.cpp b/src/main.cpp index e6ed2ae..bd09bb5 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,44 +1,7 @@ -#include -#include -#include -#include #include "GameState.h" -#include "Snake.h" int main() { GameState newGame; - newGame.gameWindow.create(newGame.gameVideoMode, "SnakePlusPlus"); - sf::RenderWindow *window = &newGame.gameWindow; - sf::Time delay = sf::milliseconds(25); - int snakeDirection = 0; - Snake Player(sf::Vector2f(25,25)); - sf::RectangleShape snakeHead(sf::Vector2f(25,25)); - sf::RectangleShape snakeFood(sf::Vector2f(25,25)); - snakeFood.setFillColor(sf::Color::Red); - snakeFood.setPosition(25,25); - - while (window->isOpen()) - { - sf::Event event; - while (window->pollEvent(event)) - { - if ((event.type == sf::Event::Closed) || (sf::Keyboard::isKeyPressed(sf::Keyboard::Escape))) - window->close(); - } - if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left)) - snakeDirection = 1; - if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up)) - snakeDirection = 2; - if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down)) - snakeDirection = 3; - if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right)) - snakeDirection = 4; - Player.MoveSnake(snakeFood); - window->clear(); - window->draw(snakeFood); - Player.DisplaySnake(*window); - window->display(); - sf::sleep(delay); - } + newGame.startNewGame(); }