資源描述:
《C語言貪吃蛇源程序代碼雙人對抗.doc》由會員上傳分享,免費在線閱讀,更多相關(guān)內(nèi)容在應(yīng)用文檔-天天文庫。
1、C語言貪吃蛇源程序代碼雙人對抗.txt第一次笑是因為遇見你,第一次哭是因為你不在,第一次笑著流淚是因為不能擁有你。#include#include#include#include#include#include#include#include#defineLEFT100#defineTOP30#defineRIGHT538#defineBOTTOM468#defineS
2、IZE20#defineSL100/*SL表示產(chǎn)生食物的個數(shù)*/typedefstructSnake/*表示蛇的結(jié)點*/{intbx,by;structSnake*next;}Snake,*SnakeList;SnakeListcreatesnake(intcx,intcy)/*創(chuàng)建一個蛇頭。*/{SnakeListl;l=(SnakeList)malloc(sizeof(Snake));l->next=NULL;l->bx=cx;l->by=cy;returnl;}voidpaintsnake(SnakeLis
3、tl,intscoler,inttcoler)/*輸出蛇身的函數(shù),也可作為消去蛇身的函數(shù)。*/{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)/*設(shè)置隨機食物*/{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);