Merge pull request #8 from TriantaTV/Development
Game is now in GameState and snakeFood has a SnakeFood class
This commit is contained in:
commit
766c223578
@ -1,6 +1,8 @@
|
||||
#include <iostream>
|
||||
// #include <iostream>
|
||||
#include <SFML\Graphics.hpp>
|
||||
#include <SFML\System.hpp>
|
||||
#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);
|
||||
}
|
||||
}
|
||||
|
@ -1,6 +1,8 @@
|
||||
// GameState.h
|
||||
#ifndef GAMESTATE_H
|
||||
#define GAMESTATE_H
|
||||
#include <SFML\Graphics.hpp>
|
||||
#include <SFML\System.hpp>
|
||||
|
||||
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();
|
||||
};
|
||||
|
||||
|
24
src/SnakeFood.cpp
Normal file
24
src/SnakeFood.cpp
Normal file
@ -0,0 +1,24 @@
|
||||
#include <random>
|
||||
#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<int> distribution(0, maxLocation);
|
||||
newPosition.x = distribution(generator);
|
||||
newPosition.y = distribution(generator);
|
||||
snakeFoodObject.setPosition(newPosition);
|
||||
}
|
16
src/SnakeFood.h
Normal file
16
src/SnakeFood.h
Normal file
@ -0,0 +1,16 @@
|
||||
// SnakeFood.h
|
||||
#ifndef SNAKEFOOD_H
|
||||
#define SNAKEFOOD_H
|
||||
#include <SFML\Graphics.hpp>
|
||||
|
||||
class SnakeFood
|
||||
{
|
||||
private:
|
||||
public:
|
||||
sf::RectangleShape snakeFoodObject;
|
||||
SnakeFood();
|
||||
SnakeFood(sf::Vector2f snakeFoodSize);
|
||||
void GenerateNewLocation(int maxLocation);
|
||||
};
|
||||
|
||||
#endif
|
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 "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();
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user