-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSnake.c
More file actions
1083 lines (981 loc) · 18.8 KB
/
Snake.c
File metadata and controls
1083 lines (981 loc) · 18.8 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
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
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include <windows.h>
#include<time.h>
#include<conio.h> //控制台输入输出
//#include "mmsystem.h"//导入声音头文件
#pragma comment(lib,"winmm.lib")//导入声音头文件库
//蛇单个坐标数据
typedef struct coordinate
{
int x;
int y;
}nate;
//装单个坐标的节点
typedef struct Node
{
nate data;
struct Node *next;
}Node;
//蛇的主体 链表的头节点
typedef struct Nate
{
Node head;
int size;
}Nate;
#define W 50//一行50个
#define H 20//一列20个
int GuanQia = 1; //当前关卡
nate GuanQiaData1[30] = { 0 };//第1关的障碍物坐标数据
nate GuanQiaData2[60] = { 0 };//第2关的障碍物坐标数据
nate GuanQiaData3[65] = { 0 };//第3关的障碍物坐标数据
nate GuanQiaData4[77] = { 0 };//第4关的障碍物坐标数据
int TongGuanLen = 20;//通关长度
int power = 0;//暂停的开关 默认假
int exit1 = 0; //结束的标志
Nate *list; //蛇数据
int MyThreadID;
nate ShiWu; //食物的坐标
HANDLE hd; //线程句柄
HANDLE hd2; //线程2句柄
int path = 2; //控制蛇移动的方向 1上 2下 3左 4右
int tmp = 200;//刷新频率
int ChuanQiangPower = 0;//穿墙模式 0为假 1为真
//图标
//int ic = ;
//char a = 178;
//char ico_0[3] = { 161,0 };
char *ico_0 = "\01"; //边框图标
char *ico_1 = "\02"; //蛇头图案
char *ico_2 = "\04"; //蛇头后面一个图案
char *ico_3 = "\05"; //除前面2个所有图案
char *ico_4 = "\03"; //食物图案
//char *ico_0 = "X"; //边框图标
//char *ico_1 = "O"; //蛇头图案
//char *ico_2 = "~"; //蛇头后面一个图案
//char *ico_3 = "#"; //除前面2个所有图案
//char *ico_4 = "$"; //食物图案
void test();
//障碍物初始化
void InitZhangAiWu();
//判断是否撞上障碍物
int Func01(int x, int y);
/*
*function: 在控制台指定的位置输出字符串
*参数:buf,输出的字符串;startX、startY为控制台的X,Y坐标
*/
void printStr(char *buf, int startX, int startY)
{
static flag = 0;
HANDLE hd;
COORD pos;
while (flag == 1)
{
Sleep(1);
}
flag = 1;
pos.X = startX;
pos.Y = startY;
hd = GetStdHandle(STD_OUTPUT_HANDLE); /*获取标准输出的句柄*/
SetConsoleCursorPosition(hd, pos); /*设置控制台光标输出的位置*/
//printf("the string is : %s at console(%d, %d) position !\n", buf, pos.X, pos.Y);
printf("%s", buf);
flag = 0;
}
//获取指定位置坐标
Node *GetValue(int pos)
{
Node *pro = &(list->head); //头节点
for (int i = 0; i < pos; i++)
{
pro = pro->next;
}
return pro->next;
}
//链表头插
void InsertArr(int x, int y)
{
if (list == NULL)
{
return;
}
Node *new = (Node *)malloc(sizeof(Node));
//赋值
new->data.x = x;
new->data.y = y;
//入链
new->next = list->head.next;
list->head.next = new;
//个数+1
list->size++;
}
//链表指定位置删除
void DelArr(int pos)
{
if (list == NULL)
{
return;
}
if (pos<0 || pos>list->size - 1)
{
return;
}
Node *pro = &(list->head); //头节点
for (int i = 0; i < pos; i++)
{
pro = pro->next;
}
Node *tmp = pro->next;
pro->next = tmp->next;
tmp->next = NULL;
free(tmp);
list->size--;
}
//遍历判断是否与数组中元素相同 有相同的返回0 没有相同的返回-1
int CmpArr(int x, int y)
{
Node *pro = list->head.next; //首节点
while (pro != NULL)
{
if (pro->data.x == x&&pro->data.y == y)
{
return 0;
}
pro = pro->next;
}
return -1;
}
//遍历判断 除蛇头外的
int CmpArr2(int x, int y)
{
Node *pro = list->head.next->next->next; //首节点下一个
while (pro != NULL)
{
if (pro->data.x == x&&pro->data.y == y)
{
return 0;
}
pro = pro->next;
}
return -1;
}
//绘制窗口1 //只打印边框 不清空
void HuiZhi1()
{
for (int i = 0; i < H; i++)
{
for (int j = 0; j < W; j++)
{
if (i == 0 || j == 0 || i == H - 1 || j == W - 1)
{
printStr(ico_0, j, i);
}
}
}
}
//绘制窗口
void HuiZhi()
{
for (int i = 0; i < H; i++)
{
for (int j = 0; j < W; j++)
{
if (i == 0 || j == 0 || i == H - 1 || j == W - 1)
{
printStr(ico_0, j, i);
}
else
printStr(" ", j, i);
}
}
char buf[10] = { 0 };
_itoa(GuanQia, buf, 10);
printStr("当前第 ", 55, 18);
printStr(" ", 62, 18);
printStr(buf, 62, 18);
printStr("关", 64, 18);
}
//蛇身初始化
void InitSnake()
{
list = (Nate *)calloc(1, sizeof(Nate));
list->head.next = NULL;
InsertArr(3, 2);
InsertArr(3, 3);
InsertArr(3, 4);
//链表默认的首节点的坐标是x=3 y=4
list->size = 3;
}
//清除蛇身
void DelSnake()
{
Node *pro = list->head.next; //首节点
while (pro != NULL)
{
printStr(" ", pro->data.x, pro->data.y);
pro = pro->next;
}
}
//绘制蛇身
void HuiZhiSnake()
{
Node *pro = list->head.next; //首节点
int i = 0;
while (pro != NULL)
{
if (i == 0)
{
printStr(ico_1, pro->data.x, pro->data.y);
}
else if (i == 1)
{
printStr(ico_2, pro->data.x, pro->data.y);
}
else
{
printStr(ico_3, pro->data.x, pro->data.y);
}
pro = pro->next;
i++;
}
}
//绘制食物
void HuiZhiShiWu()
{
srand((unsigned int)time(NULL));
int x, y;
char buf_x[10] = { 0 };
char buf_y[10] = { 0 };
char buf_size[10] = { 0 };
char buf_num[10] = { 0 };
while (1)
{
x = rand() % W;//x坐标的随机数
y = rand() % H;//y坐标随机数
if (x == W - 1 || x == 0 || y == 0 || y == H - 1) //随机到边框 重绘
{
continue;
}
if (CmpArr(x, y) == 0) //如果跟蛇身坐标一样就 重绘
{
continue;
}
if (Func01(x, y) == 0) //如果跟障碍物位置一样就重绘
{
continue;
}
ShiWu.x = x;
ShiWu.y = y;
printStr(ico_4, ShiWu.x, ShiWu.y);
//打印食物坐标
_itoa(ShiWu.x, buf_x, 10);
_itoa(ShiWu.y, buf_y, 10);
//清除
printStr(" ", 55, 3);
printStr(" ", 64, 3);
printStr(" ", 66, 3);
printStr(" ", 68, 3);
printStr(" ", 70, 3);
//绘制
printStr("食物坐标 ", 55, 3);
printStr("x:", 64, 3);
printStr(buf_x, 66, 3);
printStr("y:", 68, 3);
printStr(buf_y, 70, 3);
_itoa(list->size, buf_size, 10);
printStr(" ", 55, 6);
printStr(" ", 64, 6);
printStr("蛇身长度:", 55, 6);
printStr(buf_size, 64, 6);
//打印剩余食物个数
_itoa(TongGuanLen - list->size + 3, buf_num, 10);
printStr(" ", 55, 9);
printStr("食物剩余个数:", 55, 9);
printStr(" ", 68, 9);
printStr(buf_num, 68, 9);
break;
}
}
//销毁蛇
void FreeSnake()
{
if (list == NULL)
{
return;
}
Node *pro = list->head.next; //首节点
Node *tmp = NULL;
while (pro != NULL)
{
tmp = pro->next;
free(pro);
pro = tmp;
}
}
//结束提示
void GameOver(int x)
{
exit1 = 1;
//1撞到东西 2.过关
if (x == 1)
{
//销毁蛇
FreeSnake();
//结束线程
CloseHandle(hd);
CloseHandle(hd2);
printStr("G A M E O V E R", 55, 13);
printStr(" 再来一局? Y/N ", 55, 14);
char ch;
while (1)
{
ch = _getch();
if (ch == 'y' || ch == 'Y')
{
system("cls");
test();
return;
}
else if (ch == 'n' || ch == 'N')
{
exit(0);
}
Sleep(1000);
}
}
if (x == 2)
{
if (GuanQia >= 4)
{
system("cls");
printStr(" 恭 喜 通 关 ", 32, 9);
printStr("G A M E O V E R", 32, 13);
while (1)
{
Sleep(1000);
}
return ;
}
//销毁蛇
FreeSnake();
//结束线程
CloseHandle(hd);
CloseHandle(hd2);
GuanQia++;
printStr("恭喜过关", 55, 13);
printStr("按任意键进入下一关", 55, 14);
_getch();
system("cls");
test();
return;
}
}
//吃食物判断
void ChiShiWu()
{
switch (path)
{
case 1:
if (list->head.next->data.x == ShiWu.x && list->head.next->data.y == ShiWu.y + 1)
{
//printStr("\01", ShiWu.x, ShiWu.y);//让这个位置变成蛇一样的图案
InsertArr(ShiWu.x, ShiWu.y); //蛇头插入这个食物的坐标
HuiZhiShiWu(); //绘制食物
HuiZhiSnake();
Sleep(tmp);
}
break;
case 2:
if (list->head.next->data.x == ShiWu.x && list->head.next->data.y == ShiWu.y - 1)
{
//printStr("\01", ShiWu.x, ShiWu.y);//让这个位置变成蛇一样的图案
InsertArr(ShiWu.x, ShiWu.y); //蛇头插入这个食物的坐标
HuiZhiShiWu(); //绘制食物
HuiZhiSnake();
Sleep(tmp);
}
break;
case 3:
if (list->head.next->data.x == ShiWu.x + 1 && list->head.next->data.y == ShiWu.y)
{
//printStr("\01", ShiWu.x, ShiWu.y);//让这个位置变成蛇一样的图案
InsertArr(ShiWu.x, ShiWu.y); //蛇头插入这个食物的坐标
HuiZhiShiWu(); //绘制食物
HuiZhiSnake();
Sleep(tmp);
}
break;
case 4:
if (list->head.next->data.x == ShiWu.x - 1 && list->head.next->data.y == ShiWu.y)
{
//printStr("\01", ShiWu.x, ShiWu.y);//让这个位置变成蛇一样的图案
InsertArr(ShiWu.x, ShiWu.y); //蛇头插入这个食物的坐标
HuiZhiShiWu(); //绘制食物
HuiZhiSnake();
Sleep(tmp);
}
break;
default:
break;
}
}
//碰撞检测
int JianCe()
{
if (list->head.next == NULL)
{
return -2;
}
//头元素撞到边
if (ChuanQiangPower == 0)
{
if (list->head.next->data.x == 0 || list->head.next->data.x == W - 1 || list->head.next->data.y == 0 || list->head.next->data.y == H - 1)
{
GameOver(1);
return -1;
}
}
else//穿墙模式
{
if (list->head.next->data.x == 0)
{
//判断食物坐标是不是在另一边
list->head.next->data.x = W - 2;
//绘制窗口
HuiZhi1();
if (ShiWu.x == W - 2 && ShiWu.y == list->head.next->data.y)
{
InsertArr(ShiWu.x - 1, ShiWu.y); //蛇头插入这个食物的坐标
HuiZhiShiWu(); //绘制食物
}
//return 0;
}
if (list->head.next->data.y == 0)
{
list->head.next->data.y = H - 2;
//绘制窗口
HuiZhi1();
if (ShiWu.x == list->head.next->data.x && ShiWu.y == H - 2)
{
InsertArr(ShiWu.x, ShiWu.y - 1); //蛇头插入这个食物的坐标
HuiZhiShiWu(); //绘制食物
}
//return 0;
}
if (list->head.next->data.x == W - 1)
{
list->head.next->data.x = 1;
//绘制窗口
HuiZhi1();
if (ShiWu.x == 1 && ShiWu.y == list->head.next->data.y)
{
InsertArr(ShiWu.x + 1, ShiWu.y); //蛇头插入这个食物的坐标
HuiZhiShiWu(); //绘制食物
}
//return 0;
}
if (list->head.next->data.y == H - 1)
{
list->head.next->data.y = 1;
//绘制窗口
HuiZhi1();
if (ShiWu.x == list->head.next->data.x && ShiWu.y == 1)
{
InsertArr(ShiWu.x, ShiWu.y + 1); //蛇头插入这个食物的坐标
HuiZhiShiWu(); //绘制食物
}
//return 0;
}
}
//撞到自己身体了
if (CmpArr2(list->head.next->data.x, list->head.next->data.y) == 0)
{
GameOver(1);
return -1;
}
//撞上障碍物
if (Func01(list->head.next->data.x, list->head.next->data.y) == 0)
{
GameOver(1);
return -1;
}
//吃食物判断
ChiShiWu();
return 0;
}
//蛇身移动线程
void MoveSnake()
{
while (1)
{
if (exit1 == 1)
{
return;
}
//power在暂停时会==1
while (power == 1)
{
Sleep(50);
}
switch (path) //1上 2下 3左 4右
{
case 1:
//往上移动
InsertArr(list->head.next->data.x, list->head.next->data.y - 1); //蛇头插入
//在蛇尾地方打印一个空格 清除图标
printStr(" ", GetValue(list->size - 1)->data.x, GetValue(list->size - 1)->data.y);
DelArr(list->size - 1); //蛇尾删除
HuiZhiSnake();//绘制蛇
break;
case 2:
//往下移动
InsertArr(list->head.next->data.x, list->head.next->data.y + 1); //蛇头插入
//在蛇尾地方打印一个空格 清除图标
printStr(" ", GetValue(list->size - 1)->data.x, GetValue(list->size - 1)->data.y);
DelArr(list->size - 1); //蛇尾删除
HuiZhiSnake();//绘制蛇
break;
case 3:
//往左移动
InsertArr(list->head.next->data.x - 1, list->head.next->data.y); //蛇头插入
//在蛇尾地方打印一个空格 清除图标
printStr(" ", GetValue(list->size - 1)->data.x, GetValue(list->size - 1)->data.y);
DelArr(list->size - 1); //蛇尾删除
HuiZhiSnake();//绘制蛇
break;
case 4:
//往右移动
InsertArr(list->head.next->data.x + 1, list->head.next->data.y); //蛇头插入
//在蛇尾地方打印一个空格 清除图标
printStr(" ", GetValue(list->size - 1)->data.x, GetValue(list->size - 1)->data.y);
DelArr(list->size - 1); //蛇尾删除
HuiZhiSnake();//绘制蛇
break;
default:
break;
}
Sleep(tmp);
if (JianCe() != 0) //边框碰撞检测
{
return;
}
if (list->size >= TongGuanLen + 3)
{
GameOver(2);
return;
}
}
}
//用户输入线程
void GetFangXiang()
{
while (1)
{
if (exit1 == 1)
{
return;
}
int ch = 0;
if (_kbhit())
{
ch = _getch();
}
if (ch == 'w' || ch == 'W' || ch == 72)
{
if (path != 2) //如果原来往下移动的 不能直接往上
{
path = 1;//上
Sleep(tmp);
}
}
else if (ch == 's' || ch == 'S' || ch == 80)
{
if (path != 1) //如果原来往上移动的 不能直接往下
{
path = 2;//下
Sleep(tmp);
}
}
else if (ch == 'a' || ch == 'A' || ch == 75)
{
if (path != 4) //如果原来往右移动的 不能直接往左
{
path = 3;//左
Sleep(tmp);
}
}
else if (ch == 'd' || ch == 'D' || ch == 77)
{
if (path != 3) //如果原来往左移动的 不能直接往右
{
path = 4;//右
Sleep(tmp);
}
}
else if (ch == 32)//空格键
{
if (exit1 != 1)
{
if (power == 0)
{
printStr(" ", 55, 14);
printStr(" pause...", 55, 14);
power = 1;
}
else
{
printStr(" ", 55, 14);
power = 0;
}
}
}
//o加速 p减速
else if (ch == 'o' || ch == 'O')
{
if (tmp > 50)
{
tmp -= 10;
if (tmp < 50)tmp = 50;
char buf[10] = { 0 };
_itoa(tmp, buf, 10);
printStr(" ", 15, 22);
printStr("当前速度:", 15, 22);
printStr(" ", 24, 22);
printStr(buf, 24, 22);
}
}
else if (ch == 'p' || ch == 'P')
{
if (tmp < 1000)
{
tmp += 10;
char buf[10] = { 0 };
_itoa(tmp, buf, 10);
printStr(" ", 15, 22);
printStr("当前速度:", 15, 22);
printStr(" ", 24, 22);
printStr(buf, 24, 22);
}
}
//Sleep(tmp / 10);
setbuf(stdin, NULL);
}
}
//全局初始化
void test()
{
system("cls");//清屏
ChuanQiangPower = 0;
TongGuanLen = 20;//通关长度
setbuf(stdin, NULL);
exit1 = 0;//结束的标志
//tmp = 200; //刷新频率
path = 2; //默认往下移动 //1上2下3左4右
//绘制窗口
HuiZhi();
//初始化蛇身
InitSnake();
//绘制蛇身
HuiZhiSnake();
//障碍物初始化
InitZhangAiWu();
//绘制食物
HuiZhiShiWu();
//打印当前速度
char buf[10] = { 0 };
_itoa(tmp, buf, 10);
printStr(" ", 15, 22);
printStr("当前速度:", 15, 22);
printStr(" ", 24, 22);
printStr(buf, 24, 22);
//创建蛇身移动线程
hd = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)MoveSnake, NULL, 0, &MyThreadID);
SetThreadPriority(hd, 10);
//创建监视用户输入线程
hd2 = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)GetFangXiang, NULL, 0, &MyThreadID);
SetThreadPriority(hd2, 10);
}
int main(void)
{
system("mode con cols=80 lines=25");//设置窗口大小
ChuanQiangPower = 0; //穿墙模式
int a, b;
//打印欢迎菜单
printStr(" S N A K E ", 23, 2);
printStr(" w e l c o m e ", 23, 4);
printStr("WASD控制方向", 29, 8);
printStr("空格键暂停", 29, 9);
printStr("O键加速", 29, 10);
printStr("P键减速", 29, 11);
printStr(" 按 任 意 键 继 续 ", 25, 17);
_getch();
system("cls");
printStr(" ", 20, 8);
printStr("请选择蛇的速度(100-1000) :", 20, 8);
while (1)
{
scanf("%d", &a);
if (a >= 100 && a <= 1000)
{
tmp = a;
break;
}
else
{
printStr(" ", 20, 10);
printStr("只能输入100-1000,请重新输入 :", 20, 10);
}
setbuf(stdin, NULL);//清空缓冲区
}
printStr(" ", 20, 12);
printStr("请选择关卡(1-4) :", 20, 12);
while (1)
{
scanf("%d", &b);
if (b > 0 && b < 5)
{
GuanQia = b; //设置初始关
break;
}
else
{
printStr(" ", 20, 14);
printStr("只能输入1-4,请重新输入 :", 20, 14);
}
setbuf(stdin, NULL);//清空缓冲区
}
test();
//阻塞不让程序结束
while (1)
{
//循环播放音乐
mciSendString(L"play BGM.mp3 wait", 0, 0, 0);
Sleep(100);
//Sleep(100000);
}
printf("\n");
system("pause");
return 0;
}
//判断是否撞上障碍物
int Func01(int x, int y)
{
if (GuanQia == 1)
{
for (int i = 0; i < 30; i++)
{
if (GuanQiaData1[i].x == x&&GuanQiaData1[i].y == y)
{
return 0;
}
}
return -1;
}
//第2关
if (GuanQia == 2)
{
for (int i = 0; i < 60; i++)
{
if (GuanQiaData2[i].x == x&&GuanQiaData2[i].y == y)
{
return 0;
}
}
return -1;
}
//第三关
if (GuanQia == 3)
{
for (int i = 0; i < 65; i++)
{
if (GuanQiaData3[i].x == x&&GuanQiaData3[i].y == y)
{
return 0;
}
}
return -1;
}
if (GuanQia == 4)
{
for (int i = 0; i < 77; i++)
{
if (GuanQiaData4[i].x == x&&GuanQiaData4[i].y == y)
{
return 0;
}
}
return -1;
}
return -5;
}
//障碍物初始化
void InitZhangAiWu()
{
//第1关
if (GuanQia == 1)
{
int a = 10;
for (int i = 0; i < 30; i++)
{
GuanQiaData1[i].x = a++;
GuanQiaData1[i].y = 13;
}
a = 10;
for (int i = 0; i < 30; i++)
{
printStr(ico_0, a++, 13);
}
}
//第2关
if (GuanQia == 2)
{
//给数组内容赋值
int a = 10;
for (int i = 0; i < 30; i++)
{
GuanQiaData2[i].x = a++;
GuanQiaData2[i].y = 13;
}
a = 10;
for (int i = 30; i < 60; i++)
{
GuanQiaData2[i].x = a++;
GuanQiaData2[i].y = 7;
}
//下面打印
a = 10;
for (int i = 0; i < 30; i++)
{
printStr(ico_0, a++, 13);
}
a = 10;
for (int i = 0; i < 30; i++)
{
printStr(ico_0, a++, 7);
}
}
//第三关
if (GuanQia == 3)
{
//给数组内容赋值
int a = 10;
for (int i = 0; i < 30; i++)
{
GuanQiaData3[i].x = a++;
GuanQiaData3[i].y = 13;
}
a = 10;
for (int i = 30; i < 60; i++)
{
GuanQiaData3[i].x = a++;
GuanQiaData3[i].y = 7;
}
a = 8;
for (int i = 60; i < 65; i++)
{
GuanQiaData3[i].x = 25;
GuanQiaData3[i].y = a++;
}
//下面打印
a = 10;
for (int i = 0; i < 30; i++)
{
printStr(ico_0, a++, 13);
}
a = 10;
for (int i = 0; i < 30; i++)
{
printStr(ico_0, a++, 7);
}
a = 8;
for (int i = 0; i < 5; i++)
{
printStr(ico_0, 25, a++);
}
}
//第4关
if (GuanQia == 4)
{
//给数组内容赋值