Repository navigation
Expand file tree
/
Copy pathSnake.cpp
More file actions
204 lines (178 loc) · 6.03 KB
/
Copy pathSnake.cpp
File metadata and controls
204 lines (178 loc) · 6.03 KB
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
#include <SFML/Graphics.hpp>
#include <deque>
#include <optional>
#include <random>
#include <string>
int main()
{
const unsigned windowW = 800, windowH = 600;
const int cellSize = 20;
const int gridW = static_cast<int>(windowW / cellSize);
const int gridH = static_cast<int>(windowH / cellSize);
sf::RenderWindow window(sf::VideoMode({windowW, windowH}), "Snake");
window.setFramerateLimit(60);
sf::Font font;
if (!font.openFromFile("C:/Windows/Fonts/arial.ttf"))
return -1;
sf::Text hudText(font, "", 20);
hudText.setFillColor(sf::Color::White);
hudText.setPosition({10.f, 5.f});
sf::Text centerText(font, "", 32);
centerText.setFillColor(sf::Color::White);
std::mt19937 rng(std::random_device{}());
std::uniform_int_distribution<int> distX(0, gridW - 1);
std::uniform_int_distribution<int> distY(0, gridH - 1);
std::deque<sf::Vector2i> snake;
sf::Vector2i direction;
sf::Vector2i pendingDirection;
sf::Vector2i food;
int score = 0;
int best = 0;
bool gameOver = false;
const float moveInterval = 0.1f;
float moveTimer = 0.f;
auto spawnFood = [&]()
{
while (true)
{
sf::Vector2i pos(distX(rng), distY(rng));
bool onSnake = false;
for (const auto& seg : snake)
{
if (seg == pos)
{
onSnake = true;
break;
}
}
if (!onSnake)
{
food = pos;
return;
}
}
};
auto resetGame = [&]()
{
snake.clear();
const sf::Vector2i start(gridW / 2, gridH / 2);
snake.push_back(start);
snake.push_back({start.x - 1, start.y});
snake.push_back({start.x - 2, start.y});
direction = {1, 0};
pendingDirection = direction;
score = 0;
moveTimer = 0.f;
gameOver = false;
spawnFood();
};
resetGame();
sf::Clock clock;
while (window.isOpen())
{
while (const std::optional event = window.pollEvent())
{
if (event->is<sf::Event::Closed>())
window.close();
if (const auto* keyPressed = event->getIf<sf::Event::KeyPressed>())
{
switch (keyPressed->code)
{
case sf::Keyboard::Key::Up:
case sf::Keyboard::Key::W:
if (direction.y == 0)
pendingDirection = {0, -1};
break;
case sf::Keyboard::Key::Down:
case sf::Keyboard::Key::S:
if (direction.y == 0)
pendingDirection = {0, 1};
break;
case sf::Keyboard::Key::Left:
case sf::Keyboard::Key::A:
if (direction.x == 0)
pendingDirection = {-1, 0};
break;
case sf::Keyboard::Key::Right:
case sf::Keyboard::Key::D:
if (direction.x == 0)
pendingDirection = {1, 0};
break;
case sf::Keyboard::Key::Enter:
if (gameOver)
resetGame();
break;
default:
break;
}
}
}
const float dt = clock.restart().asSeconds();
if (!gameOver)
{
moveTimer += dt;
if (moveTimer >= moveInterval)
{
moveTimer -= moveInterval;
direction = pendingDirection;
const sf::Vector2i newHead = snake.front() + direction;
bool collided = newHead.x < 0 || newHead.x >= gridW || newHead.y < 0 || newHead.y >= gridH;
if (!collided)
{
for (std::size_t i = 0; i + 1 < snake.size(); ++i)
{
if (snake[i] == newHead)
{
collided = true;
break;
}
}
}
if (collided)
{
if (score > best)
best = score;
gameOver = true;
}
else
{
snake.push_front(newHead);
if (newHead == food)
{
score++;
if (score > best)
best = score;
spawnFood();
}
else
{
snake.pop_back();
}
}
}
}
window.clear(sf::Color(30, 30, 30));
sf::RectangleShape cell({static_cast<float>(cellSize - 1), static_cast<float>(cellSize - 1)});
cell.setFillColor(sf::Color::Green);
for (const auto& seg : snake)
{
cell.setPosition({static_cast<float>(seg.x * cellSize), static_cast<float>(seg.y * cellSize)});
window.draw(cell);
}
cell.setFillColor(sf::Color::Red);
cell.setPosition({static_cast<float>(food.x * cellSize), static_cast<float>(food.y * cellSize)});
window.draw(cell);
hudText.setString("Score: " + std::to_string(score) + " Best: " + std::to_string(best));
window.draw(hudText);
if (gameOver)
{
centerText.setString("Game Over! Press Enter to Restart");
const sf::FloatRect bounds = centerText.getLocalBounds();
centerText.setOrigin({bounds.size.x / 2.f, bounds.size.y / 2.f});
centerText.setPosition({windowW / 2.f, windowH / 2.f});
window.draw(centerText);
}
window.display();
}
return 0;
}