0%

贪吃蛇

很久以前自己写着玩的,已经是上个学期的事情了,优化可能不是很好。但至少运行着不卡。。

用的Visual Studio2015,代码复制到别的编译器可能出问题。

成品地址:链接: http://pan.baidu.com/s/1nvKrL6T 密码: 57m5

实现的功能:蛇能跑能转弯能吃豆子(废话!);按空格暂停,所以进游戏时如果发现蛇动不了,那就是你按了空格,再按一下就好;同时有三种豆子,白色的就是普通的豆子,红色的加速效果,蓝色的减速效果。不能穿墙;还有吃到豆子会有系统警告声,不要被吓到,因为这是我让游戏发出声音的最简单的办法了;

因为是当时学编程以来第一次做个成品,于是做了个极其花哨的开始界面,代码里很多都是花在界面上了,其实都是没用的代码

如果最大化窗口,会花屏,按一下R键可以稍微恢复一点界面;游戏会生成一个文件,用于储存最高分,当然完全没有加密什么的,打开就能看到你的所有成绩,你要篡改成99999也是没问题的。

还有完之前关掉输入法,不然界面显示会有问题,界面出问题了按R键刷新

还有,游戏的对话框按钮鼠标不支持,用左右或上下选择,enter确认

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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
//贪吃蛇
//全屏下一共横里236格,竖里55格
//本次横里80格(或者说是40格),竖里29格
#include<Windows.h>
#include<iostream>
#include<fstream>
#include<ctime>
#include<stdlib.h>
using namespace std;
//-----------------------
struct Xsnake {
unsigned x, y;
Xsnake* next;
}*head;
struct Xfood {
unsigned x, y, colour;//colour:0white 1red 2blue
}Food;
unsigned Score = 0;
unsigned Max = 0;
unsigned Length = 5;
unsigned SleepTime = 150;
unsigned Direction = 8;
unsigned SnakeColour = 0;
int GameStatus = 0;//0:正常,1:ESC关闭游戏,2:撞墙,3:撞尾巴
//-----------------------
int main();
void XWelcome();
void Xstart();
void Xinterface();
void Xgame();
void Xend();
void Pos(unsigned, unsigned);
void SnakeShow(Xsnake*);
void GetSnake(Xsnake*);
Xsnake* SnakeAdd(Xsnake*, unsigned, unsigned);
Xsnake* SnakeMove(Xsnake*, unsigned);
Xsnake* SnakePush_back(Xsnake*, const unsigned, const unsigned);
inline void SnakeTail(Xsnake*, unsigned&, unsigned&);
inline void SnakeClear(unsigned, unsigned);
int SnakeBite(Xsnake*);
void SnakeKey(unsigned&);
void SnakeDel(Xsnake*);
void FoodCreate(Xsnake*, Xfood&);
void FoodShow(const Xfood&);
//-----------------------

void Pos(unsigned x, unsigned y) {
COORD p;
p.X = y;p.Y = x;
HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(h, p);
}
void XWelcome() {
Pos(5, 50);
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 175);
cout << " ";
Sleep(80);
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 10);
Pos(5, 50);cout << " / ̄ ̄ ̄Y ̄ ̄。\ ";
Pos(6, 50);
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 175);
cout << " ";
Sleep(80);
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 10);
Pos(6, 50);cout << " l        l ";
Pos(7, 50);
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 175);
cout << " ";
Sleep(80);
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 10);
Pos(7, 50);cout << "ヽ,,,,,/  ̄ ̄ ̄ ヽ ";
Pos(8, 50);
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 175);
cout << " ";
Sleep(80);
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 10);
Pos(8, 50);cout << " |:::::      l ";
Pos(9, 50);
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 175);
cout << " ";
Sleep(80);
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 10);
Pos(9, 50);cout << " |:::  __  __ | ";
Pos(10, 50);
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 175);
cout << " ";
Sleep(80);
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 10);
Pos(10, 50);cout << " (6  \● ● 丨 ";
Pos(11, 50);
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 175);
cout << " ";
Sleep(80);
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 10);
Pos(11, 50);cout << " !    )..( l ";
Pos(12, 50);
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 175);
cout << " ";
Sleep(80);
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 10);
Pos(12, 50);cout << " ヽ     (三) l ";
Pos(13, 50);
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 175);
cout << " ";
Sleep(80);
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 10);
Pos(13, 50);cout << " /\   二 ノ ";
Pos(14, 50);
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 175);
cout << " ";
Sleep(80);
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 10);
Pos(14, 50);cout << " / ⌒ヽ. ‘ー—一\ ";
Sleep(80);
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 15);
Pos(12, 71);cout << ".";Sleep(120);
Pos(12, 71);cout << " 。";Sleep(120);
Pos(12, 71);cout << " o";Sleep(120);
Pos(12, 71);cout << " o";Sleep(120);
Pos(12, 71);cout << " 0";Sleep(120);
Pos(12, 76);cout << " ";
Pos(11, 84);cout << "o";Sleep(120);
Pos(11, 84);cout << " ";
Pos(10, 85);cout << ".";Sleep(120);
Pos(10, 85);cout << " ";
}
void Xstart() {
XWelcome();
const CONSOLE_CURSOR_INFO cursor{ (DWORD)100,FALSE };
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor);
head = new Xsnake;
head->x = 11;head->y = 38;
head->next = nullptr;
head = SnakeAdd(head, 10, 38);head = SnakeAdd(head, 9, 38);
head = SnakeAdd(head, 8, 38);head = SnakeAdd(head, 7, 38);
ifstream i("snake_data.dat");
if (!i) i.close();
else {
unsigned s;
while (i >> s)
if (s > Max)Max = s;
i.close();
}
Pos(16, 48);cout << "载入完毕,";system("pause");
Pos(16, 48);cout << " 贪吃蛇 by蟹脑板 ";
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 160);
Pos(18, 50);cout << " 开 始 游 戏 ";
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 15);
Pos(20, 50);cout << " 退 出 游 戏 ";
int YN = 8;
while (!GetAsyncKeyState(VK_RETURN)) {
if (GetAsyncKeyState(VK_UP)) {
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 160);
Pos(18, 50);cout << " 开 始 游 戏 ";
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 15);
Pos(20, 50);cout << " 退 出 游 戏 ";
YN = 8;
}
if (GetAsyncKeyState(VK_DOWN)) {
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 15);
Pos(18, 50);cout << " 开 始 游 戏 ";
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 160);
Pos(20, 50);cout << " 退 出 游 戏 ";
YN = 4;
}
}
if (YN == 4) exit(0);
system("cls");
}
void Xinterface() {
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 136);
Pos(0, 0);
cout << "■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■\n";//80x
for (unsigned i = 1;i <= 28;++i)cout << "■\n";
for (unsigned i = 1;i <= 28;++i) {
Pos(i, 78);
cout << "■";
}
cout << endl;
cout << "■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■";
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 15);
Pos(2, 88);cout << "得分:" << Score;
Pos(3, 88);cout << "长度:" << Length;
Pos(4, 88);cout << "速度:" << 300 - SleepTime;
Pos(5, 88);cout << "历史最高成绩:" << Max;
}
void Xgame() {
Xinterface();
unsigned tail_x = 0, tail_y = 0;
FoodCreate(head, Food);
FoodShow(Food);
while (Length < 1026) {
if (Food.x == head->x && Food.y == head->y) {
if (SnakeColour == 1)SleepTime += 40;
else if (SnakeColour == 2)SleepTime -= 40;
if (Food.colour == 1) {
SleepTime -= 40;
Score += 8;
}
else if (Food.colour == 2) {
SleepTime += 40;
Score -= 6;
}
SnakeColour = Food.colour;
Score += 10;
++Length;
head = SnakePush_back(head, tail_x, tail_y);
FoodCreate(head, Food);
FoodShow(Food);
if (SleepTime > 80) SleepTime -= 3;
Pos(2, 93);cout << Score;
Pos(3, 93);cout << Length;
Pos(4, 93);cout << 300 - SleepTime;
Pos(5, 101);cout << Max;
char bel = 7;cout << bel;
}
else SnakeClear(tail_x, tail_y);
SnakeTail(head, tail_x, tail_y);
SnakeShow(head);
Sleep(SleepTime);
SnakeKey(Direction);
if (Direction == 0) {
GameStatus = 1;
break;
}
head = SnakeMove(head, Direction);
if (head->x == 0 || head->x == 29 || head->y == 0 || head->y == 78) {
GameStatus = 2;
break;
}
if (SnakeBite(head)) {
GameStatus = 3;
break;
}
}
}
void Xend() {
ofstream o("snake_data.dat", ios_base::app);
if (!o) o.close();
else {
o << Score << endl;
o.close();
}
switch (GameStatus)
{
case 1: {
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 224);
Pos(10, 17);cout << " 提示 ";
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 14);
Pos(11, 17);cout << "▏ ▕";
Pos(12, 17);cout << "▏ ▕";
Pos(13, 17);cout << "▏▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▕\n";
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 15);
Pos(12, 19);cout << "即将退出,";
break;
}
case 2: {
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 207);
Pos(10, 17);cout << " 失败 ";
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 12);
Pos(11, 17);cout << "▏ ▕";
Pos(12, 17);cout << "▏ ▕";
Pos(13, 17);cout << "▏▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▕\n";
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 15);
Pos(12, 19);cout << "您撞墙了,";
break;
}
case 3: {
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 223);
Pos(10, 17);cout << " 失败 ";
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 13);
Pos(11, 17);cout << "▏ ▕";
Pos(12, 17);cout << "▏ ▕";
Pos(13, 17);cout << "▏▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▕\n";
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 15);
Pos(12, 19);cout << "撞到自己,";
break;
}
default: {
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 175);
Pos(10, 17);cout << " 恭喜 ";
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 10);
Pos(11, 17);cout << "▏ ▕";
Pos(12, 17);cout << "▏ ▕";
Pos(13, 17);cout << "▏▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▕\n";
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 15);
Pos(12, 19);cout << "恭喜通关,";
break;
}
}
Pos(12, 28);system("pause");
Pos(27, 88);SnakeDel(head);
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 191);
Pos(10, 17);cout << " 提示 ";
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 11);
Pos(11, 17);cout << "▏ ▕";
Pos(12, 17);cout << "▏ ▕";
Pos(13, 17);cout << "▏ ▕";
Pos(14, 17);cout << "▏ ▕";
Pos(15, 17);cout << "▏▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▕\n";
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 15);
Pos(12, 19);cout << "是否再玩一局?";
Pos(14, 24);cout << " 是 ";
Pos(14, 46);cout << " 否 ";
int YN = 6;
while (!GetAsyncKeyState(VK_RETURN)) {
if (GetAsyncKeyState(VK_LEFT)) {
YN = 4;
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 191);
Pos(14, 24);cout << " 是 ";
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 15);
Pos(14, 46);cout << " 否 ";
}
if (GetAsyncKeyState(VK_RIGHT)) {
YN = 6;
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 15);
Pos(14, 24);cout << " 是 ";
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 191);
Pos(14, 46);cout << " 否 ";
}
}
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 15);
if (YN == 4) {
Score = 0;
Max = 0;
Length = 5;
SleepTime = 150;
Direction = 8;
SnakeColour = 0;
main();
}
else Pos(29, 88);
}
void FoodCreate(Xsnake* s, Xfood& f) {
srand((unsigned)time(0));
do {
f.y = 2 * (rand() % 38 + 1);
f.x = rand() % 28 + 1;
while (s) {
if (s->x == f.x&&s->y == f.y) break;
s = s->next;
}
} while (s);
f.colour = rand() % 8;
if (f.colour > 2) f.colour = 0;
}
void FoodShow(const Xfood& f) {
Pos(f.x, f.y);
if (f.colour == 1)SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 12);
else if (f.colour == 2)SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 9);
cout << "●";
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 15);
}
Xsnake* SnakeAdd(Xsnake* s, unsigned xx, unsigned yy) {
Xsnake* n = new Xsnake;
n->x = xx;n->y = yy;
n->next = s;
return n;
}
Xsnake* SnakeMove(Xsnake* s, unsigned direction) {
Xsnake* h = s;
Xsnake* n = s;
while (s->next) {
n = s;
s = s->next;
}
s->next = h;
n->next = nullptr;
switch (direction)
{
case 4: s->y = (s->next)->y - 2, s->x = (s->next)->x;break;
case 6: s->y = (s->next)->y + 2, s->x = (s->next)->x;break;
case 8: s->x = (s->next)->x - 1, s->y = (s->next)->y;break;
case 2: s->x = (s->next)->x + 1, s->y = (s->next)->y;break;
default: exit(1);
}
return s;
}
int SnakeBite(Xsnake* s) {
unsigned xx = s->x, yy = s->y;
while (s->next) {
s = s->next;
if (s->x == xx&&s->y == yy) return 1;
}
return 0;
}
Xsnake* SnakePush_back(Xsnake* s, const unsigned xx, const unsigned yy) {
Xsnake* ss = s;
while (s->next) s = s->next;
Xsnake* n = new Xsnake;
n->x = xx, n->y = yy;
n->next = nullptr;
s->next = n;
return ss;
}
inline void SnakeTail(Xsnake* s, unsigned& xx, unsigned& yy) {
while (s->next)s = s->next;
xx = s->x;yy = s->y;
}
inline void SnakeClear(unsigned xx, unsigned yy) {
if (xx&& yy) {
Pos(xx, yy);cout << " ";
}
}
void SnakeDel(Xsnake* s) {
Xsnake* n;
int i = 0;
while (s) {
n = s;
s = s->next;
delete n;
++i;
}
cout << "本次游戏中贪吃蛇长度为" << i << '.';
}
void SnakeShow(Xsnake* s) {
if (SnakeColour == 1)SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 12);
else if (SnakeColour == 2)SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 9);
while (s) {
Pos(s->x, s->y);cout << "■";
s = s->next;
}
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 15);
}
void SnakeKey(unsigned& d) {
if (GetAsyncKeyState(VK_UP) && d != 2) d = 8;
else if (GetAsyncKeyState(VK_DOWN) && d != 8) d = 2;
else if (GetAsyncKeyState(VK_LEFT) && d != 6) d = 4;
else if (GetAsyncKeyState(VK_RIGHT) && d != 4) d = 6;
else if (GetAsyncKeyState(VK_SPACE)) {
Pos(14, 35);cout << "暂 停";
do {
Sleep(200);
} while (!GetAsyncKeyState(VK_SPACE));
Pos(14, 35);cout << " ";
SnakeShow(head);
FoodShow(Food);
}
else if (GetAsyncKeyState(VK_ESCAPE)) d = 0;
else if (GetAsyncKeyState(0x52)) Xinterface(), FoodShow(Food);
}

//-----------------------
int main()
{
system("mode con cols=120 lines=30");
system("cls");
Pos(0, 0);
Xstart();
Xgame();
Xend();
return 0;
}
//-----------------------

截图:

img
img
img
img
img
img