From eae6b4c70db60bbf1ff4e6be97248eabdbbbb29d Mon Sep 17 00:00:00 2001 From: TriantaTV <56975502+TriantaTV@users.noreply.github.com> Date: Tue, 2 Aug 2022 21:17:23 -0500 Subject: [PATCH] Game window now runs under GameState Game window now runs under GameState and removed game from being hosted in main() --- src/GameState.cpp | 37 +++++++++++++++++++++++++++++++++++++ src/GameState.h | 7 +++++-- src/main.cpp | 39 +-------------------------------------- 3 files changed, 43 insertions(+), 40 deletions(-) diff --git a/src/GameState.cpp b/src/GameState.cpp index a81ae0b..2aef0b8 100644 --- a/src/GameState.cpp +++ b/src/GameState.cpp @@ -1,6 +1,7 @@ #include #include #include +#include "Snake.h" #include "GameState.h" GameState::GameState() @@ -18,3 +19,39 @@ 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)); + 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(snakeFood); + gameWindow.clear(); + gameWindow.draw(snakeFood); + Player.DisplaySnake(gameWindow); + 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/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(); }