#include <bits/stdc++.h>
#include <conio.h>
#include <windows.h>
#include <time.h>
#define height 20
#define width 20
//cout.setf(ios::fixed);
//freopen("out.txt","w",stdout);// freopen("in.txt","r",stdin);
using namespace std;
typedef long long ll;
typedef double db;
void pos(int x,int y) //移动坐标
{
COORD coord;
coord.X=x;
coord.Y=y;
SetConsoleCursorPosition( GetStdHandle( STD_OUTPUT_HANDLE ), coord );
}
void hidden()//隐藏光标
{
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_CURSOR_INFO cci;
GetConsoleCursorInfo(hOut,&cci);
cci.bVisible=0;//赋1为显示,赋0为隐藏
SetConsoleCursorInfo(hOut,&cci);
}
int go[4][2]={0,1,0,-1,1,0,-1,0},mp[105][105];
void ini()
{
}
int ok(int x,int y)
{
if(x<=0||x>=width-1||y<=0||y>=height-1) return 0;
return 1;
}
void dfs(int x,int y)
{
mp[x][y]=1;
for(int i=0;i<4;i++)
{
int j=i;
while(j==i) j=rand()%4;
swap(go[i][0],go[j][0]);
swap(go[i][1],go[j][1]);
}
for(int i=0;i<4;i++)
{
int xx=x+2*go[i][0],yy=y+2*go[i][1];
if(ok(xx,yy)&&!mp[xx][yy]&&!mp[x+go[i][0]][y+go[i][1]])
{
mp[x+go[i][0]][y+go[i][1]]=1;
dfs(xx,yy);
}
}
}
int stx=0,sty=1;
void game()
{
while(1)
{
if(stx==width-3&&sty==height-3) break;
char ch=getch();
pos(stx*2,sty),printf(" ");
if(ch=='w')
{
if(ok(stx,sty-1)&&mp[stx][sty-1]==1) sty--;
}
else if(ch=='s')
{
if(ok(stx,sty+1)&&mp[stx][sty+1]==1) sty++;
}
else if(ch=='a')
{
if(ok(stx-1,sty)&&mp[stx-1][sty]==1) stx--;
}
else if(ch=='d')
{
if(ok(stx+1,sty)&&mp[stx+1][sty]==1) stx++;
}
pos(stx*2,sty),printf("●");
}
}
int main()
{
hidden();
srand(time(0));
dfs(1,1);
for(int i=0;i<width-1;i++)
for(int j=0;j<height-1;j++)
if(!mp[i][j]) pos(i*2,j),printf("▇");
pos(stx*2,sty),printf("●");
pos(2*(width-3),height-3),printf("出");
game();
pos(0,height);
puts("您胜利了!");
getch();
system("pause");
return 0;
}
C语言走迷宫小游戏
猜你喜欢
转载自blog.csdn.net/qq_25973789/article/details/81164758
今日推荐
周排行