资源描述:
《C语言贪吃蛇源程序代码双人对抗.doc》由会员上传分享,免费在线阅读,更多相关内容在应用文档-天天文库。
1、C语言贪吃蛇源程序代码双人对抗.txt第一次笑是因为遇见你,第一次哭是因为你不在,第一次笑着流泪是因为不能拥有你。#include#include#include#include#include#include#include#include#defineLEFT100#defineTOP30#defineRIGHT538#defineBOTTOM468#defineS
2、IZE20#defineSL100/*SL表示产生食物的个数*/typedefstructSnake/*表示蛇的结点*/{intbx,by;structSnake*next;}Snake,*SnakeList;SnakeListcreatesnake(intcx,intcy)/*创建一个蛇头。*/{SnakeListl;l=(SnakeList)malloc(sizeof(Snake));l->next=NULL;l->bx=cx;l->by=cy;returnl;}voidpaintsnake(SnakeLis
3、tl,intscoler,inttcoler)/*输出蛇身的函数,也可作为消去蛇身的函数。*/{SnakeListp;intx,y;p=l;setfillstyle(SOLID_FILL,scoler);for(;p->next;){p=p->next;x=p->bx;y=p->by;bar(x,y,x+SIZE,y+SIZE);}if(scoler==2)setfillstyle(SOLID_FILL,scoler);elsesetfillstyle(SOLID_FILL,tcoler);x=l->bx;y=l
4、->by;bar(x,y,x+SIZE,y+SIZE);}voidchangsnake(SnakeListl,intfx,intfy,ints)/*fx,fy表示蛇头移动方向,分别取值为1,-1;s表示是否吃到食物,1表示吃到,0表示没有。*/{SnakeListp=l;intx,y,t;x=p->bx;y=p->by;p->bx=x+fx*(SIZE+2);p->by=y+fy*(SIZE+2);for(;p->next;){p=p->next;t=p->bx;p->bx=x;x=t;t=p->by;p->by
5、=y;y=t;}if(s){p->next=(SnakeList)malloc(sizeof(Snake));p->next->bx=x;p->next->by=y;p->next->next=NULL;}}voidsetfood(int*fx,int*fy,SnakeListal,SnakeListbl)/*设置随机食物*/{inta,b,s=1,s2;SnakeListslist1;SnakeListslist2;setfillstyle(SOLID_FILL,6);srand(time(NULL));for
6、(;s;){slist1=al;slist2=bl;a=rand()%(SIZE);b=rand()%(SIZE);a=LEFT+(a)*22;b=TOP+(b)*22;s2=1;for(;slist1&&s2;){if(a==slist1->bx&&b==slist1->by)s2=0;elseslist1=slist1->next;}s2=1;for(;slist2&&s2;){if(a==slist2->bx&&b==slist2->by)s2=0;elseslist2=slist2->next;}if(s
7、list1==NULL&&slist2==NULL)s=0;}*fx=a;*fy=b;bar(*fx,*fy,*fx+20,*fy+20);}voidqufood(intfx,intfy)/*消去食物*/{setfillstyle(SOLID_FILL,2);bar(fx,fy,fx+20,fy+20);}voidfreesnake(SnakeListpsnake)/*释放蛇的空间。*/{SnakeListp;p=psnake->next;psnake->next=NULL;for(;p;){p=psnake->
8、next;free(psnake);psnake=p;}}intnothit(SnakeListsnake1,SnakeListsnake2,intx,inty)/*检查snake1是否撞击snake2。*/{inta1,a2,b1,b2,bj=1;a1=snake1->bx;b1=snake1->by;a1=a1+x*(SIZE+2);b1=b1+y*(SIZE+2);