C语言写的《贪吃蛇》

写这个纯属为了向同学证明“C语言什么也编不出来”这句话的错误性,耗时两节课,其它的就不多说了,附上源码,欢迎指教。有兴趣的可以点下面的链接下载游戏,解压后即可运行:

点击这里下载

——下面是《贪吃蛇》源码——-

#include<graphics.h>
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<time.h>
#define ESC 283
#define UP 18432
#define DOWN 20480
#define LEFT 19200
#define RIGHT 19712
/*lugede 2007.4.25*/

void main()
{
int gdrive,gmode;
int an,ano;
int mx=15,my=10,r=38,rs=36,l=3;
int i,j;
int rnd,ex,ey,exx,eyy;
int x,y,xo,yo;
int xx,yy,xxo,yyo;
int lv;
int xs[999],ys[999];

gdrive=DETECT;
initgraph(&gdrive,&gmode,”..\BGI”);
srand(time(NULL));
setbkcolor(BLACK);

gotoxy(30,1);
printf(“—- Snake v0.1—-    C 2007www.lugede.cn”);

setcolor(WHITE);
rectangle(26,26,600,410);

start:

/*lv++;
if(lv%2==0) dl/=2;
gotoxy(7,1);
printf(“Lv:%d”,lv);
*/

rnd=rand();x=rnd%(mx-1)+1;
rnd=rand();y=rnd%(my-1)+1;

rndx1:rnd=rand();ex=rnd%(mx-1)+1;
for(i=1;i<l;i++) if(xs[i]==ex) gotorndx1;
rndy1:rnd=rand();ey=rnd%(my-1)+1;
for(j=1;j<l;j++) if(ys[j]==ey) gotorndy1;
exx=ex*r-10;eyy=ey*r-10;
setfillstyle(1,WHITE);
circle(exx+r/2-1,eyy+r/2-1,rs/2);
floodfill(exx+r/2,eyy+r/2,WHITE);

anjian:

if(bioskey(1))
{
an=bioskey(0);
}

if(an==UP)
{
if(ano==2)y++;
else
{
y–;
ano=1;
}
}
if(an==DOWN)
{
if(ano==1)y–;
else
{
y++;
ano=2;
}
}
if(an==LEFT)
{
if(ano==4)x++;
else
{
x–;
ano=3;
}
}
if(an==RIGHT)
{
if(ano==3)x–;
else
{
x++;
ano=4;
}
}
if(an==ESC) goto end;

along:

delay(99999999);

for(i=2;i<l;i++)
{
if(xs[i]==x&&ys[i]==y&&l>3)goto die;
}
if(x<1||x>mx||y<1||y>my) gotodie;

for(j=l;j>1;j–)
{
xs[j]=xs[j-1];
ys[j]=ys[j-1];
}
xs[1]=x;
ys[1]=y;    /*body record*/

if(x==ex &&y==ey)
{
l++;

xxo=xo*r-10;yyo=yo*r-10;
setfillstyle(1,BLACK);
bar(xxo,yyo,xxo+rs,yyo+rs);

gotoad;  /*pass clean old body*/
}

xxo=xo*r-10;yyo=yo*r-10;
setfillstyle(1,BLACK);
bar(xxo,yyo,xxo+rs,yyo+rs);
goto bd;

ad: /*remakeegg*/

gotoxy(14,1);
printf(“Eaten Eggs:%d”,l-3);

rndx2:rnd=rand();ex=rnd%(mx-1)+1;
for(i=1;i<l;i++) if(xs[i]==ex) gotorndx2;
rndy2:rnd=rand();ey=rnd%(my-1)+1;
for(j=1;j<l;j++) if(ys[j]==ey) gotorndy2;
exx=ex*r-10;eyy=ey*r-10;
setfillstyle(1,WHITE);
circle(exx+r/2-1,eyy+r/2-1,rs/2);
floodfill(exx+r/2,eyy+r/2,WHITE);

bd:
xx=x*r-10;yy=y*r-10;
setfillstyle(1,GREEN);
bar(xx,yy,xx+rs,yy+rs);

xs[1]=x;ys[1]=y;
xo=xs[l];yo=ys[l];

goto anjian;

die:

setfillstyle(1,RED);
for(i=1;i<=l;i++)
&n

bsp;
> {
bar(xs[i]*r-10,ys[i]*r-10,xs[i]*r-10+rs,ys[i]*r-10+rs);
}
gotoxy(30,15);
printf(“You got %d eggs!”,l-3);

getch();

end:
an=getch();
if(an!=27) goto end;
closegraph();

}

C语言写的《贪吃蛇》》有11个想法

  1. 您莫非就是帮魅族写了好几个软件的那个牛人,路个的?没想到在这碰到了。大神!

  2. :?:许多例子里
    在程序中initgraph()函数调用之前加上一句:
    registerbgidriver(EGAVGA_driver);

  3. 前天二姐让我帮她写个C程序,赫然发现,自己竟然连一行C都写不出了。看来,编程真是不用就忘啊。

博主进行回复 取消回复

邮箱地址不会被公开。 必填项已用*标注