本文实例为大家分享了C语言实现贪吃蛇游戏的具体代码,供大家参考,具体内容如下
在读大学生一枚,五一期间用四天时间写一个小游戏。
本贪吃蛇游戏代码共计531行,开发环境是VS,游戏分为两种模式,标准模式和超大地图模式,详细看以下源代码,并进入游戏体验具体内容。
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
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
|
//贪吃蛇
#include<stdio.h>
#include<windows.h>
#include<time.h>
#include<conio.h>
//游戏窗口
#define FrameX 5
#define FrameY 5
#define Frame_width 20
#define Frame_height 20
#define FrameX2 3
#define FrameY2 3
#define Frame_width2 55
#define Frame_height2 47
//全局变量
int i, j, p, q, m; int a[2];
//蛇的结构体
struct Snack{
int x[200];
//蛇的X坐标;
int y[200];
//蛇的Y坐标;
int count;
//蛇吃食物的数量;
int length;
//蛇身体的长度;
int speed;
//蛇移动的速度;
};
//食物的结构
struct Food{
int x;
//食物的X坐标;
int y;
//食物的Y坐标;
};
//光标的位置
void gotoxy( HANDLE hOut, int x, int y){ COORD pos; pos.X = x; pos.Y = y; SetConsoleCursorPosition(hOut, pos);}
//游戏菜单
void menu(){
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
void over_game();
void start_game();
void start_game2();
int n;
gotoxy(hOut, 50, 14);
printf ( " 超贪吃的蛇" );
gotoxy(hOut, 50, 16);
printf ( "正常游戏模式【1】" );
gotoxy(hOut, 50, 17);
printf ( "超大地图模式【2】" );
gotoxy(hOut, 50, 18);
printf ( " 结束游戏【3】" );
scanf_s( "%d" , &n);
switch (n)
{
case 1:
system ( "cls" );
start_game();
over_game();
break ;
case 2:
system ( "cls" );
start_game2();
over_game();
break ;
case 3:
system ( "cls" );
over_game();
break ;
default :
printf ( "你的输入有误,请重新输入!" );
menu();
break ;
}}
//制作游戏窗口2
void make_frame2()
{
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
//定义显示器句柄变量
gotoxy(hOut, 24, 13);
//定义障碍物
printf ( "#" );
gotoxy(hOut, 14, 23);
printf ( "#" );
gotoxy(hOut, 35, 20);
//定义障碍物
printf ( "#" );
gotoxy(hOut, 40, 35);
printf ( "#" );
gotoxy(hOut, 70, 6);
//定义障碍物
printf ( "#" );
gotoxy(hOut, 80, 16);
printf ( "#" );
gotoxy(hOut, 60, 45);
//定义障碍物
printf ( "#" );
gotoxy(hOut, 28, 8);
printf ( "#" );
for (i = 0; i < 2*Frame_width2+1;i++)
{ gotoxy(hOut, FrameX2 + i, FrameY2);
//上边框
printf ( "-" ); gotoxy(hOut, FrameX2 + i, Frame_height2+FrameY2);
//下边框
printf ( "-" ); }
for (i = 0; i < Frame_height2-1; i++)
{ gotoxy(hOut, FrameX2, FrameY2 + i+1);
//左边框
printf ( "|" );
gotoxy(hOut, FrameX2+2*Frame_width2, FrameY2 + i+1);
//右边框
printf ( "|" ); }}
//制作游戏窗口
void make_frame()
{
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
//定义显示器句柄变量
gotoxy(hOut, 24, 13);
//定义障碍物
printf ( "#" );
gotoxy(hOut, 14, 23);
printf ( "#" );
for (i = 0; i < 2 * Frame_width + 1; i++)
{ gotoxy(hOut, FrameX + i, FrameY);
//上边框
printf ( "-" );
gotoxy(hOut, FrameX + i, Frame_height + FrameY);
//下边框
printf ( "-" ); }
for (i = 0; i < Frame_height - 1; i++)
{ gotoxy(hOut, FrameX, FrameY + i + 1);
//左边框
printf ( "|" );
gotoxy(hOut, FrameX + 2 * Frame_width, FrameY + i + 1);
//右边框
printf ( "|" ); }}
//初始化蛇
void intt_snack( struct Snack* snack)
{
snack->x[0] = FrameX + 5;
//蛇的横坐标;
snack->y[0] = FrameY + 6;
//蛇的纵坐标;
snack->speed = 200;
//蛇的速度
snack->length = 3;
//蛇的长度;
snack->count = 0;
for (i = 1; i < snack->length; i++)
{
snack->x[i] = snack->x[i - 1]+2;
snack->y[i] = snack->y[i - 1];
}
}
//初始化蛇2
void intt_snack2( struct Snack* snack)
{ snack->x[0] = FrameX2 + 5;
//蛇的横坐标;
snack->y[0] = FrameY2 + 6;
//蛇的纵坐标;
snack->speed = 200;
//蛇的速度;
snack->length = 3;
//蛇的长度;
snack->count = 0;
for (i = 1; i < snack->length; i++)
{ snack->x[i] = snack->x[i - 1] + 2;
snack->y[i] = snack->y[i - 1]; }}
//移动蛇
void move_snack( HANDLE hOut, struct Snack* snack)
{ gotoxy(hOut, snack->x[0], snack->y[0]); printf ( " " );
//清除蛇尾;
for (i = 1; i < snack->length; i++)
{
snack->x[i - 1] = snack->x[i];
snack->y[i - 1] = snack->y[i];
}}
//打印蛇
void print_snack( HANDLE hOut, struct Snack* snack)
{
for (i = 0; i < snack->length; i++)
{
gotoxy(hOut, snack->x[i], snack->y[i]);
if (i == 0)
{
printf ( "*" );
}
else if
(i < snack->length - 1)
{
printf ( "0" );
}
else
{
printf ( "@" );
}
}
}
//打印食物
void print_food( HANDLE hOut, struct Snack*snack, struct Food* food){ srand ((unsigned) time (0));
while (1)
{
food->x = rand () % 2*Frame_width+1; food->y = rand () % Frame_height;
if (food->x == 0 || food->y == 0) { continue ; }
food->x = food->x+FrameX;
food->y = food->y + FrameY;
for (i = 0; i < snack->length; i++)
{
if (food->x == snack->x[i] && food->y == snack->y[i])
{
break ;
}
}
if (i == snack->length)
{
gotoxy(hOut, food->x, food->y);
printf ( "$" );
break ;
}
}
}
//打印食物2
void print_food2( HANDLE hOut, struct Snack* snack, struct Food* food)
{ srand ((unsigned) time (0));
while (1)
{
food->x = ( rand () % (3*Frame_width2 + 1))+FrameX2;
food->y = ( rand () % Frame_height2)+FrameY2;
if (food->x == 0 || food->y == 0)
{
continue ;
}
for (i = 0; i < snack->length; i++)
{
if (food->x == snack->x[i] && food->y == snack->y[i])
{
break ;
}
}
if (i == snack->length)
{
gotoxy(hOut, food->x, food->y); printf ( "$" );
break ;
}
}
}
//吃食物
void eat_food( HANDLE hOut, struct Snack *snack, struct Food *food)
{ if (snack->x[snack->length-1] == food->x && snack->y[snack->length-1] == food->y)
{ snack->length++;
for (i = snack->length; i >= 1; i--)
{ snack->x[i] = snack->x[i - 1];
snack->y[i] =snack->y[i - 1]; } snack->x[0] = a[0];
snack->y[0] = a[1]; print_food(hOut,snack,food);
snack->count++; if (snack->count % 5 == 0) snack->speed -= 20; }}
//穿墙
void through_wall( HANDLE hOut, struct Snack* snack, char ch)
{ if (ch == 72 && snack->y[snack->length - 1] == FrameY)
{ snack->y[snack->length - 1] =Frame_height+4;
//如果蛇在上框且向上移动,穿墙
} if (ch == 80 && snack->y[snack->length - 1] == FrameY + Frame_height)
{ snack->y[snack->length - 1] = FrameY+1;
//如果蛇在下框且向下移动,穿墙
} if (ch == 75 && snack->x[snack->length - 1] == FrameX-1)
{ snack->x[snack->length - 1] =2*Frame_width + FrameX-1;
//如果蛇在左框且向左移动,穿墙
} if (ch == 77 && snack->x[snack->length - 1] == 2*Frame_width+FrameX+1)
{ snack->x[snack->length - 1] = FrameX+1;
//如果蛇在右框且向右移动,穿墙
}}
//穿墙2
void through_wall2( HANDLE hOut, struct Snack* snack, char ch)
{ if (ch == 72 && snack->y[snack->length - 1] == FrameY2)
{ snack->y[snack->length - 1] = Frame_height2 + 4;
//如果蛇在上框且向上移动,穿墙
} if (ch == 80 && snack->y[snack->length - 1] == FrameY2 + Frame_height2)
{ snack->y[snack->length - 1] = FrameY2 + 1;
//如果蛇在下框且向下移动,穿墙
} if (ch == 75 && snack->x[snack->length - 1] == FrameX2 - 1)
{ snack->x[snack->length - 1] = 2 * Frame_width2 + FrameX2 - 1;
//如果蛇在左框且向左移动,穿墙
} if (ch == 77 && snack->x[snack->length - 1] == 2 * Frame_width2 + FrameX2 + 1)
{ snack->x[snack->length - 1] = FrameX2 + 1;
//如果蛇在右框且向右移动,穿墙
}}
//判断蛇是否死亡
int snack_die( struct Snack *snack){
for (i = 0; i < snack->length - 1; i++)
{ if (snack->x[snack->length - 1] == snack->x[i] && snack->y[snack->length - 1] == snack->y[i])
return 0;
if (snack->x[snack->length - 1] == 24 && snack->y[snack->length - 1] == 13)
return 0;
if (snack->x[snack->length - 1] == 14 && snack->y[snack->length - 1] == 23)
return 0; }
return 1;}
//判断蛇是否死亡2
int snack_die2( struct Snack* snack)
{ for (i = 0; i < snack->length - 1; i++)
{ if (snack->x[snack->length - 1] == snack->x[i] && snack->y[snack->length - 1] == snack->y[i])
return 0;
if (snack->x[snack->length - 1] == 24 && snack->y[snack->length - 1] == 13)
return 0;
if (snack->x[snack->length - 1] == 14 && snack->y[snack->length - 1] == 23)
return 0;
if (snack->x[snack->length - 1] == 35 && snack->y[snack->length - 1] == 20)
return 0;
if (snack->x[snack->length - 1] == 40 && snack->y[snack->length - 1] == 35)
return 0;
if (snack->x[snack->length - 1] == 70 && snack->y[snack->length - 1] == 6)
return 0;
if (snack->x[snack->length - 1] == 80 && snack->y[snack->length - 1] == 16)
return 0;
if (snack->x[snack->length - 1] == 60 && snack->y[snack->length - 1] == 45)
return 0;
if (snack->x[snack->length - 1] == 28 && snack->y[snack->length - 1] == 8)
return 0; }
return 1;}
//开始游戏
void start_game(){ unsigned char ch = 77;
//定义用于接收键盘输入的字符变量
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
//定义显示器句柄变量
struct Snack s, * snack = &s;
struct Food f, * food = &f; make_frame(); intt_snack(snack);
print_food(hOut,snack, food);
while (1)
{ a[0] = snack->x[0];
//记住蛇尾的横坐标
a[1] = snack->y[0];
//记住蛇尾的竖坐标
j = 0;
if (_kbhit())
{
/* 判断是否按下键盘,如果按下,ch接收键盘输入 */
ch = _getch();
if (_kbhit())
{
/* 如果长按键盘,则加速 */
Sleep(20);
j = 1; } }
switch (ch)
{
case 72:
{
/* 向上移动 */
move_snack(hOut, snack);
//移动蛇
snack->y[snack->length - 1] -= 1;
//蛇头的竖坐标向上移,即减1
break ;
}
case 80:
{
/* 向下移动 */
move_snack(hOut, snack);
//移动蛇
snack->y[snack->length - 1] += 1;
//蛇头的竖坐标向下移,即加1
break ;
}
case 75: {
/* 向左移动 */
move_snack(hOut, snack);
//移动蛇
snack->x[snack->length - 1] -= 2;
//蛇头的横坐标向左移,即减2
break ;
}
case 77:
{
/* 向右移动 */
move_snack(hOut, snack);
//移动蛇
snack->x[snack->length - 1] += 2;
//蛇头的横坐标向右移,即加2
break ; } }
through_wall(hOut, snack, ch);
eat_food(hOut, snack, food);
//吃食物
print_snack(hOut, snack);
//打印蛇
if (snack_die(snack) == 0)
{
/* 游戏结束条件:1.蛇碰到自身 2.按Esc键 3.速度为50ms */
gotoxy(hOut, 15, 15);
printf ( "Game Over" ); Sleep(1000);
break ;
}
if (j == 0)
{
Sleep(snack->speed);
//延迟时间
}
else
{
Sleep(10);
}
}
}
void start_game2(){ unsigned char ch = 77;
//定义用于接收键盘输入的字符变量
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
//定义显示器句柄变量
struct Snack s, * snack = &s; struct Food f, * food = &f;
make_frame2();
intt_snack2(snack);
print_food(hOut, snack, food);
while (1) { a[0] = snack->x[0];
//记住蛇尾的横坐标
a[1] = snack->y[0];
//记住蛇尾的竖坐标
j = 0; if (_kbhit())
{
/* 判断是否按下键盘,如果按下,ch接收键盘输入 */
ch = _getch();
if (_kbhit()) {
/* 如果长按键盘,则加速 */
Sleep(20); j = 1; } }
switch (ch) {
case 72: {
/* 向上移动 */
move_snack(hOut, snack);
//移动蛇
snack->y[snack->length - 1] -= 1;
//蛇头的竖坐标向上移,即减1
break ;
} case 80: {
/* 向下移动 */
move_snack(hOut, snack);
//移动蛇
snack->y[snack->length - 1] += 1;
//蛇头的竖坐标向下移,即加1
break ; } case 75: {
/* 向左移动 */
move_snack(hOut, snack);
//移动蛇
snack->x[snack->length - 1] -= 2;
//蛇头的横坐标向左移,即减2
break ; } case 77: {
/* 向右移动 */
move_snack(hOut, snack);
//移动蛇
snack->x[snack->length - 1] += 2;
//蛇头的横坐标向右移,即加2
break ; } }
through_wall2(hOut, snack, ch);
eat_food(hOut, snack, food);
//吃食物
print_snack(hOut, snack);
//打印蛇
if (snack_die2(snack) == 0)
{
/* 游戏结束条件:1.蛇碰到自身 2.按Esc键 3.速度为50ms */
gotoxy(hOut, 30, 18);
printf ( "Game Over" );
Sleep(1000);
break ; }
if (j == 0) { Sleep(snack->speed);
//延迟时间
}
else { Sleep(10); } }}
void choose()
{ HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
void start_game();
void start_game2();
void over_game();
int n;
gotoxy(hOut, 10, 20);
printf ( "Continue [1]" );
gotoxy(hOut, 30, 20);
printf ( "Continue [2]" );
gotoxy(hOut, 50, 20);
printf ( "Exit [3]" );
gotoxy(hOut, 70, 20);
printf ( "选择:" );
scanf_s( "%d" , &n);
switch (n)
{ case 1: system ( "cls" );
start_game();
over_game();
break ;
case 2:
system ( "cls" );
start_game2();
over_game();
break ;
case 3:
exit (0);
break ;
default :
printf ( "※※您的输入有误,请重新输入※※" );
system ( "pause >nul" ); over_game();
choose(); break ; }}
/******结束菜单*******************************************************************/
void over_game()
{
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
system ( "cls" );
printf ( "\n\n\n\n\n\n\n\n\t\t\t\t游戏结束\n\n\n" );
choose();
system ( "pause" );
}
void main(){
menu();
start_game();
//开始游戏
start_game2();
//开始游戏
over_game();
//结束游戏
}
|
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/m0_57353463/article/details/119036101