
#include<graphics.h>
#include<stdio.h>
#include<conio.h>
#include<windows.h>
main()
{
int a[100],b[100],c,d,e[2]={10,0},f=0,g[2],i=0;
char h[2]={d};
a[0]=50;
a[1]=60;
a[2]=70;
a[3]=80;
b[0]=20;
b[1]=20;
b[2]=20;
b[3]=20;
c=3;
initgraph(500,500);
while(1)
{
if(f==0)
{
g[0]=10*(rand()%50);
g[1]=10*(rand()%50);
f=1;
}
fillrectangle(g[0],g[1],g[0]+5,g[1]+5);
if(kbhit())
{
h[1]=h[0];
h[0]=getch();
}
rectangle(0,0,500,500);
if(h[0]=='A'&&h[1]!='D')
{
e[0]=-10;
e[1]=0;
}
if(h[0]=='S'&&h[1]!='W')
{
e[0]=0;
e[1]=10;
}
if(h[0]=='D'&&h[1]!='A')
{
e[0]=10;
e[1]=0;
}
if(h[0]=='W'&&h[1]!='S')
{
e[0]=0;
e[1]=-10;
}
for(d=0;d<c;d++)
{
a[d]=a[d+1];
b[d]=b[d+1];
}
if(a[c]+e[0]!=g[0]||b[c]+e[1]!=g[1])
{
a[c]+=e[0];
b[c]+=e[1];
}
if(a[c]+e[0]==g[0]&&b[c]+e[1]==g[1])
{
a[c+1]=g[0];
b[c+1]=g[1];
f=0;
c++;
}
if(a[c]==510||b[c]==510||a[c]==-10||b[c]==-10)
break;
for(d=0;d<c;d++)
{
if(a[c]==a[d]&&b[c]==b[d])
{
i=1;
break;
}
}
if(i==1)
break;
for(d=0;d<=c;d++)
{
fillrectangle(a[d],b[d],a[d]+5,b[d]+5);
}
Sleep(100);
cleardevice();
continue;
}
printf("生活如此艰难\n你长到了%d米",c+1);
}