不完善的地方希望得到高手的指教,另外我有个QQ群可供大家交流,群号码:3228111
我的邮箱是:[email protected]
#include <graphics.h>
#include <bios.h>
#include <stdio.h>
#include <stdlib.h>
#define ESC 27
#define TO_UP 72
#define TO_DOWN 80
#define TO_RIGHT 77
#define TO_LEFT 75
int turns[10][2],turn=0;
out_queue()
{
int a,b;
for(a=1;a<10;a++)
{
for(b=0;b<2;b++)
{
turns[a-1][b]=turns[a][b];
}
if(turns[a][1]==0)break;
}
}
void begin()
{
cleardevice();
setbkcolor(BLACK);
setcolor(WHITE);
setfillstyle(SOLID_FILL,BLUE);
bar(300,240,330,250);
rectangle(120,40,520,440);
floodfill(1,1,WHITE);
}
int judgekey(int key)
{
int a;
switch(key)
{
case TO_UP:
a=TO_DOWN;
break;
case TO_DOWN:
a=TO_UP;
break;
case TO_LEFT:
a=TO_RIGHT;
break;
case TO_RIGHT:
a=TO_LEFT;
}
return(a);
}
int key()
{
int key;
key=bioskey(0);
key=key&0xff?key&0xff:key>>8;
return(key);
}
void callgraph()
{
int gdriver=DETECT,mode,errorcode;
initgraph(&gdriver,&mode,"");
errorcode=graphresult();
if(errorcode!=grOk)
{
printf("graphics error:%s/n",grapherrormsg(errorcode));
printf("press any key to exit!");
getch();
exit(1);
}
}
end()
{
setviewport(200,180,440,300,0);
setbkcolor(WHITE);
setcolor(RED);
outtextxy(10,10,"You are dead!Thank for using!");
while(!kbhit());
endgame();
}
endgame()
{
closegraph();
exit(0);
}
win()
{
setviewport(200,180,440,300,0);
setbkcolor(WHITE);
setcolor(RED);
outtextxy(10,10,"You have win!Thank for using!");
while(!kbhit());
endgame();
}
int control()
{
int head_x=20,head_y=20,head=TO_RIGHT;
int tail_x=17,tail_y=20,tail=TO_RIGHT;
int length=3;
int s_head_x,s_head_y;
int s_tail_x,s_tail_y;
int rnd_x,rnd_y,rnd_box=0;
int key1=0;
int key_head;
randomize();
while(1)
{
delay(10000);/*to contol the speed*/
if(rnd_box==0)
{
rnd_x=random(39)+1;
rnd_y=random(39)+1;
rnd_box=1;
setfillstyle(SOLID_FILL,BLUE);
bar(120+10*rnd_x,40+10*rnd_y,130+10*rnd_x,50+10*rnd_y);
}
if(bioskey(1)!=0)
{
key1=key();
key_head=judgekey(head);
if(key1==ESC||(key1!=head&&key1!=key_head))
{
switch(key1)
{
case TO_UP:
head=TO_UP; /*前进方向*/
turns[turn][0]=head_x;/*存转折点坐标*/
turns[turn][1]=TO_UP; /*存方向*/
turn++; /*多了一个转折点*/
break;
case TO_DOWN:
head=TO_DOWN;
turns[turn][0]=head_x;
turns[turn][1]=TO_DOWN;
turn++;
break;
case TO_RIGHT:
head=TO_RIGHT;
turns[turn][0]=head_y;
turns[turn][1]=TO_RIGHT;
turn++;
break;
case TO_LEFT:
head=TO_LEFT;
turns[turn][0]=head_y;
turns[turn][1]=TO_LEFT;
turn++;
break;
case ESC:
endgame();
}
}
}
if(tail_x==turns[0][0]||tail_y==turns[0][0])
{
tail=turns[0][1];
turn--;
out_queue();
}
if(head_x==rnd_x&&head_y==rnd_y)
{
length++;
rnd_box=0;
if(length>=10)win();
rnd_box=0;
switch(tail)
{
case TO_UP:
tail_y++;break;
case TO_DOWN:
tail_y--;break;
case TO_LEFT:
tail_x++;break;
case TO_RIGHT:
tail_x--;
}
}
switch(head)
{
case TO_RIGHT:
head_x++;
if(head_x>=40) end();
break;
case TO_LEFT:
head_x--;
if(head_x<0) end();break;
case TO_UP:
head_y--;
if(head_y<0) end();
break;
case TO_DOWN:
head_y++;
if(head_y>=40) end();
}
switch(tail)
{
case TO_UP:
tail_y--;break;
case TO_DOWN:
tail_y++;break;
case TO_LEFT:
tail_x--;break;
case TO_RIGHT:
tail_x++;
}
s_head_x=125+10*head_x;
s_head_y=45+10*head_y;
s_tail_x=125+10*tail_x;
s_tail_y=45+10*tail_y;
setfillstyle(SOLID_FILL,BLUE);
bar(s_head_x-5,s_head_y-5,s_head_x+5,s_head_y+5);
setfillstyle(SOLID_FILL,BLACK);/*黑色填充清除轨迹*/
bar(s_tail_x-5,s_tail_y-5,s_tail_x+5,s_tail_y+5);
}
}
main()
{
callgraph();
begin();
outtextxy(300,1,"Ready?");
getch();
outtextxy(300,1," ");
control();
}
/*Made by KevinLynx2004,5,26*/