Repository navigation
Expand file tree
/
Copy pathmain.cpp
More file actions
247 lines (218 loc) · 10.9 KB
/
Copy pathmain.cpp
File metadata and controls
247 lines (218 loc) · 10.9 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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
#include <SDL2/SDL.h>
#include <algorithm>
#include <iostream>
#include <deque>
#include <ctime>
#include <cmath>
int board_width = 20, board_height = 20;
std::deque<std::pair<int, int>> snake;
std::pair<int, int> food;
bool is_over = false;
int steps = 0;
void replace_food() {
if (snake.size() == board_width * board_height) { // Esconde a comida se a serpente já ocupou o tabuleiro
food = std::make_pair(-1, -1);
} else do { // Se não, coloca a comida em algum quadrado vago
food = std::make_pair(rand() % board_width, rand() % board_height);
} while (std::find(snake.begin(), snake.end(), food) != snake.end());
}
void init() {
if (board_width % 2) board_width++;
if (board_height % 2) board_height++;
snake.clear();
snake.push_back(std::make_pair(0, board_height / 2));
snake.push_back(std::make_pair(1, board_height / 2));
snake.push_back(std::make_pair(2, board_height / 2));
replace_food();
is_over = false;
steps = 0;
}
void update() {
if (is_over) { return; }
steps++;
auto head = snake.back();
auto tail = snake.front();
snake.pop_front();
// Cria uma lista com os quatro quadrados ao redor da cabeça.
std::deque<std::pair<int, int>> positions;
positions.push_back(std::make_pair(head.first, head.second - 1)); // Em cima
positions.push_back(std::make_pair(head.first - 1, head.second)); // À esquerda
positions.push_back(std::make_pair(head.first, head.second + 1)); // Em baixo
positions.push_back(std::make_pair(head.first + 1, head.second)); // À direita
// Cria uma lista com as direções possíveis com base na posição da cabeça.
std::deque<int> directions;
if (head.first % 2) { directions.push_back(0); } else { directions.push_back(2); }
if (head.second % 2) { directions.push_back(3); } else { directions.push_back(1); }
// Desconsidera as direções que a farão colidir na parede ou no corpo.
for (auto &direction: directions) {
auto &position = positions[direction];
if (position.first < 0 || position.first >= board_width
|| position.second < 0 || position.second >= board_height
|| std::find(snake.begin(), snake.end(), position) != snake.end()) {
directions.erase(std::find(directions.begin(), directions.end(), direction));
}
}
// Se ainda houver mais de uma direção disponível, escolhe a que irá para mais perto da comida.
if (directions.size() > 1) {
double distanceA = std::sqrt(std::pow(food.first - positions[directions[0]].first, 2)
+ std::pow(food.second - positions[directions[0]].second, 2));
double distanceB = std::sqrt(std::pow(food.first - positions[directions[1]].first, 2)
+ std::pow(food.second - positions[directions[1]].second, 2));
if (distanceA < distanceB) {
head = positions[directions[0]];
} else if (distanceB < distanceA) {
head = positions[directions[1]];
} else { // Se as distâncias forem iguais, escolhe aleatoriamente para prevenir um loop infinito.
head = positions[directions[rand() % 2]];
}
} else {
head = positions[directions[0]];
}
if (head.first < 0 || head.first >= board_width || head.second < 0 || head.second >= board_height) {
std::cout << "Você bateu na parede!" << std::endl;
is_over = true;
return;
}
if (std::find(snake.begin(), snake.end(), head) != snake.end()) {
std::cout << "Você bateu em si mesmo!" << std::endl;
is_over = true;
return;
}
snake.push_back(head);
// Checa se colidiu com a comida.
if (std::find(snake.begin(), snake.end(), food) != snake.end()) {
snake.push_front(tail);
replace_food();
}
if (snake.size() == board_width * board_height) {
std::cout << "Você ganhou com " << steps << " passos!" << std::endl;
is_over = true;
return;
}
}
void render(SDL_Renderer *renderer) {
static Uint64 count = 0;
if (is_over) { count++; } else { count = 0; }
int screen_width, screen_height;
SDL_GetRendererOutputSize(renderer, &screen_width, &screen_height);
int square_size = std::min(screen_width / board_width, screen_height / board_height);
SDL_SetRenderDrawColor(renderer, 255, 255, 255, SDL_ALPHA_OPAQUE);
SDL_RenderClear(renderer);
// Desenha o plano de fundo
SDL_SetRenderDrawColor(renderer, 223, 223, 223, SDL_ALPHA_OPAQUE);
for (int x = 0; x < board_width; x++) {
for (int y = 0; y < board_height; y++) {
SDL_Rect rect = {x * square_size, y * square_size, square_size, square_size};
SDL_RenderDrawLine(renderer, rect.x, rect.y, rect.x + rect.w, rect.y);
SDL_RenderDrawLine(renderer, rect.x + rect.w, rect.y, rect.x + rect.w, rect.y + rect.h);
SDL_RenderDrawLine(renderer, rect.x + rect.w, rect.y + rect.h, rect.x, rect.y + rect.h);
SDL_RenderDrawLine(renderer, rect.x, rect.y + rect.h, rect.x, rect.y);
}
}
// Desenha a serpente
for (int i = 0; i < snake.size(); i++) {
auto &previous = i - 1 >= 0 ? snake[i - 1] : snake[i];
auto ¤t = snake[i];
auto &next = i + 1 < snake.size() ? snake[i + 1] : snake[i];
if (!is_over || count / 30 % 2 || snake.size() == board_width * board_height) {
SDL_SetRenderDrawColor(renderer, 0, 255 - 255 * i / snake.size(), 255 * i / snake.size(), SDL_ALPHA_OPAQUE);
} else { // A serpente pisca em vermelho quando morre.
SDL_SetRenderDrawColor(renderer, 255 * i / snake.size(), 0, 0, SDL_ALPHA_OPAQUE);
}
SDL_Rect rect = {current.first * square_size, current.second * square_size, square_size, square_size};
SDL_RenderFillRect(renderer, &rect);
if (!is_over || count / 30 % 2 || snake.size() != board_width * board_height) {
SDL_SetRenderDrawColor(renderer, 0, 0, 0, SDL_ALPHA_OPAQUE);
} else { // O contorno da serpente pisca em branco quando vence.
SDL_SetRenderDrawColor(renderer, 255, 255, 255, SDL_ALPHA_OPAQUE);
}
// Contorno inteligente da serpente
if ((current.first != previous.first || current.second - 1 != previous.second)
&& (current.first != next.first || current.second - 1 != next.second)) {
SDL_RenderDrawLine(renderer, rect.x, rect.y, rect.x + rect.w, rect.y);
}
if ((current.first + 1 != previous.first || current.second != previous.second)
&& (current.first + 1 != next.first || current.second != next.second)) {
SDL_RenderDrawLine(renderer, rect.x + rect.w, rect.y, rect.x + rect.w, rect.y + rect.h);
}
if ((current.first != previous.first || current.second + 1 != previous.second)
&& (current.first != next.first || current.second + 1 != next.second)) {
SDL_RenderDrawLine(renderer, rect.x + rect.w, rect.y + rect.h, rect.x, rect.y + rect.h);
}
if ((current.first - 1 != previous.first || current.second != previous.second)
&& (current.first - 1 != next.first || current.second != next.second)) {
SDL_RenderDrawLine(renderer, rect.x, rect.y + rect.h, rect.x, rect.y);
}
}
{ // Desenha a comida
SDL_Rect rect = {food.first * square_size, food.second * square_size, square_size, square_size};
SDL_SetRenderDrawColor(renderer, 255, 0, 0, SDL_ALPHA_OPAQUE);
SDL_RenderFillRect(renderer, &rect);
SDL_SetRenderDrawColor(renderer, 0, 0, 0, SDL_ALPHA_OPAQUE);
SDL_RenderDrawLine(renderer, rect.x, rect.y, rect.x + rect.w, rect.y);
SDL_RenderDrawLine(renderer, rect.x + rect.w, rect.y, rect.x + rect.w, rect.y + rect.h);
SDL_RenderDrawLine(renderer, rect.x + rect.w, rect.y + rect.h, rect.x, rect.y + rect.h);
SDL_RenderDrawLine(renderer, rect.x, rect.y + rect.h, rect.x, rect.y);
}
SDL_RenderPresent(renderer);
}
int main(int argc, char *argv[]) {
srand(time(NULL));
if (SDL_Init(SDL_INIT_VIDEO) == 0) {
if (SDL_Window *window = SDL_CreateWindow("Snake", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 641, 641, SDL_WINDOW_RESIZABLE)) {
if (SDL_Renderer *renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED)) {
init();
Uint64 last_tick = SDL_GetPerformanceCounter();
for (bool running = true, fast_forward = false; running;) {
for (SDL_Event e; SDL_PollEvent(&e);) {
switch (e.type) {
case SDL_QUIT:
running = false;
break;
case SDL_KEYDOWN:
switch (e.key.keysym.sym) {
case SDLK_ESCAPE:
if (is_over) {
running = false;
} else {
is_over = true;
}
break;
case SDLK_SPACE:
fast_forward = true;
break;
case SDLK_RETURN:
case SDLK_RETURN2:
case SDLK_KP_ENTER:
init();
break;
}
break;
case SDL_KEYUP:
switch (e.key.keysym.sym) {
case SDLK_SPACE:
fast_forward = false;
break;
}
break;
}
}
update();
auto delta_time = ((SDL_GetPerformanceCounter() - last_tick) * 1000 / (double) SDL_GetPerformanceFrequency());
if (33 > delta_time) {
if (fast_forward) {
continue;
} else {
SDL_Delay(33 - delta_time);
}
}
last_tick = SDL_GetPerformanceCounter();
render(renderer);
}
SDL_DestroyRenderer(renderer);
} else fprintf(stderr, "O renderizador não pôde ser criado! SDL_Error: %s\n", SDL_GetError());
SDL_DestroyWindow(window);
} else fprintf(stderr, "A janela não pôde ser criada! SDL_Error: %s\n", SDL_GetError());
} else fprintf(stderr, "O SDL não pôde ser inicializado! SDL_Error: %s\n", SDL_GetError());
SDL_Quit();
}