首页 > 代码库 > 自己第一个控制台的游戏——贪吃蛇

自己第一个控制台的游戏——贪吃蛇

一直想自己写个游戏玩玩,好像很厉害的样子,终于今天下定决心写了个最经典的休闲的小游戏——贪吃蛇,当然也有借鉴别人的程序,但是整个代码都是和别人不一样的,直接上代码吧:

#include <conio.h>
#include <iostream>
#include <vector>
#include <time.h>
using namespace std;

#define ROW 22
#define COL 22
struct Point
{
	char ch;
	int x;
	int y;
};

void Refresh(vector<Point> vec1,vector<Point> vec2,Point pt,int grade,int speed);
void move(vector<Point> &vec,int dir,int touch,bool &flag);
Point produce_food(vector<Point> vec);
bool IsGameOver(vector<Point> vec1,vector<Point> vec2);
void main()
{
	cout<<endl<<endl<<endl;
	cout<<"\t"<<"游戏即将开始!";
	int start=clock();
	while(clock()-start<=1000);
	for(int i=3;i>=0;i--)
	{
		start=clock();
		while(clock()-start<=1000);
		system("cls");
		cout<<endl<<endl;
		cout<<"\t"<<i<<endl;
	}
	Point pt;
	vector<Point> snake;
	int length=1,head=3,tail=0;
	for(int i=0;i<3;i++)
	{
		pt.ch='*';
		pt.x=1;
		pt.y=i+1;
		snake.push_back(pt);
	}
	pt.ch='#';
	pt.x=1;pt.y=4;
	snake.push_back(pt);
	
	vector<Point> env;
	for(int i=0;i<ROW;i++)
	{
		for(int j=0;j<COL;j++)
		{
			if(i==0 || i==ROW-1)
			{
				pt.ch='-';pt.x=i;pt.y=j;
				env.push_back(pt);
			}
			if(i!=0 && i!=ROW-1)
			{
				if(j==0 || j==COL-1)
				{
					pt.ch='|';pt.x=i;pt.y=j;
					env.push_back(pt);
				}
				else
				{
					pt.ch=' ';pt.x=i;pt.y=j;
					env.push_back(pt);
				}
			}
		}
	}
	int direction=77;
	int speed=500,grade=1;
	int timeover=1;
	int touch=0;
	Point food;	
	srand(time(0));
	food=produce_food(env);
	bool flag=true;
	while(1)
	{
		touch=0;
		start=clock();
		while((timeover=(clock()-start<=speed)) && !kbhit());
		if(timeover) {getch();direction=getch();}
		if(food.x==(snake.end()-1)->x && food.y==(snake.end()-1)->y) 
		{
			touch=1;length++;
			food=produce_food(env);
		}
		if(length>=8) {length-=8;grade++;speed-=50;}
		move(snake,direction,touch,flag);
		if(IsGameOver(env,snake) && flag)
		{
			Refresh(env,snake,food,grade,speed);
		}
		else
		{
			cout<<"game over"<<endl;
			exit(1);
		}
			
	}
}

void Refresh(vector<Point> vec1,vector<Point> vec2,Point pt ,int grade,int speed)
{
	system("cls");
	for(vector<Point>::iterator ite2=vec2.begin();ite2<vec2.end();ite2++)
	{
		for(vector<Point>::iterator ite1=vec1.begin();ite1<vec1.end();ite1++)
		{
			if(ite1->x==ite2->x && ite1->y==ite2->y) ite1->ch=ite2->ch;
		}
	}
	for(vector<Point>::iterator ite=vec1.begin();ite<vec1.end();ite++)
	{
		if(ite->x==pt.x && ite->y==pt.y) ite->ch='$';
		cout<<ite->ch<<' ';
		if(ite->x==4 && ite->y==COL-1) cout<<"等级为:"<<grade;
		if(ite->x==6 && ite->y==COL-1) cout<<"时间间隔:"<<speed;
		if(ite->x==8 && ite->y==COL-1) cout<<"开发者:dapeng dai";
		if(ite->y==COL-1) cout<<endl;
		
	}
	
}
void move(vector<Point> &vec,int dir,int touch,bool &flag)
{
	Point pt;
	vector<Point>::iterator ite=vec.end()-1;
	switch(dir)
	{
	case 77:
		ite->ch='*';
		pt.x=(ite->x);pt.y=(ite->y)+1;pt.ch='#';
		for(vector<Point>::iterator it=vec.begin();it<vec.end();it++)
		{
			if(it->x==pt.x && it->y==pt.y) flag=false;
		}
		vec.push_back(pt);
		if(!touch) vec.erase(vec.begin());
		break;
	case 75:
		ite->ch='*';
		pt.x=(ite->x);pt.y=(ite->y)-1;pt.ch='#';
		for(vector<Point>::iterator it=vec.begin();it<vec.end();it++)
		{
			if(it->x==pt.x && it->y==pt.y) flag=false;
		}
		vec.push_back(pt);
		if(!touch) vec.erase(vec.begin());
		break;
	case 72:
		ite->ch='*';
		pt.x=(ite->x)-1;pt.y=(ite->y);pt.ch='#';
		for(vector<Point>::iterator it=vec.begin();it<vec.end();it++)
		{
			if(it->x==pt.x && it->y==pt.y) flag=false;
		}
		vec.push_back(pt);
		if(!touch) vec.erase(vec.begin());
		break;
	case 80:
		ite->ch='*';
		pt.x=(ite->x)+1;pt.y=(ite->y);pt.ch='#';
		for(vector<Point>::iterator it=vec.begin();it<vec.end();it++)
		{
			if(it->x==pt.x && it->y==pt.y) flag=false;
		}
		vec.push_back(pt);
		if(!touch) vec.erase(vec.begin());
		break;
	default:
		break;
	}
}
Point produce_food(vector<Point> vec)
{
	int x=0,y=0;
	do 
	{
		x=rand()%(ROW-2)+1;
		y=rand()%(COL-2)+1;
	} while ((vec.begin()+(ROW*x+y))->ch!=' ');
	Point pt;
	pt.x=x;pt.y=y;pt.ch='$';
	return pt;
}

bool IsGameOver(vector<Point> vec1,vector<Point> vec2)
{
	if((vec2.end()-1)->x==0 ||(vec2.end()-1)->x==ROW-1 || (vec2.end()-1)->y==0 || (vec2.end()-1)->y==COL-1)
	{
		return false;
	}
	return true;
}
程序写的有点长,但是可以完美运行了

希望大家多多指教。