代码如下(作者制作不易,还望关注一下):
#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