-
Notifications
You must be signed in to change notification settings - Fork 0
/
SnakeGame.cpp
222 lines (170 loc) · 4.51 KB
/
SnakeGame.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
#include "SnakeGame.hpp"
#include <random>
#include <thread>
#include <chrono>
#include <iostream>
using namespace Game;
inline Coords SnakeGame::generateRandomCoords()
{
std::random_device rd;
std::mt19937 generator(rd());
std::uniform_int_distribution<size_t> distribution(5, 95);
size_t y = distribution(generator) * 10;
y = y > WINDOW_HEIGHT ? (y - WINDOW_HEIGHT) : y;
Coords coord = { distribution(generator) * 10, y };
return coord;
}
void SnakeGame::moveSnake(sf::Keyboard::Key key)
{
lastKey = key;
if (head->node != nullptr)
updateNodeCoords(head->node, head->coords);
if (head->coords == food->coords) {
food->coords = generateRandomCoords();
addNode();
}
switch (key) {
case sf::Keyboard::Up:
head->coords.y -= 10;
break;
case sf::Keyboard::Left:
head->coords.x -= 10;
break;
case sf::Keyboard::Right:
head->coords.x += 10;
break;
case sf::Keyboard::Down:
head->coords.y += 10;
break;
}
}
void SnakeGame::updateNodeCoords(std::shared_ptr<Snake> node, const Coords &coords)
{
if (node->node != nullptr)
updateNodeCoords(node->node, node->coords);
node->coords = coords;
}
void SnakeGame::drawSnake()
{
sf::RectangleShape snake(sf::Vector2f(10, 10));
snake.setFillColor(sf::Color::Green);
std::shared_ptr<Snake> currentNode = head;
do {
snake.setPosition((float)currentNode->coords.x, (float)currentNode->coords.y);
window->draw(snake);
currentNode = currentNode->node;
} while (currentNode != nullptr);
}
void SnakeGame::drawFood()
{
sf::RectangleShape foodRec(sf::Vector2f(10, 10));
foodRec.setFillColor(sf::Color::Red);
foodRec.setPosition((float)food->coords.x, (float)food->coords.y);
window->draw(foodRec);
}
std::shared_ptr<Snake> SnakeGame::addNode()
{
head->size++;
std::shared_ptr<Snake> currentNode = head;
while (currentNode->node != nullptr)
currentNode = currentNode->node;
currentNode->node = std::make_shared<Snake>();
currentNode->node->size = 1;
currentNode->node->coords = currentNode->coords;
currentNode->node->node = nullptr;
return currentNode->node;
}
void SnakeGame::checkCollisions()
{
if (head->coords.x == 0 || head->coords.x == WINDOW_WIDTH ||
head->coords.y == 0 || head->coords.y == WINDOW_HEIGHT)
{
GAME_OVER = true;
return;
}
std::shared_ptr<Snake> currentNode = head->node;
while (currentNode != nullptr) {
if (currentNode->coords == head->coords) {
GAME_OVER = true;
return;
}
currentNode = currentNode->node;
}
}
void SnakeGame::watchEvents(sf::Event &event)
{
while (window->pollEvent(event)) {
switch (event.type) {
case sf::Event::Closed:
window->close();
break;
case sf::Event::KeyPressed:
moveSnake(event.key.code);
break;
}
}
moveSnake(lastKey);
std::this_thread::sleep_for(std::chrono::milliseconds(30));
}
void Game::SnakeGame::init()
{
GAME_OVER = false;
head = std::make_shared<Snake>();
head->size = 1;
head->coords = generateRandomCoords();
head->node = nullptr;
food = std::make_shared<Food>();
food->coords = generateRandomCoords();
// Moves right by default
lastKey = sf::Keyboard::Right;
window = std::make_shared<sf::RenderWindow>(
sf::VideoMode(WINDOW_WIDTH, WINDOW_HEIGHT),
"Snake Game",
sf::Style::Titlebar | sf::Style::Close);
}
void SnakeGame::run()
{
init();
while (window->isOpen()) {
sf::Event event;
if (GAME_OVER) {
gameOver();
if (window->pollEvent(event) &&
event.type == sf::Event::KeyPressed &&
event.key.code == sf::Keyboard::Enter)
{
init();
}
continue;
}
watchEvents(event);
checkCollisions();
window->clear(sf::Color::Black);
drawFood();
drawSnake();
window->display();
}
}
void SnakeGame::gameOver()
{
GAME_OVER = true;
sf::Font font;
if (!font.loadFromFile("font.ttf")) {
throw "Failed loading font!!";
}
sf::Text gameOverText("Game Over", font, 50),
sub("Press \"Enter\" to go back to play again", font, 20);
sf::FloatRect textBounds = gameOverText.getLocalBounds();
unsigned int windowWidth = window->getSize().x;
unsigned int windowHeight = window->getSize().y;
gameOverText.setFillColor(sf::Color::Red);
gameOverText.setOrigin(textBounds.left + textBounds.width / 2.0f, textBounds.top + textBounds.height / 2.0f);
gameOverText.setPosition(windowWidth / 2.0f, windowHeight / 2.3f);
textBounds = sub.getLocalBounds();
sub.setOrigin(textBounds.left + textBounds.width / 2.0f, textBounds.top + textBounds.height / 2.0f);
sub.setPosition(windowWidth / 2.0f, windowHeight / 1.8f);
window->clear(sf::Color::Black);
window->draw(gameOverText);
window->draw(sub);
window->display();
}