A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© x_starry 中级黑马   /  2016-4-14 23:22  /  1059 人查看  /  2 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

按照老师的思路自己巧了下来,看看能不能继续
  1. //推箱子游戏

  2. #include<stdio.h>
  3. #include<stdlib.h>
  4. #define rows 10
  5. #define cols 11
  6. void printMap(char map[rows][cols]);
  7. void go(char map[rows][cols],char currentX,char currentY,char nextX,char nextY);
  8. int main()
  9. {
  10.     //定义地图
  11.     char theMapOfPushBox[rows][cols] =
  12.     {
  13.         "##########",
  14.         "#0 ####  #",
  15.         "# X####  #",
  16.         "#        #",
  17.         "######   #",
  18.         "#  ####  #",
  19.         "#        #",
  20.         "#   ######",
  21.         "#         ",
  22.         "##########"
  23.     };
  24.     //定义变量存储小人的当前位置
  25.     int personCurrentX = 1;
  26.     int personCurrentY = 1;
  27.     //定义变量存储小人下一步的位置
  28.     int personNextX = personCurrentX;
  29.     int personNextY = personCurrentY;
  30.     //定义变量存储箱子的当前位置
  31.     int boxCurrentX = 2;
  32.     int boxCurrentY = 2;
  33.     //定义变量存储箱子的下一步位置
  34.     int boxNextX = boxCurrentX;
  35.     int boxNextY = boxCurrentY;
  36.     //定义变量存储用户输入的方向
  37.     char direction = '\0';
  38.     //定义变量存储是路的标志
  39.     char road = ' ';
  40.     //定义变量存储墙的标志
  41.     char wall = '#';
  42.     while(1)
  43.     {
  44.         //清屏
  45.         system("clear");
  46.         //调用函数打印地图
  47.         printMap(theMapOfPushBox);
  48.         //判断箱子的位置是否被推到了出口处
  49.         //如果推到了出口处就退出循环,并打印文字
  50.         if(boxCurrentX == 8 && boxCurrentY == 9)
  51.         {
  52.             printf("恭喜你,成功的把箱子推出了迷宫!\n");
  53.             break;
  54.         }
  55.         //从用户获取移动方向
  56.         printf("请输入移动方向:w/上 s/下 a/左 d/右 q/退出:\n");
  57.         scanf("%c",&direction);
  58.         //根据用户输入的方向判断小人应该做出的动作
  59.         switch(direction)
  60.         {
  61.             case 'w':
  62.             case 'W':
  63.                 personNextX = personCurrentX - 1;
  64.                 break;
  65.             case 's':
  66.             case 'S':
  67.                 personNextX = personCurrentX + 1;
  68.                 break;
  69.             case 'a':
  70.             case 'A':
  71.                 personNextY = personCurrentY - 1;
  72.                 break;
  73.             case 'd':
  74.             case 'D':
  75.                 personNextY = personCurrentY + 1;
  76.                 break;
  77.             case 'q':
  78.             case 'Q':
  79.                 printf("笨蛋!你正在退出......\n");
  80.                 return 0;
  81.         }
  82.         //根据相应的动作更新地图中的数据
  83.         //如果下一步是路
  84.         if(theMapOfPushBox[personNextX][personNextY] == road)
  85.         {
  86.             //那么就直接移动小人
  87.             go(theMapOfPushBox,personCurrentX,personCurrentY,personNextX,personNextY);
  88.             personCurrentX = personNextX;
  89.             personCurrentY = personNextY;
  90.         }
  91.         //如果下一步是箱子
  92.         else if(theMapOfPushBox[personNextX][personNextY] == theMapOfPushBox[boxCurrentX][boxCurrentY])
  93.         {
  94.             //计算箱子下一步坐标
  95.             boxNextX = boxCurrentX + (boxCurrentX - personCurrentX);
  96.             boxNextY = boxCurrentY + (boxCurrentY - personCurrentY);
  97.             //如果箱子下一步是路
  98.             if(theMapOfPushBox[boxNextX][boxNextY] == road)
  99.             {
  100.                 //那么就先移动箱
  101.                 go(theMapOfPushBox,boxCurrentX,boxCurrentY,boxNextX,boxNextY);
  102.                 //更新箱子的位置
  103.                 boxCurrentX = boxNextX;
  104.                 boxCurrentY = boxNextY;
  105.                 //再移动小人
  106.                 go(theMapOfPushBox,personCurrentX,personCurrentY,personNextX,personNextY);
  107.                 //更新小人的位置
  108.                 personCurrentX = personNextX;
  109.                 personCurrentY = personNextY;
  110.             }
  111.             //如果箱子的下一步是墙,小人和箱子都不能移动到其各自的下一步坐标位置
  112.             else if(theMapOfPushBox[boxNextX][boxNextY] == wall)
  113.             {
  114.                 //重置箱子下一步的位置坐标
  115.                 boxNextX = boxCurrentX;
  116.                 boxNextY = boxCurrentY;
  117.                 //重置小人下一步位置的坐标
  118.                 personNextX = personCurrentX;
  119.                 personNextY = personCurrentY;
  120.             }
  121.         }
  122.         //如果小人的下一步是墙,那么小人就不能移动到其下一步的坐标位置
  123.         else if(theMapOfPushBox[personNextX][personNextY] == wall)
  124.         {
  125.             //重置小人的下一步坐标位置
  126.             personNextX = personCurrentX;
  127.             personNextY = personCurrentY;
  128.         }
  129.     }
  130.     return 0;
  131. }
  132. /**
  133. *  打印地图
  134. *
  135. *  @param map 地图数组
  136. */
  137. void printMap(char map[rows][cols])
  138. {
  139.     for(int i=0; i<rows;i++)
  140.     {
  141.         printf("%s\n",map[i]);
  142.     }
  143. }
  144. /**
  145. *  移动
  146. *
  147. *  @param map 地图
  148. */
  149. void go(char map[rows][cols],char currentX,char currentY,char nextX,char nextY)
  150. {
  151.     char temp = map[currentX][currentY];
  152.     map[currentX][currentY] = map[nextX][nextY];
  153.     map[nextX][nextY] = temp;
  154. }






















复制代码

小游戏优化

2 个回复

倒序浏览
58行的scanf函数后面应该加个getchar();函数用作吸收回车字符,不然会打印两遍
回复 使用道具 举报
烛风小糊涂 发表于 2016-4-15 01:15
58行的scanf函数后面应该加个getchar();函数用作吸收回车字符,不然会打印两遍 ...

嗯嗯,是的,多谢
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马