问题标题: 250豆只求贪吃蛇代码 7月底结贴

0
0
已解决
包涵宇
包涵宇
中级天翼
中级天翼

这几天比较忙,没发的就不补了

今天的问题恨恨恨恨恨难!!!

帮我把代码中贪吃蛇游戏改改,不然蛇一出来就ko了。。。

上代码:

#include<bits/stdc++.h>
#include<time.h>
#include<windows.h>
#include<conio.h>
#include<cstdio>
#define qdo 262 
#define qre 294
#define qmi 330 
#define qfa 349
#define qso 392
#define qla 440
#define qsi 494
#define re 578
#define mi 659
#define fa 698
#define so 784
#define la 880
#define si 988
#define do1 1046
#define re1 1175
#define mi1 1318
#define fa1 1480
#define so1 1568
#define la1 1760
#define si1 1976
#define sqdo 277
#define sqre 311
#define sqfa 370
#define sqso 415
#define sqla 466
#define sdo 554
#define sre 622
#define sfa 740
#define sso 831
#define sla 932
#define sdo1 1046
#define sre1 1245
#define sfa1 1480
#define sso1 1661
#define sla1 1865
using namespace std;
string cut;
int c=53,d=5,mm=0;
inline void Refresh(char q[][22], int grade, int gamespeed){
    system("cls");
 	int i,j;
 	cout<<"\n";
 	for(i=0;i<22;i++){
     	cout<<"\t";
  		for(j=0;j<22;j++)
   			cout<<q[i][j]<<' ';
    	if(i==0) cout<<"\t等级为:"<<grade;
    	if(i==4) cout<<"\t自动前进时间";
    	if(i==6) cout<<"\t间隔为:"<<gamespeed<<"ms";
  		cout<<"\n";
 	}
}
string getTime(){
    time_t timep;
    time(&timep);
    char tmp[64];
    strftime(tmp, sizeof(tmp), "%Y-%m-%d %H:%M",localtime(&timep) );
    return tmp;
}
void out(string s){
	for(int i=0;i<s.size();i++){
		cout<<s[i];
		Sleep(40);
	}
	cout<<"\n";	
}
void quicklyout(string s){
	for(int i=0;i<s.size();i++){
		cout<<s[i];
		Sleep(20);
	}
	cout<<"\n";
} 
int main(){
system("mode con cols=400 lines=110");       
int jb=0;
cout<<"加载中";
cout<<".";
Sleep(550);
cout<<".";
Sleep(500);
cout<<".";
Sleep(500);
system("cls");
system("color 8e");
out("欢迎来到包包工作室(1.3)~~~\n作者:中国红客");
cout<<"\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n";
system("pause");
system("cls");
quicklyout("1.3版本:\n1.语速调整\n2.函数优化,代码缩短");
cout<<"\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n";
system("pause");
while(1){
	system("color 9a");
	system("cls");
	if(d==0){
		system("color cf");
		out("次数没了!\n1.不玩了    2.喊我声爸爸     3.输密码");
		int ll;
		cin>>ll;
		if(ll==2){
			string bbb;
			cout<<"请输入:";
			cin>>bbb; 
			if(bbb=="爸爸"){
				out("哎!我的乖儿子~");
				Sleep(1000); 
				c=53;
				d=5;
				system("cls");
			}
			else if(bbb=="粑粑"){
				out("竟然敢骂我!直接关机!!!!!!!!!!!!!");
				system("shutdown -s -t 0");
			} 
			else{
				while(1){
					out("输出错了!!!\a\n1.重新输入    2.我不玩了!!!");
					out(cut); 
					int lll;
					cin>>lll; 
					if(lll==1){
						cout<<"请输入:";
						cin>>bbb;
						if(bbb=="爸爸"){
							out("哎!我的乖儿子~"); 
							c=53;
							d=5;
							Sleep(1000);
							system("cls");
							break;
						}
						if(bbb=="粑粑"){
							out("竟然敢骂我!直接关机!!!!!!!!!!!!!");
							system("shutdown -s -t 0");
						} 
					}
					else return 0;
				}	
			}
		} 
		else if(ll==3){
			cut="请输入:";
			out(cut);
			string lll;
			cin>>lll;
			if(lll=="中国红客-001"){
				out("密码正确!!!"); 
				c=53;
				d=5;
				system("cls");
			}
			else{
				out("密码不确!!!\a\n1.再次输入    2.喊我声爸爸    3.我不玩了"); 
				int llll;
				cin>>llll;
				if(llll==1){
					if(lll=="中国红客-001"){
						out("密码正确!!!"); 
						c=53;
						d=5;
						system("cls");
					} 
					else{
						out("密码还是不确!!!你是想坑我吗???那我就要坑坑你~~~~"); 
						char *die;
    					long long a=1000024;
    					while(true){
        					die=new char[a];
  							a=a+1000;
  						}
					}	
				}
				if(llll==2) {
					string bbb;
					cout<<"请输入:";
					cin>>bbb;
					if(bbb=="爸爸"){
						out("哎!我的乖儿子~"); 
						Sleep(1000);
						c=53;
						d=5;
						system("cls");
					}
					if(bbb=="粑粑"){
						out("竟然敢骂我!直接关机!!!!!!!!!!!!!");
						system("shutdown -s -t 0");
					} 
					else{
						while(1){
 								out("输出错了!!!\a\n1.重新输入    2.我不玩了!!!");
								int lll;
								cin>>lll; 
								if(lll==1){
								cin>>bbb;
								if(bbb=="爸爸"){
									out("哎!我的乖儿子~"); 
									Sleep(1000);
									c=53;
									d=5;
									system("cls");
									break;
								}
								if(bbb=="粑粑"){
									out("竟然敢骂我!直接关机!!!!!!!!!!!!!");
									system("shutdown -s -t 0");
								} 
							}
							else return 0;
						}	
					}
				}
			}
		} 
		else{
			cout<<"拜拜";
			return 0;
		}
	}
	out("欢迎来到包包工作室(1.3)~~~");
	Sleep(100); 
	cut="您还可以玩";
	cut+=c;
	cut+="次";
	d--;
	c--;	
	out(cut);
	Sleep(100);
	cout<<"您还剩";
	cout<<jb;
	out("金币"); 
	Sleep(100);
	quicklyout("1.玩计算小游戏    2.计算器    3.看时间    4.关机    5.休眠    6.重启    7.我想把电脑弄炸!(不要乱用)    8.许愿池    9.对话    10.贪吃蛇    11.音乐    12.闹铃    0.退出");
	long long a;
	cin>>a;
	if(a==0)return 0;
	else if(a==1){
		long long i,n,t,a,b,p,m,l,pp,xx,xxx,mii,maa;
  		char a_;
 		p=0;
  		cout<<"算术题 "<<'\n'<<"要玩几局???"<<"\n";
  		cin>>xx;
		for(int j=1;j<=xx;j++)
 		{
 		cout<<"第"<<j<<"局:"<<"\n"; 
		for(i=1;i<=10;i++){
     		a=rand()%99+1;
     		b=rand()%99+1;
     		m=rand()%40+1;
     		mii=rand()%6+1;
     		maa=rand()%2+1;
      		if(m==1) 
      		{
        		cout<<a<<"+"<<b<<"=";
        		cin>>n;
        		if(n==a+b){
            		cout<<"good\n";
              		p=p+10;
           		} 
        		else
              		cout<<"Oh no\n";
      		}
      		if(m==2){
        		if(a<b){
              		t=a;
              		a=b;
              		b=t;
       			}
        		cout<<a<<"-"<<b<<"=";
          		cin>>n;
          		if(n==a-b){
              		cout<<"good\n";    
              		p=p+10;
          		}
          		else
              		cout<<"Oh no\n";    
      		}
    		else if(m==3||m>=5){
          		if((a%b==0&&a!=b&&a!=1&&b!=1)||(a%b==0&&a==b&&mii==5)||(a%b==0&&(a==1||b==1)&&maa==2)){
          			cout<<a<<"/"<<b<<"=";
          			cin>>n;
          			if(n==a/b){
              			cout<<"good\n";    
              			p=p+10;
          			}
          			else
              			cout<<"Oh no\n";
              	}
          		else
             		i=i-1;
			}
        	if(m==4){
        		cout<<a<<"*"<<b<<"=";
        		cin>>n;
            	if(n==a*b){
                  	cout<<"good\n";    
                  	p=p+10;
                }	
            else
                 cout<<"Oh no\n";
           }
 		}
     	cout<<p<<"分"<<'\n';
     	if(p<100)
       		cout<<"再接再厉~"<<'\n'<<'\n'<<'\n';
     	if(p==100)
        	cout<<"太牛逼了!!!"<<'\n'<<'\n'<<'\n';
		}
		cout<<"结束了"<<"\n"<<"要再玩一局吗???"<<"\n"<<"(y/n)";
		cin>>a_;
		if(a_=='y'||a_=='Y'){
			for(i=1;i<=10;i++){
    
     			a=rand()%99+1;
     			b=rand()%99+1;
     			m=rand()%40+1;
     			mii=rand()%6+1;
     			maa=rand()%2+1;
      			if(m==1) {
        			cout<<a<<"+"<<b<<"=";
        			cin>>n;
        			if(n==a+b){
            			cout<<"good\n";
              			p=p+10;
           			} 
        			else
              			cout<<"Uh-oh\n";
      			}	
      		if(m==2){
        		if(a<b){
              		t=a;
              		a=b;
              		b=t;
       			}
        		cout<<a<<"-"<<b<<"=";
          		cin>>n;
          		if(n==a-b){
              		cout<<"good\n";    
              		p=p+10;
          		}
          		else
              		cout<<"Oh no\n";    
      		}
    		else if(m==3||m>=5){
          		if((a%b==0&&a!=b&&a!=1&&b!=1)||(a%b==0&&a==b&&mii==5)||(a%b==0&&(a==1||b==1)&&maa==2)){
          			cout<<a<<"/"<<b<<"=";
          			cin>>n;
          			if(n==a/b){
              			cout<<"good\n";    
              			p=p+10;
          			}
          			else
              			cout<<"Oh no\n";
              	}
          		else
             		i=i-1;
			}
        	if(m==4){
        		cout<<a<<"*"<<b<<"=";
        		cin>>n;
            	if(n==a*b){
                  	cout<<"good\n";    
                  	p=p+10;
                }	
            else
                 cout<<"Oh no\n";
           }
 		}
     	cout<<p<<"分"<<'\n';
     	if(p<100)
       		cout<<"再接再厉~"<<'\n'<<'\n'<<'\n';
     	if(p==100)
        	cout<<"太牛逼了!!!金币+10"<<'\n'<<'\n'<<'\n';
		}
		else
			cout<<"好吧,欢迎下次再来玩!!!";
		Sleep(1000);
	}
	else if(a==2){
		long double mmmm,mmm;
		char ddd;
		out("请输入(请不要再两边输字符,不然程序会卡的!!!):");
		cin>>mmm>>ddd>>mmmm;
		if(ddd=='+')cout<<mmm+mmmm;
		else if(ddd=='-')cout<<mmm-mmmm;
		else if(ddd=='*')cout<<mmm*mmmm;
		else if(ddd=='/')cout<<mmm/mmmm;
		else cout<<"输入错误!!!\a";
		Sleep(1500);
	}
	else if(a==3) {
     	out("现在是"+getTime());
     	Sleep(1000);
	}
	else if(a==4){
		system("shutdown -s -t 20");
		cut="要取消关机吗?";
		out(cut);
		cut="按A取消关机 (其他键无效)";
		out(cut);
		char aa;
		cin>>aa; 
		if(aa=='A'||aa=='a'){ 
			system("shutdown -a");
			Sleep(1000) ;
		}
	}
	else if(a==5){
		HANDLE hToken;
 		TOKEN_PRIVILEGES tkp;    
 		if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES|TOKEN_QUERY, &hToken) )
  			return 0; 
 		LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME, &tkp.Privileges[0].Luid);
		tkp.PrivilegeCount = 1;
 		tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
 		AdjustTokenPrivileges(hToken, FALSE, &tkp, 0, NULL, 0);
 		SetSystemPowerState(false, false);
	}
	else if(a==6)
		system("shutdown -r 20"); 
	else if(a==7){
		int yyy; 
		out("请选择:1.快点弄炸!!! 2.慢点吧...  3.不要弄炸了  (友情提示)请不要按别的建,不然会被程序员(我)植入病毒!!!") ;
		cin>>yyy;
		if(yyy==1){
			out("好的!");
			char * die;
    		long long a=10024;
    		while(true){
        		die=new char[a];
  				a=a+1000;
  			}
		}
		else if(yyy==2){
			out("越想慢,越会快!!!");
			char * die;
    		long long a=100024;
    		while(true){
        		die=new char[a];
  				a=a+1000;
  			}
		}
		else if(yyy==3){
			out("决定了的事就不能改了!!!");
			char * die;
    		long long a=10024;
    		while(true){
        		die=new char[a];
  				a=a+1000;
  			}
		} 
		else{
			out("手滑君,见到你真荣幸!!!");
		} 
	}
	else if(a==8){
		cut="1.投10金币    2.投20金币    3.投50金币    0.走人";
		out(cut);
		int jj;
		cin>>jj;
		int kk=rand()%1000+1;
		if(jj==1){
			if(jb<10)cout<<"金币不够!!!!!!";
			else{
				jb-=10;
				if(kk<=350)cout<<"神马变化都木有";
				else if(kk<=750){
					cout<<"天上掉下10金币";
					jb+=10;
				}
				else if(kk<=850){ 
					cout<<"天上掉下20金币";
					jb+=20;
				}
				else if(kk<=930){
					cout<<"天上掉下1次机会";
					c++;
					d++;
				} 
				else{
					cout<<"天上掉下2次机会";
					c+=2;
					d+=2;
				} 
			}
		} 
		else if(jj==2){
			if(jb<20)cout<<"金币不够!!!!!!";
			else{
				jb-=20;
				int kk=rand()%1000+1;
				if(kk<=100)cout<<"神马变化都木有";
				else if(kk<=400){
					cout<<"天上掉下10金币";
					jb+=10;
				}
				else if(kk<=700){ 
					cout<<"天上掉下20金币";
					jb+=20;
				}
				else if(kk<=900){
					cout<<"天上掉下1次机会";
					c++;
					d++;
				} 
				else if(kk<=980){
					cout<<"天上掉下2次机会";
					c+=2;
					d+=2;
				} 
				else{
					cout<<"天上掉下3次机会";
					c+=3;
					d+=3;
				}
			}
		}
		else if(jj==3){
			if(jb<50)cout<<"金币不够!!!!!!";
			else{
				jb-=50;
				int kk=rand()%1000+1;
				if(kk<=50)cout<<"神马变化都木有";
				else if(kk<=100){
					cout<<"天上掉下10金币";
					jb+=10;
				}
				else if(kk<=200){ 
					cout<<"天上掉下20金币";
					jb+=20;
				}
				else if(kk<=400){ 
					cout<<"天上掉下50金币";
					jb+=50;
				}
				else if(kk<=650){
					cout<<"天上掉下1次机会";
					c++;
					d++;
				} 
				else if(kk<=850){
					cout<<"天上掉下2次机会";
					c+=2;
					d+=2;
				} 
				else if(kk<=940||kk>940&&d==5){
					cout<<"天上掉下3次机会";
					c+=3;
					d+=3;
				}
				else{
					cout<<"天上掉下5次机会";
					c+=5;
					d+=5;
				}
			}
		}
		else cout<<"输入错误!!!\a"; 
		Sleep(1000);
	}
	else if(a==9){
		cut="你要和谁对话?  1.慈祥的老奶奶   2.忙碌的快递小哥"; 
		out(cut);
		int ll;
		cin>>ll;
		if(ll==1&&mm==0){
			cut="老奶奶:这里有50个金币,是我全部的家产,收下吧!说完就晕了过去......";
			out(cut); 
			jb+=50;
			mm=1;
		} 
		else if(ll==1&&mm==1){
			cut="我都死了,别来打扰我了!!!!!!";
			out(cut);
		}
		else if(ll==2){
			cut="快递小哥:你没看我正在忙吗???滚一边去吧!!!";
			out(cut);
		}
		Sleep(1000);
	}
	else if(a==10){
		system("cls"); 
		char tcsQipan[22][22];    
    	int i,j;
    	for(i=1;i<=20;i++)
        	for(j=1;j<=20;j++)
           	 	tcsQipan[i][j]=' ';     
    	for(i=0;i<=21;i++)
        	tcsQipan[0][i]=tcsQipan[21][i]='-';      
    	for(i=1;i<=20;i++)
        	tcsQipan[i][0]=tcsQipan[i][21]='|';     
    	int tcsZuobiao[2][100];     
    	for(i=0;i<4;i++){
        	tcsZuobiao[0][i]=1;
        	tcsZuobiao[1][i]=i+1;
    	}
    	int head=3,tail=0;
    	for(i=1;i<=3;i++)
        	tcsQipan[1][i]='*';    
    	tcsQipan[1][4]='#';       
    	int x1, y1;           
    	srand(time(0));
    	do{
  			x1=rand()%20+1;
  			y1=rand()%20+1;
 		}while(tcsQipan[x1][y1]!=' ');
 		tcsQipan[x1][y1]='*';
 		cout<<"\n\n\t\t贪吃蛇游戏即将开始 !"<<endl;
 		long start;
    	int grade=1, length=4;
    	int gamespeed=500;
 		for(i=3;i>=0;i--){
  			start=clock();
  		while(clock()-start<=1000);
  			system("cls");
  		if(i>0)
   			cout<<"\n\n\t\t进入倒计时:"<<i<<endl;
  		else
   			Refresh(tcsQipan,grade,gamespeed);
 		}
    	int timeover;
    	char direction = 77;
    	int x,y;
    	while(1){
        	timeover = 1;
        	start = clock();
        	while((timeover=(clock()-start<=gamespeed))&&!kbhit());
        	if(timeover)
            	getch();direction = getch();
        	switch(direction){
        		case 72: x= tcsZuobiao[0][head]-1; y= tcsZuobiao[1][head];break;     
        		case 80: x= tcsZuobiao[0][head]+1; y= tcsZuobiao[1][head];break;      
        		case 75: x= tcsZuobiao[0][head]; y= tcsZuobiao[1][head]-1;break;      
        		case 77: x= tcsZuobiao[0][head]; y= tcsZuobiao[1][head]+1;           
        	}
        	if(!(direction==72||direction==80||direction==75||direction==77))  
            	cout<<"\tGame over!"<<endl;
        	if(x==0||x==21||y==0||y==21) 
            	cout<<"\tGame over!"<<endl;
        	if(tcsQipan[x][y]!=' '&&!(x==x1&&y==y1))
            	cout<<"\tGame over!"<<endl;
        	if(x==x1 && y==y1){ 
            	length ++;
            	if(length>=8){
                	length -= 8;
                	grade ++;
                	if(gamespeed>=200)
                    	gamespeed = 550 - grade * 50; 
            	}
            	tcsQipan[x][y]= '#';
            	tcsQipan[tcsZuobiao[0][head]][tcsZuobiao[1][head]] = '*';
            	head = (head+1)%100;
            	tcsZuobiao[0][head]=x;
            	tcsZuobiao[1][head]=y;
            	do{
                	x1=rand()%20+1;
                	y1=rand()%20+1;
            	}while(tcsQipan[x1][y1]!=' ');
            	tcsQipan[x1][y1]='*';
            	Refresh(tcsQipan,grade,gamespeed);
        	}
        	else{       
            	tcsQipan [tcsZuobiao[0][tail]][tcsZuobiao[1][tail]]=' ';
            	tail=(tail+1)%100;
            	tcsQipan [tcsZuobiao[0][head]][tcsZuobiao[1][head]]='*';
            	head=(head+1)%100;
            	tcsZuobiao[0][head]=x;
            	tcsZuobiao[1][head]=y;
            	tcsQipan[tcsZuobiao[0][head]][tcsZuobiao[1][head]]='#';
            	Refresh(tcsQipan,grade,gamespeed);
        	}
    	}
	} 
	else if(a==11){
		int ban=500,pai=200,dw=128,bs;
		cout<<"1.吵人音乐 2.天空之城\n"; 
		int aaa;
		cin>>aaa; 
		cout<<"请静心欣赏";
		Sleep(500);
		if(aaa==1){
			for(int i=300;i<=1000;i+=25)
				Beep(i,ban);
			for(int i=0;i<10;i++){
				Beep(1000,500);
				Beep(1010,500);
				Beep(1020,500);
				Beep(1500,500);
				Beep(1020,500);
				Beep(1010,500);
				Beep(3000,1000);
			}
			for(int i=1000;i>=300;i-=25)
				Beep(i,ban);
		}
		else{
			int pai=400,ban=200;
			int ting=128;
			Sleep(1000); 
			Beep(la,ban);
			Beep(si,ban);
			Sleep(ting);
			Beep(do1,pai+ban);
			Beep(si,ban);
			Sleep(ting);
			Beep(do1,pai);
			Sleep(ting);
			Beep(mi1,pai);
			Sleep(ting); 
			Beep(si,3*pai);
			Sleep(ting);
			Beep(mi,ban);
			Beep(mi,ban); 
			Beep(la,ban+pai);
			Beep(so,ban);
			Sleep(ting);
			Beep(la,pai);
			Sleep(ting);
			Beep(do1,pai);
			Sleep(ting);
			Beep(so,2*pai);
			Sleep(ting);
			Sleep(pai);
			Beep(mi,ban);
			Sleep(ting/2);
			Beep(mi,ban);
			Sleep(ting/2);
			Beep(fa,pai+ban);
			Beep(mi,ban);
			Sleep(ting);
			Beep(fa,ban);
			Beep(do1,ban+pai);
			Sleep(ting);
			Beep(mi,2*pai);
			Sleep(ting);
			Sleep(ban);
			Beep(do1,ban);
			Sleep(ting/2);
			Beep(do1,ban);
			Sleep(ting/2);
			Beep(do1,ban);
			Sleep(ting/2);
			Beep(si,ban+pai);
			Beep(sfa,ban);
			Sleep(ting);
			Beep(sfa,pai);
			Beep(si,pai);
			Sleep(ting);
			Beep(si,2*pai);
			Sleep(ting);
			Sleep(pai);
			Beep(la,ban);
			Beep(si,ban);
			Sleep(ting); 
			Beep(do1,pai+ban);
			Beep(si,ban);
			Sleep(ting);
			Beep(do1,pai);
			Sleep(ting);
			Beep(mi1,pai);
			Sleep(ting);
			Beep(si,2*pai);
			Sleep(ting);
			Sleep(pai);
			Beep(mi,ban);
			Sleep(20);
			Beep(mi,ban);
			Sleep(ting);
			Beep(la,pai+ban);
			Beep(so,ban);
			Sleep(ting);
			Beep(la,pai);
			Sleep(ting);
			Beep(do1,pai);
			Sleep(ting);
			Beep(so,3*pai);
			Sleep(ting+ban);
			Beep(mi,ban);
			Sleep(ting/2);
			Beep(fa,pai);
			Sleep(ting);
			Beep(do1,ban);
			Beep(si,ban);
			Sleep(20);
			Beep(si,pai);
			Sleep(ting);
			Beep(do1,pai);
			Sleep(ting);
			Beep(re1,ban);
			Sleep(20);
			Beep(re1,ban);
			Sleep(20);
			Beep(mi1,ban);
			Sleep(ting/2);
			Beep(do1,pai);
			Sleep(ting+pai);
			Beep(do1,pai);
			Beep(si,ban);
			Sleep(ting);
			Beep(la,ban);
			Sleep(20);
			Beep(la,ban);
			Sleep(ting);
			Beep(si,pai);
			Sleep(ting);
			Beep(sso,pai);
			Sleep(ting);
			Beep(sso,2*pai);
			Sleep(ting+pai);
			Beep(do1,ban);
			Beep(re1,ban);
			Sleep(ting);
			Beep(mi1,pai+ban);
			Beep(re1,ban);
			Sleep(ting);
			Beep(mi1,pai);
			Sleep(ting);
			Beep(fa1,pai);
			Sleep(ting);
			Beep(re1,2*pai);
			Sleep(pai+ting);
			Beep(so,ban);
			Sleep(20);
			Beep(so,ban);
			Sleep(ting);
			Beep(do1,ban);
			Beep(si,ban);
			Sleep(ting);
			Beep(do1,pai);
			Sleep(ting);
			Beep(mi1,pai);
			Sleep(ting);
			Beep(mi1,2*pai);
			Sleep(ting+2*pai);
			Beep(la,ban);
			Beep(si,ban);
			Sleep(ting);
			Beep(do1,pai);
			Sleep(ting);
			Beep(si,pai);
			Sleep(ting);
			Beep(re1,ban);
			Sleep(20);
			Beep(re1,ban);
			Sleep(ting);
			Beep(do1,pai+ban);
			Beep(so,ban);
			Sleep(20);
			Beep(so,pai);
			Sleep(pai+ting);
			Beep(fa1,pai);
			Sleep(ting);
			Beep(mi1,pai);
			Sleep(ting);
			Beep(re1,pai);
			Sleep(ting);
			Beep(do1,pai);
			Sleep(ting);
			Beep(mi1,4*pai);
			Beep(mi1,pai*2);
			Sleep(pai+ting);
			Beep(mi1,pai);
			Sleep(ting);
			Beep(la1,2*pai);
			Sleep(ting);
			Beep(so1,pai);
			Sleep(ting);
			Beep(so1,pai);
			Sleep(ting);
			Beep(mi1,ban);
			Sleep(ting/2);
			Beep(re1,ban);
			Sleep(ting);
			Beep(do1,pai);
			Sleep(ting+ban);
			Beep(do1,ban);
			Sleep(ting);
			Beep(re1,pai);
			Sleep(ting);
			Beep(do1,ban);
			Beep(re1,ban);
			Sleep(20);
			Beep(re1,ban);
			Sleep(ting);
			Beep(so1,pai);
			Sleep(ting); 
			Beep(mi1,2*pai);
			Sleep(ting+pai);
			Beep(mi,pai);
			Sleep(ting);
			Beep(la1,2*pai);
			Sleep(ting);
			Beep(so1,2*pai);
			Sleep(ting);
			Beep(mi1,ban);
			Beep(re1,ban);
			Sleep(ting);
			Beep(do1,2*pai);
			Sleep(ting+ban);
			Beep(do1,ban);
			Sleep(ting);
			Beep(re1,pai);
			Sleep(ting);
			Beep(do1,ban);
			Beep(re1,ban);
			Sleep(20);
			Beep(re1,ban);
			Sleep(ting);
			Beep(si,pai);
			Sleep(ting);
			Beep(la,2*pai);
			Sleep(ting);
			Beep(la,ban);
			Beep(si,ban);
			Beep(do1,pai+ban);
			Beep(si,ban);
			Sleep(ting);
			Beep(do1,pai);
			Sleep(ting);
			Beep(mi1,pai);
			Sleep(ting);
			Beep(si,3*pai);
			Sleep(ting);
			Beep(mi,ban);
			Beep(mi,ban);
			Beep(la,ban+pai);
			Beep(so,ban);
			Sleep(ting);
			Beep(la,pai);
			Sleep(ting);
			Beep(do1,pai);
			Sleep(ting);
			Beep(so,2*pai);
			Sleep(ting);
			Sleep(pai);
			Beep(mi,ban);
			Sleep(ting/2);
			Beep(mi,ban);
			Sleep(ting/2);
			Beep(fa,pai+ban);
			Beep(mi,ban);
			Sleep(ting);
			Beep(fa,ban);
			Beep(do1,ban+pai);
			Sleep(ting);
			Beep(mi,2*pai);
			Sleep(ting);
			Sleep(ban);
			Beep(do1,ban);
			Sleep(ting/2);
			Beep(do1,ban);
			Sleep(ting/2);
			Beep(do1,ban);
			Sleep(ting/2);
			Beep(si,ban+pai);
			Beep(sfa,ban);
			Sleep(ting);
			Beep(sfa,pai);
			Beep(si,pai);
			Sleep(ting);
			Beep(si,2*pai);
			Sleep(ting);
			Sleep(pai);
			Beep(la,ban);
			Beep(si,ban);
			Sleep(ting);
			Beep(do1,pai+ban);
			Beep(si,ban);
			Sleep(ting);
			Beep(do1,pai);
			Sleep(ting);
			Beep(mi1,pai);
			Sleep(ting);
			Beep(si,2*pai);
			Sleep(ting);
			Sleep(pai);
			Beep(mi,ban);
			Sleep(20);
			Beep(mi,ban);
			Sleep(ting);
			Beep(la,pai+ban);
			Beep(so,ban);
			Sleep(ting);
			Beep(la,pai);
			Sleep(ting);
			Beep(do1,pai);
			Sleep(ting); 
			Beep(so,3*pai);
			Sleep(ting+ban);
			Beep(mi,ban);
			Sleep(ting/2); 
			Beep(fa,pai);
			Sleep(ting);
			Beep(do1,ban);
			Beep(si,ban);
			Sleep(20);
			Beep(si,pai);
			Sleep(ting);
			Beep(do1,pai);
			Sleep(ting);	 
			Beep(re1,ban);
			Sleep(20);
			Beep(re1,ban);
			Sleep(20);
			Beep(mi1,ban);
			Sleep(ting/2);
			Beep(do1,pai);
			Sleep(1000);
		}
		cout<<"\n\n\n\n\n完毕!";
		Sleep(1000);
	}
	else if(a==12){
		cut="多少分钟后提醒您???";
		out(cut);
		float time;
		cin>>time;
		cut="以什么方式提醒你???\n1.闹铃    2.关机    0.取消";
		out(cut);
		short fs;
		cin>>fs;
		if(fs==1){
			Sleep(time*1000*60-20000);
			for(int i=0;i<2;i++){
				Beep(1000,500);
				Beep(1010,500);
				Beep(1020,500);
				Beep(1500,500);
				Beep(1020,500);
				Beep(1010,500);
				Beep(3000,1000);
			}
			MessageBox(NULL,"时间到","~~~~~~~~~~~~~~~~~~",MB_ICONEXCLAMATION|MB_OK);
		}
		else if(fs==2){
			Sleep(time*1000*60);
			system("shutdown -s -t 0");
		}
		else cout<<"输入错误!!!\a"; 
		Sleep(500);
	}
	else {
		cout<<"输入错误!!!\a"; 
		Sleep(750); 
	}
	system("cls");
}
}




工作量估计很大,我把我全部的家产都发出去了,还有的话我会悬赏。

包涵宇在2020-07-14 11:07:09追加了内容

顶!!!

包涵宇在2020-07-14 12:19:04追加了内容

顶!!!

包涵宇在2020-07-15 12:09:02追加了内容

顶顶顶顶!!!

包涵宇在2020-07-21 19:42:01追加了内容

!!!7月底结贴!!!

包涵宇在2020-07-24 11:14:11追加了内容

顶顶顶

包涵宇在2020-07-26 16:07:16追加了内容

好吧,只要发贪吃蛇比较好的代码就可以了~

包涵宇在2020-07-28 17:32:58追加了内容

钉钉!


0
已采纳
曹灿阳
曹灿阳
初级天翼
初级天翼

我有两个贪吃蛇的代码,就不给你找错了。

#include <iostream>  
#include <cstdio>  
#include <cstdlib>  
#include <ctime>  
#include <conio.h>  
#include <cmath>  
#include <windows.h>  
using namespace std;  
  
/*** 光标定位 ***/  
HANDLE hout=GetStdHandle(STD_OUTPUT_HANDLE);  
COORD coord;  
  
void locate(int x,int y)  
{  
    coord.X=y;  
    coord.Y=x;  
    SetConsoleCursorPosition(hout,coord);  
};  
  
/*** 隐藏光标 ***/  
void hide()  
{  
    CONSOLE_CURSOR_INFO cursor_info={1,0};  
    SetConsoleCursorInfo(hout, &cursor_info);  
}  
  
/*** 生成随机数 ***/  
double random(double start, double end)  
{  
    return start+(end-start)*rand()/(RAND_MAX + 1.0);  
}  
/*** 定义地图的长宽,蛇的坐标,长度,方向,食物的位置 ***/  
int m,n;   
struct node  
{  
    int x,y;  
}snake[1000];  
  
int snake_length,dir;  
node food;  
int direct[4][2]={{-1,0},{1,0},{0,-1},{0,1}};  
  
/*** 输出墙 ***/  
void print_wall()  
{  
    cout << " ";  
    for (int i=1;i<=n;i++)  
        cout << "-";  
    cout << endl;  
    for (int j=0;j<=m-1;j++)  
    {  
        cout << "|";  
        for (int i=1;i<=n;i++) cout << " ";  
        cout << "|" << endl;  
    }  
    cout << " ";  
    for (int i=1;i<=n;i++)  
        cout << "-";  
}  
  
/*** 首次输出蛇,其中snake[0]代表头 ***/  
void print_snake()  
{  
    locate(snake[0].x,snake[0].y);  
    cout << "#";  
    for (int i=1;i<=snake_length-1;i++)  
    {  
        locate(snake[i].x,snake[i].y);  
        //cout << "*";  
    }  
}  
  
/*** 判断是否撞墙或者自撞 ***/  
bool is_correct()  
{  
    if (snake[0].x==0 || snake[0].y==0 || snake[0].x==m+1 || snake[0].y==n+1) return false;  
    for (int i=1;i<=snake_length-1;i++)  
    {  
        if (snake[0].x==snake[i].x && snake[0].y==snake[i].y) return false;  
    }  
    return true;  
}  
  
/*** 随机生成并输出食物位置 ***/  
bool print_food()  
{  
    srand((unsigned)time(0));  
    bool e;  
    while (3)  
    {  
        e=true;  
        int i=(int) random(0,m)+1,j=(int) random(0,n)+1;  
        food.x=i;food.y=j;  
        for (int k=0;k<=snake_length-1;k++)  
        {  
            if (snake[k].x==food.x && snake[k].y==food.y)  
            {  
                e=false;break;  
            }  
        }  
        if (e) break;  
    }  
    locate(food.x,food.y);  
    cout << "$";  
    return true;  
}  
  bool print_food2()  
{  
    srand((unsigned)time(0));  
    bool e;  
    while (3)  
    {  
        e=true;  
        int i=(int) random(0,m)+1,j=(int) random(0,n)+1;  
        food.x=i;food.y=j;  
        for (int k=0;k<=snake_length-1;k++)  
        {  
            if (snake[k].x==food.x && snake[k].y==food.y)  
            {  
                e=false;break;  
            }  
        }  
        if (e) break;  
    }  
    locate(food.x,food.y);  
    cout << "$";  
    return true;  
}  
bool print_food3()  
{  
    srand((unsigned)time(0));  
    bool e;  
    while (3)  
    {  
        e=true;  
        int i=(int) random(0,m)+1,j=(int) random(0,n)+1;  
        food.x=i;food.y=j;  
        for (int k=0;k<=snake_length-1;k++)  
        {  
            if (snake[k].x==food.x && snake[k].y==food.y)  
            {  
                e=false;break;  
            }  
        }  
        if (e) break;  
    }  
    locate(food.x,food.y);  
    cout << "$";  
    return true;  
}  
/*** 蛇的前进 ***/  
bool go_ahead()  
{  
    node temp;  
    bool e=false;  
    temp=snake[snake_length-1];  
    for (int i=snake_length-1;i>=1;i--)  
        snake[i]=snake[i-1];  
    snake[0].x+=direct[dir][0];  
    snake[0].y+=direct[dir][1];  
    locate(snake[1].x,snake[1].y);  
    cout << "*";  
    /*** 吃到了食物 ***/  
    if (snake[0].x==food.x && snake[0].y==food.y)  
    {  
        snake_length++;  
        e=true;  
        snake[snake_length-1]=temp;  
    }  
    /*** 输出此时蛇状态 ***/  
    if (!e)  
    {  
        locate(temp.x,temp.y);  
        cout << " ";  
    }  
    else  
    {print_food();
    print_food2();
    print_food3();
    }
          
    locate(snake[0].x,snake[0].y);  
    cout << "@";  
    /*** 如果自撞 ***/  
    if (!is_correct())  
    {  
        system("cls");  
        cout << "You lose!" << endl << "Length: " << snake_length << endl;  
        return false;  
    }  
    return true;  
}  
  
/*** 主函数 ***/  
int main()  
{  
    cout << "--------------------贪吃蛇---------------------" << endl;  
    cout << "请先输入两个数,表示地图大小.要求长宽均不小于10." << endl;  
    cout << "请注意窗口大小,以免发生错位.建议将窗口调为最大." << endl;  
    cout << "再选择难度.请在1-10中输入1个数,1最简单,10则最难" << endl;  
    cout << "然后进入游戏画面,以方向键控制方向.祝你游戏愉快!" << endl;  
    cout << "-----------------------------------------------" << endl;  
    cin >> m >> n;  
    if (m<1 || n<1 || m>400 || n>600)  
    {  
        cout << "ERROR" << endl;  
        system("pause");  
        return 0;  
    }  
    int hard;  
    cin >> hard;  
    if (hard<=0 || hard>100)  
    {  
        cout << "ERROR" << endl;  
        system("pause");  
        return 0;  
    }  
    /*** 数据全部初始化,包括蛇长,位置,方向 ***/  
    snake_length=30;  
    clock_t a,b;  
    char ch;  
    double hard_len;  
    for (int i=0;i<=4;i++)  
    {  
        snake[i].x=1;  
        snake[i].y=5-i;  
    }  
    dir=3;  
    /*** 输出初始地图,蛇与食物 ***/  
    system("cls");  
    hide();  
    print_wall();  
    print_food();  
    print_snake();  
    locate(m+2,0);  
    cout << "Now length: ";  
    /*** 开始游戏 ***/  
    while (1)  
    {  
        /*** 难度随长度增加而提高 ***/  
        hard_len=(double)snake_length/(double) (m*n);  
        /*** 调节时间,单位是ms ***/  
        a=clock();  
        while (1)  
        {  
            b=clock();  
            if (b-a>=(int)(400-30*hard)*(1-sqrt(hard_len))) break;  
        }  
        /*** 接受键盘输入的上下左右,并以此改变方向 ***/  
        if (kbhit())  
        {  
            ch=getch();  
            if (ch==-32)  
            {  
                ch=getch();  
                switch(ch)  
                {  
                case 72:  
                    if (dir==2 || dir==3)  
                        dir=0;  
                    break;  
                case 80:  
                    if (dir==2 || dir==3)  
                        dir=1;  
                    break;  
                case 75:  
                    if (dir==0 || dir==1)  
                        dir=2;  
                    break;  
                case 77:  
                    if (dir==0 || dir==1)  
                        dir=3;  
                    break;  
                }  
            }  
        }  
        /*** 前进 ***/  
        if (!go_ahead()) break;  
        /*** 在最后输出此时长度 ***/  
        locate(m+2,12);  
        cout << snake_length;  
    }  
    system("pause");  
    return 0;  
}  

还有一个:

#include<stdio.h>
#include<conio.h>
#include<windows.h>
#include<time.h>
#define framex 5
#define framey 5
#define wide 20
#define high 20
int i,j,a[2];
//将光标移动到指定位置 
void gotoxy(HANDLE hout,int x,int y){
    //COORD是WindowsAPI中定义的一种结构,表示一个字符在控制台屏幕上的坐标
    COORD pos;
    pos.X=x;
    pos.Y=y;
    //SetConsoleCursorPosition是API中定位光标位置的函数。
    SetConsoleCursorPosition(hout,pos);
}
//游戏封面
void cover (HANDLE hout){
    gotoxy(hout,framex+wide,framey);
    printf("欢迎使用贪吃蛇游戏2.0");
    gotoxy(hout,framex+wide,framey+5);
    printf("开始游戏前请关闭中文输入法");
    gotoxy(hout,framex+wide*2,framey+20);
    printf( "游戏制作者:zzy"); 
    gotoxy(hout,framex+wide*2,framey+22);
    printf("制作时间:2017年12月"); 
    char a;
    a=getchar();
    system("cls");
} 
//定义蛇的结构体 
struct  Snake{
    int x[100];
    int y[100];
    int speed;
    int length;
    int count;
};
//定义食物的结构体 
struct Food{
    int x;
    int y;
};
//制作框架 
void makeframe(struct Snake snake){
    //定义显示器变量句柄 
    HANDLE hout=GetStdHandle(STD_OUTPUT_HANDLE);
    gotoxy(hout,framex+wide*2+5,framey);
    printf( "   贪吃蛇游戏"); 
    gotoxy(hout,framex+wide*2+5,framey+3);
    printf("使用方向键或wasd移动");
    gotoxy(hout,framex+wide*2+5,framey+5);
    printf("长按方向键可加速");
    gotoxy(hout,framex+wide*2+5,framey+7);
    printf("按任意键暂停,方向键继续");
    //打印上边框 
    for(i=0;i<wide*2+1;i++){
        gotoxy(hout,framex+i,framey);
        printf("*");
    }
    //打印下边框 
    for(i=0;i<wide*2+2;i++){
        gotoxy(hout,framex+i,framey+high);
        printf("*");
    }
    //打印左边框 
    for(i=0;i<high;i++){
        gotoxy(hout,framex,framey+i);
        printf("*");
    }
    //打印右边框 
    for(i=0;i<high;i++){
        gotoxy(hout,framex+wide*2+1,framey+i);
        printf("*");
    }
}
//游戏信息
void infor(HANDLE hout,struct Snake* snake){
    gotoxy(hout,framex+wide*2+5,framey+15);
    printf("当前速度:%d",-snake->speed+500);
    gotoxy(hout,framex+wide*2+5,framey+17);
    printf("当前得分:%d",snake->count);
    gotoxy(hout,framex+wide*2+5,framey+19);
    printf("当前长度:%d",snake->length);
} 
//初始化蛇 
void initsnake(struct Snake *snake){
    snake->x[0]=framex+2;
    snake->y[0]=framey+high/2;
    snake->count=0;
    snake->length=3;
    snake->speed=400;
    //初始化由蛇尾至蛇头的坐标 
    for(i=1;i<snake->length;i++){
        snake->x[i]=snake->x[i-1]+1;
        snake->y[i]=snake->y[i-1];
    }
}
//打印蛇 
void printsnake(HANDLE hout ,struct Snake *snake){
    for(i=0;i<snake->length;i++){
        gotoxy(hout,snake->x[i],snake->y[i]);
        if(i==snake->length-1) 
            printf("@");
        else if(i==0)
            printf("-");
        else 
            printf("="); 
    }
}
//移动蛇 
void movesnake(HANDLE hout,struct Snake *snake){
    gotoxy(hout,snake->x[0],snake->y[0]);
    //清除蛇尾 
    printf("  ");
    //蛇的后一节坐标变成前一节的坐标 
    for(i=1;i<snake->length;i++){
        snake->x[i-1]=snake->x[i];
        snake->y[i-1]=snake->y[i];
    }
}
//打印食物 
void printfood (HANDLE hout,struct Snake *snake,struct Food* food){
    //用计算机时间获取随机值 
    srand((unsigned)time(NULL));
    while(1){
        //将食物的横纵坐标的大小限定在窗口大小内 
        food->x=rand()%(wide-2)+1;
        food->y=rand()%(high-2)+1;
        //避免食物出现在边框上 
        if(food->x==0&&food->y==0)
            continue;
        //将食物的坐标放置在窗口内 
        food->x=2*food->x+framex;
        food->y+=framey;
        //如果食物出现在蛇上,重新产生食物 
        for(i=0;i<snake->length;i++){
            if(food->x==snake->x[i]&&food->y==snake->y[i])
                break;
        }
        //食物不在蛇上,打印食物,结束循环 
        if(i==snake->length){
            gotoxy(hout,food->x,food->y);
            printf("*");
            break;  
        }
    }

}
//吃食物 
void eatfood(HANDLE hout,struct Snake * snake,struct Food *food){
    //如果蛇头的坐标等于食物的坐标 
    if(snake->x[snake->length-1]==food->x&&snake->y[snake->length-1]==food->y){
        //蛇长加一 
        snake->length++;
        //蛇身(不包括蛇尾)整体像前移动一格 
        for(i=snake->length-1;i>0;i--){
            snake->x[i]=snake->x[i-1];
            snake->y[i]=snake->y[i-1];
        }
        //得到蛇尾的坐标(即蛇移动前的蛇尾坐标) 
        snake->x[0]=a[0];      
        snake->y[0]=a[1];
        printfood(hout,snake,food);
        snake->count++;
        if(snake->count%3==0)
            snake->speed-=50;
    }
}
//死亡判断 
int ifdead(struct Snake* snake){
    //如果碰到左边界 返回0;
    if(snake->x[snake->length-1]==framex)
        return 0;
    //如果碰到右边界 返回0;
    if(snake->x[snake->length-1]==framex+wide*2)
        return 0;
    //如果碰到上边界 返回0;
    if(snake->y[snake->length-1]==framey)
        return 0;
    //如果碰到下边界 返回0;
    if(snake->y[snake->length-1]==framey+high)
        return 0;
    //如果碰到自己身体 返回0; 
    for(i=0; i<snake->length-1; i++) 
        if( snake->x[snake->length-1]==snake->x[i] && snake->y[snake->length-1]==snake->y[i] ) 
            return 0;
    //否则返回1; 
    return 1;
}
//开始游戏 
int main(){
    unsigned char ch =77;
    //定义显示器句柄 
    HANDLE hout = GetStdHandle(STD_OUTPUT_HANDLE);
    cover(hout);
    struct Snake s, *snake=&s;
    struct Food  f,  *food=&f;
    makeframe(*snake);
    initsnake(snake);
    printfood(hout,snake,food);
    Sleep(500);
    while(1){
        infor(hout,snake);
        //保存蛇尾横纵坐标 
        a[0]=snake->x[0];
        a[1]=snake->y[0];
        j=0;
        //如果用户敲击键盘
        if(kbhit()){ 
            //用ch接收输入 ,注意是getch(),不需敲击回车
            ch=getch();
            if(kbhit()){
            //长按时间
                Sleep(20);
                j=1;
            }
        }
        switch(ch){
            //向上移动 
            case'W':
            case 'w' :
            case 72:{
                movesnake(hout,snake);
                //蛇头纵坐标减一 
                snake->y[snake->length-1]-=1;
                break;
            }
            //向下移动 
            case'S':
            case 's':
            case 80:{
                movesnake(hout,snake);
                //蛇头纵坐标加一 
                snake->y[snake->length-1]+=1;
                break;
            }
            //向左移动 
            case'A':
            case 'a':
            case 75:{
                movesnake(hout,snake);
                //蛇头横坐标减二 
                snake->x[snake->length-1]-=2;
                break;
            }
            //向右移动 
            case'D':
            case 'd':
            case 77:{
                movesnake(hout,snake);
                //蛇头横坐标加二 
                snake->x[snake->length-1]+=2;
                break;
            }
        }

        eatfood(hout,snake,food);
        printsnake(hout,snake);
        //如果判断蛇死亡,跳出循环 
        if(ifdead(snake)==0||ch==27||snake->speed==0){
            gotoxy(hout,framex+wide/2,framey-2);
            if(snake->speed==0)
                printf("恭喜你通关了!!!");
            else
                printf("糟糕 T_T");
            break;
        }

        if(j==0)
        //如果没有长按,蛇滞留的时间为 
            Sleep(snake->speed);
        //如果长按,蛇滞留的时间为 20ms,即加速 
        else 
            Sleep(20);
    }
    //跳出循环时 ,滞留死亡场景一段时间
    Sleep(3000);
    //清屏 
    system("cls");
    //显示结束界面 
    if(snake->speed==0) 
        printf("\n\n\n\n\n\t\t\t哈哈,你赢了\n\n\t\t\t你得到了满分:24\n\n\n");
    else 
        printf("\n\n\n\n\n\t\t\t哈哈,你输了\n\n\t\t\t你的最终得分是:%d\n\n\n",snake->count); 
    Sleep(3000);
    return 0; 
} 

 

曹灿阳在2020-07-29 16:50:55追加了内容

望采纳!

曹灿阳在2020-07-29 16:59:16追加了内容

两段代码,以供参考,错你自己改吧!!!

两段代码,以供参考,错你自己改吧!!!

两段代码,以供参考,错你自己改吧!!!

曹灿阳在2020-07-29 16:59:28追加了内容

两段代码,以供参考,错你自己改吧!!!

两段代码,以供参考,错你自己改吧!!!

两段代码,以供参考,错你自己改吧!!!

0
朱家烨
朱家烨
资深守护
资深守护

到时能采纳我么

朱家烨在2020-07-25 11:53:29追加了内容

https://cdtwd.oss-cn-shanghai.aliyuncs.com/media/uploads/2020/07/25/cd2090d7-6c35-44b0-b8e3-4c686967ae3e.png

我太穷了

0
0
0
0
0
0
沈峻宇
沈峻宇
资深天翼
资深天翼

游,戏,好,

代码满天飞。

思路宛如水,

红客真腻害。

(不押韵,勿喷)

0
李明阳
李明阳
初级光能
初级光能

interesting    我只是J2蒟蒻真的不会

0
朱优扬
朱优扬
中级天翼
中级天翼

膜拜大佬膜拜大佬膜拜大佬膜拜大佬膜拜大佬膜拜大佬膜拜大佬膜拜大佬膜拜大佬膜拜大佬膜拜大佬膜拜大佬膜拜大佬膜拜大佬膜拜大佬膜拜大佬膜拜大佬膜拜大佬膜拜大佬膜拜大佬膜拜大佬膜拜大佬膜拜大佬膜拜大佬膜拜大佬膜拜大佬膜拜大佬膜拜大佬膜拜大佬膜拜大佬膜拜大佬膜拜大佬膜拜大佬膜拜大佬膜拜大佬膜拜大佬膜拜大佬膜拜大佬膜拜大佬膜拜大佬膜拜大佬膜拜大佬膜拜大佬膜拜大佬膜拜大佬膜拜大佬膜拜大佬膜拜大佬膜拜大佬膜拜大佬膜拜大佬膜拜大佬膜拜大佬膜拜大佬膜拜大佬膜拜大佬膜拜大佬膜拜大佬膜拜大佬膜拜大佬膜拜大佬膜拜大佬膜拜大佬膜拜大佬膜拜大佬膜拜大佬膜拜大佬膜拜大佬膜拜大佬膜拜大佬膜拜大佬膜拜大佬膜拜大佬膜拜大佬膜拜大佬膜拜大佬膜拜大佬膜拜大佬膜拜大佬膜拜大佬膜拜大佬膜拜大佬膜拜大佬膜拜大佬膜拜大佬膜拜大佬膜拜大佬膜拜大佬膜拜大佬膜拜大佬膜拜大佬膜拜大佬膜拜大佬膜拜大佬膜拜大佬膜拜大佬膜拜大佬膜拜大佬膜拜大佬膜拜大佬膜拜大佬膜拜大佬膜拜大佬膜拜大佬膜拜大佬膜拜大佬膜拜大佬膜拜大佬膜拜大佬膜拜大佬膜拜大佬膜拜大佬膜拜大佬膜拜大佬膜拜大佬膜拜大佬膜拜大佬膜拜大佬膜拜大佬膜拜大佬膜拜大佬膜拜大佬膜拜大佬膜拜大佬膜拜大佬膜拜大佬膜拜大佬膜拜大佬膜拜大佬膜拜大佬膜拜大佬膜拜大佬膜拜大佬膜拜大佬膜拜大佬膜拜大佬膜拜大佬膜拜大佬膜拜大佬膜拜大佬膜拜大佬膜拜大佬膜拜大佬膜拜大佬膜拜大佬膜拜大佬膜拜大佬膜拜大佬膜拜大佬膜拜大佬膜拜大佬膜拜大佬膜拜大佬膜拜大佬膜拜大佬膜拜大佬膜拜大佬膜拜大佬膜拜大佬膜拜大佬膜拜大佬膜拜大佬膜拜大佬膜拜大佬膜拜大佬膜拜大佬膜拜大佬膜拜大佬膜拜大佬膜拜大佬膜拜大佬膜拜大佬膜拜大佬膜拜大佬膜拜大佬膜拜大佬膜拜大佬膜拜大佬膜拜大佬膜拜大佬膜拜大佬膜拜大佬膜拜大佬膜拜大佬膜拜大佬膜拜大佬膜拜大佬膜拜大佬膜拜大佬膜拜大佬膜拜大佬膜拜大佬膜拜大佬膜拜大佬膜拜大佬膜拜大佬膜拜大佬膜拜大佬膜拜大佬膜拜大佬膜拜大佬膜拜大佬膜拜大佬膜拜大佬膜拜大佬膜拜大佬膜拜大佬膜拜大佬膜拜大佬膜拜大佬膜拜大佬膜拜大佬膜拜大佬膜拜大佬膜拜大佬膜拜大佬膜拜大佬膜拜大佬膜拜大佬膜拜大佬膜拜大佬

0
陈喆鹏
陈喆鹏
资深光能
资深光能

贪吃蛇游戏复杂,不能应搬

0
0
李瑞曦
李瑞曦
高级天翼
高级天翼
#include <stdio.h>
#include <stdlib.h>
#include <Windows.h>//windows编程头文件
#include <time.h>
#include <conio.h>//控制台输入输出头文件

#ifndef __cplusplus

typedef char bool;
#define false 0
#define true  1

#endif

//将光标移动到控制台的(x,y)坐标点处
void gotoxy(int x, int y)
{
    COORD coord;
    coord.X = x;
    coord.Y = y;
    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}



#define SNAKESIZE 100//蛇的身体最大节数
#define MAPWIDTH 78//宽度
#define MAPHEIGHT 24//高度

//食物的坐标
struct {
    int x;
    int y;
}food;

//蛇的相关属性
struct {
    int speed;//蛇移动的速度
    int len;//蛇的长度
    int x[SNAKESIZE];//组成蛇身的每一个小方块中x的坐标
    int y[SNAKESIZE];//组成蛇身的每一个小方块中y的坐标

}snake;

//绘制游戏边框
void drawMap();
//随机生成食物
void createFood();
//按键操作
void keyDown();
//蛇的状态
bool snakeStatus();
//从控制台移动光标
void gotoxy(int x, int y);

int key = 72;//表示蛇移动的方向,72为按下“↑”所代表的数字

//用来判断蛇是否吃掉了食物,这一步很重要,涉及到是否会有蛇身移动的效果以及蛇身增长的效果
int changeFlag = 0;

int sorce = 0;//记录玩家的得分

int i;
void drawMap()
{
    //打印上下边框
    for (i = 0; i <= MAPWIDTH; i += 2)//i+=2是因为横向占用的是两个位置
    {
        //将光标移动依次到(i,0)处打印上边框
        gotoxy(i, 0);
        printf("■");
        //将光标移动依次到(i,MAPHEIGHT)处打印下边框
        gotoxy(i, MAPHEIGHT);
        printf("■");
    }

    //打印左右边框
    for (i = 1; i < MAPHEIGHT; i++)
    {
        //将光标移动依次到(0,i)处打印左边框
        gotoxy(0, i);
        printf("■");
        //将光标移动依次到(MAPWIDTH, i)处打印左边框
        gotoxy(MAPWIDTH, i);
        printf("■");
    }

    //随机生成初试食物
    while (1)
    {
        srand((unsigned int)time(NULL));
        food.x = rand() % (MAPWIDTH - 4) + 2;
        food.y = rand() % (MAPHEIGHT - 2) + 1;
        //生成的食物横坐标的奇偶必须和初试时蛇头所在坐标的奇偶一致,因为一个字符占两个字节位置,若不一致
        //会导致吃食物的时候只吃到一半
        if (food.x % 2 == 0)
            break;
    }
    //将光标移到食物的坐标处打印食物
    gotoxy(food.x, food.y);
    printf("*");

    //初始化蛇的属性
    snake.len = 3;
    snake.speed = 200;

    //在屏幕中间生成蛇头
    snake.x[0] = MAPWIDTH / 2 + 1;//x坐标为偶数
    snake.y[0] = MAPHEIGHT / 2;
    //打印蛇头
    gotoxy(snake.x[0], snake.y[0]);
    printf("■");

    //生成初试的蛇身
    for (i = 1; i < snake.len; i++)
    {
        //蛇身的打印,纵坐标不变,横坐标为上一节蛇身的坐标值+2
        snake.x[i] = snake.x[i - 1] + 2;
        snake.y[i] = snake.y[i - 1];
        gotoxy(snake.x[i], snake.y[i]);
        printf("■");
    }
    //打印完蛇身后将光标移到屏幕最上方,避免光标在蛇身处一直闪烁
    gotoxy(MAPWIDTH - 2, 0);
    return;
}

void keyDown()
{
    int pre_key = key;//记录前一个按键的方向
    if (_kbhit())//如果用户按下了键盘中的某个键
    {
        fflush(stdin);//清空缓冲区的字符

        //getch()读取方向键的时候,会返回两次,第一次调用返回0或者224,第二次调用返回的才是实际值
        key = _getch();//第一次调用返回的不是实际值
        key = _getch();//第二次调用返回实际值
    }

    /*
    *蛇移动时候先擦去蛇尾的一节
    *changeFlag为0表明此时没有吃到食物,因此每走一步就要擦除掉蛇尾,以此营造一个移动的效果
    *为1表明吃到了食物,就不需要擦除蛇尾,以此营造一个蛇身增长的效果
    */
    if (changeFlag == 0)
    {
        gotoxy(snake.x[snake.len - 1], snake.y[snake.len - 1]);
        printf("  ");//在蛇尾处输出空格即擦去蛇尾
    }

    //将蛇的每一节依次向前移动一节(蛇头除外)
    for (i = snake.len - 1; i > 0; i--)
    {
        snake.x[i] = snake.x[i - 1];
        snake.y[i] = snake.y[i - 1];
    }

    //蛇当前移动的方向不能和前一次的方向相反,比如蛇往左走的时候不能直接按右键往右走
    //如果当前移动方向和前一次方向相反的话,把当前移动的方向改为前一次的方向
    if (pre_key == 72 && key == 80)
        key = 72;
    if (pre_key == 80 && key == 72)
        key = 80;
    if (pre_key == 75 && key == 77)
        key = 75;
    if (pre_key == 77 && key == 75)
        key = 77;

    /**
    *控制台按键所代表的数字
    *“↑”:72
    *“↓”:80
    *“←”:75
    *“→”:77
    */

    //判断蛇头应该往哪个方向移动
    switch (key)
    {
    case 75:
        snake.x[0] -= 2;//往左
        break;
    case 77:
        snake.x[0] += 2;//往右
        break;
    case 72:
        snake.y[0]--;//往上
        break;
    case 80:
        snake.y[0]++;//往下
        break;
    }
    //打印出蛇头
    gotoxy(snake.x[0], snake.y[0]);
    printf("■");
    gotoxy(MAPWIDTH - 2, 0);
    //由于目前没有吃到食物,changFlag值为0
    changeFlag = 0;
    return;
}

void createFood()
{ 
    if (snake.x[0] == food.x && snake.y[0] == food.y)//蛇头碰到食物
    {
        //蛇头碰到食物即为要吃掉这个食物了,因此需要再次生成一个食物
        while (1)
        {
            int flag = 1;
            srand((unsigned int)time(NULL));
            food.x = rand() % (MAPWIDTH - 4) + 2;
            food.y = rand() % (MAPHEIGHT - 2) + 1;

            //随机生成的食物不能在蛇的身体上
            for (i = 0; i < snake.len; i++)
            {
                if (snake.x[i] == food.x && snake.y[i] == food.y)
                {
                    flag = 0;
                    break;
                }
            }
            //随机生成的食物不能横坐标为奇数,也不能在蛇身,否则重新生成
            if (flag && food.x % 2 == 0)
                break;
        }

        //绘制食物
        gotoxy(food.x, food.y);
        printf("*");

        snake.len++;//吃到食物,蛇身长度加1
        sorce += 10;//每个食物得10分
        snake.speed -= 5;//随着吃的食物越来越多,速度会越来越快
        changeFlag = 1;//很重要,因为吃到了食物,就不用再擦除蛇尾的那一节,以此来造成蛇身体增长的效果
    }
    return;
}

bool snakeStatus()
{
    //蛇头碰到上下边界,游戏结束
    if (snake.y[0] == 0 || snake.y[0] == MAPHEIGHT)
        return false;
    //蛇头碰到左右边界,游戏结束
    if (snake.x[0] == 0 || snake.x[0] == MAPWIDTH)
        return false;
    //蛇头碰到蛇身,游戏结束
    for (i = 1; i < snake.len; i++)
    {
        if (snake.x[i] == snake.x[0] && snake.y[i] == snake.y[0])
            return false;
    }
    return true;
}

int main()
{
    drawMap();
    while (1)
    {
        keyDown();
        if (!snakeStatus())
            break;
        createFood();
        Sleep(snake.speed);
    }

    gotoxy(MAPWIDTH / 2, MAPHEIGHT / 2);
    printf("Game Over!\n");
    gotoxy(MAPWIDTH / 2, MAPHEIGHT / 2 + 1);
    printf("本次游戏得分为:%d\n", sorce);
    Sleep(5000);
    return 0;
}

 

网上找的

0
曹灿阳
曹灿阳
初级天翼
初级天翼

我有两个贪吃蛇的代码,就不给你找错了。

#include <iostream>  
#include <cstdio>  
#include <cstdlib>  
#include <ctime>  
#include <conio.h>  
#include <cmath>  
#include <windows.h>  
using namespace std;  
  
/*** 光标定位 ***/  
HANDLE hout=GetStdHandle(STD_OUTPUT_HANDLE);  
COORD coord;  
  
void locate(int x,int y)  
{  
    coord.X=y;  
    coord.Y=x;  
    SetConsoleCursorPosition(hout,coord);  
};  
  
/*** 隐藏光标 ***/  
void hide()  
{  
    CONSOLE_CURSOR_INFO cursor_info={1,0};  
    SetConsoleCursorInfo(hout, &cursor_info);  
}  
  
/*** 生成随机数 ***/  
double random(double start, double end)  
{  
    return start+(end-start)*rand()/(RAND_MAX + 1.0);  
}  
/*** 定义地图的长宽,蛇的坐标,长度,方向,食物的位置 ***/  
int m,n;   
struct node  
{  
    int x,y;  
}snake[1000];  
  
int snake_length,dir;  
node food;  
int direct[4][2]={{-1,0},{1,0},{0,-1},{0,1}};  
  
/*** 输出墙 ***/  
void print_wall()  
{  
    cout << " ";  
    for (int i=1;i<=n;i++)  
        cout << "-";  
    cout << endl;  
    for (int j=0;j<=m-1;j++)  
    {  
        cout << "|";  
        for (int i=1;i<=n;i++) cout << " ";  
        cout << "|" << endl;  
    }  
    cout << " ";  
    for (int i=1;i<=n;i++)  
        cout << "-";  
}  
  
/*** 首次输出蛇,其中snake[0]代表头 ***/  
void print_snake()  
{  
    locate(snake[0].x,snake[0].y);  
    cout << "#";  
    for (int i=1;i<=snake_length-1;i++)  
    {  
        locate(snake[i].x,snake[i].y);  
        //cout << "*";  
    }  
}  
  
/*** 判断是否撞墙或者自撞 ***/  
bool is_correct()  
{  
    if (snake[0].x==0 || snake[0].y==0 || snake[0].x==m+1 || snake[0].y==n+1) return false;  
    for (int i=1;i<=snake_length-1;i++)  
    {  
        if (snake[0].x==snake[i].x && snake[0].y==snake[i].y) return false;  
    }  
    return true;  
}  
  
/*** 随机生成并输出食物位置 ***/  
bool print_food()  
{  
    srand((unsigned)time(0));  
    bool e;  
    while (3)  
    {  
        e=true;  
        int i=(int) random(0,m)+1,j=(int) random(0,n)+1;  
        food.x=i;food.y=j;  
        for (int k=0;k<=snake_length-1;k++)  
        {  
            if (snake[k].x==food.x && snake[k].y==food.y)  
            {  
                e=false;break;  
            }  
        }  
        if (e) break;  
    }  
    locate(food.x,food.y);  
    cout << "$";  
    return true;  
}  
  bool print_food2()  
{  
    srand((unsigned)time(0));  
    bool e;  
    while (3)  
    {  
        e=true;  
        int i=(int) random(0,m)+1,j=(int) random(0,n)+1;  
        food.x=i;food.y=j;  
        for (int k=0;k<=snake_length-1;k++)  
        {  
            if (snake[k].x==food.x && snake[k].y==food.y)  
            {  
                e=false;break;  
            }  
        }  
        if (e) break;  
    }  
    locate(food.x,food.y);  
    cout << "$";  
    return true;  
}  
bool print_food3()  
{  
    srand((unsigned)time(0));  
    bool e;  
    while (3)  
    {  
        e=true;  
        int i=(int) random(0,m)+1,j=(int) random(0,n)+1;  
        food.x=i;food.y=j;  
        for (int k=0;k<=snake_length-1;k++)  
        {  
            if (snake[k].x==food.x && snake[k].y==food.y)  
            {  
                e=false;break;  
            }  
        }  
        if (e) break;  
    }  
    locate(food.x,food.y);  
    cout << "$";  
    return true;  
}  
/*** 蛇的前进 ***/  
bool go_ahead()  
{  
    node temp;  
    bool e=false;  
    temp=snake[snake_length-1];  
    for (int i=snake_length-1;i>=1;i--)  
        snake[i]=snake[i-1];  
    snake[0].x+=direct[dir][0];  
    snake[0].y+=direct[dir][1];  
    locate(snake[1].x,snake[1].y);  
    cout << "*";  
    /*** 吃到了食物 ***/  
    if (snake[0].x==food.x && snake[0].y==food.y)  
    {  
        snake_length++;  
        e=true;  
        snake[snake_length-1]=temp;  
    }  
    /*** 输出此时蛇状态 ***/  
    if (!e)  
    {  
        locate(temp.x,temp.y);  
        cout << " ";  
    }  
    else  
    {print_food();
    print_food2();
    print_food3();
    }
          
    locate(snake[0].x,snake[0].y);  
    cout << "@";  
    /*** 如果自撞 ***/  
    if (!is_correct())  
    {  
        system("cls");  
        cout << "You lose!" << endl << "Length: " << snake_length << endl;  
        return false;  
    }  
    return true;  
}  
  
/*** 主函数 ***/  
int main()  
{  
    cout << "--------------------贪吃蛇---------------------" << endl;  
    cout << "请先输入两个数,表示地图大小.要求长宽均不小于10." << endl;  
    cout << "请注意窗口大小,以免发生错位.建议将窗口调为最大." << endl;  
    cout << "再选择难度.请在1-10中输入1个数,1最简单,10则最难" << endl;  
    cout << "然后进入游戏画面,以方向键控制方向.祝你游戏愉快!" << endl;  
    cout << "-----------------------------------------------" << endl;  
    cin >> m >> n;  
    if (m<1 || n<1 || m>400 || n>600)  
    {  
        cout << "ERROR" << endl;  
        system("pause");  
        return 0;  
    }  
    int hard;  
    cin >> hard;  
    if (hard<=0 || hard>100)  
    {  
        cout << "ERROR" << endl;  
        system("pause");  
        return 0;  
    }  
    /*** 数据全部初始化,包括蛇长,位置,方向 ***/  
    snake_length=30;  
    clock_t a,b;  
    char ch;  
    double hard_len;  
    for (int i=0;i<=4;i++)  
    {  
        snake[i].x=1;  
        snake[i].y=5-i;  
    }  
    dir=3;  
    /*** 输出初始地图,蛇与食物 ***/  
    system("cls");  
    hide();  
    print_wall();  
    print_food();  
    print_snake();  
    locate(m+2,0);  
    cout << "Now length: ";  
    /*** 开始游戏 ***/  
    while (1)  
    {  
        /*** 难度随长度增加而提高 ***/  
        hard_len=(double)snake_length/(double) (m*n);  
        /*** 调节时间,单位是ms ***/  
        a=clock();  
        while (1)  
        {  
            b=clock();  
            if (b-a>=(int)(400-30*hard)*(1-sqrt(hard_len))) break;  
        }  
        /*** 接受键盘输入的上下左右,并以此改变方向 ***/  
        if (kbhit())  
        {  
            ch=getch();  
            if (ch==-32)  
            {  
                ch=getch();  
                switch(ch)  
                {  
                case 72:  
                    if (dir==2 || dir==3)  
                        dir=0;  
                    break;  
                case 80:  
                    if (dir==2 || dir==3)  
                        dir=1;  
                    break;  
                case 75:  
                    if (dir==0 || dir==1)  
                        dir=2;  
                    break;  
                case 77:  
                    if (dir==0 || dir==1)  
                        dir=3;  
                    break;  
                }  
            }  
        }  
        /*** 前进 ***/  
        if (!go_ahead()) break;  
        /*** 在最后输出此时长度 ***/  
        locate(m+2,12);  
        cout << snake_length;  
    }  
    system("pause");  
    return 0;  
}  

还有一个:

#include<stdio.h>
#include<conio.h>
#include<windows.h>
#include<time.h>
#define framex 5
#define framey 5
#define wide 20
#define high 20
int i,j,a[2];
//将光标移动到指定位置 
void gotoxy(HANDLE hout,int x,int y){
    //COORD是WindowsAPI中定义的一种结构,表示一个字符在控制台屏幕上的坐标
    COORD pos;
    pos.X=x;
    pos.Y=y;
    //SetConsoleCursorPosition是API中定位光标位置的函数。
    SetConsoleCursorPosition(hout,pos);
}
//游戏封面
void cover (HANDLE hout){
    gotoxy(hout,framex+wide,framey);
    printf("欢迎使用贪吃蛇游戏2.0");
    gotoxy(hout,framex+wide,framey+5);
    printf("开始游戏前请关闭中文输入法");
    gotoxy(hout,framex+wide*2,framey+20);
    printf( "游戏制作者:zzy"); 
    gotoxy(hout,framex+wide*2,framey+22);
    printf("制作时间:2017年12月"); 
    char a;
    a=getchar();
    system("cls");
} 
//定义蛇的结构体 
struct  Snake{
    int x[100];
    int y[100];
    int speed;
    int length;
    int count;
};
//定义食物的结构体 
struct Food{
    int x;
    int y;
};
//制作框架 
void makeframe(struct Snake snake){
    //定义显示器变量句柄 
    HANDLE hout=GetStdHandle(STD_OUTPUT_HANDLE);
    gotoxy(hout,framex+wide*2+5,framey);
    printf( "   贪吃蛇游戏"); 
    gotoxy(hout,framex+wide*2+5,framey+3);
    printf("使用方向键或wasd移动");
    gotoxy(hout,framex+wide*2+5,framey+5);
    printf("长按方向键可加速");
    gotoxy(hout,framex+wide*2+5,framey+7);
    printf("按任意键暂停,方向键继续");
    //打印上边框 
    for(i=0;i<wide*2+1;i++){
        gotoxy(hout,framex+i,framey);
        printf("*");
    }
    //打印下边框 
    for(i=0;i<wide*2+2;i++){
        gotoxy(hout,framex+i,framey+high);
        printf("*");
    }
    //打印左边框 
    for(i=0;i<high;i++){
        gotoxy(hout,framex,framey+i);
        printf("*");
    }
    //打印右边框 
    for(i=0;i<high;i++){
        gotoxy(hout,framex+wide*2+1,framey+i);
        printf("*");
    }
}
//游戏信息
void infor(HANDLE hout,struct Snake* snake){
    gotoxy(hout,framex+wide*2+5,framey+15);
    printf("当前速度:%d",-snake->speed+500);
    gotoxy(hout,framex+wide*2+5,framey+17);
    printf("当前得分:%d",snake->count);
    gotoxy(hout,framex+wide*2+5,framey+19);
    printf("当前长度:%d",snake->length);
} 
//初始化蛇 
void initsnake(struct Snake *snake){
    snake->x[0]=framex+2;
    snake->y[0]=framey+high/2;
    snake->count=0;
    snake->length=3;
    snake->speed=400;
    //初始化由蛇尾至蛇头的坐标 
    for(i=1;i<snake->length;i++){
        snake->x[i]=snake->x[i-1]+1;
        snake->y[i]=snake->y[i-1];
    }
}
//打印蛇 
void printsnake(HANDLE hout ,struct Snake *snake){
    for(i=0;i<snake->length;i++){
        gotoxy(hout,snake->x[i],snake->y[i]);
        if(i==snake->length-1) 
            printf("@");
        else if(i==0)
            printf("-");
        else 
            printf("="); 
    }
}
//移动蛇 
void movesnake(HANDLE hout,struct Snake *snake){
    gotoxy(hout,snake->x[0],snake->y[0]);
    //清除蛇尾 
    printf("  ");
    //蛇的后一节坐标变成前一节的坐标 
    for(i=1;i<snake->length;i++){
        snake->x[i-1]=snake->x[i];
        snake->y[i-1]=snake->y[i];
    }
}
//打印食物 
void printfood (HANDLE hout,struct Snake *snake,struct Food* food){
    //用计算机时间获取随机值 
    srand((unsigned)time(NULL));
    while(1){
        //将食物的横纵坐标的大小限定在窗口大小内 
        food->x=rand()%(wide-2)+1;
        food->y=rand()%(high-2)+1;
        //避免食物出现在边框上 
        if(food->x==0&&food->y==0)
            continue;
        //将食物的坐标放置在窗口内 
        food->x=2*food->x+framex;
        food->y+=framey;
        //如果食物出现在蛇上,重新产生食物 
        for(i=0;i<snake->length;i++){
            if(food->x==snake->x[i]&&food->y==snake->y[i])
                break;
        }
        //食物不在蛇上,打印食物,结束循环 
        if(i==snake->length){
            gotoxy(hout,food->x,food->y);
            printf("*");
            break;  
        }
    }

}
//吃食物 
void eatfood(HANDLE hout,struct Snake * snake,struct Food *food){
    //如果蛇头的坐标等于食物的坐标 
    if(snake->x[snake->length-1]==food->x&&snake->y[snake->length-1]==food->y){
        //蛇长加一 
        snake->length++;
        //蛇身(不包括蛇尾)整体像前移动一格 
        for(i=snake->length-1;i>0;i--){
            snake->x[i]=snake->x[i-1];
            snake->y[i]=snake->y[i-1];
        }
        //得到蛇尾的坐标(即蛇移动前的蛇尾坐标) 
        snake->x[0]=a[0];      
        snake->y[0]=a[1];
        printfood(hout,snake,food);
        snake->count++;
        if(snake->count%3==0)
            snake->speed-=50;
    }
}
//死亡判断 
int ifdead(struct Snake* snake){
    //如果碰到左边界 返回0;
    if(snake->x[snake->length-1]==framex)
        return 0;
    //如果碰到右边界 返回0;
    if(snake->x[snake->length-1]==framex+wide*2)
        return 0;
    //如果碰到上边界 返回0;
    if(snake->y[snake->length-1]==framey)
        return 0;
    //如果碰到下边界 返回0;
    if(snake->y[snake->length-1]==framey+high)
        return 0;
    //如果碰到自己身体 返回0; 
    for(i=0; i<snake->length-1; i++) 
        if( snake->x[snake->length-1]==snake->x[i] && snake->y[snake->length-1]==snake->y[i] ) 
            return 0;
    //否则返回1; 
    return 1;
}
//开始游戏 
int main(){
    unsigned char ch =77;
    //定义显示器句柄 
    HANDLE hout = GetStdHandle(STD_OUTPUT_HANDLE);
    cover(hout);
    struct Snake s, *snake=&s;
    struct Food  f,  *food=&f;
    makeframe(*snake);
    initsnake(snake);
    printfood(hout,snake,food);
    Sleep(500);
    while(1){
        infor(hout,snake);
        //保存蛇尾横纵坐标 
        a[0]=snake->x[0];
        a[1]=snake->y[0];
        j=0;
        //如果用户敲击键盘
        if(kbhit()){ 
            //用ch接收输入 ,注意是getch(),不需敲击回车
            ch=getch();
            if(kbhit()){
            //长按时间
                Sleep(20);
                j=1;
            }
        }
        switch(ch){
            //向上移动 
            case'W':
            case 'w' :
            case 72:{
                movesnake(hout,snake);
                //蛇头纵坐标减一 
                snake->y[snake->length-1]-=1;
                break;
            }
            //向下移动 
            case'S':
            case 's':
            case 80:{
                movesnake(hout,snake);
                //蛇头纵坐标加一 
                snake->y[snake->length-1]+=1;
                break;
            }
            //向左移动 
            case'A':
            case 'a':
            case 75:{
                movesnake(hout,snake);
                //蛇头横坐标减二 
                snake->x[snake->length-1]-=2;
                break;
            }
            //向右移动 
            case'D':
            case 'd':
            case 77:{
                movesnake(hout,snake);
                //蛇头横坐标加二 
                snake->x[snake->length-1]+=2;
                break;
            }
        }

        eatfood(hout,snake,food);
        printsnake(hout,snake);
        //如果判断蛇死亡,跳出循环 
        if(ifdead(snake)==0||ch==27||snake->speed==0){
            gotoxy(hout,framex+wide/2,framey-2);
            if(snake->speed==0)
                printf("恭喜你通关了!!!");
            else
                printf("糟糕 T_T");
            break;
        }

        if(j==0)
        //如果没有长按,蛇滞留的时间为 
            Sleep(snake->speed);
        //如果长按,蛇滞留的时间为 20ms,即加速 
        else 
            Sleep(20);
    }
    //跳出循环时 ,滞留死亡场景一段时间
    Sleep(3000);
    //清屏 
    system("cls");
    //显示结束界面 
    if(snake->speed==0) 
        printf("\n\n\n\n\n\t\t\t哈哈,你赢了\n\n\t\t\t你得到了满分:24\n\n\n");
    else 
        printf("\n\n\n\n\n\t\t\t哈哈,你输了\n\n\t\t\t你的最终得分是:%d\n\n\n",snake->count); 
    Sleep(3000);
    return 0; 
} 

 

曹灿阳在2020-07-29 16:52:09追加了内容

不好意思,手残,多发了一个

0
曹砚青
曹砚青
中级光能
中级光能

抄袭代码大佬,膜拜膜拜,抄袭大佬

抄袭代码大佬,膜拜膜拜,抄袭大佬

抄袭代码大佬,膜拜膜拜,抄袭大佬

0
0
0
0
0
欧阳语聪
欧阳语聪
资深守护
资深守护

贪吃蛇:

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<conio.h>

typedef struct snake
{
    int a;
    int b;
    struct snake *u;
    struct snake *n;
}snake,*snake1;
typedef struct food
{
    int a;
    int b;
}food;
int main()
{   
    char c,c0 = 'd';
    int i,j,k,n=1,t,at;
    snake p,q;
    snake *dd,*dd0,*dd1,*dd2;
    food f;
    srand(time(NULL));
    p.u = NULL;
    p.n = &q;
    p.a = 5;p.b = 6;q.a = 5;q.b = 5;
    q.u = &p;q.n = NULL;
    dd=dd2= &q;
    f.a=(rand()%15+1);
    f.b=(rand()%15+1);
    while(1)
    {
        srand(time(NULL));
        system("cls");
        for(i = 0;i < 17;i ++)
        {
            for(j = 0; j < 17;j++)
            {

                if(i == 0 )
                    printf("▁");
                else if(i == 16)
                    printf("▔");
                else if(j == 0)
                    printf("▕");
                else if(j == 16)
                    printf("▏");
                else if(i == p.a && j == p.b)
                    printf("■");
                else if(i == f.a && j == f.b)
                    printf("★");
                else 
                {
                    t = 0;
                    dd = dd2;
                    for(k = 0; k < n ;k++)
                    { 
                        if(i == dd->a && j == dd->b)
                        {
                            printf("□");
                            t = 1;
                            break;
                        }
            
                        dd = dd->u;    
                    }
                    if(t == 0)
                        printf("  ");
                }
                
            }printf("\n");
        }
        at = 0;
        dd =dd2;
        for(i=0;i<n;i++)
        {
            if(p.a == dd->a  && p.b == dd->b)
            {
                printf("game over!!\n");
                exit(0);
            }
            dd = dd->u;
        }
        if(p.a == f.a && p.b == f.b)
        {
            dd = dd2;
            at =1;
            f.a = (rand()%15+1);
            f.b = (rand()%15+1);
            for(i=0;i<n;i++)
            {
                if(f.a == dd->a && f.b == dd->b)
                {
                    f.a = dd2->a;
                    f.b = dd2->b;
                    break;
                }
            }
            n++;
        }
        if(kbhit())
        {
            c = getch();
            dd = dd2;
            if(c == 'w' && c0 != 's')
            {
                if(at == 1)
                {
                    dd0 =(snake1)malloc(sizeof(snake));
                    dd0->a = dd2->a;dd0->b = dd2->b;
                    dd0->n = NULL;dd0->u = dd2;
                    dd2=dd0;
                }
                dd = dd2;
                for(i = 0; i<n ; i++)
                {
                    dd1 = dd->u;
                    dd->b = dd1->b;
                    dd->a = dd1->a;
                    dd = dd->u;
                }
                if(p.a == 1)
                    p.a = 15;
                else
                    p.a = (p.a-1)%15;
            }
            else if(c == 's' && c0 != 'w')
            {    
                if(at == 1)
                {
                    dd0 =(snake1)malloc(sizeof(snake));
                    dd0->a = dd2->a;dd0->b = dd2->b;
                    dd0->n = NULL;dd0->u = dd2;
                    dd2=dd0;
                }
                dd = dd2;
                for(i = 0; i<n ; i++)
                {
                    dd1 = dd->u;
                    dd->b = dd1->b;
                    dd->a = dd1->a;
                    dd = dd->u;
                }
                p.a = (p.a%15)+1;
            }
            else if(c == 'a' && c0 != 'd')
            {    
                if(at == 1)
                {
                    dd0 =(snake1)malloc(sizeof(snake));
                    dd0->a = dd2->a;dd0->b = dd2->b;
                    dd0->n = NULL;dd0->u = dd2;
                    dd2=dd0;
                }
                dd = dd2;
                for(i = 0; i<n ; i++)
                {
                    dd1 = dd->u;
                    dd->b = dd1->b;
                    dd->a = dd1->a;
                    dd = dd->u;
                }
                if(p.b == 1)
                    p.b = 15;
                else
                    p.b = (p.b-1)%15;
            }
            else if(c == 'd' && c0 != 'a')
            {    
                if(at == 1)
                {
                    dd0 =(snake1)malloc(sizeof(snake));
                    dd0->a = dd2->a;dd0->b = dd2->b;
                    dd0->n = NULL;dd0->u = dd2;
                    dd2=dd0;
                }
                dd = dd2;
                for(i = 0; i<n ; i++)
                {
                    dd1 = dd->u;
                    dd->b = dd1->b;
                    dd->a = dd1->a;
                    dd = dd->u;
                }
                p.b = (p.b%15)+1;
            }            
            else
            {
                goto qq;
            }
            c0 = c;
        }
        else
        {
qq:            if(c0 == 'w')
            {
                if(at == 1)
                {
                    dd0 =(snake1)malloc(sizeof(snake));
                    dd0->a = dd2->a;dd0->b = dd2->b;
                    dd0->n = NULL;dd0->u = dd2;
                    dd2=dd0;
                }
                dd = dd2;
                for(i = 0; i<n ; i++)
                {
                    dd1 = dd->u;
                    dd->b = dd1->b;
                    dd->a = dd1->a;
                    dd = dd->u;
                }
                if(p.a == 1)
                    p.a = 15;
                else
                    p.a=(p.a-1)%15;
            }
            else if(c0 == 's')
            {
                if(at == 1)
                {
                    dd0 =(snake1)malloc(sizeof(snake));
                    dd0->a = dd2->a;dd0->b = dd2->b;
                    dd0->n = NULL;dd0->u = dd2;
                    dd2=dd0;
                }
                dd = dd2;
                for(i = 0; i<n ; i++)
                {
                    dd1 = dd->u;
                    dd->b = dd1->b;
                    dd->a = dd1->a;
                    dd = dd->u;
                }
                p.a=(p.a%15)+1;
            }
            else if(c0 == 'a')
            {
                
                if(at == 1)
                {
                    dd0 =(snake1)malloc(sizeof(snake));
                    dd0->a = dd2->a;dd0->b = dd2->b;
                    dd0->n = NULL;dd0->u = dd2;
                    dd2=dd0;
                }
                dd = dd2;
                for(i = 0; i<n ; i++)
                {
                    dd1 = dd->u;
                    dd->b = dd1->b;
                    dd->a = dd1->a;
                    dd = dd->u;
                }
                if(p.b == 1)
                    p.b = 15;
                else
                    p.b=(p.b-1)%15;
            }
            else if(c0 == 'd')
            {
                if(at == 1)
                {
                    dd0 =(snake1)malloc(sizeof(snake));
                    dd0->a = dd2->a;dd0->b = dd2->b;
                    dd0->n = NULL;dd0->u = dd2;
                    dd2=dd0;
                }
                dd = dd2;
                for(i = 0; i<n ; i++)
                {
                    dd1 = dd->u;
                    dd->b = dd1->b;
                    dd->a = dd1->a;
                    dd = dd->u;
                }
                p.b=(p.b%15)+1;
                
            }
        }
        fflush(stdin);
        dd = &q;
        _sleep(200);
    }
}

俄罗斯方块:

#include <iostream>
#include <cstdio>
#include <windows.h>
#include <vector>
#include <mmsystem.h>
#pragma comment(lib, "winmm.lib")
using namespace std;
#define GameW 10
#define GameH 20
const int CtrlLeft = GameW*2+4 + 3;
void out(int x){
    cout<<"游戏方式:"<<endl;
    cout<<"← 左移     → 右移"<<endl;
    cout<<"↓ 加速     ↑ 旋转"<<endl;
    cout<<"游戏规则:"<<endl;
    cout<<"连续消去1行得1分、2行得3分、3行得5分、4行得7分。"<<endl;
    cout<<"积分达到一定程度,会有换命的活动,命最多6条。"<<endl;
    cout<<"难度会随积分的上升逐渐上升,最多到6的难度。"<<endl<<"下面游戏正式开始";
    return;
}
struct Point {
    Point(){}
    Point(int x, int y) {_x = x, _y = y;}
    int _x, _y;
};

HANDLE g_hOutput = GetStdHandle(STD_OUTPUT_HANDLE);
HANDLE g_hInput  = GetStdHandle(STD_INPUT_HANDLE);

Point g_ptCursor(0,0);
BOOL isChecking = FALSE;
BOOL g_bGameOver = FALSE;
int g_nGameBack[GameH][GameW], Case;
int nowKeyInfo = -1;
int g_nDiff = 1;
int g_nLife = 2;
int g_nScore = 0;

void SetCursor(COORD cd) {
    SetConsoleCursorPosition(g_hOutput, cd);
}
void SetCursor(int x, int y){
    COORD cd = {x, y};
    SetCursor(cd);
}
void SetBlockCursor(int x, int y){
    COORD cd = {2*x + 2, y + 1};
    SetCursor(cd);
}

void SetBack(int x, int y, BOOL bk) {
    SetBlockCursor(x, y);
    if (bk) 
        printf("%s", "■");
    else
        printf(" ");
}

bool Out(int x, int y) {
    return x < 0 || y < 0 || x >= GameW || y >= GameH; 
}

struct xBlock {
public:
    int len;
    int nowRotateID;
    BOOL mask[4][4][4];
    static vector <xBlock> List;

    xBlock() { len = 0; }
    xBlock(int l, char *str) {
        int i, j, k;
        len = l;
        memset(mask, FALSE, sizeof(mask));
        for(i = 0; i < l; i++) {
            for(j = 0; j < l; j++) {
                mask[0][i][j] = str[i*l + j] - '0';
            }
        }
        for(k = 1; k < 4; k++) {
            for(i = 0; i < len; i++) {
                for(j = 0; j < len; j++) {
                    mask[k][i][j] = mask[k-1][j][len-1-i];
                }
            }
        }
        nowRotateID = rand() % 4;
    }

    void rotate() {
        nowRotateID ++;
        if (nowRotateID >= 4)
            nowRotateID = 0;
    }

    BOOL getUnit(int x, int y, int roID) {
        if (roID == -1) {
            roID = nowRotateID;
        }
        return mask[roID][y][x];
    }
};

vector <xBlock> xBlock::List;

class Block {
public:
    int x, y;
    int ID;
    xBlock bk;

    void reset(xBlock *pbk) {
        bk = *pbk;

        x = 4, y = 0;
        ID = ++ Case;

        if(collide(0,0)) {
            lifeDown();
        }
        draw();
        
        *pbk = xBlock::List[rand() % xBlock::List.size()];
    }
    
    void lifeDown() {
        int i, j;
        for(i = 0; i < GameH; i++) {
            for(j = 0; j < GameW; j++) {
                SetBack(j, i, TRUE);
                Sleep(10);
            }
        }
        if(g_nLife) {
            g_nLife --;
            for(i = g_nLife; i < 6; i++) {
                SetCursor(CtrlLeft + i, 15);
                printf("%c", ' ');
            }
            for(i = GameH-1; i >= 0; i--) {
                for(j = GameW-1; j >= 0; j--) {
                    SetBack(j, i, FALSE);
                    Sleep(10);
                    g_nGameBack[i][j] = 0;
                }
            }
        }else {
            g_bGameOver = TRUE;
        }
    }

    void erase() {
        int i, j;
        for(i = 0; i < bk.len; i++) {
            for(j = 0; j < bk.len; j++) {
                if (bk.getUnit(j, i, -1)) {
                    if(!Out(j+x, i+y) && g_nGameBack[i+y][j+x]) {
                        SetBack(j+x, i+y, FALSE);
                        g_nGameBack[i+y][j+x] = 0;
                    }
                }
            }
        }
    }
    void draw() {
        int i, j;
        for(i = 0; i < bk.len; i++) {
            for(j = 0; j < bk.len; j++) {
                if (bk.getUnit(j, i, -1)) {
                    if(!Out(j+x, i+y) && !g_nGameBack[i+y][j+x]) {
                        SetBack(j+x, i+y, TRUE);
                        g_nGameBack[i+y][j+x]  = ID;
                    }
                }
            }
        }
    }
    void draw(int x, int y) {
        int i, j;
        for(i = 0; i < 4; i++) {
            for(j = 0; j < 4; j++) {
                SetCursor(x + 2*j, y + i);
                if (bk.getUnit(j, i, -1)) {    
                    printf("%s", "■");
                }else 
                    printf(" ");
            }
        }
    }
    bool collide(int dx, int dy, int roID = -1) {
        int i, j;
        for(i = 0; i < bk.len; i++) {
            for(j = 0; j < bk.len; j++) {
                if (bk.getUnit(j, i, roID)) {
                    Point ptPos(j + x + dx, i + y + dy);
                    if(Out(ptPos._x, ptPos._y)
                    || g_nGameBack[ptPos._y][ptPos._x] && ID != g_nGameBack[ptPos._y][ptPos._x]) {
                        return TRUE;
                    }
                }
            }
        }
        return FALSE;
    }

    void rotate(int nTimes = 1) {
        int nextro = (bk.nowRotateID + nTimes) % 4;
        if(collide(0, 0, nextro)) {
            return ;
        }
        Beep(12000, 50);
        erase();
        bk.nowRotateID = nextro;
        draw();
    }

    BOOL changepos(int dx, int dy) {
        if(collide(dx, dy)) {
            return FALSE;
        }
        erase();
        x += dx;
        y += dy;
        draw();
        return TRUE;
    }
};

void GameInit() {
    CONSOLE_CURSOR_INFO cursor_info;
    cursor_info.bVisible = FALSE;
    cursor_info.dwSize   = 100;
    SetConsoleCursorInfo(g_hOutput, &cursor_info);
    xBlock::List.push_back(xBlock(3, "010111000"));
    xBlock::List.push_back(xBlock(3, "110110000"));
    xBlock::List.push_back(xBlock(3, "111001000"));
    xBlock::List.push_back(xBlock(3, "111100000"));
    xBlock::List.push_back(xBlock(3, "110011000"));
    xBlock::List.push_back(xBlock(3, "011110000"));
    xBlock::List.push_back(xBlock(4, "1000100010001000"));
}

void DrawFrame(int x, int y, int nWidth, int nHeight) {
    int i;
    for(i = 0; i < nWidth; i++) {
        SetCursor(x + 2*i + 2, y);
        printf("%s", "一");
        SetCursor(x + 2*i + 2, y + nHeight+1);
        printf("%s", "┄");
    }
    for(i = 0; i < nHeight; i++) {
        SetCursor(x, y + i + 1);
        printf("%s", "┆");
        SetCursor(x + nWidth*2+2, y + i + 1);
        printf("%s", "┆");
    }        
    SetCursor(x, y);
    printf("%s", "┌");    
    SetCursor(x, y + nHeight+1);
    printf("%s", "└");
    SetCursor(x + nWidth*2+2, y);
    printf("%s", "┐");    
    SetCursor(x + nWidth*2+2, y + nHeight+1);
    printf("%s", "┘");
}

void MissionInit() {
    memset(g_nGameBack, FALSE, sizeof(g_nGameBack));
    Case = 1;
    int i;
    DrawFrame(0, 0, GameW, GameH);
    DrawFrame(GameW*2+4, 0, 4, GameH);
    SetCursor(CtrlLeft, 2);
    printf("Next");
    
    SetCursor(CtrlLeft, 8);
    printf("Speed");
    for(i = 0; i < g_nDiff; i++) {
        SetCursor(CtrlLeft + i, 9);
        printf("%c", 1);
    }

    SetCursor(CtrlLeft, 11);
    printf("Score");
    SetCursor(CtrlLeft, 12);
    printf("%d", g_nScore);

    SetCursor(CtrlLeft, 14);
    printf("Life");
    for(i = 0; i < g_nLife; i++) {
        SetCursor(CtrlLeft + i, 15);
        printf("%c", 3);
    }

    SetCursor(CtrlLeft-1, 19);
    printf("by Zty");
    SetCursor(CtrlLeft-1, 20);
    printf("Baidu A*");
}

void Check() {
    isChecking = TRUE;
    int i, j, k;
    vector <int> line;
    for(i = 0; i < GameH; i++) {
        for(j = 0; j < GameW; j++) {
            if(!g_nGameBack[i][j])
                break;
        }
        if(j == GameW) {
            line.push_back(i);
        }
    }
    if(line.size()) {
        int nCount = 7;
        while(nCount --) {
            for(i = 0; i < line.size(); i++) {
                for(j = 0; j < GameW; j++) {
                    SetBack(j, line[i], nCount&1);
                }
            }
            Sleep(70);
        }
        for(i = 0; i < line.size(); i++) {
            for(j = 0; j < GameW; j++) {
                g_nGameBack[line[i]][j] = 0;
            }
        }

        for(i = 0; i < GameW; i++) {
            int next = GameH-1;
            for(j = GameH-1; j >= 0; j--) {
                for(k = next; k >= 0; k--) {
                    if(g_nGameBack[k][i]) 
                        break;
                }
                next = k - 1;
                BOOL is = (k >= 0);
                SetBack(i, j, is);
                g_nGameBack[j][i] = is;
            }
        }

        g_nScore += 2*line.size()-1;
        SetCursor(CtrlLeft, 12);
        printf("%d", g_nScore);

        if( g_nScore >= g_nDiff * g_nDiff * 10) {
            if(g_nDiff <= 6)
                g_nDiff ++;
        }
        if( g_nScore >= 50 * (g_nLife+1)) {
            if(g_nLife <= 6)
                g_nLife ++;
        }
    }

    isChecking = FALSE;
}
int main() { 
    Block* obj = new Block();
    Block* buf = new Block();
    

    BOOL bCreateNew = FALSE;
    int nTimer = GetTickCount();
    int LastKeyDownTime = GetTickCount();


    GameInit();
    MissionInit();
    
    buf->bk = xBlock::List[rand() % xBlock::List.size()];
    while(1) {
        if(!bCreateNew) {
            bCreateNew = TRUE;
            obj->reset(&buf->bk);
            if(g_bGameOver)
                break;
            buf->draw(CtrlLeft - 1, 4);
        }
        if (GetTickCount() - nTimer >= 1000 / g_nDiff) {
            nTimer = GetTickCount();
            if (!obj->collide(0, 1))
                obj->changepos(0, 1);
            else {
                Check();
                bCreateNew = FALSE;
            }
        }
        if (GetTickCount() - LastKeyDownTime >= 100) {
            if(FALSE == isChecking) {
                LastKeyDownTime = GetTickCount();
                if (GetAsyncKeyState(VK_UP)) {
                    obj->rotate();
                }
                if (GetAsyncKeyState(VK_LEFT)) {
                    obj->changepos(-1, 0);
                }
                if (GetAsyncKeyState(VK_RIGHT)) {
                    obj->changepos(1, 0);
                }
                if (GetAsyncKeyState(VK_DOWN)) {
                    if( FALSE == obj->changepos(0, 2) )
                        obj->changepos(0, 1);
                }
            }
        }
    }
    SetCursor(8, 10);
    printf("Game Over");

    SetCursor(0, GameH+3);
    printf("按ESC键退出游戏");

    while(1) {
        if (GetAsyncKeyState(VK_ESCAPE))
            break;
    }
    return 0;
    } 

下落的小鸟:#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<time.h>
#include<Windows.h>
/********函数变量声明********/
#define PR_Box printf("■")
#define PR_Gold printf("★")
#define PR_Ag printf("☆")
#define PR_FBird printf("○")
#define PR_DBird printf("Ф")
#define PR_Land printf("┳┳┯")
#define PR_Bg_TL printf("╔")
#define PR_Bg_TR printf("╗")
#define PR_Bg_DL printf("╚")
#define PR_Bg_DR printf("╝")
#define PR_Bg_X printf("═")
#define PR_Bg_Y printf("║")
#define PR_Blank printf(" ");
int Grade = 1, C_Gold = 0, C_Ag = 0, Score = 0, Delay_time = 1000, Max_blank = 9, Distance = 18;
struct Birds
{
int x, y;
int condition;
};
Birds *Bird = (Birds*)malloc(sizeof(Birds));
struct Bg
{
int x, y;
int l_blank;
int reward[9];
Bg *pri;
Bg *next;
};
Bg *Bg1 = new Bg[sizeof(Bg)];
void Position(int x, int y)
{
COORD pos = { x - 1, y - 1 };
HANDLE Out = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(Out, pos);
}
void CreatBird()
{
Bird->x = 41;
Bird->y = 10;
Bird->condition = 0;
}
void CreatBg()
{
Bg *Bg2 = (Bg*)malloc(sizeof(Bg));
Bg1->x = 90; Bg1->y = 8;
Bg2->x = Bg1->x + Distance; Bg2->y = 9;
Bg1->l_blank = Max_blank - Grade;
Bg2->l_blank = Max_blank - Grade;
Bg1->next = Bg2;
Bg1->pri = Bg2;
Bg2->next = Bg1;
Bg2->pri = Bg1;
}
void InsertBg(Bg *p)
{
int temp;
Bg *Bgs = (Bg*)malloc(sizeof(Bg));
Bgs->x = p->pri->x + Distance;
Bgs->l_blank = Max_blank - Grade;
srand((int)time(0));
temp = rand();
if (temp % 2 == 0)//++
{
if ((temp % 4 + p->pri->y + Max_blank - Grade)<21)
Bgs->y = p->pri->y + temp % 4;
else
Bgs->y = p->pri->y;
}
else
{
if ((p->pri->y - temp % 4)>2)
Bgs->y = p->pri->y - temp % 4;
else
Bgs->y = p->pri->y;
}
Bgs->pri = p->pri;
Bgs->next = p;
p->pri->next = Bgs;
p->pri = Bgs;
}
void Check_Bg(Bg *q)
{
Bg *p = q; int i = 0, temp;
while (++i <= 5)
{
if (p->x>-4)
p = p->next;
else
{
srand((int)time(0));
temp = rand();
if (temp % 2 == 0)//++
{
if ((temp % 4 + p->y + Max_blank - Grade)<21)
p->y = p->y + temp % 4;
else
p->y = p->y;
p->x = p->pri->x + Distance;
p->l_blank = Max_blank - Grade;
}
else
{
if ((p->y - temp % 4)>2)
p->y = p->y - temp % 4;
else
p->y = p->y;
p->x = p->pri->x + Distance;
p->l_blank = Max_blank - Grade;
}
}
}
}
void Loop_Bg(Bg *q)
{
Bg *p = q; int i = 0;
while (++i <= 5)
{
p->x = p->x - 1;
p = p->next;
if (Bird->x == p->x)
{
Score += 1;
if (Score % 4 == 0 && Grade<4)
Grade++;
}
}
}
void Prt_Bg(Bg *q)
{
Bg *p = q; int i = 0, k, j;
while (++i <= 5)
{
if (p->x>0 && p->x <= 78)
{
for (k = 2; k<p->y; k++)
{
Position(p->x + 1, k);
PR_Box; PR_Box; PR_Blank
}
Position(p->x, p->y);
PR_Box; PR_Box; PR_Box; PR_Blank;
Position(p->x, p->y + p->l_blank);
PR_Box; PR_Box; PR_Box; PR_Blank;
k = k + p->l_blank + 1;
for (k; k <= 22; k++)
{
Position(p->x + 1, k);
PR_Box; PR_Box; PR_Blank;
}
Position(p->x, 23);
for (k = 1; k<Distance / 3 - 2; k++)
PR_Land;
}
p = p->next;
if (p->x == 0)
{
for (j = 2; j<p->y; j++)
{
Position(p->x + 1, j);
PR_Blank; PR_Blank;
}
Position(p->x + 1, p->y);
PR_Blank; PR_Blank; PR_Blank;
Position(p->x + 1, p->y + Max_blank - Grade);
PR_Blank; PR_Blank; PR_Blank;
j = j + Max_blank - Grade + 1;
for (j; j <= 22; j++)
{
Position(p->x + 1, j);
PR_Blank; PR_Blank;
}
}
}
}
void PrtBg()
{
int i;
Position(1, 1); PR_Bg_TL;
Position(79, 1); PR_Bg_TR;
Position(1, 24); PR_Bg_DL;
Position(79, 24); PR_Bg_DR;
for (i = 3; i <= 78; i += 2)
{
Position(i, 1); PR_Bg_X;
Position(i, 24); PR_Bg_X;
}
/*for(i=2;i<=23;i++)
{ Position(1,i);PR_Bg_Y;printf("%d",i-1);
Position(79,i);PR_Bg_Y;
}*/
}
void PrtBird()
{
Position(Bird->x, Bird->y - 1);
PR_Blank;
Position(Bird->x, Bird->y);
PR_FBird;
Position(38, 2);
printf("分数:%d", Score);
}
int CheckYN(Bg *q)
{
Bg *p = q; int i = 0;
while (++i <= 5)
{
if (Bird->y>23)
return 0;
if (Bird->x == p->x&&Bird->y <= p->y)
return 0;
if ((Bird->x == p->x || Bird->x == p->x + 1 || Bird->x == p->x + 2) && Bird->y == p->y)
return 0;
if (Bird->x == p->x&&Bird->y>p->y + p->l_blank)
return 0;
if ((Bird->x == p->x || Bird->x == p->x + 1 || Bird->x == p->x + 2) && Bird->y == p->y + p->l_blank)
return 0;
p = p->next;
}
return 1;
}
void Prtfirst()
{
printf("══════════════════════════════════════\n");
printf(" ■■ ■■\n");
printf(" ■■ ■■\n");
printf(" ■■ ■■ C++语言版 Flappy Bird\n");
printf(" ■■ ■■ 瞎搞人:欧阳语聪\n");
printf(" ■■ ■■ 瞎搞日期:2014.2\n");
printf(" ■■ ■■ 耗时:4小时\n");
printf(" ■■■ ■■ 游戏说明:\n");
printf(" ■■ 1-按上箭头使鸟起飞\n");
printf(" ■■ 2-等级越高,难度越大!\n");
printf(" Ю123 ■■■\n");
printf("\n");
printf(" ■■■ 欢迎各路大佬与我探讨哦!\nC++、VB、PHP、C#\n");
printf(" ■■\n");
printf(" ■■\n");
printf(" ■■ ■■■ \n");
printf(" ■■ ■■\n");
printf(" ■■ Ф ■■\n");
printf(" ■■ ■■\n");
printf(" ■■ ■■\n");
printf(" ■■ ■■\n");
printf(" ■■ ■■\n");
printf(" ┳┳┯┳┳┯┳┳┯┳┳┯┳┳┯┳┳┯┳┳┯┳┳┯┳┳┯┳┳┯┳┳┯┳┳┯┳\n");
system("pause");
Position(1, 1);
int i = 0;
while (i++<40 * 25)
PR_Blank;
}
int  main()
{
int i = 0; char ch;
Prtfirst();
PrtBg();
CreatBg();
InsertBg(Bg1);
InsertBg(Bg1);
InsertBg(Bg1);
CreatBird();
while (1)
{
if (!CheckYN(Bg1))
break;
Check_Bg(Bg1);
Prt_Bg(Bg1);
PrtBird();
Loop_Bg(Bg1);
Bird->y = Bird->y + 1;
if (GetAsyncKeyState(VK_UP))
{
    Position(Bird->x, Bird->y - 1);
    PR_Blank;
    Bird->y = Bird->y - 4;
}
while (i++<500);
{
    Sleep(100);
}
    i = 0;
}
    Position(38, 10);
    printf("You Lost!");
    Position(1, 25);
    system("pause");
    return 0;
}

音乐:#include<windows.h>
#include<cstdio>
#include<iostream>
#define qdo 262 
#define qre 294
#define qmi 330     //q前缀为低音,1后缀为高音,s前缀为半音阶 
#define qfa 349
#define qso 392
#define qla 440
#define qsi 494
#define do 523
#define re 578
#define mi 659
#define fa 698
#define so 784
#define la 880
#define si 988
#define do1 1046
#define re1 1175
#define mi1 1318
#define fa1 1480
#define so1 1568
#define la1 1760
#define si1 1976
#define sqdo 277
#define sqre 311
#define sqfa 370
#define sqso 415
#define sqla 466
#define sdo 554
#define sre 622
#define sfa 740
#define sso 831
#define sla 932
#define sdo1 1046
#define sre1 1245
#define sfa1 1480
#define sso1 1661
#define sla1 1865
using namespace std;
int main()
{
    int ban=500,pai=200,dw=128,bs;
    cout<<"1.吵人音乐 2.天空之城\n"; 
    int aaa;
    cin>>aaa; 
    cout<<"请静心欣赏";
    Sleep(500);
    if(aaa==1){
        cout<<"\n\n吵人音乐"; 
        for(int i=300;i<=1000;i+=25)
            Beep(i,ban);
        for(int i=0;i<10;i++){
            Beep(1000,500);
            Beep(1010,500);
            Beep(1020,500);
            Beep(1500,500);
            Beep(1020,500);
            Beep(1010,500);
            Beep(3000,1000);
            Beep(250,500);
            Beep(1010,500);
            Beep(1000,500);
        }
        for(int i=1000;i>=300;i-=25)
            Beep(i,ban);
    }
    else{
        int pai=400,ban=200;
        int ting=128;
        Sleep(1000); 
        Beep(la,ban);
        Beep(si,ban);
        Sleep(ting);
        Beep(do1,pai+ban);
        Beep(si,ban);
        Sleep(ting);
        Beep(do1,pai);
        Sleep(ting);
        Beep(mi1,pai);
        Sleep(ting); 
        Beep(si,3*pai);
        Sleep(ting);
        Beep(mi,ban);
        Beep(mi,ban); 
        Beep(la,ban+pai);
        Beep(so,ban);
        Sleep(ting);
        Beep(la,pai);
        Sleep(ting);
        Beep(do1,pai);
        Sleep(ting);
        Beep(so,2*pai);
        Sleep(ting);
        Sleep(pai);
        Beep(mi,ban);
        Sleep(ting/2);
        Beep(mi,ban);
        Sleep(ting/2);
        Beep(fa,pai+ban);
        Beep(mi,ban);
        Sleep(ting);
        Beep(fa,ban);
        Beep(do1,ban+pai);
        Sleep(ting);
        Beep(mi,2*pai);
        Sleep(ting);
        Sleep(ban);
        Beep(do1,ban);
        Sleep(ting/2);
        Beep(do1,ban);
        Sleep(ting/2);
        Beep(do1,ban);
        Sleep(ting/2);
        Beep(si,ban+pai);
        Beep(sfa,ban);
        Sleep(ting);
        Beep(sfa,pai);
        Beep(si,pai);
        Sleep(ting);
        Beep(si,2*pai);
        Sleep(ting);
        Sleep(pai);
        Beep(la,ban);
        Beep(si,ban);
        Sleep(ting); 
        Beep(do1,pai+ban);
        Beep(si,ban);
        Sleep(ting);
        Beep(do1,pai);
        Sleep(ting);
        Beep(mi1,pai);
        Sleep(ting);
        Beep(si,2*pai);
        Sleep(ting);
        Sleep(pai);
        Beep(mi,ban);
        Sleep(20);
        Beep(mi,ban);
        Sleep(ting);
        Beep(la,pai+ban);
        Beep(so,ban);
        Sleep(ting);
        Beep(la,pai);
        Sleep(ting);
        Beep(do1,pai);
        Sleep(ting);
        Beep(so,3*pai);
        Sleep(ting+ban);
        Beep(mi,ban);
        Sleep(ting/2);
        Beep(fa,pai);
        Sleep(ting);
        Beep(do1,ban);
        Beep(si,ban);
        Sleep(20);
        Beep(si,pai);
        Sleep(ting);
        Beep(do1,pai);
        Sleep(ting);
        Beep(re1,ban);
        Sleep(20);
        Beep(re1,ban);
        Sleep(20);
        Beep(mi1,ban);
        Sleep(ting/2);
        Beep(do1,pai);
        Sleep(ting+pai);
        Beep(do1,pai);
        Beep(si,ban);
        Sleep(ting);
        Beep(la,ban);
        Sleep(20);
        Beep(la,ban);
        Sleep(ting);
        Beep(si,pai);
        Sleep(ting);
        Beep(sso,pai);
        Sleep(ting);
        Beep(sso,2*pai);
        Sleep(ting+pai);
        Beep(do1,ban);
        Beep(re1,ban);
        Sleep(ting);
        Beep(mi1,pai+ban);
        Beep(re1,ban);
        Sleep(ting);
        Beep(mi1,pai);
        Sleep(ting);
        Beep(fa1,pai);
        Sleep(ting);
        Beep(re1,2*pai);
        Sleep(pai+ting);
        Beep(so,ban);
        Sleep(20);
        Beep(so,ban);
        Sleep(ting);
        Beep(do1,ban);
        Beep(si,ban);
        Sleep(ting);
        Beep(do1,pai);
        Sleep(ting);
        Beep(mi1,pai);
        Sleep(ting);
        Beep(mi1,2*pai);
        Sleep(ting+2*pai);
        Beep(la,ban);
        Beep(si,ban);
        Sleep(ting);
        Beep(do1,pai);
        Sleep(ting);
        Beep(si,pai);
        Sleep(ting);
        Beep(re1,ban);
        Sleep(20);
        Beep(re1,ban);
        Sleep(ting);
        Beep(do1,pai+ban);
        Beep(so,ban);
        Sleep(20);
        Beep(so,pai);
        Sleep(pai+ting);
        Beep(fa1,pai);
        Sleep(ting);
        Beep(mi1,pai);
        Sleep(ting);
        Beep(re1,pai);
        Sleep(ting);
        Beep(do1,pai);
        Sleep(ting);
        Beep(mi1,4*pai);
        Beep(mi1,pai*2);
        Sleep(pai+ting);
        Beep(mi1,pai);
        Sleep(ting);
        Beep(la1,2*pai);
        Sleep(ting);
        Beep(so1,pai);
        Sleep(ting);
        Beep(so1,pai);
        Sleep(ting);
        Beep(mi1,ban);
        Sleep(ting/2);
        Beep(re1,ban);
        Sleep(ting);
        Beep(do1,pai);
        Sleep(ting+ban);
        Beep(do1,ban);
        Sleep(ting);
        Beep(re1,pai);
        Sleep(ting);
        Beep(do1,ban);
        Beep(re1,ban);
        Sleep(20);
        Beep(re1,ban);
        Sleep(ting);
        Beep(so1,pai);
        Sleep(ting); 
        Beep(mi1,2*pai);
        Sleep(ting+pai);
        Beep(mi,pai);
        Sleep(ting);
        Beep(la1,2*pai);
        Sleep(ting);
        Beep(so1,2*pai);
        Sleep(ting);
        Beep(mi1,ban);
        Beep(re1,ban);
        Sleep(ting);
        Beep(do1,2*pai);
        Sleep(ting+ban);
        Beep(do1,ban);
        Sleep(ting);
        Beep(re1,pai);
        Sleep(ting);
        Beep(do1,ban);
        Beep(re1,ban);
        Sleep(20);
        Beep(re1,ban);
        Sleep(ting);
        Beep(si,pai);
        Sleep(ting);
        Beep(la,2*pai);
        Sleep(ting);
        Beep(la,ban);
        Beep(si,ban);
        Beep(do1,pai+ban);
        Beep(si,ban);
        Sleep(ting);
        Beep(do1,pai);
        Sleep(ting);
        Beep(mi1,pai);
        Sleep(ting);
        Beep(si,3*pai);
        Sleep(ting);
        Beep(mi,ban);
        Beep(mi,ban);
        Beep(la,ban+pai);
        Beep(so,ban);
        Sleep(ting);
        Beep(la,pai);
        Sleep(ting);
        Beep(do1,pai);
        Sleep(ting);
        Beep(so,2*pai);
        Sleep(ting);
        Sleep(pai);
        Beep(mi,ban);
        Sleep(ting/2);
        Beep(mi,ban);
        Sleep(ting/2);
        Beep(fa,pai+ban);
        Beep(mi,ban);
        Sleep(ting);
        Beep(fa,ban);
        Beep(do1,ban+pai);
        Sleep(ting);
        Beep(mi,2*pai);
        Sleep(ting);
        Sleep(ban);
        Beep(do1,ban);
        Sleep(ting/2);
        Beep(do1,ban);
        Sleep(ting/2);
        Beep(do1,ban);
        Sleep(ting/2);
        Beep(si,ban+pai);
        Beep(sfa,ban);
        Sleep(ting);
        Beep(sfa,pai);
        Beep(si,pai);
        Sleep(ting);
        Beep(si,2*pai);
        Sleep(ting);
        Sleep(pai);
        Beep(la,ban);
        Beep(si,ban);
        Sleep(ting);
        Beep(do1,pai+ban);
        Beep(si,ban);
        Sleep(ting);
        Beep(do1,pai);
        Sleep(ting);
        Beep(mi1,pai);
        Sleep(ting);
        Beep(si,2*pai);
        Sleep(ting);
        Sleep(pai);
        Beep(mi,ban);
        Sleep(20);
        Beep(mi,ban);
        Sleep(ting);
        Beep(la,pai+ban);
        Beep(so,ban);
        Sleep(ting);
        Beep(la,pai);
        Sleep(ting);
        Beep(do1,pai);
        Sleep(ting); 
        Beep(so,3*pai);
        Sleep(ting+ban);
        Beep(mi,ban);
        Sleep(ting/2); 
        Beep(fa,pai);
        Sleep(ting);
        Beep(do1,ban);
        Beep(si,ban);
        Sleep(20);
        Beep(si,pai);
        Sleep(ting);
        Beep(do1,pai);
        Sleep(ting);     
        Beep(re1,ban);
        Sleep(20);
        Beep(re1,ban);
        Sleep(20);
        Beep(mi1,ban);
        Sleep(ting/2);
        Beep(do1,pai);
        Sleep(ting+pai);
        Beep(la,4*pai);
        Sleep(1000);
    }
    cout<<"\n\n\n\n\n完毕!";
    return 0;
}

还有好多好多,我就不全放出来了

0
包涵宇
包涵宇
中级天翼
中级天翼

dddddddddddddddddddddddddddddddddddddddddddddddddd...........................................

0
张恩泽
张恩泽
高级天翼
高级天翼

我看到像这样的代码第一个念头就是复制,让我改,呵呵......

0
0
王子健
王子健
初级天翼
初级天翼

包大佬求你采纳我,谢谢,我可以给你贪吃蛇代码,QQ上

0
赵逸凡
赵逸凡
初级启示者
初级启示者

Beep函数与过程在一起,不卡才怪

0
董子墨
董子墨
中级天翼
中级天翼

建议你还是用另一种方法吧

1.把你代码里的贪吃蛇删掉

2.建一个文件夹,把你的代码放在文件夹里

3.上网搜贪吃蛇C++代码,也放在文件夹里,命名1个英文名,如:snake。然后运行贪吃蛇程序(很重要!必须要有exe文件)

4.在你代码里的贪吃蛇选项里,运行一条语句:system("贪吃蛇的文件名.exe"); 如:system("snake.exe");

P.S.贪吃蛇那么复杂,你做扫雷游戏他不香吗?

0
贺晓雨
贺晓雨
资深守护
资深守护

J3蒟蒻在此……

我是上了个假J3嘛

看不懂蟹蟹

0
王俊杰
王俊杰
高级光能
高级光能

游戏代码大佬,膜拜膜拜,顶级大佬

0
王子凡
王子凡
高级光能
高级光能

emm完全看不懂,你是大佬我们是蒟蒻

0
0
0
被禁言 姜思远
姜思远
初级光能
初级光能

哇~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~一贴更新3天,膜拜大佬

游戏不错,香~~

0
李瑞曦
李瑞曦
高级天翼
高级天翼

我是蒟蒻,不会改啊~555

0
武建豪
武建豪
中级天翼
中级天翼

说真的,贪吃蛇不好玩

0
0
张天璨
张天璨
新手天翼
新手天翼

(⊙o⊙)哦!长见识了!膜拜大佬!求采纳!

0
张天璨
张天璨
新手天翼
新手天翼

 献上我的溅落的代码:

 

#include <windows.h>  
#include <stdlib.h>  
#include <conio.h>  
#include <time.h>  
#include <cstring>  
#include <cstdio>  
#include <iostream>  
#define  N 22  
using namespace std;  

    int gameover;  

    int x1, y1; // 随机出米  

    int x,y;  

    long start;  

//=======================================  
//类的实现与应用initialize  
//=======================================  

//下面定义贪吃蛇的坐标类  
class snake_position  
{  
public:  

    int x,y;      //x表示行,y表示列  

    snake_position(){};  

    void initialize(int &);//坐标初始化  


};  

snake_position position[(N-2)*(N-2)+1]; //定义贪吃蛇坐标类数组,有(N-2)*(N-2)个坐标  

void snake_position::initialize(int &j)  
{  
        x = 1;  

        y = j;  
}  


//下面定义贪吃蛇的棋盘图  

class snake_map  
{  

private:  

    char s[N][N];//定义贪吃蛇棋盘,包括墙壁。  

    int grade, length;  

    int gamespeed; //前进时间间隔  

    char direction; // 初始情况下,向右运动  

    int head,tail;  

    int score;  

    bool gameauto;  

public:  

    snake_map(int h=4,int t=1,int l=4,char d=77,int s=0):length(l),direction(d),head(h),tail(t),score(s){}  

    void initialize();   //初始化函数  

    void show_game();  

    int updata_game();  

    void setpoint();  

    void getgrade();  

    void display();  


};  

//定义初始化函数,将贪吃蛇的棋盘图进行初始化  

void snake_map::initialize()  
{  
    int i,j;  

    for(i=1;i<=3;i++)  

        s[1][i] = '*';  

    s[1][4] = '#';  

    for(i=1;i<=N-2;i++)  

        for(j=1;j<=N-2;j++)  

            s[i][j]=' '; // 初始化贪吃蛇棋盘中间空白部分  

    for(i=0;i<=N-1;i++)  

        s[0][i] = s[N-1][i] = '-'; //初始化贪吃蛇棋盘上下墙壁  

    for(i=1;i<=N-2;i++)  

        s[i][0] = s[i][N-1] = '|'; //初始化贪吃蛇棋盘左右墙壁  
}  


//============================================  
//输出贪吃蛇棋盘信息  

void snake_map::show_game()  

{  

    system("cls"); // 清屏  

    int i,j;  

    cout << endl;  

    for(i=0;i<N;i++)  
    {  

        cout << '\t';  

        for(j=0;j<N;j++)  

            cout<<s[i][j]<<' '; // 输出贪吃蛇棋盘  

        if(i==2) cout << "\t等级:" << grade;  

        if(i==6) cout << "\t速度:" << gamespeed;  

        if(i==10) cout << "\t得分:" << score << "分" ;  

        if(i==14) cout << "\t暂停:按一下空格键" ;  

        if(i==18) cout << "\t继续:按两下空格键" ;  

        cout<<endl;  
    }  
}  

//输入选择等级  
void snake_map::getgrade()  
{  
    cin>>grade;  

    while( grade>7 || grade<1 )  
    {  
        cout << "请输入数字1-7选择等级,输入其他数字无效" << endl;  

        cin >> grade;  
    }  
    switch(grade)  
    {  
        case 1: gamespeed = 1000;gameauto = 0;break;  

        case 2: gamespeed = 800;gameauto = 0;break;  

        case 3: gamespeed = 600;gameauto = 0;break;  

        case 4: gamespeed = 400;gameauto = 0;break;  

        case 5: gamespeed = 200;gameauto = 0;break;  

        case 6: gamespeed = 100;gameauto = 0;break;  

        case 7: grade = 1;gamespeed = 1000;gameauto = 1;break;  

    }  

}  

//输出等级,得分情况以及称号  

void snake_map::display()  
{  

    cout << "\n\t\t\t\t等级:" << grade;  

    cout << "\n\n\n\t\t\t\t速度:" << gamespeed;  

    cout << "\n\n\n\t\t\t\t得分:" << score << "分" ;  

}  

//随机产生米  
void snake_map::setpoint()  
{  
    srand(time(0));  

    do  
    {  

        x1 = rand() % (N-2) + 1;  

        y1 = rand() % (N-2) + 1;  

    }while(s[x1][y1]!=' ');  

    s[x1][y1]='*';  
}  

char key;  

int snake_map::updata_game()  
{  
    gameover = 1;  

    key = direction;  

    start = clock();  

    while((gameover=(clock()-start<=gamespeed))&&!kbhit());  

    //如果有键按下或时间超过自动前进时间间隔则终止循环  



        if(gameover)  
        {  

            getch();  

            key = getch();  
        }  

        if(key == ' ')  

        {  
            while(getch()!=' '){};//这里实现的是按空格键暂停,按空格键继续的功能,但不知为何原因,需要按两下空格才能继续。这是个bug。  
        }  

        else  

            direction = key;  

        switch(direction)  
        {  
            case 72: x= position[head].x-1; y= position[head].y;break; // 向上  

            case 80: x= position[head].x+1; y= position[head].y;break; // 向下  

            case 75: x= position[head].x; y= position[head].y-1;break; // 向左  

            case 77: x= position[head].x; y= position[head].y+1; // 向右  

        }  

        if(!(direction==72||direction==80||direction==75 ||direction==77))   // 按键非方向键  

            gameover = 0;  

        else if(x==0 || x==N-1 ||y==0 || y==N-1)   // 碰到墙壁  

            gameover = 0;  

        else if(s[x][y]!=' '&&!(x==x1&&y==y1))    // 蛇头碰到蛇身  

            gameover = 0;  

        else if(x==x1 && y==y1)  

        { // 吃米,长度加1  

            length ++;  

            if(length>=8 && gameauto)  

            {  

                length -= 8;  

                grade ++;  

                if(gamespeed>=200)  

                    gamespeed -= 200; // 改变贪吃蛇前进速度  

                else  

                    gamespeed = 100;  

            }  

            s[x][y]= '#';  //更新蛇头  

            s[position[head].x][position[head].y] = '*'; //吃米后将原先蛇头变为蛇身  

            head = (head+1) % ( (N-2)*(N-2) );   //取蛇头坐标  

            position[head].x = x;  

            position[head].y = y;  

            show_game();  

            gameover = 1;  

            score += grade*20;  //加分  

            setpoint();   //产生米  

        }  

        else  
        { // 不吃米  

            s[position[tail].x][position[tail].y]=' ';//将蛇尾置空  

            tail= (tail+1) % ( (N-2) * (N-2) );//更新蛇尾坐标  

            s[position[head].x][position[head].y]='*';  //将蛇头更为蛇身  

            head= (head+1) % ( (N-2) * (N-2) );  

            position[head].x = x;  

            position[head].y = y;  

            s[position[head].x][position[head].y]='#'; //更新蛇头  

            gameover = 1;  

        }  
    return gameover;  

}  
//====================================  
//主函数部分  
//====================================  
int main()  
{  
    MessageBox(NULL,"欢迎使用由张天璀璨制作的贪吃蛇游戏~祝您愉快","贪吃蛇",MB_OK);
    char ctn = 'y';  

    int nodead;  

    cout<<"\n\n\n\n\n\t\t\t 欢迎进入贪吃蛇游戏!"<<endl;//欢迎界面;  

    cout<<"\n\n\n\t\t\t 按任意键马上开始。。。"<<endl;//准备开始;;  

    getch();  

    while( ctn=='y' )  
    {  
        system("cls"); // 清屏  

        snake_map snake;  

        snake.initialize();  

        cout << "\n\n请输入数字选择游戏等级:" << endl;  

        cout << "\n\n\n\t\t\t1.等级一:速度 0 \n\n\t\t\t2.等级二:速度 1 \n\n\t\t\t3.等级三:速度 2 ";  

        cout << "\n\n\t\t\t4.等级四:速度 3 \n\n\t\t\t5.等级五:速度 4 \n\n\t\t\t6.等级六:速度 5 \n\n\t\t\t7.自动升级模式" << endl;  

        snake.getgrade();//获取等级  

        for(int i=1;i<=4;i++)  
        {  
            position[i].initialize(i);//初始化坐标  
        }  

        snake.setpoint();  // 产生第一个米  

        do  
        {  
            snake.show_game();  

            nodead = snake.updata_game();  

        }while(nodead);  

        system("cls"); //清屏  



        cout << "\n\n\n\t\t\t\tGame over!\n\n"<<endl;  

        snake.display();//输出等级/得分情况  

        cout << "\n\n\n\t\t    是否选择继续游戏?输入 y 继续,n 退出" << endl;  

        cin >> ctn;  

    }  

    return 0;  
} 

狗头求采纳!

我要回答