-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
180 lines (152 loc) · 5.92 KB
/
Copy pathmain.cpp
File metadata and controls
180 lines (152 loc) · 5.92 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
#include <iostream>
#include <list>
#include <thread>
#include <chrono>
#include <vector>
#include <string>
#include <termios.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/ioctl.h>
using namespace std;
struct sSnakeSegment {
int x;
int y;
};
// Função para obter o tamanho do terminal
void getTerminalSize(int &width, int &height) {
struct winsize w;
ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
width = w.ws_col;
height = w.ws_row;
}
// Global para restaurar o terminal
struct termios orig_termios;
void restoreTerminal() {
tcsetattr(STDIN_FILENO, TCSANOW, &orig_termios);
printf("\e[?25h\e[2J\e[H");
cout << flush;
}
void setupTerminal() {
tcgetattr(STDIN_FILENO, &orig_termios);
atexit(restoreTerminal); // Garante a restauração ao sair
struct termios newt = orig_termios;
newt.c_lflag &= ~(ICANON | ECHO); // Desativa modo canônico e echo
tcsetattr(STDIN_FILENO, TCSANOW, &newt);
// Configura stdin para não-bloqueante
int oldf = fcntl(STDIN_FILENO, F_GETFL, 0);
fcntl(STDIN_FILENO, F_SETFL, oldf | O_NONBLOCK);
}
// Função para ler teclado simplificada
int kbhit(void) {
int ch = getchar();
if (ch != EOF) {
ungetc(ch, stdin);
return 1;
}
return 0;
}
int main() {
setupTerminal();
int nScreenWidth, nScreenHeight;
getTerminalSize(nScreenWidth, nScreenHeight);
// Inicialização do Jogo
list<sSnakeSegment> snake = {
{nScreenWidth / 2, nScreenHeight / 2},
{nScreenWidth / 2 + 1, nScreenHeight / 2},
{nScreenWidth / 2 + 2, nScreenHeight / 2}
};
int nFoodX = rand() % nScreenWidth;
int nFoodY = (rand() % (nScreenHeight - 4)) + 3;
int nScore = 0;
int nSnakeDirection = 3; // 0: Norte, 1: Leste, 2: Sul, 3: Oeste
bool bDead = false;
// Variáveis para controle de delta updates
string lastHeader = "";
vector<char> lastField;
int lastW = 0, lastH = 0;
// Esconder cursor e limpar a tela totalmente uma única vez
printf("\e[?25l\e[2J");
while (!bDead) {
// Verifica tamanho atual
getTerminalSize(nScreenWidth, nScreenHeight);
bool bResized = (nScreenWidth != lastW || nScreenHeight != lastH);
// 1. Timing & Input
this_thread::sleep_for(chrono::milliseconds(100));
if (kbhit()) {
char key = getchar();
if (key == 'w' && nSnakeDirection != 2) nSnakeDirection = 0;
if (key == 'd' && nSnakeDirection != 3) nSnakeDirection = 1;
if (key == 's' && nSnakeDirection != 0) nSnakeDirection = 2;
if (key == 'a' && nSnakeDirection != 1) nSnakeDirection = 3;
}
// 2. Game Logic
sSnakeSegment nextNode = snake.front();
switch (nSnakeDirection) {
case 0: nextNode.y--; break;
case 1: nextNode.x++; break;
case 2: nextNode.y++; break;
case 3: nextNode.x--; break;
}
if (nextNode.x == nFoodX && nextNode.y == nFoodY) {
nScore++;
while (true) {
nFoodX = rand() % nScreenWidth;
nFoodY = (rand() % (nScreenHeight - 4)) + 3;
bool bOnSnake = false;
for (auto const& s : snake) if (s.x == nFoodX && s.y == nFoodY) bOnSnake = true;
if (!bOnSnake) break;
}
} else {
snake.pop_back();
}
if (nextNode.x < 0 || nextNode.x >= nScreenWidth || nextNode.y < 3 || nextNode.y >= nScreenHeight) bDead = true;
for (auto const& s : snake) if (s.x == nextNode.x && s.y == nextNode.y) bDead = true;
if (!bDead) snake.push_front(nextNode);
// 3. Render Logic (Surgical Updates)
string currentHeader = " SNAKE GAME - Pontos: " + to_string(nScore) + " | Tamanho: " + to_string(nScreenWidth) + "x" + to_string(nScreenHeight);
int nFieldHeight = (nScreenHeight - 3 > 0) ? nScreenHeight - 3 : 0;
vector<char> currentField(nScreenWidth * nFieldHeight, ' ');
if (nFoodY >= 3 && nFoodY < nScreenHeight && nFoodX >= 0 && nFoodX < nScreenWidth)
currentField[(nFoodY - 3) * nScreenWidth + nFoodX] = 'X';
for (auto const& s : snake) {
if (s.y >= 3 && s.y < nScreenHeight && s.x >= 0 && s.x < nScreenWidth) {
currentField[(s.y - 3) * nScreenWidth + s.x] = (s.x == snake.front().x && s.y == snake.front().y) ? '@' : '#';
}
}
string updates = "";
if (bResized) {
// Redesenho total em caso de redimensionamento
updates += "\e[2J\e[H";
for (int i = 0; i < nScreenWidth; i++) updates += "=";
updates += "\e[2;1H\e[K" + currentHeader;
updates += "\e[3;1H";
for (int i = 0; i < nScreenWidth; i++) updates += "=";
for (int y = 0; y < nFieldHeight; y++) {
updates += "\e[" + to_string(y + 4) + ";1H";
for (int x = 0; x < nScreenWidth; x++) updates += currentField[y * nScreenWidth + x];
}
lastW = nScreenWidth; lastH = nScreenHeight;
lastField = currentField;
lastHeader = currentHeader;
} else {
// Delta updates: apenas o que mudou
if (currentHeader != lastHeader) {
updates += "\e[2;1H\e[K" + currentHeader;
lastHeader = currentHeader;
}
for (int i = 0; i < (int)currentField.size(); i++) {
if (i >= (int)lastField.size() || currentField[i] != lastField[i]) {
int x = i % nScreenWidth;
int y = i / nScreenWidth;
updates += "\e[" + to_string(y + 4) + ";" + to_string(x + 1) + "H";
updates += currentField[i];
}
}
lastField = currentField;
}
cout << updates << flush;
}
cout << "GAME OVER! Pontuação final: " << nScore << endl;
return 0;
}