自己第一个控制台的游戏——贪吃蛇 (转载)

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

 

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

  1. #include <conio.h> 
  2. #include <iostream> 
  3. #include <vector> 
  4. #include <time.h> 
  5. using namespace std; 
  6.  
  7. #define ROW 22 
  8. #define COL 22 
  9. struct Point 
  10.     char ch; 
  11.     int x; 
  12.     int y; 
  13. }; 
  14.  
  15. void Refresh(vector<Point> vec1,vector<Point> vec2,Point pt,int grade,int speed); 
  16. void move(vector<Point> &vec,int dir,int touch,bool &flag); 
  17. Point produce_food(vector<Point> vec); 
  18. bool IsGameOver(vector<Point> vec1,vector<Point> vec2); 
  19. void main() 
  20.     cout<<endl<<endl<<endl; 
  21.     cout<<"\t"<<"游戏即将开始!"
  22.     int start=clock(); 
  23.     while(clock()-start<=1000); 
  24.     for(int i=3;i>=0;i--) 
  25.     { 
  26.         start=clock(); 
  27.         while(clock()-start<=1000); 
  28.         system("cls"); 
  29.         cout<<endl<<endl; 
  30.         cout<<"\t"<<i<<endl; 
  31.     } 
  32.     Point pt; 
  33.     vector<Point> snake; 
  34.     int length=1,head=3,tail=0; 
  35.     for(int i=0;i<3;i++) 
  36.     { 
  37.         pt.ch='*'
  38.         pt.x=1; 
  39.         pt.y=i+1; 
  40.         snake.push_back(pt); 
  41.     } 
  42.     pt.ch='#'
  43.     pt.x=1;pt.y=4; 
  44.     snake.push_back(pt); 
  45.      
  46.     vector<Point> env; 
  47.     for(int i=0;i<ROW;i++) 
  48.     { 
  49.         for(int j=0;j<COL;j++) 
  50.         { 
  51.             if(i==0 || i==ROW-1) 
  52.             { 
  53.                 pt.ch='-';pt.x=i;pt.y=j; 
  54.                 env.push_back(pt); 
  55.             } 
  56.             if(i!=0 && i!=ROW-1) 
  57.             { 
  58.                 if(j==0 || j==COL-1) 
  59.                 { 
  60.                     pt.ch='|';pt.x=i;pt.y=j; 
  61.                     env.push_back(pt); 
  62.                 } 
  63.                 else 
  64.                 { 
  65.                     pt.ch=' ';pt.x=i;pt.y=j; 
  66.                     env.push_back(pt); 
  67.                 } 
  68.             } 
  69.         } 
  70.     } 
  71.     int direction=77; 
  72.     int speed=500,grade=1; 
  73.     int timeover=1; 
  74.     int touch=0; 
  75.     Point food;  
  76.     srand(time(0)); 
  77.     food=produce_food(env); 
  78.     bool flag=true
  79.     while(1) 
  80.     { 
  81.         touch=0; 
  82.         start=clock(); 
  83.         while((timeover=(clock()-start<=speed)) && !kbhit()); 
  84.         if(timeover) {getch();direction=getch();} 
  85.         if(food.x==(snake.end()-1)->x && food.y==(snake.end()-1)->y)  
  86.         { 
  87.             touch=1;length++; 
  88.             food=produce_food(env); 
  89.         } 
  90.         if(length>=8) {length-=8;grade++;speed-=50;} 
  91.         move(snake,direction,touch,flag); 
  92.         if(IsGameOver(env,snake) && flag) 
  93.         { 
  94.             Refresh(env,snake,food,grade,speed); 
  95.         } 
  96.         else 
  97.         { 
  98.             cout<<"game over"<<endl; 
  99.             exit(1); 
  100.         } 
  101.              
  102.     } 
  103.  
  104. void Refresh(vector<Point> vec1,vector<Point> vec2,Point pt ,int grade,int speed) 
  105.     system("cls"); 
  106.     for(vector<Point>::iterator ite2=vec2.begin();ite2<vec2.end();ite2++) 
  107.     { 
  108.         for(vector<Point>::iterator ite1=vec1.begin();ite1<vec1.end();ite1++) 
  109.         { 
  110.             if(ite1->x==ite2->x && ite1->y==ite2->y) ite1->ch=ite2->ch; 
  111.         } 
  112.     } 
  113.     for(vector<Point>::iterator ite=vec1.begin();ite<vec1.end();ite++) 
  114.     { 
  115.         if(ite->x==pt.x && ite->y==pt.y) ite->ch='$'
  116.         cout<<ite->ch<<' '
  117.         if(ite->x==4 && ite->y==COL-1) cout<<"等级为:"<<grade; 
  118.         if(ite->x==6 && ite->y==COL-1) cout<<"时间间隔:"<<speed; 
  119.         if(ite->x==8 && ite->y==COL-1) cout<<"开发者:dapeng dai"
  120.         if(ite->y==COL-1) cout<<endl; 
  121.          
  122.     } 
  123.      
  124. void move(vector<Point> &vec,int dir,int touch,bool &flag) 
  125.     Point pt; 
  126.     vector<Point>::iterator ite=vec.end()-1; 
  127.     switch(dir) 
  128.     { 
  129.     case 77: 
  130.         ite->ch='*'
  131.         pt.x=(ite->x);pt.y=(ite->y)+1;pt.ch='#'
  132.         for(vector<Point>::iterator it=vec.begin();it<vec.end();it++) 
  133.         { 
  134.             if(it->x==pt.x && it->y==pt.y) flag=false
  135.         } 
  136.         vec.push_back(pt); 
  137.         if(!touch) vec.erase(vec.begin()); 
  138.         break
  139.     case 75: 
  140.         ite->ch='*'
  141.         pt.x=(ite->x);pt.y=(ite->y)-1;pt.ch='#'
  142.         for(vector<Point>::iterator it=vec.begin();it<vec.end();it++) 
  143.         { 
  144.             if(it->x==pt.x && it->y==pt.y) flag=false
  145.         } 
  146.         vec.push_back(pt); 
  147.         if(!touch) vec.erase(vec.begin()); 
  148.         break
  149.     case 72: 
  150.         ite->ch='*'
  151.         pt.x=(ite->x)-1;pt.y=(ite->y);pt.ch='#'
  152.         for(vector<Point>::iterator it=vec.begin();it<vec.end();it++) 
  153.         { 
  154.             if(it->x==pt.x && it->y==pt.y) flag=false
  155.         } 
  156.         vec.push_back(pt); 
  157.         if(!touch) vec.erase(vec.begin()); 
  158.         break
  159.     case 80: 
  160.         ite->ch='*'
  161.         pt.x=(ite->x)+1;pt.y=(ite->y);pt.ch='#'
  162.         for(vector<Point>::iterator it=vec.begin();it<vec.end();it++) 
  163.         { 
  164.             if(it->x==pt.x && it->y==pt.y) flag=false
  165.         } 
  166.         vec.push_back(pt); 
  167.         if(!touch) vec.erase(vec.begin()); 
  168.         break
  169.     default
  170.         break
  171.     } 
  172. Point produce_food(vector<Point> vec) 
  173.     int x=0,y=0; 
  174.     do  
  175.     { 
  176.         x=rand()%(ROW-2)+1; 
  177.         y=rand()%(COL-2)+1; 
  178.     } while ((vec.begin()+(ROW*x+y))->ch!=' '); 
  179.     Point pt; 
  180.     pt.x=x;pt.y=y;pt.ch='$'
  181.     return pt; 
  182.  
  183. bool IsGameOver(vector<Point> vec1,vector<Point> vec2) 
  184.     if((vec2.end()-1)->x==0 ||(vec2.end()-1)->x==ROW-1 || (vec2.end()-1)->y==0 || (vec2.end()-1)->y==COL-1) 
  185.     { 
  186.         return false
  187.     } 
  188.     return true
#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;
}
程序写的有点长,但是可以完美运行了

希望大家多多指教。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值