c++贪吃蛇(没实力,做的不好)

wchjh 2024-09-15 22:44:46 15
#include<bits/stdc++.h>
#include<windows.h>
#include<conio.h>
using namespace std;
int main(){
short x,y,xy[35][20],l[455][2],n=1,hx,hy;
char a;
hx=15;
hy=7;
//蛇头初始坐标 
memset(xy, 0, sizeof(xy));
srand(time(0));
x=rand()%30+1;		 
y=rand()%15+1;		 
while(xy[x][y]!=0){		 
	x=(rand()%30)+1;	
	y=(rand()%15)+1;	 
}  						
xy[x][y]=-1;
//生成豆子	
while(1){
	l[1][0]=hx;
	l[1][1]=hy;
	a=_getch();
	if(a=='w'){
		hy--;
	}
	if(a=='d'){
		hx++;
	}
	if(a=='s'){
		hy++;
	}
	if(a=='a'){
		hx--;
	}
	//按下了哪个键并移动蛇头 
	if(hx<0||hx>30||hy<0||hy>15||xy[hx][hy]==1){break;}//碰到边缘或身体死亡 
	xy[hx][hy]=2;							 
	if(hx==x&&hy==y){ 
		x=rand()%30+1;		
		y=rand()%15+1;		 
		while(xy[x][y]!=0){	 
			x=(rand()%30)+1; 
			y=(rand()%15)+1;		
		}					 
		xy[x][y]=-1;		 
		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;
	}
	//蛇身移动(移动到前一个蛇身的位置) 
	system("cls");
	//清屏 
	cout<<"--------------------------------";//生成上边界 
	for(int i=1;i<=15;i++){
		cout<<"\n|";
		for(int j=1;j<=30;j++){
			if(xy[j][i]==0){cout<<' ';}
			else if(xy[j][i]==2){cout<<'#';}
			else if(xy[j][i]==1){cout<<'*';}
			else if(xy[j][i]==-1){cout<<'o';}
		}
		cout<<"|";
	}
	//生成左右边界、豆子、蛇身以及蛇头 
	cout<<"\n--------------------------------";//生成下边界 
}
system("cls");//死亡后清屏; 
cout<<"                    *       *    ***    *     *         *****     *****   *******\n                     *     *    *   *   *     *         *    *      *     *\n                      *   *    *     *  *     *         *     *     *     *\n                       * *     *     *  *     *         *     *     *     *\n                        *      *     *  *     *         *     *     *     *******\n                        *      *     *  *     *         *     *     *     *\n                        *      *     *  *     *         *     *     *     *\n                        *       *   *    *   *          *    *      *     *\n                        *        ***      ***           *****     *****   *******";
//打印YOU DIE 
}
{{ vote && vote.total.up }}

共 3 条回复

wchjh
改了亿点点(因为怕别人不会改,窝系补系很银杏)
#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;
unsigned long long sd;
bool zt=0,e=0,cd,bj,aj=1;
char a,b='d';
cout<<"------------------------------------------------------------\n              输入正常输,乱输会报错或影响运行!\n------------------------------------------------------------\n输入地图宽度(大于0,尽量小于等于20):"; 
scanf("%hd",&lx);
cout<<"输入地图高度(大于0,尽量小于等于20):"; 
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("%llu",&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;
	t=clock();
	aj=1;
	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 
} 
T001

E

wchjh
稍微改进了
#include<bits/stdc++.h>
#include<windows.h>
#include<conio.h>
using namespace std;
int main(){
short x,y,xy[35][20],l[455][2],n=5,hx,hy;//n的初始值相当于初始长度(包括蛇头) 
char a;
hx=15;
hy=7;
//蛇头初始坐标 
memset(xy, 0, sizeof(xy));
memset(l, 0, sizeof(l));
srand(time(0));
x=rand()%30+1;		 
y=rand()%15+1;		 
while(xy[x][y]!=0){		 
	x=(rand()%30)+1;	
	y=(rand()%15)+1;	 
}  						
xy[x][y]=-1;
//生成豆子	
while(1){
	l[1][0]=hx;
	l[1][1]=hy;
	a=_getch();
	if(a=='w'){
		hy--;
	}
	if(a=='d'){
		hx++;
	}
	if(a=='s'){
		hy++;
	}
	if(a=='a'){
		hx--;
	}
	//按下了哪个键并移动蛇头 
	if(hx<=0||hx>30||hy<=0||hy>15||xy[hx][hy]==1){break;}//碰到边缘或身体死亡(把“||xy[hx][hy]==1”去掉碰到身体不会死亡)					 
	if(hx==x&&hy==y){ 
		x=rand()%30+1;		
		y=rand()%15+1;		 
		while(xy[x][y]!=0){	 
			x=(rand()%30)+1; 
			y=(rand()%15)+1;		
		}					 
		xy[x][y]=-1;		 
		n=n+1;//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<<"--------------------------------";//生成上边界 
	for(int i=1;i<=15;i++){
		cout<<"\n|";
		for(int j=1;j<=30;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<<' ';}
		}
		cout<<"|";
	}
	//生成左右边界、豆子、蛇身以及蛇头 
	cout<<"\n--------------------------------";//生成下边界 
}
system("cls");//死亡后清屏; 
cout<<"                    *       *    ***    *     *         *****     *****   *******\n                     *     *    *   *   *     *         *    *      *     *\n                      *   *    *     *  *     *         *     *     *     *\n                       * *     *     *  *     *         *     *     *     *\n                        *      *     *  *     *         *     *     *     *******\n                        *      *     *  *     *         *     *     *     *\n                        *      *     *  *     *         *     *     *     *\n                        *       *   *    *   *          *    *      *     *\n                        *        ***      ***           *****     *****   *******";
//打印YOU DIE 
}