贪吃蛇游戏代码C++.docx
- 文档编号:23626274
- 上传时间:2023-05-19
- 格式:DOCX
- 页数:11
- 大小:16.26KB
贪吃蛇游戏代码C++.docx
《贪吃蛇游戏代码C++.docx》由会员分享,可在线阅读,更多相关《贪吃蛇游戏代码C++.docx(11页珍藏版)》请在冰豆网上搜索。
贪吃蛇游戏代码C++
#include
#include<>
#include<>
#include<>
#include<>
usingnamespacestd;
intGameOver=0;
structBody//蛇身(链表结构)
{
intx,y;//蛇身结点坐标
Body*next;//下一个结点
};
voidSetPos(inti,intj)//设定光标位置
{
COORDpos={i-1,j-1};//坐标变量
HANDLEOut=GetStdHandle(STD_OUTPUT_HANDLE);//获取输出句柄
SetConsoleCursorPosition(Out,pos);//设定光标位置
}
classSneak//贪吃蛇类
{
private:
Body*head;//蛇头指针
intDirection;//移动方向。
1,2,3,4对应上左下右
intCount;//蛇长
intSpeed;//速度(等级)
intFoodExist;//食物存在标记
intFood_x;//食物X坐标
intFood_y;//食物Y坐标
public:
Sneak(inta=3,intb=4,intc=1,intd=0)//构造函数
{
FoodExist=d;//起始不存在食物
Body*temp1,*temp2;
head=new(Body);//申请起始蛇身3节
head->x=4;
head->y=2;
temp1=new(Body);
temp1->x=3;
temp1->y=2;
temp2=new(Body);
temp2->x=2;
temp2->y=2;
head->next=temp1;
temp1->next=temp2;
temp2->next=NULL;
Direction=b;//方向为右
Count=a;//长为3
Speed=c;//等级1
}
voidMap();//画界面函数
voidPaint();//画蛇身函数
voidFood();//生成食物
intOver();//判断游戏是否结束
voidGaming();//游戏进程函数
voidMove();//移动
};
voidSneak:
:
Map()//使用
{
inti;
for(i=1;i<=50;i++)
{
SetPos(i,1);
cout<<"-";
}
for(i=2;i<=25;i++)
{
SetPos(1,i);
cout<<"|";
SetPos(50,i);
cout<<"|";
}
for(i=1;i<=50;i++)
{
SetPos(i,25);
cout<<"-";
}
SetPos(54,3);
cout<<"贪吃蛇";
SetPos(54,5);
cout<<"贪吃蛇长度为:
"< SetPos(54,7); cout<<"等级: "< } voidSneak: : Food() { Body*p; intInBody=0;//判断食物是否产生在蛇体内 srand((int)time(0));//用系统时间来做随机数种子 while (1) { Food_x=rand()%48+2;//随机出食物的坐标 Food_y=rand()%23+2; p=head; while(p! =NULL)//判断食物是否产生在蛇体内 { if(p->x==Food_x&&p->y==Food_y) { InBody=1; break; } p=p->next; } if(InBody==0)//食物不在蛇身。 生成成功 break; InBody=0; } } intSneak: : Over() { Body*p; p=head; if((head->x)>=50||(head->x)<=1||(head->y)<=1||(head->y)>=25)//是否撞到边缘 return1; p=head->next; while(p! =NULL)//是否撞到蛇身 { if((head->x==p->x)&&(head->y==p->y)) return1; p=p->next; } return0; } voidSneak: : Paint() { Body*p; p=head; while(p! =NULL) { SetPos(p->x,p->y); cout<<"*"; p=p->next; } SetPos(Food_x,Food_y); cout<<"*"; } voidSneak: : Move() { Body*New; New=new(Body);//新的蛇身结点 if(Direction==1)//确定新蛇头的坐标 { New->x=head->x; New->y=head->y-1; New->next=head; head=New; } if(Direction==2) { New->x=head->x-1; New->y=head->y; New->next=head; head=New; } if(Direction==3) { New->x=head->x; New->y=head->y+1; New->next=head; head=New; } if(Direction==4) { New->x=head->x+1; New->y=head->y; New->next=head; head=New; } } voidSneak: : Gaming() { system("cls");//刷新屏幕 charx; Body*p; Map();//画界面的先 Paint();//再画蛇身 while (1) { if(_kbhit())//_kbhit()判断是否有键盘操作 { x=_getch();//重缓冲区读出一个字符赋给x if((x=='W'||x=='w')&&Direction! =3)//改变蛇的方向(不可以是反方向) Direction=1; if((x=='S'||x=='s')&&Direction! =1) Direction=3; if((x=='A'||x=='a')&&Direction! =4) Direction=2; if((x=='D'||x=='d')&&Direction! =2) Direction=4; while(_kbhit())//读掉这之后所有的键盘输入 _getch(); } if(FoodExist==0)//如果食物被吃了或刚开始游戏,要生成新的食物 { Food(); FoodExist=1; } Move();//移动蛇 if(head->x==Food_x&&head->y==Food_y)//如果蛇吃到了食物 { FoodExist=0; Count++;//蛇身+1 SetPos(54,5); cout<<"贪吃蛇长度: "< if(Count%10==0)//每十个蛇身升一级 { Speed++; SetPos(54,7); cout<<"LEVEL: "< } if(Speed==10)//最高等级达成。 退出游戏 break; } Paint();//画新的蛇身 if(FoodExist==1)//如果没有吃到食物,需要删除蛇尾。 { p=head; while((p->next)->next! =NULL) p=p->next; SetPos(p->next->x,p->next->y); cout<<""; delete(p->next); p->next=NULL; } if(Over())//判断是否游戏结束 break; Sleep(500-Speed*50);//等待,具体时间和等级有关 } system("cls"); if(Speed==10)//通关 { SetPos(25,25); cout<<"恭喜你,你已经通关了"< system("pause"); } else//失败 { SetPos(25,10); cout<<"gameover,最终长度为"< system("pause"); } } intmain() { system("color6a"); Sneakgame; system("cls"); cout<<"*****************************************************"< cout<<"*极品贪吃蛇小游戏*"< cout<<"*****************************************************"< cout<<"*说明: *"< cout<<"*W,A,S,D控制移动*"< cout<<"*每10节蛇身升一级,并提高速度,10级通关*"< cout<<"*****************************************************"< cout<<"*制作人12计科01班魏梦阳*"< cout<<"*********按任意键开始**********************"< _getch(); (); return0; }
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 贪吃 游戏 代码 C+