代码如下,时间太晚,有空补注释:
#include<stdio.h>
#include<string.h>
#include<time.h>
#include<stdlib.h>
#include<windows.h>
#define n 21
#define m 37
#define up 1
#define down 2
#define left 3
#define right 4
#define Key_Up 72
#define Key_Down 80
#define Key_Right 77
#define Key_Left 75
typedef struct Point
{
int x;
int y;
int pos;
}point;
HANDLE h_out_buf;//缓冲技术解决闪屏
COORD coord={,};
DWORD bytes=;
char data[n+][m*+];
int maze[n+][m+];
point p[];
int l=;
point start,end;
void add(point t)
{
p[l]=t;
l++;
}
void add_neibor(point t)//将当前点周围的邻墙加入数组中
{
point q;
if(t.x+<=n)
{
q=t;
q.x=t.x+;
q.pos=down;
add(q);
}
if(t.x->=)
{
q=t;
q.x=t.x-;
q.pos=up;
add(q);
}
if(t.y+<=m)
{
q=t;
q.y=t.y+;
q.pos=right;
add(q);
}
if(t.y->=)
{
q=t;
q.y=t.y-;
q.pos=left;
add(q);
}
}
void del(int index)
{
int i;
for(i=index;i<l-;i++)
p[i]=p[i+];
l--;
}
void show_maze()
{
int i,j;
for(i=;i<n+;i++)
{
for(j=;j<m+;j++)
{
if(!maze[i][j])
{
data[i][*j]='*';
data[i][*j+]=' ';
}
else if(maze[i][j]==)
{
data[i][*j]=' ';
data[i][*j+]=' ';
}
else if(maze[i][j]==)
{
data[i][*j]=;
data[i][*j+]=' ';
}
else
{
data[i][*j]=;
data[i][*j+]=' ';
}
}
}
for(i=;i<n+;i++)
{
coord.Y=i;
WriteConsoleOutputCharacterA(h_out_buf, data[i], *j, coord, &bytes);
}
SetConsoleActiveScreenBuffer(h_out_buf);
}
void creat_maze()
{
int i,j,x,y;
point q;
memset(maze,,sizeof(maze));
srand((unsigned)time(NULL));
start.x=;
start.y=;
end.x=n;
end.y=m;
maze[][]=;
add_neibor(start);
while(l)
{
int r=rand()%l;
if(p[r].pos==up)
{
x=p[r].x-;
y=p[r].y;
}
if(p[r].pos==down)
{
x=p[r].x+;
y=p[r].y;
}
if(p[r].pos==left)
{
x=p[r].x;
y=p[r].y-;
}
if(p[r].pos==right)
{
x=p[r].x;
y=p[r].y+;
}
if(maze[x][y]==)
{
maze[p[r].x][p[r].y]=;
maze[x][y]=;
q.x=x;
q.y=y;
add_neibor(q);
}
del(r);
}
maze[start.x][start.y]=;
maze[end.x][end.y]=;
}
void win()
{
int i,j;
char a[]="CONGRATULATIONS!";
char b[]="YOU WIN THIS GAME!";
char c[]="AUTHOR:天道铸魂";
char d[]="按任意键结束游戏";
memset(data,,sizeof(data));
for(i=;i<n+;i++)
{
for(j=;j<m+;j++)
{
data[i][*j]=' ';
data[i][*j+]=' ';
}
}
strcpy(data[]+,a);
strcpy(data[]+,b);
strcpy(data[]+,c);
strcpy(data[]+,d);
for(i=;i<n+;i++)
{
coord.Y=i;
WriteConsoleOutputCharacterA(h_out_buf, data[i], *j, coord, &bytes);
}
SetConsoleActiveScreenBuffer(h_out_buf);
}
void play_game()
{
int key1,key;
point now;
now.x=now.y=;
while()
{
show_maze();
if(now.x==n&&now.y==m)
{
system("cls");
win();
break;
}
key1=getch();
key=getch();
if(key==Key_Up)
{
if(now.x->)
{
if(maze[now.x-][now.y])
{
maze[now.x][now.y]=;
now.x--;
maze[now.x][now.y]=;
}
}
}
else if(key==Key_Down)
{
if(now.x+<=n)
{
if(maze[now.x+][now.y])
{
maze[now.x][now.y]=;
now.x++;
maze[now.x][now.y]=;
}
}
}
else if(key==Key_Left)
{
if(now.y->)
{
if(maze[now.x][now.y-])
{
maze[now.x][now.y]=;
now.y--;
maze[now.x][now.y]=;
}
} }
else if(key==Key_Right)
{
if(now.y+>)
{
if(maze[now.x][now.y+])
{
maze[now.x][now.y]=;
now.y++;
maze[now.x][now.y]=;
}
}
}
}
}
void start_game()
{
creat_maze();
play_game();
}
int main()
{
h_out_buf = CreateConsoleScreenBuffer(GENERIC_WRITE,FILE_SHARE_WRITE,NULL,CONSOLE_TEXTMODE_BUFFER,NULL);
CONSOLE_CURSOR_INFO cci;
cci.bVisible = ;
cci.dwSize = ;
SetConsoleCursorInfo(h_out_buf, &cci);
start_game();
return ;
}