使用C++语言模拟实现贪吃蛇小游戏

需求分析:

    用C++开发一个能在编译窗口中运行的贪吃蛇小游戏,实现环境为:Microsoft visual C++6.0集成开发环境


算法及程序说明:
用类构造蛇 

class Snake   
{
public:
           int head,tail,body[200],length;
};

Snake T;
void init()       //初始化函数
{
       T.head=3;
       T.tail=0;
       memset(T.body,0,sizeof(T.body));       // memset(array,值,count)
//     此函数功能:将array数组从头到count的内容全部  设置为//这个“值”。
}

绘图实现

int map[100][100];     // 定义一个整形的二维数组做标记
char maze[100][100];         // 根据map数组的不同数值显示不同符号,   -1表示墙,1表
   //示食物,0表示空,以此绘图
for(i=1;i<20;i++)
    for(j=1;j<40;j++){
     card[k++]=i*100+j;
   }//用card数组来存障碍墙内位置的编号
random_shuffle(card,card+19*39);//打乱编号的次序,用于后//面随机生成食物
//random_shuffle(card,card+19*39)
//将card数组中前19*39个数据的顺序打乱

地图的预处理

for(i=0;i<=40;i++)
map[i][0]=map[20][i]=map[i][40]=map[0][i]=-1;
memset(maze,' ',sizeof(maze));
for(i=0;i<=20;i++)
       for(j=0;j<=40;j++){
              if(map[i][j]==-1)
              maze[i][j]='#';
              else if(map[i][j]==1)
              maze[i][j]='o';
       }
       for(i=T.tail;i!=T.head;){
              sum=T.body[i];
              maze[sum/100][sum0]='*';
              ++i;
              i%=ML;
       }

sum=T.body[(i-1+ML)%ML];
maze[sum/100][sum0]='@';

最后生成图像

for(i=0;i<=20;i++)
{
       for(j=0;j<=40;j++)
       {
              cout<<maze[i][j];
       }
       cout<<endl;
}

画面动态显示实现
主要运用system("CLS")的刷屏功能来实现画面的动态效果。
利用临时无穷循环制作刷屏时间控制

int TM=300;    //300毫秒
(TM>100)?(TM=300-T.length*10):(TM=100);
int start=clock();
while(clock()-start<=TM && !kbhit())     //kbhit()检查当前是否有键盘输入,若有则返回一个非0值,否//则返回0
{
              ;
}



蛇运动的实现(以向上为例)

if(order=='w'){
       jud=order;
       if(map[sum/100-1][sum0]!=-1){
              for(i=T.tail;i!=T.head;){                     
                     temp=T.body[i];                         
                     map[temp/100][temp0]=0;           
                     i++;
                     i%=ML;
              }
              up();
       }else{
                     system("CLS");//提示游戏结束
                     break;
       }
}

UP()函数

void up()
{
       int sum,i;
       sum=T.body[(T.head-1+ML)%ML]-100;
       if(map[sum/100][sum0]==1){
              T.length++;
              T.body[T.head++]=sum;
              T.head%=ML;
              map[sum/100][sum0]=0;
              for(i=T.tail;i!=T.head;){
                     sum=T.body[i];
                     map[sum/100][sum0]=-1;
                     i++;
                     i%=ML;
              }
                            while(1){
                     sum=getnum();
                     if(map[sum/100][sum0]==0){
                            map[sum/100][sum0]=1; break;
                     }
              }
              for(i=T.tail;i!=T.head;){
                     sum=T.body[i];
                     map[sum/100][sum0]=0;
                     i++;
                     i%=ML;
              }
       }else{
              T.body[T.head++]=sum;
              T.head%=ML;
              T.tail=(++T.tail)%ML;
       }
}


测试报告

游戏开始时,score为0;小蛇默认从左上角向右走。
每吃一个食物,蛇身长度+1,score+10。
 


当蛇头触碰到四周墙壁或触碰到蛇身时,游戏结束,显示最终得分。


当蛇吃食物的个数到达程序中设定的某个值时,挑战游戏成功。


完整代码:    》仅供参考,如有错误尽请谅解《

#include<iostream>
#include<cstdlib>//该函数主要可以提供一些函数与符号常量
#include<algorithm>//提供大量基于迭代器的非成员模版函数
#include<conio.h>//其中定义了通过控制台进行数据输入和数据输出的函数
#include<time.h>//日期和时间头文件
#include<windows.h>
#define ML 100   
using namespace std;
class Snake    //声明一个类Snake
{
public:
	int head,tail,body[200],length;
};
Snake T;			  //定义一个对象T
int map[100][100];   	//定义一个整形的二维数组做标记用
char maze[100][100];	//根据map数组的不同数值显示不同符号,以此绘图
void init()			//初始化函数
{
	T.head=3;
	T.tail=0;
//	T.length=0;
	memset(T.body,0,sizeof(T.body));
}
int card[800];
char order;		//接收指令
int getnum()
{
	static int n=0;
	n++;
	n=n%800;
	return card[n];
}

void up()
{
	int sum,i;
	sum=T.body[(T.head-1+ML)%ML]-100;
	if(map[sum/100][sum%100]==1)
	{
		T.length++;
		T.body[T.head++]=sum;
		T.head%=ML;
		map[sum/100][sum%100]=0;
		for(i=T.tail;i!=T.head;)
		{
			sum=T.body[i];
			map[sum/100][sum%100]=-1;
			i++;
			i%=ML;
		}
		while(1)
		{
			sum=getnum();
			if(map[sum/100][sum%100]==0)
			{
				map[sum/100][sum%100]=1;
				break;
			}
		}
		for(i=T.tail;i!=T.head;)
		{
			sum=T.body[i];
			map[sum/100][sum%100]=0;
			i++;
			i%=ML;
		}
	}
	else
	{
		T.body[T.head++]=sum;
		T.head%=ML;
		T.tail=(++T.tail)%ML;
	}
}
void down()
{
	int sum,i;
	sum=T.body[(T.head-1+ML)%ML]+100;
	if(map[sum/100][sum%100]==1)
	{
		T.length++;
		T.body[T.head++]=sum;
		T.head%=ML;
		map[sum/100][sum%100]=0;
		for(i=T.tail;i!=T.head;)
		{
			sum=T.body[i];
			map[sum/100][sum%100]=-1;
			i++;
			i%=ML;
		}
		while(1)
		{
			sum=getnum();
			if(map[sum/100][sum%100]==0)
			{
				map[sum/100][sum%100]=1;
				break;
			}
		}
		for(i=T.tail;i!=T.head;)
		{
			sum=T.body[i];
			map[sum/100][sum%100]=0;
			i++;
			i%=ML;
		}
	}
	else
	{
		T.body[T.head++]=sum;
		T.head%=ML;
		T.tail=(++T.tail)%ML;
	}
}
void right()
{
	int sum,i;
	sum=T.body[(T.head-1+ML)%ML]+1;
	if(map[sum/100][sum%100]==1)
	{
		T.length++;
		T.body[T.head++]=sum;
		T.head%=ML;
		map[sum/100][sum%100]=0;
		for(i=T.tail;i!=T.head;)
		{
			sum=T.body[i];
			map[sum/100][sum%100]=-1;
			i++;
			i%=ML;
		}
		while(1)
		{
			sum=getnum();
			if(map[sum/100][sum%100]==0)
			{
				map[sum/100][sum%100]=1;
				break;
			}
		}
		for(i=T.tail;i!=T.head;)
		{
			sum=T.body[i];
			map[sum/100][sum%100]=0;
			i++;
			i%=ML;
		}
	}
	else
	{
		T.body[T.head++]=sum;
		T.head%=ML;
		T.tail=(++T.tail)%ML;
	}
}
void left()
{
	int sum,i;
	sum=T.body[(T.head-1+ML)%ML]-1;
	if(map[sum/100][sum%100]==1)
	{
		T.length++;
		T.body[T.head++]=sum;
		T.head%=ML;
		map[sum/100][sum%100]=0;
		for(i=T.tail;i!=T.head;)
		{
			sum=T.body[i];
			map[sum/100][sum%100]=-1;
			i++;
			i%=ML;
		}
		while(1)
		{
			sum=getnum();
			if(map[sum/100][sum%100]==0)
			{
				map[sum/100][sum%100]=1;
				break;
			}
		}
		for(i=T.tail;i!=T.head;)
		{
			sum=T.body[i];
			map[sum/100][sum%100]=0;
			i++;
			i%=ML;
		}
	}
	else
	{
		T.body[T.head++]=sum;
		T.head%=ML;
		T.tail=(++T.tail)%ML;
	}
}
int main()
{
	memset(map,0,sizeof(map));
	memset(maze,' ',sizeof(maze));
	int i,j,sum=0,k=0,temp;
	for(i=1;i<20;i++)
		for(j=1;j<40;j++)
		{
			card[k++]=i*100+j;
		}
	srand(time(0));
	temp=rand()%10+1;
	while(temp--)
	random_shuffle(card,card+19*39);
	for(i=0;i<=40;i++)
		map[i][0]=map[20][i]=map[i][40]=map[0][i]=-1;
	init();
	T.length=1;
	T.body[T.head++]=101;
	T.head%=ML;
	
	sum=getnum();
	map[sum/100][sum%100]=1;
	
	char jud='d';
	int TM=300,start;
	while(1)
	{	
		(TM>100)?(TM=300-T.length*60):(TM=50);
		start=clock();
		//利用临时无穷循环制作刷屏时间
		while(clock()-start<=TM && !kbhit())           //kbhit() 检查当前是否有键盘输入,若有则返回一个非0值,否则返回0
		{
			;
		}
		if(kbhit()&&(order=getch(),order=='w'||order=='s'||order=='a'||order=='d'))
		{
		
			sum=T.body[(T.head-1+ML)%ML];
			system("CLS");
			for(i=T.tail;i!=T.head;)
			{
				temp=T.body[i];
				map[temp/100][temp%100]=-1;
				i++;
				i%=ML;
			}
			if(order=='w')
			{
				jud=order;
				//cout<<"-->w\n";
				if(map[sum/100-1][sum%100]!=-1)
				{
					for(i=T.tail;i!=T.head;)
					{
						temp=T.body[i];
						map[temp/100][temp%100]=0;
						i++;
						i%=ML;
					}
					up();
				}
				else
				{
					system("CLS");
					cout<<"============================================"<<endl;
					cout<<"       很遗憾,你输了!!!最后得分为:"<<T.length*10-10<<endl;
					cout<<"============================================"<<endl;
					break;
				}
				
			}
			else if(order=='a')
			{
				jud=order;
				//cout<<"-->a\n";
				if(map[sum/100][sum%100-1]!=-1)
				{
					for(i=T.tail;i!=T.head;)
					{
						temp=T.body[i];
						map[temp/100][temp%100]=0;
						i++;
						i%=ML;
					}
					left();
				}
				else
				{
					system("CLS");
					cout<<"============================================"<<endl;
					cout<<"       很遗憾,你输了!!!最后得分为:"<<T.length*10-10<<endl;
					cout<<"============================================"<<endl;
					break;
				}
			}
			else if (order=='s')
			{
				jud=order;
				//cout<<"-->s\n";
				if(map[sum/100+1][sum%100]!=-1)
				{
					for(i=T.tail;i!=T.head;)
					{
						temp=T.body[i];
						map[temp/100][temp%100]=0;
						i++;
						i%=ML;
					}
					down();
				}
				else
				{
					system("CLS");
					cout<<"============================================"<<endl;
					cout<<"       很遗憾,你输了!!!最后得分为:"<<T.length*10-10<<endl;
					cout<<"============================================"<<endl;
					break;
				}
			}
			else if(order=='d')
			{
				jud=order;
				//cout<<"-->d\n";
				if(map[sum/100][sum%100+1]!=-1)
				{
					for(i=T.tail;i!=T.head;)
					{
						temp=T.body[i];
						map[temp/100][temp%100]=0;
						i++;
						i%=ML;
					}
					right();
				}
				else
				{
					system("CLS");
					cout<<"============================================"<<endl;
					cout<<"       很遗憾,你输了!!!最后得分为:"<<T.length*10-10<<endl;
					cout<<"============================================"<<endl;
					break;
				}
			}
			memset(maze,' ',sizeof(maze));
			for(i=0;i<=20;i++)
				for(j=0;j<=40;j++)
				{
					if(map[i][j]==-1)
						maze[i][j]='#';
					else if(map[i][j]==1)
						maze[i][j]='o';
				}
			for(i=T.tail;i!=T.head;)
			{
				sum=T.body[i];
				maze[sum/100][sum%100]='*';
				++i;
				i%=ML;
			}
			sum=T.body[(i-1+ML)%ML];
			maze[sum/100][sum%100]='@';
			cout<<"游戏操作:  ↑:w   ↓:s   ←:a   →:d"<<endl;
			cout<<"score: "<<T.length*10-10<<endl;
			for(i=0;i<=20;i++)
			{
				for(j=0;j<=40;j++)
				{
					cout<<maze[i][j];
				}
				cout<<endl;
			}
		}
		else
		{
				sum=T.body[(T.head-1+ML)%ML];
				system("CLS");
				for(i=T.tail;i!=T.head;)
				{
					temp=T.body[i];
					map[temp/100][temp%100]=-1;
					i++;
					i%=ML;
				}
				if(jud=='w')
				{
				//cout<<"-->w\n";
				if(map[sum/100-1][sum%100]!=-1)
				{
					for(i=T.tail;i!=T.head;)
					{
						temp=T.body[i];
						map[temp/100][temp%100]=0;
						i++;
						i%=ML;
					}
					up();
				}
				else
				{
					system("CLS");
					cout<<"============================================"<<endl;
					cout<<"       很遗憾,你输了!!!最后得分为:"<<T.length*10-10<<endl;
					cout<<"============================================"<<endl;
					break;
				}
				
			}
			else if(jud=='a')
			{
				//cout<<"-->a\n";
				if(map[sum/100][sum%100-1]!=-1)
				{
					for(i=T.tail;i!=T.head;)
					{
						temp=T.body[i];
						map[temp/100][temp%100]=0;
						i++;
						i%=ML;
					}
					left();
				}
				else
				{
					system("CLS");
					cout<<"============================================"<<endl;
					cout<<"       很遗憾,你输了!!!最后得分为:"<<T.length*10-10<<endl;
					cout<<"============================================"<<endl;
					break;
				}
			}                                                       
			else if (jud=='s')
			{
				//cout<<"-->s\n";
				if(map[sum/100+1][sum%100]!=-1)
				{
					for(i=T.tail;i!=T.head;)
					{
						temp=T.body[i];
						map[temp/100][temp%100]=0;
						i++;
						i%=ML;
					}
					down();
				}
				else
				{
					system("CLS");
					cout<<"============================================"<<endl;
					cout<<"       很遗憾,你输了!!!最后得分为:"<<T.length*10-10<<endl;
					cout<<"============================================"<<endl;
					break;
				}
			}
			else if(jud=='d')
			{
				//cout<<"-->d\n";
				if(map[sum/100][sum%100+1]!=-1)
				{
					for(i=T.tail;i!=T.head;)
					{
						temp=T.body[i];
						map[temp/100][temp%100]=0;
						i++;
						i%=ML;
					}
					right();
				}
				else
				{
					system("CLS");
					cout<<"============================================"<<endl;
					cout<<"       很遗憾,你输了!!!最后得分为:"<<T.length*10-10<<endl;
					cout<<"============================================"<<endl;
					break;
				}
			}
			
			if(T.length>=20){
					system("CLS");
					cout<<"============================================"<<endl;
					cout<<"                 恭喜,过关!!! "<<endl;
					cout<<"============================================"<<endl;
					break;
			}

			memset(maze,' ',sizeof(maze));
			for(i=0;i<=20;i++)
				for(j=0;j<=40;j++)
				{
					if(map[i][j]==-1)
						maze[i][j]='#';
					else if(map[i][j]==1)
						maze[i][j]='o';
				}
			for(i=T.tail;i!=T.head;)
			{
				sum=T.body[i];
				maze[sum/100][sum%100]='*';
				++i;
				i%=ML;
			}
			sum=T.body[(i-1+ML)%ML];
			maze[sum/100][sum%100]='@';
			cout<<"游戏操作:  ↑:w   ↓:s   ←:a   →:d"<<endl;
			cout<<"score: "<<T.length*10-10<<endl;
			for(i=0;i<=20;i++)
			{
				for(j=0;j<=40;j++)
				{
					cout<<maze[i][j];
				}
				cout<<endl;
			}
		}
		
	}
	system("pause");
	return 0;
}


posted @ 2014-03-09 22:47  zwwill  阅读(437)  评论(0编辑  收藏  举报