-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsnake.cpp
More file actions
217 lines (185 loc) · 5.71 KB
/
Copy pathsnake.cpp
File metadata and controls
217 lines (185 loc) · 5.71 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
#include <windows.h>
#include <conio.h>
#include <chrono>
#include <deque>
#include <iostream>
#include <random>
#include <string>
struct Point {
int x;
int y;
bool operator==(const Point& other) const { return x == other.x && y == other.y; }
};
enum class Dir { Up, Down, Left, Right };
static void set_cursor_visible(bool visible) {
HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_CURSOR_INFO ci{};
GetConsoleCursorInfo(h, &ci);
ci.bVisible = visible ? TRUE : FALSE;
SetConsoleCursorInfo(h, &ci);
}
static void move_cursor_home() {
HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE);
COORD pos{0, 0};
SetConsoleCursorPosition(h, pos);
}
static void clear_screen() {
HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_SCREEN_BUFFER_INFO csbi{};
GetConsoleScreenBufferInfo(h, &csbi);
DWORD cells = static_cast<DWORD>(csbi.dwSize.X) * static_cast<DWORD>(csbi.dwSize.Y);
DWORD written = 0;
COORD home{0, 0};
FillConsoleOutputCharacterA(h, ' ', cells, home, &written);
FillConsoleOutputAttribute(h, csbi.wAttributes, cells, home, &written);
SetConsoleCursorPosition(h, home);
}
static bool is_opposite(Dir a, Dir b) {
if (a == Dir::Up && b == Dir::Down) return true;
if (a == Dir::Down && b == Dir::Up) return true;
if (a == Dir::Left && b == Dir::Right) return true;
if (a == Dir::Right && b == Dir::Left) return true;
return false;
}
static bool read_input(Dir& dir, bool& paused, bool& quit) {
bool changed = false;
while (_kbhit()) {
int ch = _getch();
if (ch == 0 || ch == 224) {
int a = _getch();
Dir nd = dir;
if (a == 72) nd = Dir::Up;
if (a == 80) nd = Dir::Down;
if (a == 75) nd = Dir::Left;
if (a == 77) nd = Dir::Right;
if (!is_opposite(dir, nd)) {
dir = nd;
changed = true;
}
continue;
}
if (ch == 'p' || ch == 'P' || ch == ' ') {
paused = !paused;
} else if (ch == 'q' || ch == 'Q' || ch == 27) {
quit = true;
}
}
return changed;
}
static Point step_from(Point p, Dir d) {
if (d == Dir::Up) p.y -= 1;
if (d == Dir::Down) p.y += 1;
if (d == Dir::Left) p.x -= 1;
if (d == Dir::Right) p.x += 1;
return p;
}
static bool contains_body(const std::deque<Point>& snake, const Point& p) {
for (const auto& s : snake) {
if (s == p) return true;
}
return false;
}
static Point spawn_food(std::mt19937& rng, int w, int h, const std::deque<Point>& snake) {
std::uniform_int_distribution<int> dx(1, w - 2);
std::uniform_int_distribution<int> dy(1, h - 2);
for (;;) {
Point p{dx(rng), dy(rng)};
if (!contains_body(snake, p)) return p;
}
}
int main() {
constexpr int W = 50;
constexpr int H = 22;
HANDLE hout = GetStdHandle(STD_OUTPUT_HANDLE);
DWORD out_mode = 0;
GetConsoleMode(hout, &out_mode);
SetConsoleMode(hout, out_mode | ENABLE_PROCESSED_OUTPUT);
set_cursor_visible(false);
clear_screen();
std::mt19937 rng{static_cast<unsigned>(GetTickCount())};
std::deque<Point> snake;
snake.push_back({W / 2, H / 2});
snake.push_back({W / 2 - 1, H / 2});
snake.push_back({W / 2 - 2, H / 2});
Dir dir = Dir::Right;
Point food = spawn_food(rng, W, H, snake);
int score = 0;
int delay_ms = 600;
bool paused = false;
bool quit = false;
bool game_over = false;
auto last = std::chrono::steady_clock::now();
std::string frame;
frame.reserve(static_cast<size_t>(W + 2) * static_cast<size_t>(H + 2));
while (!quit) {
read_input(dir, paused, quit);
if (quit) break;
auto now = std::chrono::steady_clock::now();
auto elapsed_ms = std::chrono::duration_cast<std::chrono::milliseconds>(now - last).count();
if (!paused && !game_over && elapsed_ms >= delay_ms) {
last = now;
Point head = snake.front();
Point next = step_from(head, dir);
if (next.x <= 0 || next.x >= W - 1 || next.y <= 0 || next.y >= H - 1) {
game_over = true;
} else {
bool hits_self = false;
for (size_t i = 0; i < snake.size(); ++i) {
if (snake[i] == next) {
hits_self = true;
break;
}
}
if (hits_self) {
game_over = true;
}
}
if (!game_over) {
snake.push_front(next);
if (next == food) {
score += 10;
if (delay_ms > 85) delay_ms -= 1;
food = spawn_food(rng, W, H, snake);
} else {
snake.pop_back();
}
}
}
frame.clear();
frame += "Snake | 方向键移动 P/空格暂停 Q/ESC退出";
frame += "\r\n";
frame += "Score: " + std::to_string(score);
frame += " Speed: " + std::to_string(1000 / delay_ms);
if (paused) frame += " [PAUSED]";
if (game_over) frame += " [GAME OVER: Press Q to quit]";
frame += "\r\n";
for (int y = 0; y < H; ++y) {
for (int x = 0; x < W; ++x) {
char c = ' ';
if (y == 0 || y == H - 1 || x == 0 || x == W - 1) {
c = '#';
} else if (food.x == x && food.y == y) {
c = '@';
} else {
bool placed = false;
for (size_t i = 0; i < snake.size(); ++i) {
if (snake[i].x == x && snake[i].y == y) {
c = (i == 0) ? 'O' : 'o';
placed = true;
break;
}
}
if (!placed) c = ' ';
}
frame.push_back(c);
}
frame += "\r\n";
}
move_cursor_home();
DWORD written = 0;
WriteConsoleA(hout, frame.c_str(), static_cast<DWORD>(frame.size()), &written, nullptr);
Sleep(8);
}
set_cursor_visible(true);
return 0;
}