| 网站首页 | 文章 | 下载 | 图库 | 留言 | 论坛 | 邮箱 | 黑客商城 | 资源 | 博客 | 会员区 | 黑客动画 | 视频资源 | 经典收集 | 
免费服务 发表文章 在线破解 黑客游戏 精华收集 电影频道 音乐频道 二级域名 免费代理 免费 FTP 交换资源
收费服务 黑客培训 光盘刻录 黑客书籍 视频下载 主力频道 空间域名 网站建设 特色服务 解决方案 我要投诉
您现在的位置: 华夏黑客同盟 >> 经典收集 >> 程序设计 >> C语言 >> 游戏编程 >> 收集正文 用户登录 新用户注册
文本方式下双链表实现的食豆蛇游戏           ★★★★ 【字体:
文本方式下双链表实现的食豆蛇游戏
作者:unknown 文章来源:unknown 点击数: 更新时间:2005-9-4

/*双向链表的应用

注重算法,游戏界面我并未多加考虑

希望大家多给建议,注释不多,望大家见凉

未经许可,请毋转载*/ 

/*UP : 'w'   DOWN : 's'    LEFT : 'a'      RIGHT : 'd'      EXIT : 'q'

注意:须是小写*/

#include "stdio.h"
 #include "stdlib.h"
 #define LEN sizeof(struct node)
 #define RIGHT 1
 #define LEFT  2
 #define UP    3
 #define DOWN  4
 #define SPEED 4

/*next1表示后趋节点,next2为前趋节点*/
 struct node {int status,x,y;struct node *next1,*next2;};

 struct node *dz=NULL; 


 struct node *creat()          /*创建一个蛇体长度为4的食豆蛇*/
 {struct node *head,*p1,*p2; int i=4;
  head=p2=(struct node *)malloc(LEN);
  p2->status=RIGHT;
  p2->x=i;p2->y=1;
  while(i>1)
  {p1=(struct node *) malloc(LEN);
   p1->status=RIGHT;
   p1->x=--i;p1->y=1;
   p2->next1=p1;p1->next2=p2;
   p2=p1;
  }
  p2->next1=head;
  head->next2=p2;
  return head;}
 void show(struct node *head)
 {struct node *p=head;
      do{gotoxy(p->x,p->y);
     putchar('*');p=p->next1;
     }while(p!=head);
 }

 void eat(struct node* head)                 /*吃掉一个豆子*/
 {struct node *p2=head->next2;
  p2->next1=dz;dz->next2=p2;dz->next1=head;head->next2=dz;
  switch(p2->status)
    {case RIGHT:dz->x=p2->x-1;dz->y=p2->y;break;
     case LEFT:dz->x=p2->x+1;dz->y=p2->y;break;
     case UP:dz->x=p2->x;dz->y=p2->y-1;break;
     case DOWN:dz->x=p2->x;dz->y=p2->y+1;}
  dz=NULL;
  }

 struct node * update(struct node* head,unsigned char c)    /*根据按键更新蛇体*/
 {struct node* p2,*p1;p1=head->next2;p2=p1->next2;
  do{p1->x=p2->x;p1->y=p2->y;p1->status=p2->status;
     p2=p2->next2;p1=p1->next2;
    }while(p1!=head);
  if(c)
    switch(c)
        {case 'a':if(head->status!=RIGHT) {(head->x)--;head->status=LEFT;}
              else c=0;break;
        case 's':if(head->status!=UP) {(head->y)++;head->status=DOWN;}
             else c=0;break;
        case 'd':if(head->status!=LEFT) {(head->x)++;head->status=RIGHT;}
             else c=0;break;
        case 'w':if(head->status!=DOWN) {(head->y)--;head->status=UP;}
             else c=0;break;
        default:c=0;
        }
   if(!c)                 /*c=0时表示未按键或按的是无效键*/
    switch(head->status)
        {case UP:head->y--;break;
         case DOWN:head->y++;break;
         case RIGHT:head->x++;break;
         case LEFT:head->x--;break;
        }

 if(head->x>80) head->x=1;
 else if(head->x<1) head->x=80;
 if(head->y>25) head->y=1;
 else if(head->y<1) head->y=25;
 if(head->x==dz->x&&head->y==dz->y) eat(head);
 return head; }

void delay_t(int i)                 /*蛇的移动速度*/
{int j;
 for(j=0;j
 void rand_dz()                   /*随机产生豆子*/
{if(dz==NULL) {dz=(struct node*) malloc(LEN);
          dz->x=random(79)+1;dz->y=random(24)+1;}
}

show_dz()
{gotoxy(dz->x,dz->y);putchar('*');}

void  exitgame(struct node* head)             /*退出游戏,销毁蛇体,释放空间*/
{struct node* p1,*p2;p1=p2=head->next2;
 while(p2!=head){p2=p1->next2;free(p1);p1=p2;}
 free(head);}

 main()
 {struct node *head;int key;unsigned char c;
  head=creat();rand_dz();
  clrscr();
  show(head);
  show_dz();
  delay_t(SPEED);
  while(1)
  {c=0;
   gotoxy(head->next2->x,head->next2->y);putchar(' ');
   while(bioskey(1)) c=bioskey(0);
   update(head,c);
   if(c=='q') break;
   if(dz==NULL) {rand_dz();show_dz();}  /*豆子被吃,就重新产生豆子*/
   show(head);
   delay_t(SPEED);}
 exitgame(head);
 }

 
收集录入:华夏总编辑    责任编辑:华夏总编辑 
  • 上一篇收集:

  • 下一篇收集: 没有了
  • (只显示最新5条。评论内容只代表网友观点,与本站立场无关!)
    最新热点资讯
    最新推荐资讯
    相关收集
  • 关于文曲星上猜数字游戏的c编程方…

  • 一份详细的五子棋说明

  • 大家一起来玩十点半

  • 五道棋的完整程序

  • MINI-FOOLFLY 游戏代码

  • 24点扑克牌游戏

  • 一架小钢琴——图形界面与发声技…

  • 一个模拟显示自然云彩的C程序

  • 图形模式下的搬运工

  • 仿LINUX下KTron的游戏

  • 最新推荐软件
    最新推荐视频
    最新推荐动画

    Copyright @ 2005 77169.Net Inc. All rights reserved. 华夏黑客同盟 版权所有
    北京市电信通提供网络带宽

    mailto:webmaster@77169.net
    咨询QQ号:836982 / 59280880
    联系站长 有事给站长留言!
    热线电话: 86-10-63643422/63643423
    京ICP证041431号