Merge pull request #7 from TriantaTV/Development

GameState now handles RenderWindow
This commit is contained in:
TriantaTV 2022-07-29 16:31:29 -05:00 committed by GitHub
commit eb0c19e631
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 26 additions and 31 deletions

View File

@ -5,20 +5,16 @@
GameState::GameState()
{
videoSizeHorizontal = 1024;
videoSizeVertical = 725;
sf::Vector2u newVideoSize(videoSizeHorizontal, videoSizeVertical);
window.setSize(newVideoSize);
window.setTitle("SnakePlusPlus");
sf::VideoMode tempVideoMode(1024, 725);
gameVideoMode = tempVideoMode;
sf::RenderWindow gameWindow(gameVideoMode, "SnakePlusPlus");
return;
}
GameState::GameState(int newHorizontal, int newVertical)
{
videoSizeHorizontal = newHorizontal;
videoSizeVertical = newVertical;
sf::Vector2u newVideoSize(videoSizeHorizontal, videoSizeVertical);
window.setSize(newVideoSize);
window.setTitle("SnakePlusPlus");
sf::VideoMode tempVideoMode(newHorizontal, newVertical);
gameVideoMode = tempVideoMode;
sf::RenderWindow tempWindow(gameVideoMode, "SnakePlusPlus");
return;
}

View File

@ -5,12 +5,16 @@
class GameState
{
private:
int videoSizeHorizontal;
int videoSizeVertical;
sf::RenderWindow window;
public:
sf::VideoMode gameVideoMode;
sf::RenderWindow gameWindow;
/*
gameGridHorizontal = (videoSizeHorizontal // 25) * 25;
gameGridVertical = (videoSizeVertical // 25) * 25;
*/
GameState();
GameState(int newHorizontal, int newVertical);
// sf::Vector2f GetGameBoundaries();
};
#endif

View File

@ -1,35 +1,30 @@
#include <iostream>
#include <stdlib.h>
#include <SFML\Graphics.hpp>
#include <SFML\System.hpp>
#include "GameState.h"
#include "Snake.h"
int main()
{
int videoSizeHorizontal, videoSizeVertical;
videoSizeHorizontal = 1024;
videoSizeVertical = 725;
/*
gameGridHorizontal = (videoSizeHorizontal // 25) * 25;
gameGridVertical = (videoSizeVertical // 25) * 25;
*/
sf::RenderWindow window(sf::VideoMode(videoSizeHorizontal, videoSizeVertical), "SnakePlusPlus");
sf::Time delay = sf::milliseconds(50);
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())
while (window->isOpen())
{
sf::Event event;
while (window.pollEvent(event))
while (window->pollEvent(event))
{
if ((event.type == sf::Event::Closed) || (sf::Keyboard::isKeyPressed(sf::Keyboard::Escape)))
window.close();
window->close();
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
snakeDirection = 1;
@ -40,10 +35,10 @@ int main()
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
snakeDirection = 4;
Player.MoveSnake(snakeFood);
window.clear();
window.draw(snakeFood);
Player.DisplaySnake(window);
window.display();
window->clear();
window->draw(snakeFood);
Player.DisplaySnake(*window);
window->display();
sf::sleep(delay);
}
}