双打贪吃蛇游戏,值得一玩
本人费尽心血,终于编出这个游戏,自认为还可以。游戏规则很简单,如下:
单人玩时,只有方向键和Esc键起作用;
双人玩时,增加w,s,a,d为方向键;
当碰到墙壁或者对方时,就会损失一条命;长度每增加20,就会获得一条命;
如果你想加快游戏速度,只要增大delay(n)函数中的中的n值即可;
#include<graphics.h>
#include<stdio.h>
#include<stdlib.h>
#define PS1(X) rectangle(140+100*(X%4),100+100*(X/4),200+100*(X%4),160+100*(X/4));
#define PS2(X) rectangle(138+100*(X%4),98+100*(X/4),202+100*(X%4),162+100*(X/4));
#define RUN1(B,C,D) { B++; putimage(C[i][0],C[i][1],D,COPY_PUT); do{x=random(620)/9*9+9; y=(random(410)/9*9+57);}while(getpixel(x,y)!=0); k=random(15)+1;setfillstyle(SOLID_FILL,k);bar(x,y,x+8,y+8);}
#define RUN2(A,B,C,D) { A--; for(j=i-1;j>=i-B;j--) { k=j; if(k<0) k+=100;putimage(C[k][0],C[k][1],store,COPY_PUT);} B=1; if(A!=0) {do{ C[i][0]=random(620)/9*9+9;C[i][1]=(random(410)/9*9+57);} while(getpixel(C[i][0],C[i][1])!=0); putimage(C[i][0],C[i][1],D,COPY_PUT);}}
#define RUN3(B,C,D) { dis=i-B;if(dis<0) dis+=100;putimage(C[i][0],C[i][1],D,COPY_PUT); putimage(C[dis][0],C[dis][1],store,COPY_PUT);}
main()
{
char *store_1,*store_2,*store;
int x1,y1,x2,y2,i,j,k,cr_1=0,cr_2=0;
int pos_1[201][2],pos_2[201][2],life_1=10,life_2=0,gay;
int m,n,dir_1,dir_2,dis,length_1=1,length_2=1;
int x,y,flag,Driver=DETECT, Mode;
initgraph(&Driver,&Mode,"");
loop_1: cleardevice(); setcolor(GREEN);
setbkcolor(0); settextstyle(1,0,4); settextjustify(1,1);
outtextxy(320,150,"This game is made by LiHongyong");
outtextxy(320,200,"Have a good time !"); sleep(4); cleardevice();
outtextxy(320,150,"1. player"); outtextxy(325,250,"2. players");
setcolor(RED); rectangle(220,120,420,180);
while(1)
{ while(!bioskey(1)){}
if((k=getch())==13) break;
else if((k=getch())==80)
{ gay=2;life_2=10; setcolor(0); rectangle(220,120,420,180);
setcolor(GREEN); rectangle(220,220,420,280);}
else if(k==72)
{ gay=1;life_2=0; setcolor(0); rectangle(220,220,420,280);
setcolor(RED); rectangle(220,120,420,180);}
} cleardevice();
outtextxy(320,50,"Please select snake's color");
for(k=0;k<16;k++)
{ m=k%4; n=k/4; setfillstyle(SOLID_FILL,k);
bar(150+100*m,110+100*n,190+100*m,150+100*n);}
setcolor(RED); rectangle(140,100,200,160);
if(gay==2) { setcolor(GREEN); rectangle(138,98,202,162);}
while(1)
{ while(!bioskey(1)){}
if((k=getch())==13) break;
if(gay==2&&k!=0)
switch(k)
{ case 'w': case'W':
if(cr_2>3)
{ i=cr_2; cr_2-=4; setcolor(0); PS2(i)
setcolor(GREEN); PS2(cr_2) } break;
case 's': case 'S':
if(cr_2<12)
{ i=cr_2; cr_2+=4; setcolor(0); PS2(i)
setcolor(GREEN); PS2(cr_2) } break;
case 'a': case 'A':
if(cr_2>0)
{ i=cr_2; cr_2--; setcolor(0); PS2(i)
setcolor(GREEN); PS2(cr_2) } break;
case 'd': case 'D':
if(cr_2<15)
{ i=cr_2; cr_2++; setcolor(0); PS2(i)
setcolor(GREEN); PS2(cr_2) } break; }
else
switch(k=getch())
{ case 72: if(cr_1>3)
{j=cr_1; cr_1-=4; setcolor(0); PS1(j) setcolor(RED); PS1(cr_1)}break;
case 80: if(cr_1<12)
{j=cr_1; cr_1+=4; setcolor(0); PS1(j) setcolor(RED); PS1(cr_1)} break;
case 75: if(cr_1>0)
{j=cr_1; cr_1--; setcolor(0); PS1(j) setcolor(RED); PS1(cr_1)}break;
case 77: if(cr_1<15)
{j=cr_1; cr_1++; setcolor(0); PS1(j) setcolor(RED); PS1(cr_1) } break; }
} cleardevice();
pos_1[0][0]=90; pos_1[0][1]=120; dir_1=77; x1=90; y1=120;
pos_2[0][0]=90; pos_2[0][1]=210; dir_2=77; x2=90; y2=210;
store=malloc(imagesize(0,0,8,8)); getimage(0,0,8,8,store);
store_1=malloc(imagesize(0,0,8,8)); setfillstyle(SOLID_FILL,cr_1);
bar(pos_1[0][0],pos_1[0][1],pos_1[0][0]+8,pos_1[0][1]+8);
getimage(pos_1[0][0],pos_1[0][1],pos_1[0][0]+8,pos_1[0][1]+8,store_1);
if(gay==2)
{ store_2=malloc(imagesize(0,0,8,8)); setfillstyle(SOLID_FILL,cr_2);
bar(pos_2[0][0],pos_2[0][1],pos_2[0][0]+8,pos_2[0][1]+8);
getimage(pos_2[0][0],pos_2[0][1],pos_2[0][0]+8,pos_2[0][1]+8,store_2);}
loop_2:setfillstyle(6,YELLOW); bar(0,48,639,56); bar(0,471,639,479);
bar(0,48,8,479); bar(630,48,639,479); gotoxy(15,2); printf("snake 1");
gotoxy(6,3); printf("LIFE : %-9d",life_1); gotoxy(23,3); printf("length : %-3d",length_1);
if(gay==2) { gotoxy(57,2); printf("snake 2");
gotoxy(48,3);printf("LIFE : %-9d",life_2); gotoxy(65,3); printf("length : %-3d",length_2);}
do { x=random(620)/9*9+9; y=random(410)/9*9+57;} while(getpixel(x,y)!=0);
k=random(15)+1; setfillstyle(SOLID_FILL,k); bar(x,y,x+8,y+8);
for(i=1;i<101;i++)
{ if(i==100) i=0;
if(!bioskey(1))
{ if(life_1==0&&life_2==0) goto loop_3;
if(life_1!=0)
switch(dir_1)
{ case 72: { y1-=9; pos_1[i][0]=x1; pos_1[i][1]=y1;
if(x1==x&&y1==y) {RUN1(length_1,pos_1,store_1)if(length_1%20==0)goto loop_3;
gotoxy(32,3); printf("%-3d",length_1);}
else if(y1<57||getpixel(x1,y1)!=0)
{ RUN2(life_1,length_1,pos_1,store_1) x1=pos_1[i][0];y1=pos_1[i][1];gotoxy(13,3);
if(life_1!=0) {printf("%-9d",life_1); gotoxy(32,3); printf("1 ");}else printf("GAME OVER");}
else RUN3(length_1,pos_1,store_1) } break;
case 80: { y1+=9; pos_1[i][0]=x1; pos_1[i][1]=y1;
if(x1==x&&y1==y) {RUN1(length_1,pos_1,store_1)if(length_1%20==0)goto loop_3;
gotoxy(32,3); printf("%-3d",length_1);}
else if(y1>470||getpixel(x1,y1)!=0)
{ RUN2(life_1,length_1,pos_1,store_1) x1=pos_1[i][0];y1=pos_1[i][1];gotoxy(13,3);
if(life_1!=0) {printf("%-9d",life_1); gotoxy(32,3); printf("1 ");}else printf("GAME OVER");}
else RUN3(length_1,pos_1,store_1) } break;
case 75: { x1-=9; pos_1[i][0]=x1; pos_1[i][1]=y1;
if(x1==x&&y1==y) {RUN1(length_1,pos_1,store_1)if(length_1%20==0)goto loop_3;
gotoxy(32,3); printf("%-3d",length_1);}
else if(x1<9||getpixel(x1,y1)!=0)
{ RUN2(life_1,length_1,pos_1,store_1) x1=pos_1[i][0];y1=pos_1[i][1];gotoxy(13,3);
if(life_1!=0) {printf("%-9d",life_1); gotoxy(32,3); printf("1 ");}else printf("GAME OVER");}
else RUN3(length_1,pos_1,store_1) } break;
case 77: { x1+=9; pos_1[i][0]=x1; pos_1[i][1]=y1;
if(x1==x&&y1==y) {RUN1(length_1,pos_1,store_1)if(length_1%20==0)goto loop_3;
gotoxy(32,3); printf("%-3d",length_1);}
else if(x1>629||getpixel(x1,y1)!=0)
{ RUN2(life_1,length_1,pos_1,store_1) x1=pos_1[i][0];y1=pos_1[i][1];gotoxy(13,3);
if(life_1!=0) {printf("%-9d",life_1); gotoxy(32,3); printf("1 ");}else printf("GAME OVER");}
else RUN3(length_1,pos_1,store_1) } break;
}
if(gay==2&&life_2!=0)
switch(dir_2)
{ case 72:{ y2-=9; pos_2[i][0]=x2; pos_2[i][1]=y2;
if(x2==x&&y2==y) {RUN1(length_2,pos_2,store_2)if(length_2%20==0) goto loop_3;
gotoxy(74,3); printf("%-3d",length_2);}
else if(y2<57||getpixel(x2,y2)!=0)
{ RUN2(life_2,length_2,pos_2,store_2) x2=pos_2[i][0];y2=pos_2[i][1];gotoxy(55,3);
if(life_2!=0){printf("%-9d",life_2); gotoxy(74,3); printf("1 ");}else printf("GAME OVER");}
else RUN3(length_2,pos_2,store_2)} break;
case 80:{ y2+=9; pos_2[i][0]=x2; pos_2[i][1]=y2;
if(x2==x&&y2==y) {RUN1(length_2,pos_2,store_2)if(length_2%20==0)goto loop_3;
gotoxy(74,3); printf("%-3d",length_2);}
else if(y2>470||getpixel(x2,y2)!=0)
{ RUN2(life_2,length_2,pos_2,store_2) x2=pos_2[i][0];y2=pos_2[i][1];gotoxy(55,3);
if(life_2!=0){printf("%-9d",life_2); gotoxy(74,3); printf("1 ");}else printf("GAME OVER");}
else RUN3(length_2,pos_2,store_2) } break;
case 75:{ x2-=9; pos_2[i][0]=x2; pos_2[i][1]=y2;
if(x2==x&&y2==y) {RUN1(length_2,pos_2,store_2)if(length_2%20==0)goto loop_3;
gotoxy(74,3); printf("%-3d",length_2);}
else if(x2<9||getpixel(x2,y2)!=0)
{ RUN2(life_2,length_2,pos_2,store_2) x2=pos_2[i][0];y2=pos_2[i][1];gotoxy(55,3);
if(life_2!=0){printf("%-9d",life_2); gotoxy(74,3); printf("1 ");}else printf("GAME OVER");}
else RUN3(length_2,pos_2,store_2) } break;
case 77:{ x2+=9; pos_2[i][0]=x2; pos_2[i][1]=y2;
if(x2==x&&y2==y) {RUN1(length_2,pos_2,store_2)if(length_2%20==0)goto loop_3;
gotoxy(74,3); printf("%-3d",length_2);}
else if(x2>629||getpixel(x2,y2)!=0)
{ RUN2(life_2,length_2,pos_2,store_2) x2=pos_2[i][0];y2=pos_2[i][1];gotoxy(55,3);
if(life_2!=0){printf("%-9d",life_2); gotoxy(74,3); printf("1 ");}else printf("GAME OVER");}
else RUN3(length_2,pos_2,store_2) } break;
} for(j=0;j<50;j++) delay(1000);
}
else if((k=getch())!=0)
{ i--; if(k==27) goto loop_4;
if(gay==2)
{ if((k== 'w'||k=='W')&&dir_2!=80) dir_2=72;
else if((k== 's'||k== 'S')&&dir_2!=72) dir_2=80;
else if((k== 'a'||k== 'A')&&dir_2!=77) dir_2=75;
else if((k== 'd'||k== 'D')&&dir_2!=75) dir_2=77;
}
}
else {i--; k=getch();
if(k==72&&dir_1!=80) dir_1=72;
else if(k==80&&dir_1!=72) dir_1=80;
else if(k==75&&dir_1!=77) dir_1=75;
else if(k==77&&dir_1!=75) dir_1=77;
}
}
loop_3: cleardevice(); settextstyle(1,0,4); settextjustify(1,1);
if(length_1%20==0){ length_1+=5; outtextxy(320,180,"Snake 1 -- you have get one life");
outtextxy(320,240,"Congratulations");life_1++; sleep(3); cleardevice(); goto loop_2;}
else if(length_2%20==0){ length_2+=5;outtextxy(320,180,"Snake 2 -- you have get one life");
outtextxy(320,240,"Congratulations");life_2++; sleep(3); cleardevice(); goto loop_2;}
else if(life_1==0&&life_2==0)
{outtextxy(320,100,"GAME OVER");
loop_4: settextstyle(1,0,2); setcolor(GREEN);
outtextxy(320,240,"Play again Quit");
rectangle(150,220,300,260);
while(1)
{ while(!bioskey()){}
if((k=getch())!=0&&k==13) break;
if((k=getch())==77)
{ flag=0;
setcolor(0); rectangle(150,220,300,260);
setcolor(GREEN); rectangle(380,220,500,260);}
else if(k==75)
{ flag=1; life_1=10; life_2=10; cr_1=0; cr_2=0;
setcolor(0); rectangle(380,220,500,260);
setcolor(RED); rectangle(150,220,300,260);}
} cleardevice();
if(flag) goto loop_1; closegraph();}
}