荔园在线

荔园之美,在春之萌芽,在夏之绽放,在秋之收获,在冬之沉淀

[回到开始] [上一篇][下一篇]


发信人: autodotcom (!!!!!), 信区: Linux
标  题: 一个小游戏贪吃蛇的原码。
发信站: 荔园晨风BBS站 (Sun Jun  9 12:18:16 2002), 转信

花了一天时间编了个小游戏,现将原码(兼注释)贴上,想玩的兄弟可以编译后运
行。
性能还可以吧。不要担心,不会死机的。
速度可以将第二十行当三十行的#define delaytime 1000的1000改为其他数值。

#include<stdio.h>
#include<graphics.h>
#include<bios.h>
#include<ctype.h>
/*****keybrd******/
#define  _KEYBRD_READY        0x01
#define  _KEYBRD_READ         0x00
#define  _KEYBRD_SHIFSTATUS   0x02
#define  _end       0x11b     /*esc键的扫描码*/
#define  goup       0x4800    /*向上键的。。*/
#define  godown     0x5000
#define  goleft     0x4b00
#define  goright    0x4d00
#define  f1         0x3b00
#define  f2         0x3c00
/****color********/
#define black  0
#define white  15
#define blue   1
#define green  2
/*****struct****/
#define NULL   0
#define LEN  sizeof(struct square)
/*****vect*****/
#define  up     1        /*蛇头的方向*/
#define  down   2
#define  left   3
#define  right  4
/*****drawcircle**/
#define  write 1
#define  clear 0
/******snake*****/
#define  snakecolor white
#define  snakewide  2
#define  delaytime  1000
#define  dead       0
/*****box********/
#define  box_xa  220
#define  box_ya  140
#define  box_xb  423
#define  box_yb  343
/******livebox**************/
#define  live_xa 224
#define  live_ya 144
#define  live_xb 419
#define  live_yb 339
struct square
{int x;
 int y;
 struct square *next;
 struct square *pre;
};
/***********构造n个struct的链表********************/
struct square * initsquare(int n)
{
  struct square *p1,*p2,*head;
  int i;
  p1=head=(struct square *)malloc(LEN);
  for(i=1;i<n;i++)
  {p2=(struct square * )malloc(LEN);
   p1->next=p2;
   p2->pre=p1;
   p1=p2;
  }
  head->pre=p2;
  p2->next=head;
  return(head);
}
/***********初始蛇的坐标写入链表*************************/
void initsnake(struct square *head)
{
 struct square * p;
 int i;
 p=head;
 for(i=0;i<10;i++)
 {p->x=384-5*i;
  p->y=334;
  p=p->next;
 }
 for(i=0;i<10;i++)
 { p->x=339+5*i;
   p->y=329;
   p=p->next;
 }
}
/**********划一个bar中间点位(x,y)********************/
void solidsquare(int x,int y,int n)
{
  int xa,ya,xb,yb;
  xa=x-n;ya=y-n;
  xb=x+n;yb=y+n;
  bar(xa,ya,xb,yb);
}
/**********划蛇****************************/
void drawsnake(struct square*head,struct square *tail,int how)
{struct square *p1;
 int i;
 if(how==write)
   setcolor(white);
 else if(how==clear)
   setcolor(black);
 p1=head;
 do
 {solidsquare(p1->x,p1->y,snakewide);
  p1=p1->next;
  }while(p1!=tail);
}
/**********没键输入时蛇向前走一步,根据蛇头原方向
**************************/
void gonum(int vect,struct square *head)
{struct square*p1;
 p1=head->pre;
 switch(vect)
  {
   case right: {p1->x=head->x+5;p1->y=head->y;break;}
   case left:  {p1->x=head->x-5;p1->y=head->y;break;}
   case up:    {p1->x=head->x;  p1->y=head->y-5;break;}
   case down:  {p1->x=head->x;  p1->y=head->y+5;break;}
  }
 if((head->x==224)||(head->x==419)||(head->y==144)||(head->y==339))
   delay(2*delaytime);
}
/**********gonum后划蛇头擦去蛇尾****************************/
void goact(struct square*head,struct square*tail)
{solidsquare(head->x,head->y,snakewide);
 setfillstyle(1,black);
 solidsquare(tail->next->x,tail->next->y,snakewide);
 setfillstyle(1,white);
}
/*********check是否吃了冬冬******************************/
check(int posi_x,int posi_y,struct square*head)
{if((head->x==posi_x)&&(head->y==posi_y))
  return(1);
 else return(0);
}
/**********check是否蛇头动到边框or蛇身********************/
int check_dead(struct square*head,struct square *tail)
{struct square*p1,*p2;
 p1=head->next;
 do{if((head->x==p1->x)&&(head->y==p1->y))
       return(dead);
     p1=p1->next;
   }
 while(p1!=tail);
 if((head->x<=223)||(head->x>=420)||(head->y<=143)||(head->y>=340))
   return(dead);
   return(1);
}

main()
{
  int gd=DETECT,gmode,maxx,maxy;
  int i,k,live=1,ate=1,vect=right,j;
  int key,score=0;
  int posi_x,posi_y;
  struct square *head,*tail,*p;
  initgraph(&gd,&gmode,"c:\\tc");
  setbkcolor(black);
  setlinestyle(0,0,3);
  setcolor(green);
  rectangle(box_xa,box_ya,box_xb,box_yb);  /*划边框*/
  tail=head=initsquare(1000);
  initsnake(head);
  for(k=0;k<10;k++)
  tail=tail->next;
  drawsnake(head,tail,white);
  printf("\n\n\n\n                           press any key to
begin\n");
  printf("                           press F1 to pause\n");
  printf("                           press F2 to restart\n");
  getch();
  delay(100*delaytime);
  while(live==1)
   {if(ate)                   /*产生一个被吃的冬冬*/
       {
        another:
        posi_x=5*(rand()%40)+3+box_xa+1;
        posi_y=5*(rand()%40)+3+box_ya+1;
        p=head;
        do{if((p->x==posi_x)&&(p->y==posi_y))     /*冬冬不能是蛇本身    */
            goto another;
            p=p->next;
           }while(p!=tail);
        solidsquare(posi_x,posi_y,snakewide);
        ate=0;
       }
    if(bioskey(_KEYBRD_READY)!=0)      /*CHECK是否有键输入,有则输入*/

    {key=bioskey(_KEYBRD_READ);
      switch(key)
       { case _end:    goto ends;           /*对不同键设置蛇头方向*/
         case goup:    {if(vect==right||vect==left)vect=up;break;}
         case godown:  {if(vect==right||vect==left)vect=down;break;}
         case goleft:  {if(vect==up||vect==down)vect=left;break;}
         case goright: {if(vect==up||vect==down)vect=right;break;}
         case f1:                             /*是否暂停????*/
                      {while(1){delay(delaytime);
                                if(bioskey(_KEYBRD_READY)!=0)
                                key=bioskey(_KEYBRD_READ);
                                if(key==f2)break;          /*是否重新开始??*/
                                if(key==_end)goto ends;
                                }
                       }
     }
   }
  delay(delaytime*4);
  gonum(vect,head);
  head=head->pre;
  tail=tail->pre;
  goact(head,tail);
  j=check(posi_x,posi_y,head);
  if(j==1)
     { score=score+10;
       tail=tail->next->next;
       ate=1;
     }
  live=check_dead(head,tail);
  }
  printf("\n\n\n\n\n\n\n\n\n\n");
  printf("                                   game over \n");
  printf("                                you score are %d",score);


  while(1)
  {
   if(bioskey(_KEYBRD_READY)!=0)
       key=bioskey(_KEYBRD_READ);
   if(key==_end)goto ends;
    /*  if(key==f2)goto restart;  */
  }
 ends:
      {closegraph();
       p=head;
       do{p=p->next;
          free(p->pre);
         }
        while(p!=head);
      abort(0);
      }
}
--

※ 来源:·荔园晨风BBS站 bbs.szu.edu.cn·[FROM: 192.168.55.48]


[回到开始] [上一篇][下一篇]

荔园在线首页 友情链接:深圳大学 深大招生 荔园晨风BBS S-Term软件 网络书店