#include<bits/stdc++.h>
#include<windows.h>
#include<conio.h>
#include<time.h>
using namespace std;
int main(){
short x,y,n,hx,hy,lx,ly,zl,t;
long long sd;
bool zt=0,e=0,cd,bj,aj=1;
char a,b='d';
cout<<"------------------------------------------------------------\n 输入正常输,乱输会报错或影响运行!\n------------------------------------------------------------\n输入地图宽度(大于0,尽量小于等于50):";
scanf("%hd",&lx);
cout<<"输入地图高度(大于0,尽量小于等于50):";
scanf("%hd",&ly);
cout<<"输入初始长度(小于等于"<<lx*ly<<",尽量很小):";
scanf("%hd",&n);
cout<<"输入初始x坐标(大于0,小于等于"<<lx<<"):";
scanf("%hd",&hx);
cout<<"输入初始y坐标(大于0,小于等于"<<ly<<"):";
scanf("%hd",&hy);
cout<<"输入吃豆子增加的长度(尽量很小):";
scanf("%hd",&zl);
cout<<"输入刷新速度(单位ms):";
scanf("%lld",&sd);
cout<<"身体可以重叠(1开,0关):";
cin>>cd;
cout<<"可以穿过边界(1开,0关):";
cin>>bj;
short xy[lx+5][ly+5],l[lx*ly+5][2];
memset(xy, 0, sizeof(xy));
memset(l, 0, sizeof(l));
xy[hx][hy]=2;
srand(time(0));
x=rand()%lx+1;
y=rand()%ly+1;
while(xy[x][y]!=0){
x=(rand()%lx)+1;
y=(rand()%ly)+1;
}
xy[x][y]=-1;
//生成豆子
while(1){
while(1){
l[1][0]=hx;
l[1][1]=hy;
aj=1;
t=clock();
while(aj&&!kbhit()){aj=clock()-t<=sd;}
if(aj){a=_getch();}
else{a=b;}
if(a=='w'||a=='W'||a==72){
if(hy==1&&bj){hy=ly;}
else{hy--;}
}
else if(a=='d'||a=='D'||a==77){
if(hx==lx&&bj){hx=1;}
else{hx++;}
}
else if(a=='s'||a=='S'||a==80){
if(hy==ly&&bj){hy=1;}
else{hy++;}
}
else if(a=='a'||a=='A'||a==75){
if(hx==1&&bj){hx=lx;}
else{hx--;}
}
else if(a=='r'||a=='R'){
zt=1;
break;
}
else{break;}
b=a;//备用,取消暂停或刷新时使用
//按下了哪个键并移动蛇头
if((hx<=0||hx>lx||hy<=0||hy>ly)&&!bj){e=1;break;}//碰到边界死亡
if(xy[hx][hy]==1&&!cd){e=1;break;}//碰到身体死亡
if(hx==x&&hy==y){
x=rand()%lx+1;
y=rand()%ly+1;
while(xy[x][y]!=0){
x=(rand()%lx)+1;
y=(rand()%ly)+1;
}
xy[x][y]=-1;
n=n+zl;//n的增量相当于吃豆子增加的长度
}
//吃下豆子后生成新豆子
else{xy[(l[n][0])][(l[n][1])]=0;}//不吃豆子长度不变
for(int i=n;i>1;i--){
l[i][0]=l[i-1][0];
l[i][1]=l[i-1][1];
xy[(l[i][0])][(l[i][1])]=1;
}
//蛇身移动(移动到前一个蛇身的位置)
xy[hx][hy]=2;
system("cls");
//清屏
cout<<"WASD移动 当前长度:"<<n<<"\n↑←↓→移动 ";
for(int i=1;i<=lx;i++){cout<<"--";}//生成上边界
cout<<"-\nR暂停/继续 ";
for(int i=1;i<=ly;i++){
cout<<'|';
for(int j=1;j<=lx;j++){
if(xy[j][i]==2){cout<<'#';}
else if(xy[j][i]==1){cout<<'*';}
else if(xy[j][i]==-1){cout<<'o';}
else if(xy[j][i]==0){cout<<' ';}
if(j!=lx){cout<<' ';}//因为每行的空隙比每列空隙大,用空格弥补差距
}
cout<<"|\n ";
}
//生成左右边界、豆子、蛇身以及蛇头
for(int i=1;i<=lx;i++){cout<<"--";}//生成下边界
cout<<'-';
}
if(zt){
a=_getch();
while(a!='r'&&a!='R'){
a=_getch();
}
zt=0;
}
if(e){break;}
}
system("cls");//死亡后清屏;
cout<<" * * *** * * ***** ***** *******\n * * * * * * * * * *\n * * * * * * * * * *\n * * * * * * * * * *\n * * * * * * * * *******\n * * * * * * * * *\n * * * * * * * * *\n * * * * * * * * *\n * *** *** ***** ***** *******";
//打印YOU DIE
}
共 2 条回复
只是有亿点点问题而已( •̀ ω •́ )
太好玩了(you dian wen ti)