#include <graphics.h>
#include <stdlib.h>
#include <dos.h>
#define LEFT 0x4b00
#define RIGHT 0x4d00
#define DOWN 0x5000
#define UP 0x4800
#define A 0x1e61
#define D 0x2064
#define S 0x1f73
#define W 0x1177
#define ESC 283
#define N 100
long movespeed[5]={40000,20000,10000,8000,5000};/*蛇移动的速度*/
struct snake
{
int x[N];
int y[N];
int knot;/*节数*/
int direction;/*移动方向*/
int live;/* 0活着,1死亡*/
}snake[2];
int food_x,food_y,food_sigh;
int i,max,key,play1score=0,play2score=0,count;
void draw(void) /*左上(47,40),右下(621,361)方框*/
{
setfillstyle(1,9);
bar(47,40,621,49);
bar(47,40,58,370);
bar(47,361,621,370);
bar(611,40,621,370);
setfillstyle(1,10);
bar(0,0,47,479);
bar(0,479,640,370);
bar(621,479,640,0);
bar(0,0,640,40);
}
void Count(void)/*级数,如果级数越高移动越快*/
{
char str[10];
setfillstyle(1,5);
bar(450,15,611,35);
setcolor(GREEN);
settextstyle(0,0,2);
sprintf(str,"count:%d",count);
outtextxy(451,20,str);
}
void printfscore(void)/*输出分数,每吃一个就加十*/
{
char str1[10];
char str2[10];
setfillstyle(9,12);
bar(250,15,400,35);
setcolor(GREEN);
settextstyle(0,0,2);
sprintf(str1,"play2:%d",play2score);/*play2的分数*/
outtextxy(250,20,str1);
setfillstyle(1,5);
bar(45,15,195,35);
setcolor(GREEN);
sprintf(str2,"play1:%d",play1score);/*play1的分数*/
outtextxy(45,20,str2);
}
void maker(void) /*作者*/
{
char str1[30]={"MAKER:"};
char str2[50]={"WJYang GuiLin!"};
char str3[30]={"Snake GAME"};
setcolor(RED);
settextstyle(0,0,2);
outtextxy(47,420,str1);
settextstyle(0,0,1);
outtextxy(150,428,str2);
settextstyle(0,0,2);
outtextxy(400,420,str3);
}
void gameover(void) /*结束函数*/
{
cleardevice();
Count();
maker();
printfscore();
setcolor(RED);
settextstyle(0,0,5);
outtextxy(150,200,"GAME OVER");
moveto(150,300);
outtextxy(150,300,"^_^^_^^_^");
settextstyle(0,0,5);
}
void gamestard(void)
{
randomize();
food_sigh=1;/*1需要出现,0已经存在*/
{/*蛇1初始*/
snake[0].live=0;/*显示*/
snake[0].direction=1;/*右*/
snake[0].x[0]=70;/*头*/
snake[0].y[0]=70;
snake[0].x[1]=80;
snake[0].y[1]=70;
snake[0].knot=3;/*节数*/
}
{/*蛇2初始*/
snake[1].live=0;/*显示*/
snake[1].direction=1;/*右*/
snake[1].x[0]=70;/*头*/
snake[1].y[0]=100;
snake[1].x[1]=80;
snake[1].y[1]=100;
snake[1].knot=3;/*节数*/
}
Count();
maker();
printfscore();
while(1)/*没按键就一直循环*/
{
while(!kbhit())
{
if(food_sigh==1)/*需要出现新食物*/
{
food_x=rand()%533+62;
food_y=rand()%294+53;/*出现的位置*/
for(i=0;(i=food_x%10)!=0;i++)/*出现必须在整数的格内,那样蛇才能吃上*/
food_x++;
for(i=0;(i=food_y%10)!=0;i++)
food_y++;
food_sigh=0;
}
if(food_sigh==0)/*画出食物*/
{
setfillstyle(1,4);
bar(food_x,food_y,food_x+10,food_y-10);
}
for(i=snake[0].knot-1;i>0;i--)/*蛇1的每个环节往前移动*/
{
snake[0].x[i]=snake[0].x[i-1];
snake[0].y[i]=snake[0].y[i-1];
}
for(i=snake[1].knot-1;i>0;i--)/*蛇2的每个环节往前移动*/
{
snake[1].x[i]=snake[1].x[i-1];
snake[1].y[i]=snake[1].y[i-1];
}
switch(snake[0].direction)/*蛇1的方向*/
{
case 1: snake[0].x[0]+=10;break;
case 2: snake[0].x[0]-=10;break;
case 3: snake[0].y[0]-=10;break;
case 4: snake[0].y[0]+=10;break;
}
switch(snake[1].direction)/*蛇2的方向*/
{
case 1: snake[1].x[0]+=10;break;
case 2: snake[1].x[0]-=10;break;
case 3: snake[1].y[0]-=10;break;
case 4: snake[1].y[0]+=10;break;
}
for(i=0;i<(max=snake[1].knot>snake[0].knot?snake[1].knot:snake[0].knot);i++)/*它们相撞就死*/
{
if((snake[0].x[0]==snake[1].x[i]&&snake[1].y[i]==snake[0].y[0])||(snake[0].x[i]==snake[1].x[0]&&snake[0].y[i]==snake[1].y[0]))
{
gameover();
snake[0].live=1;
snake[1].live=1;
break;
}
}
for(i=3;i<snake[0].knot;i++)/*从第四节开始撞到自己*/
{
if(snake[0].x[i]==snake[0].x[0]&&snake[0].y[i]==snake[0].y[0])
{
gameover();
snake[0].live=1;
break;
}
}
for(i=3;i<snake[1].knot;i++)/*从第四节开始撞到自己*/
{
if(snake[1].x[i]==snake[1].x[0]&&snake[1].y[i]==snake[1].y[0])
{
gameover();
snake[1].live=1;
break;
}
}
if(snake[0].x[0]<58||snake[0].x[0]>605||snake[0].y[0]<55||snake[0].y[0]>360)/*撞到墙壁*/
{
gameover();
snake[0].live=1;
}
if(snake[1].x[0]<58||snake[1].x[0]>605||snake[1].y[0]<55||snake[1].y[0]>360)/*撞到墙壁*/
{
gameover();
snake[1].live=1;
}
if(snake[0].live==1)
break;
if(snake[1].live==1)
break;
if(snake[0].x[0]==food_x&&snake[0].y[0]==food_y)/*吃到东西*/
{
setfillstyle(1,11);
bar(food_x,food_y,food_x+10,food_y-10);
snake[0].knot++;
food_sigh=1;/*需要出现新的食物*/
play1score+=10;
printfscore();
}
if(snake[1].x[0]==food_x&&snake[1].y[0]==food_y)/*吃到东西*/
{
setfillstyle(1,11);
bar(food_x,food_y,food_x+10,food_y-10);
snake[1].knot++;
food_sigh=1;/*需要出现新的食物*/
play2score+=10;
printfscore();
}
setfillstyle(1,8); /*画蛇1*/
bar(snake[0].x[0]-1,snake[0].y[0]+1,snake[0].x[0]+11,snake[0].y[0]-11);
setlinestyle(SOLID_LINE,0,THICK_WIDTH);
setcolor(8);
for(i=1;i<snake[0].knot;i++)
rectangle(snake[0].x[i],snake[0].y[i],snake[0].x[i]+10,snake[0].y[i]-10);
setfillstyle(9,4);/*画蛇2*/
bar(snake[1].x[0],snake[1].y[0],snake[1].x[0]+10,snake[1].y[0]-10);
setfillstyle(11,4);
for(i=1;i<snake[1].knot;i++)
bar(snake[1].x[i],snake[1].y[i],snake[1].x[i]+10,snake[1].y[i]-10);
if(snake[0].knot<=7)/*级数1*/
{
delay(movespeed[0]);
count=1;
}
if(snake[0].knot>7&&snake[0].knot<=11)/*级数2*/
{
delay(movespeed[1]);
count=2;
}
if(snake[0].knot>11&&snake[0].knot<=15)/*级数3*/
{
delay(movespeed[2]);
count=3;
}
if(snake[0].knot>15&&snake[0].knot<=20)/*级数4*/
{
delay(movespeed[3]);
count=4;
}
if(snake[0].knot>20&&snake[0].knot<=N)/*级数5*/
{
delay(movespeed[4]);
count=5;
}
Count();
if(snake[0].knot==8)/*当节数为8时,显示"GOOD!!"*/
{ setcolor(GREEN);
settextstyle(0,0,5);
outtextxy(230,150,"GOOD!!");
}
if(snake[0].knot==9)/*当节数为9时,除去"GOOD!!"*/
{ setcolor(LIGHTCYAN);
settextstyle(0,0,5);
outtextxy(230,150,"GOOD!!");
}
if(snake[0].knot==12)/*当节数为12时,显示"GREAD!!"*/
{ setcolor(MAGENTA);
settextstyle(0,0,5);
outtextxy(230,150,"GREAT!!");
}
if(snake[0].knot==13)/*当节数为12时,除去"GREAD!!"*/
{ setcolor(LIGHTCYAN);
settextstyle(0,0,5);
outtextxy(230,150,"GREAT!!");
}
setfillstyle(8,11);/*用背景色涂去蛇2最后一节*/
bar(snake[1].x[snake[1].knot-1],snake[1].y[snake[1].knot-1],snake[1].x[snake[1].knot-1]+10,snake[1].y[snake[1].knot-1]-10);
setfillstyle(1,11);
bar(snake[0].x[0]-1,snake[0].y[0]+1,snake[0].x[0]+11,snake[0].y[0]-11);
setcolor(11);/*用背景色涂去蛇1最后一节*/
rectangle(snake[0].x[snake[0].knot-1],snake[0].y[snake[0].knot-1],snake[0].x[snake[0].knot-1]+10,snake[0].y[snake[0].knot-1]-10);
}
/*蛇1的方向判断*/
if(snake[0].live==1)/*死了就结束*/
break;
key=bioskey(0);/*按键的控制*/
{
if(key==ESC)/*退出*/
break;
else if(key==LEFT&&snake[0].direction!=1)/*判断方向*/
snake[0].direction=2;
else if(key==DOWN&&snake[0].direction!=3)
snake[0].direction=4;
else if(key==UP&&snake[0].direction!=4)
snake[0].direction=3;
else if(key==RIGHT&&snake[0].direction!=2)
snake[0].direction=1;
}
/*蛇2的方向判断*/
if(snake[1].live==1)
break;
{
if(key==A&&snake[1].direction!=1)/*判断方向*/
snake[1].direction=2;
else if(key==S&&snake[1].direction!=3)
snake[1].direction=4;
else if(key==W&&snake[1].direction!=4)
snake[1].direction=3;
else if(key==D&&snake[1].direction!=2)
snake[1].direction=1;
}
}
}
void main(void)/*主函数*/
{
void maker(void);
void Count(void);
void draw(void);
void gameover(void);
void gamestard(void);
void printfscore(void);
int gdrive=DETECT,gmode;
initgraph(&gdrive,&gmode,"");
cleardevice();
setbkcolor(LIGHTCYAN);
draw();
gamestard();
maker();
getch();
closegraph();
}