Fork me on GitHub

C语言实现变色贪吃蛇

今天我们实现一下用C语言编写一个能变色、能调速、能调整地图大小、有成就系统、并且能用文件保存分数和成就的贪吃蛇游戏··
工具:VS2013
语言:C

我们先看一下效果:



头文件

TCS.h

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
#pragma once
#ifndef __TCS_H__
#define __TCS_H__
#include<stdio.h>
#include<time.h>
#include<windows.h>
#include<stdlib.h>
#include<math.h>
#include "mmsystem.h"
#pragma comment(lib, "WINMM.LIB")
#define Filename "tcs.52m"
#define U 1
#define D 2
#define L 3
#define R 4 //蛇的状态,U:上 ;D:下;L:左 R:右

typedef struct SNAKE //蛇身的一个节点
{
int x;
int y;
struct SNAKE *next;
}snake;
typedef struct PLAYER
{
char name[20];
int scores;
char len[3];
int steps;
}player;
typedef struct ACHEVE
{
int step;
int eat;
int score;
}acheve;
typedef struct POS
{
int x;
int y;
}pos,*ppos;
typedef struct LEL
{
ppos pos;
int sz;
int capacity;
}lel,plel;
typedef struct list
{
char * name[5];
int sz;
}List;
//声明全部函数//
void Pos();
void creatMap();
void initsnake();
int biteself();
void createfood(int x);
void cantcrosswall();
void snakemove();
void pause();
void gamecircle();
void welcometogame();
void gamestart();
void endgame();
void print();
void menu();
void inittop();
void showtop();
void sorttop();
void startshow();
void selectmap();
void mapmenu();
int topmenu();
void loadtop();
void savetop();
void color();
void showach();
void loadach();
void saveach();
void tfree(snake* p);
void initlevel();
void printlevel();
void push(int l,int x,int y);
void selectmode();
void uplevel();
void modemenu();
int Select(List mmenu);
void printmenu(List menu, int t);
#endif//__TCS_H__

源文件

tcs.c

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
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
#define _CRT_SECURE_NO_WARNINGS
#define TEST
#include "TCS.h"
//全局变量//
int level = 0; //关卡数,0表示无尽模式
int score = 0, add = 10; //总得分与每次吃食物得分
int status, sleeptime = 200; //每次运行的时间间隔
snake *head, *food; //蛇头指针,食物指针
snake *q; //遍历蛇的时候用到的指针
int endgamestatus = 0; //游戏结束的情况,1:撞到墙;2:咬到自己;3:主动退出游戏。
int run = 0; //判断游戏是否还能运行
player top[61]; //排行榜
int MAX_LL = 130; //游戏窗口长度
int MAX_CC = 36; //游戏窗口高度
int MAX_L = 92; //游戏区域长度
int MAX_C = 35; //游戏区域高度
int steps = 0; //计数步数
int lenth = 4; //计数长度
acheve ach; //成就数据
int mode = 0; //游戏模式 0 正常 1 躲避
int istest = 0; //是否为测试模式
lel map[6];
int weisuoyuwei = 0; //为所欲为模式
int Color = 0;
List mainmenu = { { "开始游戏","游戏说明","排行总榜","成就系统","退出游戏" },5 };
List Modemenu = { { "自由无限模式", "障碍无限模式", "自由闯关模式", "障碍闯关模式" }, 4 };
List Mapmenu = { { "小型地图", "中号地图", "大型地图" }, 3 };
List Maptopmenu = { { "小型地图排行", "中号地图排行", "大型地图排行" }, 3 };
void printmenu(List menu, int t)
{
int i;
for (i = 0; i <= menu.sz; i++)
{
Pos(MAX_LL / 2 - 6, MAX_CC / 2 - 7+i*2);
if (i == t)
{
color(15*16);
}
else
{
color(0);
}
if (i <= menu.sz-1)
printf(" %s \n\n", menu.name[i]);
}
}
int Select(List menu)
{
int t = 0;
int recordt = 1;
system("cls");
print();
Pos((MAX_LL - 78) / 2 - 1, MAX_CC / 5);
printf(" ■■■■■■■■ ★ ■■■■■■■■");
while (1)
{
if (recordt != t )
{
printmenu(menu, t);
}
Pos(158, 43);
recordt = t;
if (GetAsyncKeyState(VK_DOWN) && t<menu.sz-1)
{
t++;
Sleep(200);
}
if (GetAsyncKeyState(VK_UP) && t>0)
{
t--;
Sleep(200);
}
if (GetAsyncKeyState(VK_RETURN))
{
Sleep(200);
return t;
}
}

}
//设置关卡
void push(int l,int x, int y)
{
if (map[l].sz == map[l].capacity)
{
ppos tmp = realloc(map[l].pos, (map[l].capacity + 10)*sizeof(pos));
if (tmp == NULL)
{
perror("realloc");
exit(1);
}
map[l].pos = tmp;
map[l].capacity += 10;
}
map[l].pos[map[l].sz].x = x;
map[l].pos[map[l].sz].y = y;
map[l].sz++;
}
//初始化关卡
void initlevel()
{
//MAX_LL = 160;
//MAX_CC = 45;
//MAX_L = 120;
//MAX_C = 44;
int i = 0;
for (i = 0; i < 6; i++)
{
map[i].pos = (ppos)malloc(3 * sizeof(pos));
map[i].capacity = 3;
map[i].sz = 0;
}
for (i = 20; i <= 100; i += 4)
{
push(1, i, 22);
push(2, i, 22);
push(5, i, 22);
}
for (i = 8; i < 38; i+=2)
{
push(2, 60, i);
push(3, 60, i);
push(5, 60, i);
}
for (i = 28; i <= 92; i+=4)
{
push(3, i, 14);
push(3, i, 28);
}
for (i = 20; i <= 100; i += 2)
{
if (i == 50)i = 72;
push(4, i, 8);
push(4, i, 36);
push(5, i, 8);
push(5, i, 36);
}
for (i = 9; i <= 36; i++)
{
if (i == 18)i = 28;
push(4, 20, i);
push(4, 100, i);
push(5, 20, i);
push(5, 100, i);
}
}
//打印关卡障碍
void printlevel()
{
int i = 0;
for (i = 0; i < map[level].sz; i++)
{
Pos(map[level].pos[i].x, map[level].pos[i].y);
printf("█");
}
}
void tfree(snake *p)
{
if (p->next)
tfree(p->next);
free(p);
}
//设置字体颜色
void color(int x)
{
static int tmp = 0;
if (x < 0)
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), tmp);
return;
}
if (!x)
{
do
{
x = rand() % 6 + 9;
} while (tmp == x);
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), x); //只有一个参数,改变字体颜色
Color = tmp;
tmp = x;
}
else
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), x);
}
//设置光标位置
void Pos(int x, int y)
{
COORD pos;
HANDLE hOutput;
pos.X = x;
pos.Y = y;
hOutput = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(hOutput, pos);
}
//创建地图
void creatMap()
{
int i;
PlaySound(NULL, NULL, NULL);
for (i = 0; i<MAX_L; i += 2)//打印上下边框
{
Pos(i, 0);
color(0);
printf("■");
Pos(i, MAX_C);
color(0);
printf("■");
Sleep(10);
}
for (i = 1; i<MAX_C; i++)//打印左右边框
{
Pos(0,i);
color(0);
printf("■");
Pos(MAX_L-2,i);
color(0);
printf("■");
Sleep(10);
}
}
//初始化蛇身
void initsnake()
{
snake *tail;
int i;
tail=(snake*)malloc(sizeof(snake));//从蛇尾开始,头插法,以x,y设定开始的位置//
tail->x=24;
tail->y = 15;
tail->next = NULL;
for (i = 1; i <= 4; i++)
{
head = (snake*)malloc(sizeof(snake));
head->next = tail;
head->x = 24 + 2 * i;
head->y = 15;
tail = head;
}
Pos(tail->x, tail->y);
printf("●");
tail = tail->next;
while (tail != NULL)//从头到尾,输出蛇身
{
Pos(tail->x, tail->y);
printf("■");
tail = tail->next;
}
}
//判断是否咬到了自己
int biteself()
{
snake *self;
self = head->next;
while (self != NULL)
{
if (self->x == head->x && self->y == head->y)
{
return 1;
}
self = self->next;
}
return 0;
}
//随机出现食物/障碍
void createfood(int x)
{
snake *food_1;
res:
food_1 = (snake*)malloc(sizeof(snake));
food_1->x = (rand() % (MAX_L - 4) + 2) / 2 * 2;//保证其为偶数,使得食物能与蛇头对其
food_1->y = rand() % (MAX_C - 1) + 1;
q = head;
while (q != NULL)
{
if (q->x == food_1->x && q->y == food_1->y) //判断蛇身是否与食物重合
{
free(food_1);
goto res;
}
q = q->next;
}
if (level)
{
int i = 0;
for (i = 0; i < map[level].sz; i++)
{
if (food_1->x == map[level].pos[i].x && food_1->y == map[level].pos[i].y)
{
free(food_1);
goto res;
}
}
}
if (mode)
{
int i = 0;
for (i = 0; i < map[0].sz; i++)
{
if (food_1->x == map[0].pos[i].x && food_1->y == map[0].pos[i].y)
{
free(food_1);
goto res;
}
}
}
Pos(food_1->x, food_1->y);
food = food_1;
color(0);
printf("★");
if (mode&&x)
{
snake *food_1;
res2:
food_1 = (snake*)malloc(sizeof(snake));
food_1->x = (rand() % (MAX_L - 4) + 2) / 2 * 2;//保证其为偶数,使得食物能与蛇头对其
food_1->y = rand() % (MAX_C - 1) + 1;
q = head;
while (q != NULL)
{
if (q->x == food_1->x && q->y == food_1->y) //判断蛇身是否与食物重合
{
free(food_1);
goto res2;
}
q = q->next;
}
if (level)
{
int i = 0;
for (i = 0; i < map[level].sz; i++)
{
if (food_1->x == map[level].pos[i].x && food_1->y == map[level].pos[i].y)
{
free(food_1);
goto res;
}
}
}
if (mode)
{
int i = 0;
for (i = 0; i < map[0].sz; i++)
{
if (food_1->x == map[0].pos[i].x && food_1->y == map[0].pos[i].y)
{
free(food_1);
goto res;
}
}
}
Pos(food_1->x, food_1->y);
push(0, food_1->x, food_1->y);
//color(0);
printf("█");
}
}
//不能穿墙
void cantcrosswall()
{
if (head->x == 0 || head->x == MAX_L - 2 || head->y == 0 || head->y == MAX_C)
{
system("color cf");
Sleep(200);
system("color 0f");
Sleep(200);
system("color cf");
Sleep(200);
system("color 0f");
endgamestatus = 1;
endgame();
}
}
//过关
void uplevel()
{
system("cls");
print();
Pos(60, 22);
printf("恭喜过关,三秒后切换关卡");
Sleep(3000);
system("cls");
endgame();
lenth = 4;
creatMap();
initsnake();
createfood(mode);
printlevel();
status = R;
PlaySound("running.wav", NULL, SND_ASYNC | SND_FILENAME | SND_LOOP);
}
//蛇移动
void snakemove()
{
int t = 0;
snake * nexthead;
if (!weisuoyuwei)
{
cantcrosswall();
}
if (run)return;
nexthead = (snake*)malloc(sizeof(snake));
if (status == U)
{
nexthead->x = head->x;
nexthead->y = head->y - 1;
}
else if (status == D)
{
nexthead->x = head->x;
nexthead->y = head->y + 1;
}
else if (status == L)
{
nexthead->x = head->x - 2;
nexthead->y = head->y;
}
else if (status == R)
{
nexthead->x = head->x + 2;
nexthead->y = head->y;
}
if (weisuoyuwei)
{
if (nexthead->x == 0)nexthead->x = MAX_L - 4;
if (nexthead->x == MAX_L - 2)nexthead->x = 2;
if (nexthead->y == 0)nexthead->y = MAX_C - 1;
if (nexthead->y == MAX_C)nexthead->y = 1;
}
if (level)
{
int i = 0;
for (i = 0; i < map[level].sz; i++)
{
if (nexthead->x == map[level].pos[i].x && nexthead->y == map[level].pos[i].y)
{
system("color cf");
Sleep(200);
system("color 0f");
Sleep(200);
system("color cf");
Sleep(200);
system("color 0f");
endgamestatus = 4;
endgame();
}
if (run)return;
}
}
if (mode)
{
int i = 0;
for (i = 0; i < map[0].sz;i++)
{
if (nexthead->x == map[0].pos[i].x && nexthead->y == map[0].pos[i].y)
{
system("color cf");
Sleep(200);
system("color 0f");
Sleep(200);
system("color cf");
Sleep(200);
system("color 0f");
endgamestatus = 4;
endgame();
}
if (run)return;
}
}
else;
if ((nexthead->x == food->x) && (nexthead->y == food->y) )//如果下一个有食物//
{
nexthead->next = head;
head = nexthead;
createfood(1);
q = head;
Pos(q->x, q->y);
color(Color);
printf("●");
Pos(q->next->x, q->next->y);
printf("■");
while (q->next != NULL)
{
q = q->next;
}
Pos(q->x, q->y);
printf("■");
PlaySound("error.wav", NULL, SND_FILENAME | SND_ASYNC | SND_NOSTOP);
score = score + add*(mode+1);
lenth++;
t = 0;
}
else //如果没有食物//
{
nexthead->next = head;
head = nexthead;
q = head;
Pos(q->x, q->y);
//color(Color);
printf("●");
Pos(q->next->x, q->next->y);
//color(0);
printf("■");
while (q->next->next != NULL)
{
q = q->next;
}
Pos(q->next->x, q->next->y);
printf(" ");
free(q->next);
q->next = NULL;
}
steps++;
t++;
if (biteself() == 1) //判断是否会咬到自己
{
endgamestatus = 2;
system("color cf");
Sleep(200);
system("color 0f");
Sleep(200);
system("color cf");
Sleep(200);
system("color 0f");
endgame();
}
if (run)return;
if (t % 300 == 0)
{
Pos(food->x, food->y);
printf(" ");
createfood(mode);
}
Pos(MAX_LL, MAX_CC);
}
//暂停
void pause()
{
while (1)
{
Sleep(300);
if (GetAsyncKeyState(VK_SPACE))
{
break;
}
}
}
//控制游戏
void gamecircle()
{
static int tscore = -1;
static int tlenth = -1;
static int tadd = -1;
PlaySound("running.wav", NULL, SND_ASYNC | SND_FILENAME | SND_LOOP);
Pos(MAX_L+4, 15);
color(0);
printf("不能穿墙,不能咬到自己\n");
Pos(MAX_L + 4, 16);
color(0);
printf("用↑.↓.←.→分别控制蛇的移动.");
Pos(MAX_L + 4, 17);
color(0);
printf("F1 为加速,F2 为减速\n");
Pos(MAX_L + 4, 18);
color(0);
printf("ESC :退出游戏.Space:暂停游戏.");
Pos(MAX_L + 4, 20);
color(0);
if (mode)
{
Pos(MAX_L + 4, 22);
printf("躲避模式下也不能碰到障碍!!");
}
printf("\n");
status = R;
while (1)
{
if (tscore != score||tadd!=add)
{
Pos(MAX_L + 6, 10);
printf("得分:%d ", score);
Pos(MAX_L + 6, 11);
printf("每个食物得分:%d分", add*(mode + 1));
}
if (GetAsyncKeyState(VK_UP) && status != D)
{
status = U;
}
else if (GetAsyncKeyState(VK_DOWN) && status != U)
{
status = D;
}
else if (GetAsyncKeyState(VK_LEFT) && status != R)
{
status = L;
}
else if (GetAsyncKeyState(VK_RIGHT) && status != L)
{
status = R;
}
else if (GetAsyncKeyState(VK_SPACE))
{
pause();
}
else if (GetAsyncKeyState(VK_ESCAPE))
{
endgamestatus = 3;
PlaySound(NULL,NULL,NULL);
break;
}
else if (GetAsyncKeyState(VK_F1))
{
if (sleeptime >= 50)
{
sleeptime = sleeptime - 30;
add = add + 2;
if (sleeptime == 320)
{
add = 2;//防止减到1之后再加回来有错
}
}
}
else if (GetAsyncKeyState(VK_F2))
{
if (sleeptime<350)
{
sleeptime = sleeptime + 30;
add = add - 2;
if (sleeptime == 350)
{
add = 1; //保证最低分为1
}
}
}
if (GetAsyncKeyState(VK_F5))
{
weisuoyuwei = 1;
}
if (GetAsyncKeyState(VK_F6))
{
weisuoyuwei = 0;
}
if (level&&tlenth!=lenth)
{
Pos(MAX_L + 6, 12);
printf("关卡进度:%d/25", (lenth - 4) % 25);
if (lenth - 4 == 25 && level - 5)
{
level++;
endgamestatus = 0;
uplevel();
}
}
Sleep(sleeptime);
tscore = score;
tlenth = lenth;
snakemove();
if (run)return;
}
}
//简介界面
void welcometogame()
{
system("cls");
//int i = 7;
//color(13);
//Pos(6, 7);
//printf("■■■■ ★");
//for (i = 8; i < 20; i++)
//{
// Pos(6, i);
// printf("■");
// Pos(MAX_L + 6, i);
// printf("■");
//}
//Pos(6, i);
//for (i = 6; i <= MAX_L + 6; i+=2)
// printf("■");
print();
Pos(MAX_L/2-12, 7);
color(15);
printf("用");
color(10);
printf("↑ ↓ ← →");
color(15);
printf("分别控制蛇的移动");
Pos(MAX_L / 2 - 12, 9);
color(12);
printf("F1 ");
color(15);
printf("为加速");
color(12);
printf(" F2 ");
color(15);
printf("为减速\n");
Pos(MAX_L / 2 - 12, 11);
color(15);
printf("可以多点几下");
color(11);
printf(" F1 ");
color(15);
printf("或者");
color(11);
printf(" F2 ");
color(15);
printf("切换到你认为最适合你的难度\n");
Pos(MAX_L/2-12, 13);
printf("加速将能得到");
color(13);
printf("更高的分数。");
Pos(MAX_L / 2 - 12, 15);
color(14);
printf("开始进行愉快的贪吃蛇游戏吧 ^_^\n");
Pos(MAX_L / 2-12, 25);
color(15);
#ifndef TEST
system("pause");
#endif
#ifdef TEST
printf("按下空格键返回...");
while (1)
{
if (GetAsyncKeyState(VK_SPACE))
return;
}
#endif
}
//结束游戏
void endgame()
{
char c;
system("cls");
Pos(MAX_L/2, 12);
if (endgamestatus == 1)
{
color(12);
printf("很遗憾,你撞到了墙。游戏结束.");
}
else if (endgamestatus == 2)
{
color(12);
printf("很遗憾,你咬到了自己。游戏结束.");
}
else if (endgamestatus == 3)
{
color(12);
printf("你已经结束了游戏。");
}
else if (endgamestatus == 4)
{
color(12);
printf("你撞上了障碍,挂了!!!");
}
else
{
ach.step += steps;
ach.score += score;
ach.eat += (lenth - 4);
saveach();
tfree(head);
map[0].sz = 0;
return;
}
print();
PlaySound("death.wav", NULL, SND_FILENAME | SND_ASYNC);
Pos(MAX_L / 2, 13);
printf("你的得分是:%d,在本局游戏中你一共走了:%d格", score,steps);
if (score >= top[(MAX_LL-70)/30*20-1].scores&&endgamestatus)
{
Pos(MAX_L / 2, 14);
system("pause");
top[60].scores = score;
sprintf(top[60].len, "%d", lenth);
top[60].steps = steps;
Pos(MAX_L / 2, 14);
printf("恭喜你进入排行榜!!!!");
fflush(stdin);
Pos(MAX_L / 2, 15);
printf("是否留下你的名字?(y/n)");
scanf("%c", &c);
if ('y' == c || 'Y' == c)
{
Pos(MAX_L / 2, 16);
printf("请输入你的昵称:>");
scanf("%s", top[60].name);
sorttop();
Pos(MAX_L / 2, 17);
printf("排行榜已更新...");
}
else
{
strcpy(top[60].name, "noname");
sorttop();
Pos(MAX_L / 2, 17);
printf("排行榜已更新...");
}
}
ach.step += steps;
ach.score += score;
ach.eat += (lenth - 4);
saveach();
tfree(head);
run = 1;
Pos(MAX_L / 2, 18);
map[0].sz = 0;
#ifndef TEST
system("pause");
#endif
#ifdef TEST
printf("按下空格键返回...");
while (1)
{
if (GetAsyncKeyState(VK_SPACE))
return;
}
#endif
}
//游戏初始化
void gamestart()
{
char p[50];
sleeptime = 200;
score = 0;
add = 10;
steps = 0;
lenth = 4;
system("cls");
run = 0;
sprintf((char *)p, "mode con cols=%d lines=%d", MAX_LL, MAX_CC);
system((const char*)p);
creatMap();
initsnake();
createfood(mode);
printlevel();
}
//选择地图界面
void mapmenu()
{
print();
Pos((MAX_LL - 78) / 2 - 1, MAX_CC / 5);
printf("■■■■■■■■ ★ ■■■■■■■■");
Pos(MAX_LL / 2 - 6, MAX_CC / 2 - 5);
printf("1.地图:小");
Pos(MAX_LL / 2 - 6, MAX_CC / 2 - 3);
printf("2.地图:中");
Pos(MAX_LL / 2 - 6, MAX_CC / 2 - 1);
printf("3.地图:大");
Pos((MAX_LL - 78) / 2 - 1, MAX_CC / 2+1);
printf("选择你的地图大小:>");
}
//选择地图
void selectmap()
{
int input;
do
{
if (istest)
input = Select(Mapmenu)+istest;
#ifndef TEST
system("cls");
mapmenu();
fflush(stdin);
scanf("%d", &input);
#endif
switch (input)
{
case 1:
MAX_LL = 100;
MAX_CC = 30;
MAX_L = 64;
MAX_C = 29;
return;
break;
case 2:
MAX_LL = 130;
MAX_CC = 36;
MAX_L = 92;
MAX_C = 35;
return;
break;
case 3:
MAX_LL = 160;
MAX_CC = 45;
MAX_L = 120;
MAX_C = 44;
return;
break;
default:
Pos((MAX_LL - 78) / 2 - 1, MAX_CC / 2 + 2);
printf("输入有误!");
Sleep(500);
break;
}
} while (1);
}
//打印边框
void print()
{
int i;
for (i = 0; i<MAX_LL; i += 2)//打印上下边框
{
Pos(i, 0);
color(0);
printf("■");
Pos(i, MAX_CC - 1);
color(0);
printf("■");
}
for (i = 1; i<MAX_CC; i++)//打印左右边框
{
Pos(0, i);
color(0);
printf("■");
Pos(MAX_LL - 2, i);
color(0);
printf("■");
}
}
//主菜单
void menu()
{
print();
Pos((MAX_LL-78)/2-1, MAX_CC / 5);
printf("■■■■■■■■ ★ ■■■■■■■■");
Pos(MAX_LL / 2-6, MAX_CC / 2-7);
printf("1.开始游戏");
Pos(MAX_LL / 2-6, MAX_CC / 2-5);
printf("2.游戏简介");
Pos(MAX_LL / 2 - 6, MAX_CC / 2-3);
printf("3.排行榜");
Pos(MAX_LL / 2 - 6, MAX_CC / 2 - 1);
printf("4.成就");
Pos(MAX_LL / 2 - 6, MAX_CC / 2 + 1);
printf("0.退出游戏");
}
//选择模式界面
void modemenu()
{
print();
Pos((MAX_LL - 78) / 2 - 1, MAX_CC / 5);
printf("■■■■■■■■ ★ ■■■■■■■■");
Pos(MAX_LL / 2 - 6, MAX_CC / 2 - 5);
printf("1.无限模式");
Pos(MAX_LL / 2 - 6, MAX_CC / 2 - 3);
printf("2.障碍无限模式");
Pos(MAX_LL / 2 - 6, MAX_CC / 2 - 1);
printf("3.闯关模式");
Pos(MAX_LL / 2 - 6, MAX_CC / 2 + 1);
printf("4.障碍闯关模式");
Pos((MAX_LL - 78) / 2 - 1, MAX_CC / 2 + 5);
printf("选择你的模式:>");
}
//选择模式
void selectmode()
{
int input;
do
{
if (istest)
input = Select(Modemenu)+istest;
#ifndef TEST
system("cls");
modemenu();
fflush(stdin);
scanf("%d", &input);
#endif
switch (input)
{
case 2:
level = 0;
mode = 1;
selectmap();
return;
case 1:
level = 0;
mode = 0;
selectmap();
return;
break;
case 4:
mode = 1;
MAX_LL = 160;
MAX_CC = 45;
MAX_L = 120;
MAX_C = 44;
level = 1;
return;
case 3:
mode = 0;
MAX_LL = 160;
MAX_CC = 45;
MAX_L = 120;
MAX_C = 44;
level = 1;
return;
default:
Pos((MAX_LL - 78) / 2 - 1, MAX_CC / 2 + 6);
printf("输入有误!");
Sleep(500);
break;
}
} while (1);
}
//游戏流程控制
void game()
{
//system("color 3f");
selectmode();
gamestart();
gamecircle();
system("cls");
}
//开始动画
void startshow()
{
int i = 0;
int t;
PlaySound("show.wav", NULL, SND_ASYNC | SND_FILENAME);
Pos((MAX_LL - 56) / 2-6, (MAX_CC - 10) / 2 + i++);
Sleep(5);
color(0);
printf("██ ██████████ ████████");
Pos((MAX_LL - 56) / 2 - 6, (MAX_CC - 10) / 2 + i++);
//color(0);
Sleep(45);
printf("██ ██████████ ████████");
Pos((MAX_LL - 56) / 2 - 6, (MAX_CC - 10) / 2 + i++);
//color(0);
Sleep(55);
printf("██ █ █ ██");
Pos((MAX_LL - 56) / 2 - 6, (MAX_CC - 10) / 2 + i++);
//color(0);
Sleep(45);
printf("██ █ █ ██");
Pos((MAX_LL - 56) / 2 - 6, (MAX_CC - 10) / 2 + i++);
//color(0);
Sleep(35);
printf("██ ██████████ ██");
Pos((MAX_LL - 56) / 2 - 6, (MAX_CC - 10) / 2 + i++);
//color(0);
Sleep(25);
printf("██ ██████████ ████████");
Pos((MAX_LL - 56) / 2 - 6, (MAX_CC - 10) / 2 + i++);
//color(0);
Sleep(15);
printf("██ ███◣ ████████");
Pos((MAX_LL - 56) / 2 - 6, (MAX_CC - 10) / 2 + i++);
//color(0);
Sleep(15);
printf("██ ██◥█◣ ██");
Pos((MAX_LL - 56) / 2 - 6, (MAX_CC - 10) / 2 + i++);
//color(0);
Sleep(25);
printf("██ ██ ◥█◣ ██");
Pos((MAX_LL - 56) / 2 - 6, (MAX_CC - 10) / 2 + i++);
//color(0);
Sleep(35);
printf("██ ██ ◥█◣ ██");
Pos((MAX_LL - 56) / 2 - 6, (MAX_CC - 10) / 2 + i++);
//color(0);
Sleep(45);
printf("█████████ ██ ◥█◣ ████████");
Pos((MAX_LL - 56) / 2 - 6, (MAX_CC - 10) / 2 + i++);
//color(0);
Sleep(55);
printf("█████████ ██ ◥█◣ ████████");
Sleep(500);
Pos( MAX_LL/ 2+10, (MAX_CC - 10) / 2 + i+3);
color(13);
printf("★");
Pos(0, (MAX_CC - 10) / 2 +i+3);
t = (MAX_CC - 10) / 2 + i + 3;
color(14);
for (i = 0; i < MAX_LL; i += 2)
{
Pos(i, t);
printf("■");
if (i>=16)
{
Pos(i-16, t);
printf(" ");
}
if (i >= (MAX_LL / 2 + 8) && i <= (MAX_LL / 2 + 10))
{
color(13);
}
Sleep(15);
}
//system("pause");
}
//测试食物是否会生成在地图外
void test()
{
int x = 0;
int y = 0;
system("cls");
creatMap();
color(15);
Pos(MAX_L + 4, 15);
printf("按下Esc结束测试状态");
while(1) //保证其为偶数,使得食物能与蛇头对其
{
x = (rand() % (MAX_L - 4) + 2)/2*2;
y = rand() % (MAX_C - 1) + 1;
Pos(x,y);
color(0);
printf("█");
if (GetAsyncKeyState(VK_ESCAPE))
{
break;
}
}
}
void testlevel()
{
system("mode con cols=160 lines=45");
level = 5;
initlevel();
printlevel();
system("pause");
}
//主函数
int main()
{
int t = 0;
int input;
char p[50];
istest = 1;
system("title LRS-贪吃蛇");
initlevel();
color(0);
srand((unsigned)time(NULL));
inittop();
loadach();
sprintf((char *)p, "mode con cols=%d lines=%d", MAX_LL, MAX_CC);
system((const char*)p);
startshow();
do{
PlaySound("begin.wav", NULL, SND_ASYNC | SND_FILENAME | SND_LOOP | SND_NOSTOP);
weisuoyuwei = 0;
fflush(stdin);
Sleep(300);
//keybd_event(VK_ESCAPE, (BYTE)0, 0, 0);
//keybd_event(VK_ESCAPE, (BYTE)0, KEYEVENTF_KEYUP, 0);
if (GetAsyncKeyState(VK_RETURN) | GetAsyncKeyState(VK_UP) | GetAsyncKeyState(VK_DOWN));
input = (Select(mainmenu)+1)%5;
#ifndef TEST
system("cls");
menu();
Pos(MAX_LL / 2 - 14, MAX_CC / 2 + 3);
printf("请选择:>");
scanf("%d", &input);
#endif
switch (input)
{
case 5:
test();
break;
case 4:
showach();
break;
case 3:
showtop();
break;
case 2:
welcometogame();
break;
case 1:
game();
break;
case 0:
Sleep(100);
break;
default:
Pos(MAX_LL / 2 - 14, MAX_CC / 2 + 4);
printf("输入有误请重新输入!\n");
Sleep(500);
break;
}
} while (input);
Pos(MAX_LL / 2 - 14, MAX_CC / 2 + 4);
printf("感谢你的游玩,再见!");
Sleep(800);
return 0;
}

top.c

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
#define _CRT_SECURE_NO_WARNINGS
#define TEST
#include "TCS.h"
//全局变量//
extern int level ; //关卡数,0表示无尽模式
extern int score , add ; //总得分与每次吃食物得分
extern int status, sleeptime ; //每次运行的时间间隔
extern snake *head, *food; //蛇头指针,食物指针
extern snake *q; //遍历蛇的时候用到的指针
extern int endgamestatus ; //游戏结束的情况,1:撞到墙;2:咬到自己;3:主动退出游戏。
extern int run ; //判断游戏是否还能运行
extern player top[61]; //排行榜
extern int MAX_LL ;
extern int MAX_CC ;
extern int MAX_L ;
extern int MAX_C ;
extern int istest;
extern List Maptopmenu;
//初始化排行
void inittop()
{
int j = 0;
for (j = 0; j < 3; j++)
{
int i = 0;
top[20 * j + i].scores = 999;
sprintf(top[20 * j + i].len, "%d", 56);
top[20 * j + i].steps = 1658;
for (i = 1; i < 20; i++)
{
top[20 * j + i].scores = 480 - 20 * i;
sprintf(top[20 * j + i].len ,"%d", 48 - 2 * i);
top[20 * j + i].steps = 999 - 20 * i;
}
for (i = 0; i < 21; i++)
{
strcpy(top[20 * j + i].name, "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0");
strcpy(top[20 * j + i].name, "LRS");
}
}
top[60].scores = 0;
loadtop();
}
//排行菜单
int topmenu()
{
int input;
do
{
if (istest)
input = Select(Maptopmenu) + istest;
#ifndef TEST
system("cls");
mapmenu();
fflush(stdin);
scanf("%d", &input);
#endif
switch (input)
{
case 1:
return 0;
break;
case 2:
return 1;
break;
case 3:
return 2;
break;
default:
Pos((MAX_LL - 78) / 2 - 1, MAX_CC / 2 + 2);
printf("输入有误!");
Sleep(500);
break;
}
} while (1);
}
//显示排行
void showtop()
{
int i = 0;
int j = topmenu();
system("cls");
print();
Pos(MAX_LL / 5, i + 3);
color(15);
printf("%-5s\t%-20s%-6s\t%-3s\t%-8s", "名次", "昵称", "分数","体长","路程");
for (i = 0; i < 20; i++)
{
Pos(MAX_LL / 5, i + 4);
if (i < 20)color(15);
if (i < 10)color(10);
if (i < 5)color(9);
if (i < 3)color(13);
if (i < 2)color(14);
if (i < 1)color(12);
printf("%-5d\t%-20s%-6d\t%-3s\t%-8d", i + 1, top[j * 20 + i].name, top[j * 20 + i].scores, top[j * 20 + i].len, top[j * 20 + i].steps);
}
Pos(MAX_LL / 5, i + 6);
#ifndef TEST
system("pause");
#endif
#ifdef TEST
printf("按下空格键返回...");
while (1)
{
if (GetAsyncKeyState(VK_SPACE))
return;
}
#endif
}
//排序排行
void sorttop()
{
int i = 0;
int j = 0;
int m = (MAX_LL - 100) / 30 * 20;
for (i = 0; i < 20; i++)
{
if (top[60].scores >= top[m + i].scores)
{
for (j = 19 + m; j >(i + m); j--)
{
top[j] = top[j - 1];
}
top[i + m] = top[60];
break;
}
}
savetop();
}
//加载
void loadtop()
{
int ret = 0;
FILE* c = fopen(Filename,"a+");
FILE* load = fopen(Filename, "rb");
fclose(c);
c = NULL;
if (load == NULL)
{
perror("load top");
exit(1);
}
while(fread(&top[ret++], sizeof(player), 1, load));
//printf("%d", ret);
//system("pause");
fclose(load);
load = NULL;
}
//保存
void savetop()
{
int i = 0;
FILE* save = fopen(Filename, "w");
if (save == NULL)
{
perror("save top");
exit(1);
}
fwrite(&top[0], sizeof(player), 60, save);
fclose(save);
save = NULL;
}

ach.c

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
#define _CRT_SECURE_NO_WARNINGS
#include "TCS.h"
#define TEST
extern acheve ach;
extern int MAX_LL;
struct achname
{
char name[20];
int num;
}name[9] = { { "旅行蛇",10000 }
,{ "环游中国",100000 } ,{"遨游太空",1000000 },{"大胃王", 100}
,{ "上古腐鲸蛇",10000 },{ "宇宙级大蛇", 100000},{ "得分高手",10000 }
,{ "得分大师",100000 },{"得分宗师",1000000 }};
//展示成就
void showach()
{
int i = 0;
system("cls");
print();
Pos(MAX_LL / 5, i + 3);
color(15);
printf("%-20s\t %-12s\t", "成就", "状态");
for (i = 0; i < 9; i++)
{
color(i % 3 + 10);//判断颜色
Pos(MAX_LL / 5, 2 * (i + 1) + 3);
printf("%-20s\t", name[i].name);
switch (i / 3)//判断类型
{
case 0:
if (ach.step >= name[i].num)
{
printf("%10d/%-9d", name[i].num, name[i].num);
}
else
printf("%10d/%-9d", ach.step, name[i].num);
break;
case 1:
if (ach.eat >= name[i].num)
{
printf("%10d/%-9d", name[i].num, name[i].num);
}
else
printf("%10d/%-9d", ach.eat, name[i].num);
break;
case 2:
if (ach.score >= name[i].num)
{
printf("%10d/%-9d", name[i].num, name[i].num);
}
else
printf("%10d/%-9d", ach.score, name[i].num);
break;
}
}
Pos(MAX_LL / 5, 2 * (i + 1) + 5);
printf("走过的总步数:%d步\t吃过的食物总数:%d个\t得到的总分数:%d分", ach.step, ach.eat, ach.score);
Pos(MAX_LL / 5, 2 * (i + 1) + 7);
#ifndef TEST
system("pause");
#endif
#ifdef TEST
printf("按下空格键返回...");
while (1)
{
if (GetAsyncKeyState(VK_SPACE))
return;
}
#endif
}
//加载
void loadach()
{
FILE* c = fopen("ach.52m","a+");//如果没有就新建
FILE* load = fopen("ach.52m", "rb");
fclose(c);
c = NULL;
if (load == NULL)
{
perror("load ach");
exit(1);
}
ach.eat = 0;
ach.score = 0;
ach.step = 0;
fread(&ach, sizeof(acheve), 1, load);
fclose(load);
load = NULL;
}
//保存
void saveach()
{
FILE* save = fopen("ach.52m", "wb");
if (save == NULL)
{
perror("save ach");
exit(1);
}
fwrite(&ach, sizeof(acheve), 1, save);
fclose(save);
save = NULL;
}

这样我们的贪吃蛇游戏就做好了…

因为本人比较懒,所以用了很多全局变量 > _ <

怎么样?是不是相当…花里胡哨呢?
注:2018.6.21之后的版本需要下载对应的音乐文件,下载链接:https://github.com/lrsand52m/tiny_project/tree/master/LRS_Tcs

下载里面的.wav文件放进你程序对应文件夹下就能正常工作了

更新日志:  
2018.6.10   增加障碍躲避模式   
2018.6.18   增加关卡模式   
2018.6.20   增加为所欲为模式  分数计算更加合理
2018.6.21   增加操作优化及界面优化使其玩起来更像是游戏而不是程序
-------------本文结束感谢您的阅读-------------
坚持原创技术分享,您的支持将鼓励我继续创作!