找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 3074|回复: 2
打印 上一主题 下一主题
收起左侧

C语言写的游戏_走迷宫源程序

[复制链接]
跳转到指定楼层
楼主


C语言写的游戏__走迷宫

  1. #include <stdio.h>
  2. #include <conio.h>
  3. #include <windows.h>
  4. #include <time.h>
  5. #define Height 31 //迷宫的高度,必须为奇数
  6. #define Width  25 //迷宫的宽度,必须为奇数
  7. #define Wall 1
  8. #define Road 0
  9. #define Start 2
  10. #define End 3
  11. #define Esc 5
  12. #define Up 1
  13. #define Down 2
  14. #define Left 3
  15. #define Right 4
  16. int map[Height+2][Width+2];

  17. void gotoxy(int x,int y) //移动坐标
  18. {
  19.         COORD coord;
  20.         coord.X=x;
  21.         coord.Y=y;
  22.         SetConsoleCursorPosition( GetStdHandle( STD_OUTPUT_HANDLE ), coord );
  23. }

  24. void hidden()//隐藏光标
  25. {
  26.         HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
  27.         CONSOLE_CURSOR_INFO cci;
  28.         GetConsoleCursorInfo(hOut,&cci);
  29.         cci.bVisible=0;//赋1为显示,赋0为隐藏
  30.         SetConsoleCursorInfo(hOut,&cci);
  31. }

  32. void create(int x,int y) //随机生成迷宫
  33. {
  34.         int c[4][2]={0,1,1,0,0,-1,-1,0}; //四个方向
  35.         int i,j,t;
  36.         //将方向打乱
  37.         for(i=0;i<4;i++)
  38.         {
  39.                 j=rand()%4;
  40.                 t=c[i][0];c[i][0]=c[j][0];c[j][0]=t;
  41.                 t=c[i][1];c[i][1]=c[j][1];c[j][1]=t;
  42.         }
  43.         map[x][y]=Road;
  44.         for(i=0;i<4;i++)
  45.                 if(map[x+2*c[i][0]][y+2*c[i][1]]==Wall)
  46.                 {
  47.                         map[x+c[i][0]][y+c[i][1]]=Road;
  48.                         create(x+2*c[i][0],y+2*c[i][1]);
  49.                 }
  50. }

  51. int get_key() //接收按键
  52. {
  53.         char c;
  54.         while(c=getch())
  55.         {
  56.                 if(c==27) return Esc; //Esc
  57.                 if(c!=-32)continue;
  58.                 c=getch();
  59.                 if(c==72) return Up; //上
  60.                 if(c==80) return Down; //下
  61.                 if(c==75) return Left; //左
  62.                 if(c==77) return Right; //右
  63.         }
  64.         return 0;
  65. }

  66. void paint(int x,int y) //画迷宫
  67. {
  68.         gotoxy(2*y-2,x-1);
  69.         switch(map[x][y])
  70.         {
  71.         case Start:
  72.                 printf("入");break; //画入口
  73.         case End:
  74.                 printf("出");break; //画出口
  75.         case Wall:
  76.                 printf("※");break; //画墙
  77.         case Road:
  78.                 printf(" ");break; //画路
  79.         }
  80. }
  81. void game()
  82. {
  83.         int x=2,y=1; //玩家当前位置,刚开始在入口处
  84.         int c; //用来接收按键
  85.         while(1)
  86.         {
  87.                 gotoxy(2*y-2,x-1);
  88.                 printf("☆"); //画出玩家当前位置
  89.                 if(map[x][y]==End) //判断是否到达出口
  90.                 {
  91.                         gotoxy(30,24);
  92.                         printf("到达终点,按任意键结束");
  93.                         getch();
  94.                         break;
  95.                 }
  96.                 c=get_key();
  97.                 if(c==Esc)
  98.                 {
  99.                         gotoxy(0,24);
  100.                         break;
  101.                 }
  102.                 switch(c)
  103.                 {
  104.                 case Up: //向上走
  105.                         if(map[x-1][y]!=Wall)
  106.                         {
  107.                                 paint(x,y);
  108.                                 x--;
  109.                         }
  110.                         break;
  111.                 case Down: //向下走
  112.                         if(map[x+1][y]!=Wall)
  113.                         {
  114.                                 paint(x,y);
  115.                                 x++;
  116.                         }
  117.                         break;
  118.                 case Left: //向左走
  119.                         if(map[x][y-1]!=Wall)
  120.                         {
  121.                                 paint(x,y);
  122.                                 y--;
  123.                         }
  124.                         break;
  125.                 case Right: //向右走
  126.                         if(map[x][y+1]!=Wall)
  127.                         {
  128.                                 paint(x,y);
  129.                                 y++;
  130.                         }
  131.                         break;
  132.                 }
  133.         }
  134. }
  135. int main()
  136. {
  137.         int i,j;
  138.         srand((unsigned)time(NULL)); //初始化随即种子
  139.         hidden(); //隐藏光标
  140.         for(i=0;i<=Height+1;i++)
  141.                 for(j=0;j<=Width+1;j++)
  142.                         if(i==0||i==Height+1||j==0||j==Width+1) //初始化迷宫
  143.                                 map[i][j]=Road;
  144.                         else map[i][j]=Wall;
  145.                         
  146.                         create(2*(rand()%(Height/2)+1),2*(rand()%(Width/2)+1)); //从随机一个点开始生成迷宫,该点行列都为偶数
  147.                         for(i=0;i<=Height+1;i++) //边界处理
  148.                         {
  149.                                 map[i][0]=Wall;
  150.                                 map[i][Width+1]=Wall;
  151.                         }
  152.                         
  153.                         for(j=0;j<=Width+1;j++) //边界处理
  154.                         {
  155.                                 map[0][j]=Wall;
  156.                                 map[Height+1][j]=Wall;
  157.                         }
  158.                         map[2][1]=Start; //给定入口
  159.                         map[Height-1][Width]=End; //给定出口
  160.                         for(i=1;i<=Height;i++)
  161.                                 for(j=1;j<=Width;j++) //画出迷宫
  162.                                         paint(i,j);
  163.                                 game(); //开始游戏
  164.                                 getch();
  165.                                 return 0;
  166. }
复制代码


以上是全部代码,可直接复制编辑



分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶 踩
回复

使用道具 举报

沙发
ID:478073 发表于 2020-4-3 07:26 | 只看该作者
不错啊!真能用!
回复

使用道具 举报

板凳
ID:91165 发表于 2020-4-7 07:37 | 只看该作者
谢谢楼主分享
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表