You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

273 lines
7.0 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#include <stdio.h>
#include <conio.h>
#include <windows.h>
#include <time.h>
#define Height 25//迷宫的高度
#define Width 25//迷宫的宽度
#define Wall 1
#define Road 0
#define Start 2
#define End 3
#define Esc 5
#define Up 1
#define Down 2
#define Left 3
#define Right 4
static int flag=0,hei=0; //旗帜们
static int a=51,b=5; //'嘟'叫声的坐标
int map[Height+2][Width+2];
void gotoxy(int x,int y) //移动坐标
{
COORD coord; /*COORD实际上是一个C语言内部做好的结构体*/
coord.X=x;
coord.Y=y; //SetConsoleCursorPosition这个函数是用来移动光标的也是由C语言直接提供给你的直接使用就可以。
SetConsoleCursorPosition( GetStdHandle( STD_OUTPUT_HANDLE ), coord );
} //GetStdHandle(STD_OUTPUT_HANDLE)这里就是一个固定的函数格式,获得标准输出函数的句柄。
void hidden()//隐藏光标
{
HANDLE hout = GetStdHandle(STD_OUTPUT_HANDLE); //GetStdHandle这个函数也是C语言内部已经设定好的所以这里直接调用就行。
CONSOLE_CURSOR_INFO cci;
GetConsoleCursorInfo(hout,&cci);
cci.bVisible=0;//赋1为显示赋0为隐藏
SetConsoleCursorInfo(hout,&cci);
}
void create(int x,int y) //随机生成迷宫
{
int c[4][2]={{0,1},{1,0},{0,-1},{-1,0}}; //四个方向
int i,j,t;
//将方向打乱
for(i=0;i<4;i++)
{
j=rand()%4;
t=c[i][0];
c[i][0]=c[j][0];
c[j][0]=t;
t=c[i][1];
c[i][1]=c[j][1];
c[j][1]=t;
}
map[x][y]=Road;
for(i=0;i<4;i++)
if(map[x+2*c[i][0]][y+2*c[i][1]]==Wall)
{ //这个递归四向搜索可以完成对整个地图生成随机的路径
map[x+c[i][0]][y+c[i][1]]=Road;
create(x+2*c[i][0],y+2*c[i][1]);
}
}
int get_key() //接收按键
{
char c;
while(c=getch())
{
if(c==27) return Esc; //Esc
//if(c!=-32)continue;
//c=getch();
if(c==72) return Up; //上
if(c==80) return Down; //下
if(c==75) return Left; //左
if(c==77) return Right; //右
}
return 0;
}
void paint(int x,int y) //画迷宫
{
gotoxy(2*y-2,x-1);//?????为什么x和y的坐标反着搞的。
//gotoxy(y,x);
switch(map[x][y])
{
case Start:
//SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 9); 想给某个坐标单独上色呃,
printf("->");break; //画入口
case End:
//SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 9); 但是这样不行所以也就没继续搞。
printf("->");break; //画出口
case Wall:
//SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 9);
printf("");break; //画墙
case Road:
//SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 9);
printf(" ");break; //画路
}
}
int game()
{
int x=2,y=1; //玩家当前位置,刚开始在入口处
int c; //用来接收按键
while(1)
{
if(a>70)// 撞墙累死设定;
break;
gotoxy(2*y-2,x-1);// 系统上的坐标x为纵向y为横向 英文字符为半角,宽为高的一半
printf(""); //画出玩家当前位置
if(map[x][y]==End) //判断是否到达出口
{
gotoxy(42,26);
printf("胜利~~~~~");
gotoxy(42,30);
printf("按空格继续试炼~");
gotoxy(38,45);
printf("桀桀桀,轮回之人,终究逃不过宿命....");
getch();
break;
}
c=get_key();
if(c==Esc)
{
gotoxy(0,24);
flag=1;
break;
}
switch(c)
{
case Up: //向上走
if(map[x-1][y]!=Wall)
{
paint(x,y);//避免留下残影
x--;
}
else
{
gotoxy(a++,b++);
printf("嘟!你试图破墙而出,但对墙的攻击貌似毫无效果...体力-1");
if(a>=70)
{
gotoxy(42,26);
printf("啊,你累死了。");
hei++;
break;
}
}
break;
case Down: //向下走
if(map[x+1][y]!=Wall)
{
paint(x,y);
x++;
}
else
{
gotoxy(a++,b++);
printf("嘟!你试图破墙而出,但对墙的攻击貌似毫无效果...体力-1");
if(a>=70)
{
gotoxy(42,26);
printf("啊,你累死了。");
hei++;
break;
}
}
break;
case Left: //向左走
if(map[x][y-1]!=Wall)
{
paint(x,y);
y--;
}
else
{
gotoxy(a++,b++);
printf("嘟!你试图破墙而出,但对墙的攻击貌似毫无效果...体力-1");
if(a>=70)
{
gotoxy(42,26);
printf("啊,你累死了。");
hei++;
break;
}
}
break;
case Right: //向右走
if(map[x][y+1]!=Wall)
{
paint(x,y);
y++;
}
else
{
gotoxy(a++,b++);
printf("嘟!你试图破墙而出,但对墙的攻击貌似毫无效果...体力-1");
if(a>=70)
{
gotoxy(42,26);
printf("啊,你累死了。");
hei++;
break;
}
}
break;
}
}
}
int main()
{
system("color F3");
out://通关后可重置迷宫goto在最后几行
int i,j;
char k;
flag=0,hei=0; a=51,b=5;
system("title 始乱幻阵");
gotoxy(51,0);
printf("伟大的勇者,在这个始末幻阵,一旦踏入, 便无回首,望你于此阵中得道,集大成而无敌。");
gotoxy(51,2);
printf("I进入按空格吧~\n II否则请'ESC'咯~");
k=getch();
if(k==' '){
gotoxy(51,4);
printf("OK~~");
}
else
{
gotoxy(51,4);
printf("Goodbye");
return 0;
}
srand((unsigned)time(NULL)); //利用系统时间作参数初始化随机种子
hidden(); //隐藏光标
for(i=0;i<=Height+1;i++)
for(j=0;j<=Width+1;j++)
if(i==0||i==Height+1||j==0||j==Width+1) //初始化迷宫,外围先设定为路防止误走
map[i][j]=Road;
else map[i][j]=Wall; //wall==0,road==1;
create(2*(rand()%(Height/2)+1),2*(rand()%(Width/2)+1)); //从随机一个点开始生成迷宫,该点行列都为偶数
for(i=0;i<=Height+1;i++) //边界处理
{
map[i][0]=Wall;
map[i][Width+1]=Wall;
}
for(j=0;j<=Width+1;j++) //边界处理
{
map[0][j]=Wall;
map[Height+1][j]=Wall;
}
map[2][1]=Start; //给定入口 start==2
map[Height-1][Width]=End; //给定出口 end==3
for(i=1;i<=Height;i++)
for(j=1;j<=Width;j++) //画出迷宫
paint(i,j);
game(); //开始游戏
if(hei==0)
{
system("cls");
goto out;
}
else
{
return 0;
}
if(flag==1)
return 0;
getch();
return 0;
}