一个C语言写简单贪吃蛇源代码
#include
#include
#include
#include
#include
#include
int grade=5,point=0,life=3;
void set(),menu(),move_head(),move_body(),move(),init_insect(),left(),upon(),right(),down(),init_graph(),food_f(),ahead(),crate();
struct bug
{
int x;
int y;
struct bug *last;
struct bug *next;
};
struct fd
{
int x;
int y;
int judge;
}food={0,0,0};
struct bug *head_f=NULL,*head_l,*p1=NULL,*p2=NULL;
void main()
{
char ch;
initgraph(800,600);
set();
init_insect();
while(1)
{
food_f();
Sleep(grade*10);
setcolor(BLACK);
circle(head_l->x,head_l->y,2);
setcolor(WHITE);
move_body();
if(kbhit())
{
ch=getch();
if(ch==27)
{
ahead();
set();
}
else if(ch==-32)
{
switch(getch())
{
case 72:upon();break;
case 80:down();break;
case 75:left();break;
case 77:right();break;
}
}
else ahead();
}
else
{
ahead();
}
if(head_f->x==food.x&&head_f->y==food.y)
{
Sleep(100);
crate();
food.judge=0;
point=point+(6-grade)*10;
if(food.x<30||food.y<30||food.x>570||food.y>570)
life++;
menu();
}
if(head_f->x<5||head_f->x>595||head_f->y<5||head_f->y>595)
{
Sleep(1000);
life--;
food.judge=0;
init_graph();
init_insect();
menu();
}
for(p1=head_f->next;p1!=NULL;p1=p1->next)
{
if(head_f->x==p1->x&&head_f->y==p1->y)
{
Sleep(1000);
life--;
food.judge=0;
init_graph();
init_insect();
menu();
break;
}
}
if(life==0)
{
outtextxy(280,300,"游戏结束!");
getch();
return;
}
move();
};
}
void init_graph()
{
clearviewport();
setlinestyle(PS_SOLID,1,5);
rectangle(2,2,600,598);
setlinestyle(PS_SOLID,1,1);
}
void set()
{
init_graph();
outtextxy(640,50,"1、开始 / 返回");
outtextxy(640,70,"2、退出");
outtextxy(640,90,"3、难度");
outtextxy(640,110,"4、重新开始");
switch(getch())
{
case '1': menu();setcolor(GREEN);circle(food.x,food.y,2);setcolor(WHITE);return;
case '2': exit(0);break;