自己第一個控制檯的遊戲——貪喫蛇

一直想自己寫個遊戲玩玩,好像很厲害的樣子,終於今天下定決心寫了個最經典的休閒的小遊戲——貪喫蛇,當然也有借鑑別人的程序,但是整個代碼都是和別人不一樣的,直接上代碼吧:

#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;
}
程序寫的有點長,但是可以完美運行了

希望大家多多指教。

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


 


 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章