C++贪吃蛇+闯关

C++有关卡de贪吃蛇

以前我发布了一篇博客,是我第一次做的贪吃蛇,有一些小bug,详

这是新的,增加了一个关卡模块:

#include<bits/stdc++.h>
#include<conio.h>
#include<windows.h>
using namespace std;
int asn,vb=3,endx,endy,foodx,n[250]={15,16,17},m[250]={34,34,34},foody,tim=170,v[30][100],j,i,x=3;
char d,df;
bool bz,c;
float opo=70;
void go(int x,int y)
{
    COORD pos={y,x};
    HANDLE hOut=GetStdHandle(STD_OUTPUT_HANDLE);
    SetConsoleCursorPosition(hOut,pos);
}
void cv(unsigned int ForeColor=7,unsigned int BackGroundColor=0)
{
	HANDLE handle=GetStdHandle(STD_OUTPUT_HANDLE);
	SetConsoleTextAttribute(handle,ForeColor+BackGroundColor*0x10);
}
void win()
{
	system("cls");
	go(14,40);
	cout<<"通关成功!记得截屏留念哦~";
}
void ziji()
{
	system("cls");
	go(14,40);
	cout<<"吃 到 自 己 !游 戏 结 束 !";
	go(16,45);
	if(asn==2) cout<<"失败!Defeat"; 
	if(asn==1)
	{
		go(16,48);
		cout<<"得 分 :"<<x;
		go(18,45);	
		int ji;
		FILE*fw=fopen("D:\\贪吃蛇的存储数据.txt","r");
		fscanf(fw,"%d",&ji);
		if(x>ji)
		cout<<"历 史 最 高 :"<<x<<" 分 ( 破 记 录 !)";
		else
		cout<<"历 史 最 高 :"<<ji<<" 分";
		if(x>ji)
		{
	 		FILE *outFile=fopen("D:\\贪吃蛇的存储数据.txt", "w");
	    	fprintf(outFile, "%d ",x);
	   	 	fclose(outFile);
		}
	}
	go(27,0);
	system("pause"); 
}
void prinf()
{
	if(asn==1)
	{
		if(tim<=140&&tim>=101)
		{
			cv(6,5);
			go(4,103);
			cout<<"  ";
			go(4,103);
			cout<<"中";
		}
		if(tim<=100&&tim>=65)
		{
			cv(6,5);
			go(4,103);
			cout<<"  ";
			go(4,103);
			cout<<"高";
		}
		if(tim<=64&&tim>=15) 
		{
			cv(6,5);
			go(4,103);
			cout<<"  ";
			go(4,103);
			cout<<"大";
			go(5,103);
			cout<<"   ";
			go(5,103);
			cout<<"师";
			go(6,103);
			cout<<"级"; 
		}
		if(tim<15)
		{
			system("cls");
			go(14,42);
			cout<<"恭 喜 !游 戏 成 功 !";
		}
		if(x%3==0&&x!=vb&&x!=3) tim-=12;
	}
	if(asn==2)
	{
		go(2,103);
		cout<<"  ";
		go(2,103);
		cout<<opo;
	}
	cv(1,7);
	for(i=0;i<30;i++)
	{
		for(j=0;j<99;j++)
		{
			if(v[i][j]==1)
			{
				go(i,j);
				cout<<"█";
			}
		}
	} 
	go(29,102);
}
void wei()
{
	v[n[x-1]][m[x-1]]=0;
	go(n[x-1],m[x-1]);
	cv(0,7);
	cout<<"  ";
}
void food(int x)
{
	if(asn==1)
	{
		srand(time(NULL));
		
		for(int ll=0;;ll++)
		{
			foodx=rand()%28+0;
			foody=rand()%95+0;
			if(foody%2==0) foody++;
			c=false;
			for(int l=0;l<x;l++)
			{
				if(n[l]==foodx&&m[l]==foody-1) 
				{
					c=true;
					break;
				}
			}
			if(c==false) break;
		}
		
		v[foodx][foody-1]=2;
		go(foodx,foody-1);
		cv(4,7);
		cout<<"●";
	}
	else
	{
		int cx,cy;
		srand(time(NULL));
		for(int ii=0;ii<x;ii++)
		{
			cx=rand()%28+0;
			cy=rand()%95+0;
			while(v[cx][cy]!=0)
			{
				srand(time(NULL));
				cx=rand()%28+0;
			    cy=rand()%95+0;
			}
			if(cy%2==0) cy++;
			if(v[cx][cy]!=0)
			{
				srand(time(NULL));
				cx=rand()%28+0;
				cy=rand()%95+0;
			}
			if(cy%2==0) cy++;
			v[cx][cy-1]=2;
			go(cx,cy-1);
			cv(4,7);
			cout<<"●";
		}
		go(29,3);
		cout<<"地图生成完毕!按方向键开始...";
	}
}
void qiang()
{
	system("cls");
	go(14,43);
	cout<<"撞 墙 !游 戏 结 束 !";
	go(16,46);
	if(asn==2) cout<<"失败!Defeat"; 
	if(asn==1)
	{
		go(16,49);
		cout<<"得 分 :"<<x;
		go(18,45);
		int ji;
		FILE*fw=fopen("D:\\贪吃蛇的存储数据.txt","r");
		fscanf(fw,"%d",&ji);
		if(x>ji)
		cout<<"历 史 最 高 :"<<x<<" 分 ( 破 记 录 !)";
		else
		cout<<"历 史 最 高 :"<<ji<<" 分";
		if(x>ji)
		{
	 		FILE *outFile=fopen("D:\\贪吃蛇的存储数据.txt", "w");
	    	fprintf(outFile, "%d ",x);
	   	 	fclose(outFile);
		}
	}
	go(27,0);
	system("pause");
}
int guan(int w)
{
	while(1)
	{
		while(kbhit()==0)
		{
			switch(df)
			{
			case 72:
				wei();
				endx=n[x-1];
				endy=m[x-1];
				for(int qwe=x-1;qwe>=1;qwe--) n[qwe]=n[qwe-1];
				for(int qwe=x-1;qwe>=1;qwe--) m[qwe]=m[qwe-1];
				if(v[n[0]-1][m[0]]==2)
				{
					if(w==1)
					{
						win();
						return 0;
					}
					else w--;
				}
				if(n[0]-1<0)
				{
					qiang();bz=true;
					return 0;
				}
				if(v[n[0]-1][m[0]]==1)
				{
					ziji();bz=true;
					return 0;
				 }
				v[--n[0]][m[0]]=1;
				break;
			case 80:
				wei();
				endx=n[x-1];
				endy=m[x-1];
				for(int qwe=x-1;qwe>=1;qwe--) n[qwe]=n[qwe-1];
				for(int qwe=x-1;qwe>=1;qwe--) m[qwe]=m[qwe-1];
				if(v[n[0]+1][m[0]]==2)
				{
					if(w==1)
					{
						win();
						return 0;
					}
					else w--;
				}
				if(n[0]==27)
				{
					qiang();bz=true;
					return 0;
				}
				if(v[n[0]+1][m[0]]==1)
				{
					ziji();bz=true;
					return 0;
				}
				v[++n[0]][m[0]]=1;
				break;
			case 75:
				wei();
				endx=n[x-1];
				endy=m[x-1];
				for(int qwe=x-1;qwe>=1;qwe--) n[qwe]=n[qwe-1];
				for(int qwe=x-1;qwe>=1;qwe--) m[qwe]=m[qwe-1];
				if(v[n[0]][m[0]-2]==2)
				{
					if(w==1)
					{
						win();
						return 0;
					 } 
					else w--;
				}
				if(m[0]-1<0)
				{
					qiang();bz=true;
					return 0;
				}
				if(v[n[0]][m[0]-2]==1)
				{
					ziji();bz=true;
					return 0;
				 }
				m[0]-=2;
				v[n[0]][m[0]]=1;
				break;
			case 77:
				wei();
				endx=n[x-1];
				endy=m[x-1];
				for(int qwe=x-1;qwe>=1;qwe--) n[qwe]=n[qwe-1];
				for(int qwe=x-1;qwe>=1;qwe--) m[qwe]=m[qwe-1];
				if(v[n[0]][m[0]+2]==2)
				{
					if(w==1)
					{
						win();
						return 0;
					 } 
					else w--;
				}
				if(m[0]+2==100)
				{
					qiang();bz=true;
					return 0;
				}
				if(v[n[0]][m[0]+2]==1)
				{
					ziji();bz=true;
					return 0;
				}
				m[0]+=2;
				v[n[0]][m[0]]=1;
				break;
			}
			opo-=0.25;
			prinf();
			Sleep(150);
			
			if(opo==0)
			{
				bz=true;
				system("cls");
				go(15,48);
				cout<<"时间到!";
				go(16,44);
				cout<<"失败!Defeat!";
				return 0;
			}
		}
		if(w==0) break;
		d=getch();
		df=d;
		d=0;
	}
	return 0;
}
int main()
{
	system("color 71");
	cout<<"请选择模式:\n经典模式输入1    闯关模式输入2\n";
	cin>>asn;
	if(asn==1)
	{
		system("cls"); 
		cv(6,5);
		go(1,103);
		cout<<"级";
		go(2,103);
		cout<<"别";
		go(3,103);
		cout<<"  ";
		go(4,103);
		cout<<"初";
		go(5,103);
		cout<<"级";
		for(i=0;i<=100;i++) 
		{
			cv(1,2);
			go(28,i);
			cout<<" ";
		}
		for(i=0;i<=28;i++)
		{
			cv(1,2);
			go(i,100);
			cout<<"  ";
		}
		v[17][34]=1;
		v[16][34]=1;
		v[15][34]=1;
		for(i=0;i<29;i++)
		{
			for(j=0;j<95;j++)
			{
				if(v[i][j]==1) 
				{
					cv(3,7);
					go(i,j);
					cout<<"█";
				}
			}
		}
		go(15,34);
		food(0);
		while(1)
		{
			while(kbhit()==0)
			{
				vb=x;
				switch(df)
				{
				case 72:
					if(v[n[0]-1][m[0]]!=2)
					wei();
					endx=n[x-1];
					endy=m[x-1];
					for(int qwe=x-1;qwe>=1;qwe--) n[qwe]=n[qwe-1];
					for(int qwe=x-1;qwe>=1;qwe--) m[qwe]=m[qwe-1];
					if(v[n[0]-1][m[0]]==2)
					{
						x++;
						n[x-1]=endx;
						m[x-1]=endy;
						food(0);
					}
					if(n[0]-1<0) 
					{
						qiang();
						
						return 0;
					}
					if(v[n[0]-1][m[0]]==1)
					{
						ziji();
						return 0;
					 }
					v[--n[0]][m[0]]=1;
					break;
				case 80:
					if(v[n[0]+1][m[0]]!=2)
					wei();
					endx=n[x-1];
					endy=m[x-1];
					for(int qwe=x-1;qwe>=1;qwe--) n[qwe]=n[qwe-1];
					for(int qwe=x-1;qwe>=1;qwe--) m[qwe]=m[qwe-1];
					if(v[n[0]+1][m[0]]==2)
					{
						x++;
						n[x-1]=endx;
						m[x-1]=endy;
						food(0);
					}
					if(n[0]==27)
					{
						qiang();
						return 0;
						return 0;
					}
					if(v[n[0]+1][m[0]]==1)
					{
						ziji();
						return 0;
						return 0;
					 }
					v[++n[0]][m[0]]=1;
					break;
				case 75:
					if(v[n[0]][m[0]-2]!=2)
					wei();
					endx=n[x-1];
					endy=m[x-1];
					for(int qwe=x-1;qwe>=1;qwe--) n[qwe]=n[qwe-1];
					for(int qwe=x-1;qwe>=1;qwe--) m[qwe]=m[qwe-1];
					if(v[n[0]][m[0]-2]==2)
					{
						x++;
						n[x-1]=endx;
						m[x-1]=endy;
						food(0);
					}
					if(m[0]-1<0)
					{
						qiang();
						return 0;
						return 0;
					}
					if(v[n[0]][m[0]-2]==1)
					{
						ziji();
						return 0;
						return 0;
					 }
					m[0]-=2;
					v[n[0]][m[0]]=1;
					break;
				case 77:
					if(v[n[0]][m[0]+2]!=2)
					wei();
					endx=n[x-1];
					endy=m[x-1];
					for(int qwe=x-1;qwe>=1;qwe--) n[qwe]=n[qwe-1];
					for(int qwe=x-1;qwe>=1;qwe--) m[qwe]=m[qwe-1];
					if(v[n[0]][m[0]+2]==2)
					{
						x++;
						n[x-1]=endx;
						m[x-1]=endy;
						food(0);
					}
					if(m[0]+2==100)
					{
						qiang();
						return 0;
					}
					if(v[n[0]][m[0]+2]==1)
					{
						ziji();
						return 0;
					 }
					m[0]+=2;
					v[n[0]][m[0]]=1;
					break;
				}
				prinf();
				Sleep(tim);
			}
			d=getch();
			df=d;
			d=0;
		}
	}
	if(asn==2)
	{
			system("cls");
			int x,z;
			cout<<"选择关卡:\nno.1  no.2  no.3  no.4\nno.5  no.6  no.7  no.8\n";
			cin>>z;
			system("cls");
			for(i=0;i<=100;i++) 
			{
				cv(1,2);
				go(28,i);
				cout<<" ";
			}
			for(i=0;i<=28;i++)
			{
				cv(1,2);
				go(i,100);
				cout<<"  ";
	     	}
	     	v[17][34]=1;
			v[16][34]=1;
			v[15][34]=1;
			for(i=0;i<29;i++)
			{
				for(j=0;j<95;j++)
				{
					if(v[i][j]==1) 
					{
						cv(3,7);
						go(i,j);
						cout<<"█";
					}
				}
			}
			go(1,103);
			cout<<"剩余时间:";
			switch(z)
			{
				case 8:
					x=50;
					food(x);
					guan(x);
					go(27,0);
					system("pause");
					return 0;
				case 7:
					x=44;
					food(x);
					guan(x);
					go(27,0);
					system("pause");
					return 0;
				case 6:
					x=38;
					food(x);
					guan(x);
					go(27,0);
					system("pause");
					return 0;
				case 5:
					x=32;
					food(x);
					guan(x);
					go(27,0);
					system("pause");
					return 0;
				case 4:
					x=27;
					food(x);
					guan(x);
					go(27,0);
					system("pause");
					return 0;
				case 3:
					x=24;
					food(x);
					guan(x);
					go(27,0);
					system("pause");
					return 0;
				case 2:
					x=19;
					food(x);
					guan(x);
					go(27,0);
					system("pause");
					return 0;
				case 1:
					x=16;
					food(x);
					guan(x);
					go(27,0);
					system("pause");
					return 0;
			}
	}
}

码罢低声问oj,画main深浅入时无

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
以下是使用VS Code和C++编写贪吃蛇的步骤: 1. 安装VS Code和MinGW - 下载并安装VS Code:https://code.visualstudio.com/ - 下载并安装MinGW:http://www.mingw.org/ 2. 配置环境变量 - 将MinGW的bin目录添加到系统环境变量中 3. 创建C++项目 - 在VS Code中创建一个新的文件夹,作为项目的根目录 - 在VS Code中打开该文件夹 - 在VS Code的终端中输入以下命令,创建一个名为main.cpp的文件: ``` touch main.cpp ``` - 在main.cpp中编写贪吃蛇的代码 4. 编写贪吃蛇代码 - 可以使用C++的图形库,如graphics.h - 也可以使用第三方图形库,如SDL、SFML等 5. 编译和运行代码 - 在VS Code的终端中输入以下命令,编译代码: ``` g++ main.cpp -o main ``` - 在VS Code的终端中输入以下命令,运行代码: ``` ./main ``` 以下是一个简单的C++贪吃蛇代码示例: ```c++ #include <iostream> #include <conio.h> #include <windows.h> using namespace std; bool gameOver; const int width = 20; const int height = 20; int x, y, fruitX, fruitY, score; int tailX[100], tailY[100]; int nTail; enum eDirection { STOP = 0, LEFT, RIGHT, UP, DOWN }; eDirection dir; void Setup() { gameOver = false; dir = STOP; x = width / 2; y = height / 2; fruitX = rand() % width; fruitY = rand() % height; score = 0; } void Draw() { system("cls"); for (int i = 0; i < width + 2; i++) cout << "#"; cout << endl; for (int i = 0; i < height; i++) { for (int j = 0; j < width; j++) { if (j == 0) cout << "#"; if (i == y && j == x) cout << "O"; else if (i == fruitY && j == fruitX) cout << "F"; else { bool print = false; for (int k = 0; k < nTail; k++) { if (tailX[k] == j && tailY[k] == i) { cout << "o"; print = true; } } if (!print) cout << " "; } if (j == width - 1) cout << "#"; } cout << endl; } for (int i = 0; i < width + 2; i++) cout << "#"; cout << endl; cout << "Score:" << score << endl; } void Input() { if (_kbhit()) { switch (_getch()) { case 'a': dir = LEFT; break; case 'd': dir = RIGHT; break; case 'w': dir = UP; break; case 's': dir = DOWN; break; case 'x': gameOver = true; break; } } } void Logic() { int prevX = tailX[0]; int prevY = tailY[0]; int prev2X, prev2Y; tailX[0] = x; tailY[0] = y; for (int i = 1; i < nTail; i++) { prev2X = tailX[i]; prev2Y = tailY[i]; tailX[i] = prevX; tailY[i] = prevY; prevX = prev2X; prevY = prev2Y; } switch (dir) { case LEFT: x--; break; case RIGHT: x++; break; case UP: y--; break; case DOWN: y++; break; default: break; } if (x > width || x < 0 || y > height || y < 0) gameOver = true; for (int i = 0; i < nTail; i++) if (tailX[i] == x && tailY[i] == y) gameOver = true; if (x == fruitX && y == fruitY) { score += 10; fruitX = rand() % width; fruitY = rand() % height; nTail++; } } int main() { Setup(); while (!gameOver) { Draw(); Input(); Logic(); Sleep(50); } return 0; } ```
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值