代码:
#include<bits/stdc++.h>
#include<windows.h>
#include<conio.h>
#include<climits>
#define endl "\n"
const int inf=INT_MAX;
typedef long long ll;
typedef double db;
using namespace std;
const int dx[]={0,1,0,-1};
const int dy[]={-1,0,1,0};
void sc(int c){
HANDLE h=GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(h,c);
}
void gotoxy(int x,int y){
COORD pos;
pos.X=x;
pos.Y=y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),pos);
}
mt19937 gen(time(0));
int ran(int a,int b){
if(a>b)swap(a,b);
uniform_int_distribution<int>dist(a,b);
return dist(gen);
}
string ranstr(int length){
const string chars="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz!@#$%^&*()_+-=[]{}|;:,.<>?";
string str;
for(int i=0;i<length;i++){
str+=chars[ran(0,chars.size()-1)];
}
return str;
}
void c_r(string s){
for(char c:s){
cout<<c;
Sleep(40);
}
}
void c_r2(string s){
c_r(s);
cout<<endl;
}
void c_r3(string s,int co){
for(char c:s){
sc(co);
cout<<c;
Sleep(40);
}
sc(7);
cout<<endl;
}
struct player{
int x,y;
int color;
};
char mp[21][42];
int mpcolor[21][42];
char mark[21][42];
player p;
void init(){
p.x=10;
p.y=10;
memset(mp,' ',sizeof(mp));
for(int i=0;i<21;i++){
for(int j=0;j<42;j++){
mpcolor[i][j]=116;
}
}
for(int i=0;i<42;i++)mpcolor[0][i]=140;
for(int i=1;i<=19;i++){
mpcolor[i][0]=140;
mpcolor[i][1]=140;
mpcolor[i][40]=140;
mpcolor[i][41]=140;
}
for(int i=0;i<42;i++)mpcolor[20][i]=140;
}
void paint(){
for(int i=0;i<21;i++){
for(int j=0;j<42;j++){
sc(mpcolor[i][j]);
if(i==p.x&&j==p.y)cout<<'@';
else cout<<mp[i][j];
}
cout<<endl;
}
}
void go(int dic,int x,int y){
int nx=dx[dic]+x;
int ny=dy[dic]+y;
if(nx>0&&nx<20&&ny>1&&ny<40&&mpcolor[nx][ny]!=140){
p.x=nx;
p.y=ny;
}
}
void hidecursor(){
HANDLE hConsole=GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_CURSOR_INFO cursorInfo;
GetConsoleCursorInfo(hConsole,&cursorInfo);
cursorInfo.bVisible=false;
SetConsoleCursorInfo(hConsole,&cursorInfo);
}
void in(){
while(1){
paint();
if(_kbhit()){
char c=_getch();
if(c=='a')go(0,p.x,p.y);
else if(c=='s')go(1,p.x,p.y);
else if(c=='d')go(2,p.x,p.y);
else if(c=='w')go(3,p.x,p.y);
else if((c>='A'&&c<='F'))p.color=((int(c-'A')+10)<<4)+4;
else if((c>='0'&&c<='9'))p.color=(int(c-'0')<<4)+4;
else if(c==' '){
if(mpcolor[p.x][p.y]==116)mpcolor[p.x][p.y]=p.color;
else mpcolor[p.x][p.y]=116;
}
}
gotoxy(0,0);
}
}
signed main(){
hidecursor();
init();
in();
sc(7);
return 0;
}