首页 > 用户发贴区 > 编程问题提问区 > 求救关于C语言迷宫
2010
09-19

#include <stdio.h>
#include <bios.h>
#include <conio.h>
#define ESC  0x011b
#define UP 0×4800
#define DOWN 0×5000
#define LEFT 0x4b00
#define RIGHT 0x4d00
typedef struct
{ int x;
  int y;
}point;
char map[10][20]=
{ “####################”,
  “##              #”,
  “#  #########  # #  #”,
  “# ####     #   ###”,
  “# #    ###   #  ##”,
  “# #  ##### #   #  #”,
  “# #     ##   #   #”,
  “#  #####  # ## #  #”,
  “#             #    #”,
  “####################”
};
DrawMan(int x,int y)
{ gotoxy(x+10,y+5);
  printf(“%c\b”,2);
}
DrawSpace(int x,int y)
{ gotoxy(x+10,y+5);
  printf(” “);
}
DrawWall(int x,int y)
{ gotoxy(x+10,y+5);
  textcolor(RED);
  putch(219);
}
void DrawMap()
{ int x,y;
  for(y=0;y<10;++y)
   for(x=0;x<20;++x)
    if(map[y][x]==’#') DrawWall(x,y);
  gotoxy(38,2);
  textcolor(RED);
  printf(“Man Game”);
  gotoxy(48,6);
  printf(“play game:”);
  gotoxy(48,8);
  printf(“push key move man!”);
  gotoxy(48,10);
  printf(“push Esc Exit game!”);
}
void main()
{ point man={1,1},des={18,8};
int key=0;
clrscr();
DrawMap();
DrawMan(man.x,man.y);
textcolor(YELLOW);
gotoxy(des.x+10,des.y+5);
putch(‘*’);
while(key!=ESC)
{ key=bioskey(0);
  switch(key)
  { case UP:
      if(map[man.y-1][man.x]==’#') break;
      DrawSpace(man.x,man.y);
      –man.y;
      DrawMan(man.x,man.y);
    case DOWN:
      if(map[man.y+1][man.x]==’#') break;
      DrawSpace(man.x,man.y);
      ++man.y;
      DrawMan(man.x,man.y);
    case LEFT:
      if(map[man.y][man.x-1]==’#') break;
      DrawSpace(man.x,man.y);
      –man.x;
      DrawMan(man.x,man.y);
    case RIGHT:
      if(map[man.y][man.x+1]==’#') break;
      DrawSpace(man.x,man.y);
      ++man.x;
      DrawMan(man.x,man.y);
  }
  if(man.x==des.x&&man.y==des.y)
  { gotoxy(38,14);
    printf(“success!”);
    getch();
    key=ESC;
  }
}
}

运行后确实会出现迷宫,但不能向上和向左走,不知道是为什么,求高手教我改~


求救关于C语言迷宫》有 1 条评论

  1. hiroki 说:

    你没有把键盘输入参数加进去 你把主函数参数加进去就可以了

留下一个回复