-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgameWindow.cpp
64 lines (51 loc) · 1.42 KB
/
gameWindow.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include <SFML/Graphics.hpp>
#include "gameWindow.hpp"
GameWindow::GameWindow(int x, int y)
{
this->setSettings(x, y);
this->window.create(sf::VideoMode(x, y), "Snake");
this->window.setFramerateLimit(20);
}
void GameWindow::setSettings(int x, int y)
{
this->gameSettings.x = x;
this->gameSettings.y = y;
}
GameWindow::~GameWindow() {}
void GameWindow::runningLoop()
{
movementDirection direction = right;
while (this->window.isOpen())
{
sf::Event event;
while (this->window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
{
this->window.close();
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::W))
{
direction = up;
}
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::S))
{
direction = down;
}
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::D))
{
direction = right;
}
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::A))
{
direction = left;
}
}
this->window.clear(sf::Color::Black);
snake.snakeMove(direction);
snake.checkCollisions(this->gameSettings, &this->food);
food.drawFood(&this->window);
snake.drawSnake(&this->window);
this->window.display();
}
}