C++的游戏--贪吃蛇

这是一个使用C++编写的贪吃蛇游戏,包含了游戏菜单选择、难度设定、排行榜显示等功能。玩家可以通过键盘控制蛇的移动,游戏难度可调,并具备智能辅助。游戏结束时,可以选择重新开始、返回菜单或退出。
摘要由CSDN通过智能技术生成

代码如下(作者制作不易,还望关注一下):

#include<cstdio>
#include<windows.h>
#define UP 1
#define LEFT 2
#define DOWN 3
#define RIGHT 4
#define HEAD 5
#define WALL 22
struct botton{
int x;
int y;
int length;
};
struct Snake{
Snake(int a,bool b,int c);
bool life;
int Flag ;
int Length ;
int Sum ;
int color;
int speed;
POINT Head ;
POINT End ;
void Smarter() ;
void Move() ;
void Go(bool iiSmart,bool iiSmarter,bool iiMove,int up,int left,int down,int right) ;
};
void GradeIn() ;
void LevelChoose() ;
void MenuPrint() ;
void Menu() ;
void GradePrint() ;
void Setting() ;
void Prepare() ;
void Walk(int ,long&,long&) ;
void Dead() ;
void Foods() ;
void GradeOut(int) ;
void Check() ;
void Pedge(int,int,int,bool) ;
int Pposition(int,int) ;
int Pcolor(int,int) ;
char *Psame(int,char);
char *Pempty(int) ;
POINT Food ;
Snake Person(10,true,1) ;
Snake Computer(1,false,1) ;
Snake Another(14,false,1) ;
int Map[WALL][WALL] ={} ;
int Level = 0 ;
int Score[4] ={} ;
bool iBegin = true ;
bool iMenu = false ;
bool iDead = true ;
bool iFor = false ;
bool iSmart = false ;
bool iCheck = false ;
char wLevel[4][5] ={"简单","普通","困难","炼狱"} ;
Snake::Snake(int a,bool b,int c)
{
color=a;
life=b;
speed=c;
}
/* 数据导入 */
void GradeIn()
{
if ( fopen("score.txt","rb")){
fscanf(fopen("score.txt","rb"),"%d%d%d%d",&Score[0],&Score[1],&Score[2]),&Score[3] ;
}
}
/* 选择菜单 */
void Menu()
{
int one=0 ;
POINT edge[5]={ {16,7},{13,10},{13,13},{23,13},{16,16}} ;
int length[5]={12,18,10,8,12} ;
Print:
system("cls") ;
printf("\n\n\n%s贪%s吃%s蛇\n\n\n\n",Pempty(17),Pempty(2),Pempty(2),Pcolor( 15, 3)) ;
printf("\n%s开始游戏\n\n",Pempty(18)) ;
printf("\n%s游戏难度 :%s\n\n",Pempty(15),wLevel[Level]) ;
printf("\n%s排行榜%s设置\n\n",Pempty(15),Pempty(4)) ;
printf("\n%s退出游戏",Pempty(18)) ;
Pedge(edge[one].x,edge[one].y,length[one],true);
Scan:
Sleep(125) ;
if ( GetAsyncKeyState(' ')||GetAsyncKeyState(VK_RETURN)){
void (*name[])()={Prepare,LevelChoose,GradePrint,Setting} ;
if(one<4){
name[one]() ;
}
if(one==0||one==4){
iBegin=(one==4)?false:true ;
return ;
}
one=0 ;
}
else if ((GetAsyncKeyState('S')||GetAsyncKeyState(VK_DOWN))&& one!=4){
one+=(one==2)?2:1 ;
}
else if ((GetAsyncKeyState('W')||GetAsyncKeyState(VK_UP))&& one!=0){
one-=(one==3)?2:1 ;
}
else if ((GetAsyncKeyState('D')||GetAsyncKeyState(VK_RIGHT))&& one==2){
one++ ;
}
else if ((GetAsyncKeyState('A')||GetAsyncKeyState(VK_LEFT))&& one==3){
one-- ;
}
else{
goto Scan ;
}
for(int i=0;i

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值