贪吃蛇代码.docx

上传人:b****4 文档编号:24267733 上传时间:2023-05-25 格式:DOCX 页数:19 大小:18.07KB
下载 相关 举报
贪吃蛇代码.docx_第1页
第1页 / 共19页
贪吃蛇代码.docx_第2页
第2页 / 共19页
贪吃蛇代码.docx_第3页
第3页 / 共19页
贪吃蛇代码.docx_第4页
第4页 / 共19页
贪吃蛇代码.docx_第5页
第5页 / 共19页
点击查看更多>>
下载资源
资源描述

贪吃蛇代码.docx

《贪吃蛇代码.docx》由会员分享,可在线阅读,更多相关《贪吃蛇代码.docx(19页珍藏版)》请在冰豆网上搜索。

贪吃蛇代码.docx

贪吃蛇代码

贪吃蛇编写思路及C语言源码

规则:

每吃上一个点,就长大一点,不能撞墙或者撞上自己身体。

教训:

在有限的空间里,可以贪吃,但是要注意安全哦。

这个游戏是我刚工作一年的时候写的,当时对C语言有了些了解,想做点东西,刚好看到同事的手机上有这个

游戏(那时我还没有手机呢,呵呵),觉得挺好玩,要实现的话也还比较简单,就在电脑上用TC上做出来了

刚做出来的时候,那种喜悦,确实不是玩别人的游戏所能带来的。

这个程序是入门级别的,适合编程刚入门的朋友们试一试。

高手达人们请跳过。

下面我简单说一下我的思路,理解下面几点会比较清楚一些。

1,基础:

你首先要能画出一个带颜色的方块。

举一反三:

可以画一个就可以画很多个了。

(TC中画方块用到了这个文件)

2,移动:

一个方块消失,相邻地方一个方块出现,在视觉上就是移动了。

3,消失:

用背景颜色在同样的地方画同样大小的方块。

4,相对坐标:

视觉上像素这个单位太小,用方块的大小作为相对坐标的单位。

5,随机点:

使用伪随机函数,参数一般用上系统当前时间,你再随意捏造个四则运算,就会产生出独一无二

的随机数了。

6,链表:

这个是精髓啊,你看那蛇不是就像一个链表吗,这个可是我认为在这个游戏中使用的最高深的结构

了,呵呵。

7,长大:

链表头遇上一个食物(随机产生的方块),链表上添加一个节点。

8,死亡:

链表头撞上了自身或者撞墙。

也就这么多,理解了这几点,整个框架也就出来了。

下面就是源代码:

#include<>

#include<>

#include<>

#include<>

#defineLENsizeof(structlist)

#defineucharunsignedchar

intn,sco,r,t,speed=8800;

uchari,a,x1,y1,hit,long1=16,cycy=0,str1[200],xisu;

structlist

{

intx,y;

    structlist*next,*last;

};

structlist*p1,*p2,*head,*eof1;

voidup(void);                voiddown();

voidleft();                  voidright();

voidunit(uchar,uchar,uchar);voidscore();

voiddl(int);                 voidsave1();

voidload1();                 voidsave();

main()

{

intdriver=VGA,mode=VGAHI;

unsignedcharss[8];

initgraph(&driver,&mode,"c:

\\tc");

setfillstyle(1,8);

setcolor(8);

bar(2,2,600,400);

setfillstyle(1,7);   

setcolor(7);

       bar(117,117,170,140);

setfillstyle(1,8);

setcolor(8);

bar(118,118,170,140);

setcolor(14);

/*settextstyle(2,0,5);*/

sprintf(ss,"enter");

outtextxy(120,125,ss);

setcolor(0);

line(118,141,171,141);

line(118,142,172,142);

line(171,118,171,141);

line(172,118,172,142);

setfillstyle(1,14);

setcolor(14);

circle(15,15,10);sprintf(ss,"fast");    /*threestatuses,selectuse'Tab'*/

outtextxy(30,15,ss);

       circle(15,55,10);sprintf(ss,"normol");

outtextxy(30,55,ss);

       circle(15,95,10);sprintf(ss,"slow");

outtextxy(30,95,ss);

circle(15,15,6);floodfill(15,15,14);xisu=0;

do

{

           a=getch();

if(a==9)       /*Tab*/

{

  setcolor(14);setfillstyle(1,14);

  a=8;      

  xisu++;

  circle(15,15+(xisu%3)*40,6);

  floodfill(15,15+(xisu%3)*40,14);

  setfillstyle(1,7);     

  setcolor(7);

  circle(15,15+((xisu-1)%3)*40,6);

  floodfill(15,15+((xisu-1)%3)*40,7);

}

if((a==76)||(a==108))      /*'L'or'l'loadthestatusoflasttime*/

{

  load1();

  xisu=2;

  gotoLOOP;

}

}

       while(a!

=13);

setcolor(8);

line(118,141,171,141);  

line(171,118,171,141);

dl(speed);

p2=(structlist*)malloc(LEN);       

hit=1;         /*headofsnake*/

    p1=p2;head=p2;

    p2->x=20;

p2->y=20;

p2->last=NULL;

p2->next=NULL;

rectangle(0,0,600,450);

setfillstyle(1,0);

setcolor(0);

bar(1,1,599,449);    

setfillstyle(1,14);

setcolor(14);

speed=speed*(xisu%3+1);

for(i=0;i<15;i++)                              /*16unitswhencreating*/

{

p2=(structlist*)malloc(LEN);

p2->x=21+i;

p2->y=20;     

unit(21+i,20,14);

               p1->next=p2;

p2->last=p1;  

p2->next=NULL;

eof1=p2;

p1=p2;

}

unit(head->x,head->y,14);

/*srand(100);*/

LOOP:

  while

(1)

{

if(hit==1)     /*createarandomunit*/

{

  randomize();

  x1=random(30);

  y1=random(20);

  x1=x1+3;

  y1=y1+3;

  p2=head;

  p1=p2;

  while(p1->next!

=NULL)

  {

   if((x1==p2->x)&&(y1==p2->y))/*maketheunitnotinthesnake*/

   {

    x1=random(30)+3;y1=random(20)+3;p2=head;p1=p2;

   }/*ifin,new*/

   else

   {

    p1=p2;

    p2=p1->next;

   }

  }

  unit(x1,y1,14);

  hit=0;

}

if(kbhit()!

=0)

        a=getch();

switch(a)               /*dowithkey-down*/

      {

  case72:

up();   break;

  case80:

down();break;

  case75:

left();break;

  case77:

right();break;

  case83:

  case115:

dl(500);save1();break;/*'S'or's'savestatusandexit*/

}

if(cycy==1)

    break;

}

do

{

   a=getch();

}

while((a>14)||(a<13));   /*while'Enter',exit*/

closegraph();

}

voidup(void)

{

p2=head->next;

p1=p2;

while(p1->next!

=NULL)

{

if((head->x==p2->x)&&(head->y-1==p2->y))/*ifhitself,out*/

{

  score();   

  break;

}

else

{

  p1=p2;

  p2=p1->next;

}

}

if((hit==0)&&(head->x==x1)&&(head->y-1==y1))/*ifhitnewunit,add*/

{

p2=(structlist*)malloc(LEN);

     p2->x=x1;

p2->y=y1;

p2->next=head;

head->last=p2;

head=p2;                    hit=1;long1++;

}

else

{

p2=eof1;

eof1=p2->last;

/*p2->last=NULL;*/

eof1->next=NULL;/*eof1disappear,headadd1unit*/

unit(p2->x,p2->y,0);

p2->x=head->x;

p2->y=head->y-1;

p2->next=head;

head->last=p2;

head=p2;

head->last=NULL;

unit(p2->x,p2->y,14);

if(p2->y<1)

    score();

}

   dl(speed);

}

voiddown(void)

{

  p2=head->next;p1=p2;

while(p1->next!

=NULL)

{

if((head->x==p2->x)&&(head->y+1==p2->y))/*hitself,out*/

{

  score();

  break;

}

else

{

  p1=p2;

  p2=p1->next;

}

}

if((hit==0)&&(head->x==x1)&&(head->y+1==y1))/*hitunit,add*/

{

p2=(structlist*)malloc(LEN);

     p2->x=x1;p2->y=y1;

     p2->next=head;

head->last=p2;

head=p2;  

hit=1;

long1++;

}

else

{

p2=eof1;

eof1=p2->last;

eof1->next=NULL;

unit(p2->x,p2->y,0);

p2->next=head;

head->last=p2;

p2->x=head->x;

p2->y=head->y+1;

head=p2;

head->last=NULL;

unit(p2->x,p2->y,14);

if(p2->y>=30)            /*hitwall,out*/

            score();

}

dl(speed);

}

voidleft(void)

{   

p2=head->next;p1=p2;

while(p1->next!

=NULL)

{

if((head->x-1==p2->x)&&(head->y==p2->y))

{

  score();

  break;

}

else

{

  p1=p2;

  p2=p1->next;

}

}

if((hit==0)&&(head->x-1==x1)&&(head->y==y1))

{

p2=(structlist*)malloc(LEN);

     p2->x=x1;

p2->y=y1;

     p2->next=head;

head->last=p2;

head=p2;        

hit=1;

long1++;

}

else

{

        p2=eof1;

eof1=p2->last;

eof1->next=NULL;

unit(p2->x,p2->y,0);

p2->next=head;

head->last=p2;

p2->x=head->x-1;

p2->y=head->y;

head=p2;

head->last=NULL;

unit(p2->x,p2->y,14);

if(p2->x<1)

           score();

}

       dl(speed);

}

voidright(void)

{  

p2=head->next;p1=p2;

while(p1->next!

=NULL)

{

if((head->x+1==p2->x)&&(head->y==p2->y))

{

  score();

  break;

}

else

{

  p1=p2;

  p2=p1->next;

}

}

if((hit==0)&&(head->x+1==x1)&&(head->y==y1))

{

p2=(structlist*)malloc(LEN);

     p2->x=x1;

p2->y=y1;

     p2->next=head;

head->last=p2;

head=p2;

hit=1;

long1++;

}

else

       {    

p2=eof1;

eof1=p2->last;

eof1->next=NULL;

unit(p2->x,p2->y,0);

p2->next=head;

head->last=p2;

p2->x=head->x+1;

p2->y=head->y;

head=p2;

head->last=NULL;

unit(p2->x,p2->y,14);

if(p2->x>=40)

score();

}

   dl(speed);

}

voiddl(inta)

{

intr,n;

    for(r=0;r

          for(n=0;n<6000;n++)

{

  n++;

  n--;

}

}

voidunit(ucharx,uchary,ucharcolor)

{

setfillstyle(1,color);

    setcolor(color);

bar(x*15,y*15,(x+1)*15-2,(y+1)*15-2);

}

voidscore(void)

{

       ucharss[20];

if(long1>50)

   sco=(long1-50)*3+(long1-30)*2+14;

elseif(long1>30)

   sco=(long1-30)*2+14;

    else

   sco=long1-16;

    sprintf(ss,"yourscoreis%d",sco);

outtextxy(50,50,ss);

cycy=1;

    save();

}

voidsave(void)

{

FILE*fp;

inti;

i=0;

fp=fopen("","rb+");

i=(int)(fgetc(fp))-48;

if(fp==NULL)

          i=0;

       fclose(fp);

if(i

{

fp=fopen("","wb+");

   fprintf(fp,"%d",sco);

   fclose(fp);

   printf("\n");

   printf("Yourscore:

%darethehighest!

",sco);

}

/*getch();*/

}

voidsave1()

{

  FILE*fl1;

  i=0;

  dl(1500);

  p1=head;

  p2=p1;

  while(p1!

=NULL)

{

str1[i]=(char)(p1->x+30);

   str1[i+1]=(char)(p1->y+30);

   i=i+2;

   p2=p1;p1=p2->next;

}

dl(1500);

  fl1=fopen("","w+");

  if(fl1!

=NULL)

  {

fwrite(str1,i,1,fl1);

dl(1500);

fclose(fl1);

  }

  else

printf("bad\n");

  dl(1500);

  cycy=1;

}

voidload1()

{

FILE*fl1;

  intcc;

  i=0;

  fl1=fopen("","r");

  if(fl1!

=NULL)

{

fseek(fl1,0,SEEK_SET);

   while(feof(fl1)==0)

{  

  str1[i++]=fgetc(fl1);

}

   cc=i-1;

  /*fread(str1,200,1,fl1);*/

   cc=ftell(fl1);

   fclose(fl1);

}

  else

printf("bad\n");

       setcolor(8);

line(118,141,171,141);  

line(171,118,171,141);

       dl(speed);

p2=(structlist*)malloc(LEN);       hit=1;

    p1=p2;

head=p2;

p2->x=(int)(str1[0])-30;

p2->y=(int)(str1[1])-30;

p2->last=NULL;

p2->next=NULL;

rectangle(0,0,600,450);

setfillstyle(1,0);

setcolor(0);

bar(1,1,599,449);    

setfillstyle(1,14);

setcolor(14);

xisu=1;

speed=speed*(xisu%3+1);

for(i=2;i

{

p2=(structlist*)malloc(LEN);

p2->x=(int)(str1[i])-30;

p2->y=(int)(str1[i+1])-30;

unit(p2->x,p2->y,14);

p1->next=p2;

p2->last=p1;  

p2->next=NULL;

eof1=p2;

p1=p2;

}

long1=cc/2;

unit(head->x,head->y,14);

}

里面有个速度设置,这样一句话:

“speed=8800;”其实是个延迟时间,各位根据电脑CPU的速度可以改动一下,到你自己舒服为止哦。

我做了个存储功能,记分功能,还可以加一些其他功能的,有兴趣的朋友可以试一下,加上了通知我一声啰。

精心搜集整理,只为你的需要

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > PPT模板 > 节日庆典

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1