1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <getch.h>
4 #include <time.h>
5
6 int main()
7 {
8 //定义迷宫地图
9 char map[10][10] = {
10 {1,1,1,1,1,1,1,1,1,1},
11 {1,0,1,1,1,1,0,0,0,0},
12 {1,0,0,2,1,1,0,1,1,1},
13 {1,1,1,0,1,1,0,0,0,1},
14 {1,1,1,0,1,1,1,1,0,1},
15 {1,1,1,0,1,1,0,0,0,1},
16 {1,1,1,0,1,1,0,1,1,1},
17 {1,1,1,0,0,0,0,1,1,1},
18 {1,1,1,1,1,1,1,1,1,1},
19 {1,1,1,1,1,1,1,1,1,1}
20 };
21 //定义角色坐标
22 int x = 2,y = 3;
23 unsigned long start_time = time(NULL);
24 for(;;)
25 {
26 //清理屏幕
27 system("clear");
28 //打印地图
29 for(int i=0;i<10;i++)
30 {
31 for(int j=0;j<10;j++)
32 {
33 switch(map[i][j])
34 {
35 case 0:printf(" ");break;
36 case 1:printf(" #");break;
37 case 2:printf(" @");break;
38 }
39 }
40 printf("\n");
41 }
42 //获取方向键
43 if(x == 1 && y == 9)
44 {
45 printf("游戏结束,共用时%lu秒\n",time(NULL)-start_time);
46 return 0;
47 }
48 switch(getch())
49 {
50 case 183:
51 if(x-1 >= 0 && map[x-1][y] == 0)
52 {
53 map[x-1][y] = 2;
54 map[x][y] = 0;
55 x = x-1;
56 }break;
57 case 184:
58 if(x+1 <= 9 && map[x+1][y] == 0)
59 {
60 map[x+1][y] = 2;
61 map[x][y] = 0;
62 x = x+1;
63 }break;
64 case 186:
65 if(y-1 >=0 && map[x][y-1] == 0)
66 {
67 map[x][y-1] = 2;
68 map[x][y] = 0;
69 y = y-1;
70 }break;
71 case 185:
72 if(y+1 <= 9 && map[x][y+1] == 0)
73 {
74 map[x][y+1] = 2;
75 map[x][y] = 0;
76 y = y+1;
77 }break;
78 }
79 }
80 }
C语言 简单迷宫设计
最新推荐文章于 2023-08-29 18:49:21 发布