C++#include<windows.h>
#include<conio.h>
#include<time.h>
#include<bits/stdc++.h>
using namespace std;
int x,i,j,n,m,q1,w1,q2,w2,k,l=5;
char cx;
int f[21][31];
int a[5000],b[5000];
int flag=1;
string ans[5000][5000];
bool sz;
void gotoxy(int x, int y)
{
COORD coord;
HANDLE hscr;
coord.X=x;
coord.Y=y;
hscr=GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(hscr, coord);
}
int main()
{
CONSOLE_CURSOR_INFO cursor_info={1,0};
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE),&cursor_info);
cout<<"上移:w"<<endl;
cout<<"下移:s"<<endl;
cout<<"左移:a"<<endl;
cout<<"右移:d"<<endl;
cout<<"当你撞墙或装到自己身体,你就输了!"<<endl;
system("pause");
system("cls");
n=20;
m=30;
cout<<endl;
srand((unsigned)time(NULL));
cout<<"加载中......";
a[1]=rand()%n+1;
b[1]=rand()%m+1;
for(i=2;i<=5;i++)
{
x=rand()%4+1;
if(x==1)
{
a[i]=a[i-1]+1;
b[i]=b[i-1];
}
if(x==2)
{
a[i]=a[i-1]-1;
b[i]=b[i-1];
}
if(x==3)
{
a[i]=a[i-1];
b[i]=b[i-1]+1;
}
if(x==4)
{
a[i]=a[i-1];
b[i]=b[i-1]-1;
}
if(f[a[i]][b[i]]!=0)
{
i--;
continue;
}
f[a[i]][b[i]]=i;
}
f[a[1]][b[1]]=1;
i=0;
for(i=1;i<=n;i++)
{
for(j=1;j<=m;j++)
{
gotoxy(j*2-1,i-1);
if(f[i][j]==1) cout<<"头";
else if(f[i][j]>1) cout<<"身";
else if(f[i][j]==-1) cout<<"■";
else cout<<"—";
}
cout<<endl;
}
while(!kbhit());
cx=getch();
if(cx=='w'||cx=='W') flag=1;
if(cx=='s'||cx=='S') flag=2;
if(cx=='a'||cx=='A') flag=3;
if(cx=='d'||cx=='D') flag=4;
string ch;
while(1)
{
for(i=1;i<=n;i++)
{
for(j=1;j<=m;j++)
{
gotoxy(j*2-2,i-1);
if(f[i][j]==1) ch="头";
else if(f[i][j]>1)ch="身";
else if(f[i][j]==-1) ch="■";
else ch="—";
if(ch!=ans[i][j])
{
ans[i][j]=ch;
cout<<ch;
}
}
cout<<endl;
}
q1=a[1];
w1=b[1];
f[a[1]][b[1]]=2;
Sleep(20);
if(kbhit())
{
cx=getch();
if(cx=='w'||cx=='W') flag=1;
if(cx=='s'||cx=='S') flag=2;
if(cx=='a'||cx=='A') flag=3;
if(cx=='d'||cx=='D') flag=4;
}
if(flag==1) a[1]--;
if(flag==2) a[1]++;
if(flag==3) b[1]--;
if(flag==4) b[1]++;;
if(f[a[1]][b[1]]>0)
{
system("cls");
cout<<"GAMEOVER!"<<endl;
system("pause");
return 0;
}
if(f[a[i]][b[i]]==-1)
sz=1;
f[a[1]][b[1]]=1;
if(a[1]==0||a[1]>n||b[1]==0||b[1]>m)
{
system("cls");
cout<<"GAMEOVER!"<<endl;
system("pause");
return 0;
}
for(i=2;i<l;i++)
{
f[a[i]][b[i]]=i+1;
q2=a[i];
w2=b[i];
a[i]=q1;
b[i]=w1;
q1=q2;
w1=w2;
}
f[a[l]][b[l]]=0;
a[l]=q1;
b[l]=w1;
if(sz)
{
l++;
a[l]=a[l-1];
b[l]=b[l-1];
sz=0;
}
k=rand()%15;
if(!k)
{
q1=rand()%n+1;
w1=rand()%m+1;
f[q1][w1]=-1;
}
Sleep(100);
}
return 0;
}