跳到正文

个人记录

走迷宫

abs20187约 5 分钟
原文
文章信息与版本

文章信息

发布
2024年10月4日
收录
2026年6月1日
更新
2026年8月24日
篇幅
约 5 分钟

版本

  1. v1当前
C++
#include<bits/stdc++.h>
#include<windows.h>
#include<conio.h>
using namespace std;
string a[21]={
	"########################",
	"#@##...............##..#",
	"#..##.###..####.##.###.#",
	"##.##.##...###..##.###.#",
	"##.##.##...##..##..##..#",
	"#..#..##.###...#..##..##",
	"#.###.#..####..##....###",
	"#...#.#.###...#####....#",
	"#..#..#..#..########.#.#",
	"#..#..#.##...######..#.#",
	"#..#..#..#....######.###",
	"#.###.##..###.###....###",
	"#..#..#..#....######.#.#",
	"##...##.###.#######....#",
	"######..###.#########..#",
	"##########.%...#######.#",
	"######.#####..###......#",
	"#####...#####..#########",
	"###...........##########",
	"##########.............#",
	"########################"
};
void Gt(int x,int y)
{
	COORD c;
	c.X=y;
	c.Y=x;
	HANDLE a=GetStdHandle(STD_OUTPUT_HANDLE);
	SetConsoleCursorPosition(a,c);
}
int blx,bly;
void print()
{
	for(int i=0;i<=20;i++)
	{
		Gt(i+10,10);
		for(int j=0;j<=23;j++)
		{
			if(a[i][j]=='#')cout<<"\033[38;2;67;252;247m"<<"▓ "<<"\033[0m";
			if(a[i][j]=='.')
			{
				cout<<"  ";
			}
			if(a[i][j]=='%')
			{
				cout<<"\033[38;2;255;0;0m"<<"▓ "<<"\033[0m";
				blx=i,bly=j;
			}
			if(a[i][j]=='@')
			{
				cout<<"\033[38;2;240;120;250m"<<"●"<<"\033[0m"; 
			}
		}
		cout<<"\n";
	}
}
int dx[]={-1,1,0,0};
int dy[]={0,0,-1,1};
int dkey[]={'W','S','A','D'};
const int bx=10,by=10;
int main()
{
	system("color 07");
	//qwq
	int px,py;
	px=py=1;
	print();
	int t=0;
	int f=-1;
	while(!(px==19&&py==22))
	{
		
		/*char c=getch();
		while(c!='w'&&c!='a'&&c!='s'&&c!='d')
		{
			c=getch();
		}*/
		for(int i=0;i<4;i++)
		{
			if(GetAsyncKeyState(dkey[i]))
			{
				int nx=px+dx[i],ny=py+dy[i];
				if(a[nx][ny]=='.')
				{
					Gt(bx+nx,bx+2*ny);
					cout<<"\033[38;2;240;120;250m"<<"●"<<"\033[0m";  
					Gt(bx+px,by+2*py);
					cout<<"  "; 
					a[nx][ny]='@';
					a[px][py]='.';
					px=nx,py=ny;
				} 
			}
		}
		Sleep(100);
		t+=1;
		if(t==4)
		{
			Gt(bx+blx,by+2*bly);
			cout<<"  ";
			a[blx][bly]='.';
			bly+=f;
			f=0-f;
			Gt(bx+blx,by+2*bly);
			cout<<"\033[38;2;255;0;0m"<<"▓ "<<"\033[0m";
			if(a[blx][bly]=='@')
			{
				system("cls");
				cout<<"You die";
				return 0;
			}
			a[blx][bly]='%';
			t=0;
		}
		//system("cls");
	}
	system("cls");
	cout<<"Youwin!";
	return 0;
}
原文