Game window now runs under GameState

Game window now runs under GameState and removed game from being hosted in main()
This commit is contained in:
TriantaTV
2022-08-02 21:17:23 -05:00
parent f64577d2cd
commit eae6b4c70d
3 changed files with 43 additions and 40 deletions
+37
View File
@@ -1,6 +1,7 @@
#include <iostream>
#include <SFML\Graphics.hpp>
#include <SFML\System.hpp>
#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);
}
}