使用C++语言模拟实现贪吃蛇小游戏
2016-10-04 21:19
423 查看
需求分析:
用C++开发一个能在编译窗口中运行的贪吃蛇小游戏,实现环境为:Microsoft visual C++6.0集成开发环境
算法及程序说明:
用类构造蛇
[cpp] view
plain copy
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的内容全部 设置为//这个“值”。
}
绘图实现
[cpp] view
plain copy
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个数据的顺序打乱
地图的预处理
[cpp] view
plain copy
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]='@';
最后生成图像
[cpp] view
plain copy
for(i=0;i<=20;i++)
{
for(j=0;j<=40;j++)
{
cout<<maze[i][j];
}
cout<<endl;
}
画面动态显示实现
主要运用system("CLS")的刷屏功能来实现画面的动态效果。
利用临时无穷循环制作刷屏时间控制
[cpp] view
plain copy
int TM=300; //300毫秒
(TM>100)?(TM=300-T.length*10):(TM=100);
int start=clock();
while(clock()-start<=TM && !kbhit()) //kbhit()检查当前是否有键盘输入,若有则返回一个非0值,否//则返回0
{
;
}
蛇运动的实现(以向上为例)
[cpp] view
plain copy
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()函数
[cpp] view
plain copy
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。
当蛇头触碰到四周墙壁或触碰到蛇身时,游戏结束,显示最终得分。
当蛇吃食物的个数到达程序中设定的某个值时,挑战游戏成功。
完整代码: 》仅供参考,如有错误尽请谅解《
[cpp] view
plain copy
#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
;
}
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;
}
转载:http://blog.csdn.net/willspace/article/details/20866733
用C++开发一个能在编译窗口中运行的贪吃蛇小游戏,实现环境为:Microsoft visual C++6.0集成开发环境
算法及程序说明:
用类构造蛇
[cpp] view
plain copy
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的内容全部 设置为//这个“值”。
}
绘图实现
[cpp] view
plain copy
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个数据的顺序打乱
地图的预处理
[cpp] view
plain copy
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]='@';
最后生成图像
[cpp] view
plain copy
for(i=0;i<=20;i++)
{
for(j=0;j<=40;j++)
{
cout<<maze[i][j];
}
cout<<endl;
}
画面动态显示实现
主要运用system("CLS")的刷屏功能来实现画面的动态效果。
利用临时无穷循环制作刷屏时间控制
[cpp] view
plain copy
int TM=300; //300毫秒
(TM>100)?(TM=300-T.length*10):(TM=100);
int start=clock();
while(clock()-start<=TM && !kbhit()) //kbhit()检查当前是否有键盘输入,若有则返回一个非0值,否//则返回0
{
;
}
蛇运动的实现(以向上为例)
[cpp] view
plain copy
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()函数
[cpp] view
plain copy
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。
当蛇头触碰到四周墙壁或触碰到蛇身时,游戏结束,显示最终得分。
当蛇吃食物的个数到达程序中设定的某个值时,挑战游戏成功。
完整代码: 》仅供参考,如有错误尽请谅解《
[cpp] view
plain copy
#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
;
}
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;
}
转载:http://blog.csdn.net/willspace/article/details/20866733
相关文章推荐
- 使用C++语言模拟实现贪吃蛇小游戏
- Java实现简单的贪吃蛇小游戏(使用线程、内部类、双向链表等)
- 使用JavaScript实现简单的小游戏-贪吃蛇
- [iOS]使用NSProxy实现消息转发机制,模拟多重继承
- AS3:小游戏“贪吃蛇”的实现
- 使用java简单模拟ping和telnet的实现
- 使用java简单模拟ping和telnet的实现
- 使用java简单模拟ping和telnet的实现
- 使用jQuery插件实现 模拟dialog的遮罩效果
- 使用UI Automation实现自动化测试--7.1 (模拟键盘输入数据在自动化测试中的应用)
- 使用UI Automation实现自动化测试--7.2 (模拟键盘复杂操作在自动化测试中的应用)
- 使用线程池模拟实现数据库连接池
- Excel下实现贪吃蛇小游戏
- Java枚举(用Java普通类模拟枚举的实现原理及JDK枚举API使用示例)
- Java枚举(用Java普通类模拟枚举的实现原理及JDK枚举API使用示例)
- 使用最近未使用页淘汰(NRU)算法模拟实现页淘汰进程
- 使用js来实现模拟无刷新文件上传。
- 使用" 参数化基类" 和" 成员函数指针" 模拟实现虚函数--在实际中的应用
- 使用java简单模拟ping和telnet的实现
- 使用ConnectifyInstaller软件模拟wifi热点,实现网络共享