C语言贪吃蛇源程序代码双人对抗#include <conio.h>#include <dos.h>#include <graphics.h>#include <stdlib.h>#include <bios.h>#include <time.h>#include <malloc.h>#define LEFT 100#define TOP 30#define RIGHT 538#define BOTTOM 468#define SIZE 20#define SL 100 /*SL表示产生食物的个数*/typedef struct Snake/*表示蛇的结点*/{int bx,by;struct Snake *next;}Snake,*SnakeList;SnakeList createsnake(int cx,int cy)/*创建一个蛇头。
*/{SnakeList l;l=(SnakeList)malloc(sizeof(Snake));l->next=NULL;l->bx=cx;l->by=cy;return l;}void paintsnake(SnakeList l,int scoler,int tcoler)/*输出蛇身的函数,也可作为消去蛇身的函数。
*/{SnakeList p;int x,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->by;bar(x,y,x+SIZE,y+SIZE);}void changsnake(SnakeList l,int fx,int fy,int s) /*fx,fy表示蛇头移动方向,分别取值为1,-1;s 表示是否吃到食物,1表示吃到,0表示没有。
*/{SnakeList p=l;int x,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=y;y=t;}if(s){p->next=(SnakeList)malloc(sizeof(Snake));p->next->bx=x;p->next->by=y;p->next->next=NULL;}}void setfood(int *fx,int *fy,SnakeList al,SnakeList bl)/*设置随机食物*/ {int a,b,s=1,s2;SnakeList slist1;SnakeList slist2;setfillstyle(SOLID_FILL,6);srand(time(NULL));for(;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(slist1==NULL&&slist2==NULL)s=0;}*fx=a;*fy=b;bar(*fx,*fy,*fx+20,*fy+20);}void qufood(int fx,int fy)/*消去食物*/setfillstyle(SOLID_FILL,2);bar(fx,fy,fx+20,fy+20);}void freesnake(SnakeList psnake)/*释放蛇的空间。
*/{SnakeList p;p=psnake->next;psnake->next=NULL;for(;p;){p=psnake->next;free(psnake);psnake=p;}}int nothit(SnakeList snake1,SnakeList snake2,int x,int y)/*检查snake1是否撞击snake2。
*/ {int a1,a2,b1,b2,bj=1;a1=snake1->bx;b1=snake1->by;a1=a1+x*(SIZE+2);b1=b1+y*(SIZE+2);a2=snake2->bx;b2=snake2->by;for(;snake2&&bj;){if(a1==a2&&b1==b2){bj=0;}else{snake2=snake2->next;a2=snake2->bx;b2=snake2->by;}return bj;}void paintpig()/*画一个猪头。
*/{setwritemode(1);/*设置绘制线条的模式,1表示异或模式*/setfillstyle(1,2);/*设置图片填充模式和颜色,1表示实色填充,2表示绿色*/pieslice(220,140,0,360,80);/*画耳朵*/pieslice(420,140,0,360,80);setfillstyle(1,6);pieslice(320,240,0,360,150);/*画脸*/arc(260,180,30,160,30);/*画眉毛*/arc(380,180,30,160,30);setfillstyle(1,4);sector(320,260,0,360,50,40);/*画鼻子*/setfillstyle(1,8);pieslice(300,260,0,360,5);/*鼻孔*/pieslice(340,260,0,360,5);arc(320,260,230,310,60);/*画嘴*/}void numtostr(int n,char a[3])/*把一个数字n转换成字符串,并存储在a中。
*/{int w;w=n%10;a[1]=w+'0';w=n/10;a[0]=w+'0';a[2]='\0';}void main(){int gr=DETECT,gm;int s1=0,s2=0,fx1=0,fy1=1,fx2=0,fy2=1;/*s1表示snake1的身长,s2表示snake2的身长;fx*,fy*表示蛇头运动方向;*/int dx,dy,add1,add2,k,k1=1,k2=1;/*dx,dy表示食物的位置;add*=1表示是否吃到食物;k*=1表示蛇头越界*/int i=1,j=0,q=1;/*j=0表示只有一条蛇,j=1表示有两条蛇,j=2表示没有蛇退出游戏*/ unsigned long t;/*t表示延迟的时间*/char w[3];SnakeList lsnake1,lsnake2;/*registerbgidriver(EGAVGA_driver);*//*detectgraph(*gr, *gm);*/initgraph(&gr,&gm,"");/*设置屏幕为图形模式*/ cleardevice();/*清屏函数*/for(;q;){cleardevice();paintpig();settextstyle(0,0,3);outtextxy(100,10," I like pigs!");settextstyle(1,0,2);outtextxy(300,400,"One snake!");outtextxy(300,420,"Two snakes!");outtextxy(300,440,"Quit Game");outtextxy(450,420,"Producer:Chen Chen");outtextxy(450,440,"Time:May 23 2012");outtextxy(30,370,"Keys used:Arrow keys");outtextxy(30,380,"'W''A''S'D'");outtextxy(30,390," 'enter'"); setfillstyle(1,4);pieslice(280,400+j*20,0,360,5);i=1;for(;i;){k=bioskey(0);/*获取按键的扫描码*/sound(1300);/*响声函数*/delay(10000);/*延迟函数*/nosound();/*关闭响声函数*/switch (k){case 20480:/*向下键的扫描码*/setfillstyle(1,0);pieslice(280,400+j*20,0,360,5);j=(j+1)%3;setfillstyle(1,4);pieslice(280,400+j*20,0,360,5);break;case 7181:/*enter键的扫描码*/if(j==0||j==1){i=0;}else{closegraph();/*关闭图形模式的函数*/exit(0); /*退出程序函数*/}break;default : break;}}cleardevice();setfillstyle(SOLID_FILL,2);/*设置填充图案的模式和颜色*/ bar(LEFT,TOP,RIGHT,BOTTOM);/*绘画屏幕*/lsnake1=createsnake(496,140);/*生成lsnake1的蛇头*/ lsnake2=createsnake(144,140);/*生成lsnake2的蛇头*/ setfood(&dx,&dy,lsnake1,lsnake2);k1=1,k2=1;fx1=0,fy1=1,fx2=0,fy2=1;s1=0,s2=0;t=100000;for(;(k1||k2)&&(s1+s2<SL);){add1=0;add2=0;if(kbhit())/*kbhit()函数检测是否有按键*/{sound(1200);delay(10000);k=bioskey(0);/*获取按键的扫描码*/delay(1000);nosound();switch(k){case 19200: /*按向左键*/if(fx1==0){fx1=-1;fy1=0;}break;case 19712: /*按向右键*/ if(fx1==0){fx1=1;fy1=0;}break;case 18432: /*按向上键*/ if(fy1==0){fx1=0;fy1=-1;} break;case 20480: /*按向下键*/ if(fy1==0){fx1=0;fy1=1;}break;case 7777: /*按向左键*/ if(fx2==0){fx2=-1;fy2=0;}break;case 8292: /*按向右键*/ if(fx2==0){fx2=1;fy2=0;}break;case 4471: /*按向上键*/ if(fy2==0)fx2=0;fy2=-1;}break;case 8051: /*按向下键*/if(fy2==0){fx2=0;fy2=1;}break;default: break;}}if(j==1){if((lsnake2->bx)>=LEFT&&(lsnake2->bx+SIZE)<=RIGHT&&(lsn ake2->by)>=TOP&&(lsnake2->by+SIZE)<=BOTTOM)/*检测snake2的蛇头是否出界*/{if((lsnake2->bx==dx)&&(lsnake2->by==dy))/*检测snake2是否吃到食物*/ {t=t-800;setfillstyle(1,0);bar(48,60,88,100);qufood(dx,dy);add2=1;s2++;setfillstyle(1,1);bar(48,60,88,100);numtostr(s2,w);outtextxy(58,70,w);outtextxy(58,80,"Blue");}}else{k2=0;/*freesnake(lsnake2);*/}if(k2&&nothit(lsnake2,lsnake1,fx2,fy2))paintsnake(lsnake2,2,2);/*消去蛇*/changsnake(lsnake2,fx2,fy2,add2);/*蛇身坐标移动*/paintsnake(lsnake2,14,1);/*绘制蛇*/}}else{k2=0;paintsnake(lsnake2,14,9);}if((lsnake1->bx)>=LEFT&&(lsnake1->bx+SIZE)<=RIGHT&&(lsn ake1->by)>=TOP&&(lsnake1->by+SIZE)<=BOTTOM){if((lsnake1->bx==dx)&&(lsnake1->by==dy)){t=t-800;/*t减小表示延迟减小,即表示蛇移动速度加快*/setfillstyle(1,0);bar(540,60,600,100);qufood(dx,dy);add1=1;s1++;setfillstyle(1,4);bar(560,60,600,100);numtostr(s1,w);outtextxy(570,70,w);outtextxy(570,80,"Red");}}else{k1=0;/*freesnake(lsnake1);*/}if(k1&& nothit(lsnake1,lsnake2,fx1,fy1)){paintsnake(lsnake1,2,2);changsnake(lsnake1,fx1,fy1,add1);paintsnake(lsnake1,5,4);}if(add1==1||add2==1)/*当两条蛇有一条吃到食物是,重新设置食物*/ setfood(&dx,&dy,lsnake1,lsnake2);/*产生食物*/delay(t);delay(t);delay(t);delay(t);delay(t);}if(j==0){if(s1==SL){settextstyle(0,0,3);outtextxy(100,200,"You are Win!");getch();settextstyle(1,0,1);}else{settextstyle(0,0,5);outtextxy(100,200,"GAME OVER !");getch();settextstyle(1,0,1);}}else{if(s1>s2){settextstyle(0,0,5);outtextxy(100,200,"Red Win!");getch();settextstyle(1,0,1);}else if(s1<s2){settextstyle(0,0,5);outtextxy(100,200,"Blue Win!"); getch();settextstyle(1,0,1);}else{settextstyle(0,0,5);outtextxy(100,200,"A draw!"); getch();settextstyle(1,0,1);}}}}。