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:
parent
f64577d2cd
commit
eae6b4c70d
@ -1,6 +1,7 @@
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <SFML\Graphics.hpp>
|
#include <SFML\Graphics.hpp>
|
||||||
#include <SFML\System.hpp>
|
#include <SFML\System.hpp>
|
||||||
|
#include "Snake.h"
|
||||||
#include "GameState.h"
|
#include "GameState.h"
|
||||||
|
|
||||||
GameState::GameState()
|
GameState::GameState()
|
||||||
@ -18,3 +19,39 @@ GameState::GameState(int newHorizontal, int newVertical)
|
|||||||
sf::RenderWindow tempWindow(gameVideoMode, "SnakePlusPlus");
|
sf::RenderWindow tempWindow(gameVideoMode, "SnakePlusPlus");
|
||||||
return;
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -1,6 +1,8 @@
|
|||||||
// GameState.h
|
// GameState.h
|
||||||
#ifndef GAMESTATE_H
|
#ifndef GAMESTATE_H
|
||||||
#define GAMESTATE_H
|
#define GAMESTATE_H
|
||||||
|
#include <SFML\Graphics.hpp>
|
||||||
|
#include <SFML\System.hpp>
|
||||||
|
|
||||||
class GameState
|
class GameState
|
||||||
{
|
{
|
||||||
@ -8,12 +10,13 @@ private:
|
|||||||
public:
|
public:
|
||||||
sf::VideoMode gameVideoMode;
|
sf::VideoMode gameVideoMode;
|
||||||
sf::RenderWindow gameWindow;
|
sf::RenderWindow gameWindow;
|
||||||
|
GameState();
|
||||||
|
GameState(int newHorizontal, int newVertical);
|
||||||
|
void startNewGame();
|
||||||
/*
|
/*
|
||||||
gameGridHorizontal = (videoSizeHorizontal // 25) * 25;
|
gameGridHorizontal = (videoSizeHorizontal // 25) * 25;
|
||||||
gameGridVertical = (videoSizeVertical // 25) * 25;
|
gameGridVertical = (videoSizeVertical // 25) * 25;
|
||||||
*/
|
*/
|
||||||
GameState();
|
|
||||||
GameState(int newHorizontal, int newVertical);
|
|
||||||
// sf::Vector2f GetGameBoundaries();
|
// sf::Vector2f GetGameBoundaries();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
39
src/main.cpp
39
src/main.cpp
@ -1,44 +1,7 @@
|
|||||||
#include <iostream>
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <SFML\Graphics.hpp>
|
|
||||||
#include <SFML\System.hpp>
|
|
||||||
#include "GameState.h"
|
#include "GameState.h"
|
||||||
#include "Snake.h"
|
|
||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
GameState newGame;
|
GameState newGame;
|
||||||
newGame.gameWindow.create(newGame.gameVideoMode, "SnakePlusPlus");
|
newGame.startNewGame();
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user