如下
#include<iostream>
#include<conio.h>
#include<stdio.h>
const char* map[40][40];
int main()
{
int x=5,y=5,a,b;
bool gamestart = 1,up,down,left,right;
for(a=0;a<40;a++)
for(b=0;b<40;b++)
map[a][b]="x";
for(a=1;a<39;a++)
for(b=1;b<39;b++)
map[a][b]=" ";
map[3][31]="x";
map[1][31]="x";
map[2][31]="x";
for(a=31;a<=38;a++)
{
map[3][a]="x";
}
map[3][34]=" ";
map[3][35]=" ";
map[y][x]="o"; //sidio出品
while(gamestart)
{
for(a=0;a<40;a++){for(b=0;b<40;b++){std::cout<<map[a][b];} std::cout<<std::endl;}
std::cout<<y<<" "<<x;
char ch = _getch();
up=1,down=1,left=1,right=1;
if(ch=='w')
{
if(map[y-1][x]!=" "){up=0;}
if(up!=0)
{
map[y-1][x]="o";
map[y][x]=" ";
y-=1;
}
}
if(ch=='s')
{
if(map[y+1][x]!=" "){down=0;}
if(down!=0)
{
map[y+1][x]="o";
map[y][x]=" ";
y+=1;
}
}
if(ch=='a')
{
if(map[y][x-1]!=" "){left=0;}
if(left!=0)
{
map[y][x-1]="o";
map[y][x]=" ";
x-=1;
}
}
if(ch=='d')
{
if(map[y][x+1]!=" "){right=0;}
if(right!=0)
{
map[y][x+1]="o";
map[y][x]=" ";
x+=1;
}
}
system("cls");
}
return 0;
}
这玩意我写了2天呢,看在这么努力的份上,点个赞呗(doge)