-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathGameScreen.cpp
51 lines (37 loc) · 1.02 KB
/
GameScreen.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <SFML/Graphics.hpp>
#include <random>
#include <memory>
#include "GameScreen.h"
#include "GameOverScreen.h"
#include "Game.h"
using namespace sfSnake;
GameScreen::GameScreen() : snake_()
{
}
void GameScreen::handleInput(sf::RenderWindow& window)
{
snake_.handleInput();
}
void GameScreen::update(sf::Time delta)
{
if (fruit_.size() == 0)
generateFruit();
snake_.update(delta);
snake_.checkFruitCollisions(fruit_);
if (snake_.hitSelf())
Game::Screen = std::make_shared<GameOverScreen>(snake_.getSize());
}
void GameScreen::render(sf::RenderWindow& window)
{
snake_.render(window);
for (auto fruit : fruit_)
fruit.render(window);
}
void GameScreen::generateFruit()
{
static std::default_random_engine engine;
engine.seed(time(NULL));
static std::uniform_int_distribution<int> xDistribution(0, Game::Width - SnakeNode::Width);
static std::uniform_int_distribution<int> yDistribution(0, Game::Height - SnakeNode::Height);
fruit_.push_back(Fruit(sf::Vector2f(xDistribution(engine), yDistribution(engine))));
}