码迷,mamicode.com
首页 > 编程语言 > 详细

C语言(简单游戏)-走出迷宫

时间:2015-09-17 01:09:49      阅读:214      评论:0      收藏:0      [点我收藏+]

标签:

 1 #include <stdio.h>
 2 //宏定义 maze[ROWS][COLS];行和列;
 3 #define ROWS 7
 4 #define COLS 6
 5 //绘制迷宫(全局变量)
 6 char maze[ROWS][COLS]= {
 7     {#,#,#,#,#,#},
 8     {#,0,#, , , },
 9     {#, ,#, ,#,#},
10     {#, ,#, , ,#},
11     {#, , ,#, ,#},
12     {#,#, , , ,#},
13     {#,#,#,#,#,#}
14 };
15 //设置X,Y坐标(全局变量);
16 int currentX=1,currentY=1;
17 //移动后的XY坐标(全局变量);
18 int nextX,nextY;
19 //看下一步是否能走  int[x][y]==‘ ‘ ;
20 char street =  ;
21 
22 //初始化函数
23 void printMaze();
24 void moveToNextPosition();
25 void calculateNextPosition(char direction);
26 
27 
28 
29 int main(int argc, const char * argv[]) {
30     nextX = currentX;
31     nextY = currentY;
32     //屏幕打印出迷宫;
33     printMaze();
34     char direction;
35     while (1) {
36         printf("请移动人物,用键盘W/S/A/D(上下左右)操作\n");
37         scanf("%c",&direction);
38         calculateNextPosition(direction);
39         moveToNextPosition();
40         printMaze();
41         if (currentX==ROWS-1||currentY==COLS-1){
42             printf("通关了,呵呵!");
43             break;
44         }
45     }
46     return 0;
47 }
48 
49 
50 //打印地图
51 void printMaze(){
52     for(int i = 0;i<ROWS;i++){
53         for (int j = 0;j<COLS;j++) {
54             printf("%c",maze[i][j]);
55         }
56         printf("\n");
57     }
58 }
59 //移动人物
60 void moveToNextPosition(){
61     if (maze[nextX][nextY]==street) {
62         char temp = maze[currentX][currentY];
63         maze[currentX][currentY] = maze[nextX][nextY];
64         maze[nextX][nextY] = temp;
65         currentX = nextX;
66         currentY = nextY;
67         
68     }else{
69         nextX = currentX;
70         nextY = currentY;
71     }
72 }
73 //计算下一个位置
74 void calculateNextPosition(char direction){
75     switch (direction) {
76         case w:{
77             nextX = currentX - 1;
78             break;
79         }
80         case s:{
81             nextX = currentX + 1;
82             break;
83         }
84         case a:{
85             nextY = currentY - 1;
86             break;
87         }
88         case d:{
89             nextY = currentY + 1;
90             break;
91         }
92         default:
93             break;
94     }
95 }

 

C语言(简单游戏)-走出迷宫

标签:

原文地址:http://www.cnblogs.com/glchan/p/4814992.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!