新人指南
宝湾注册及激活教程
单机游戏发售表
电脑游戏安装指南
幻灵游侠2.5SF - 宠爱家园
仙侠梦宠休闲网络游戏
奇迹SF1.05H圣女降临
浓情暑期推荐
返回列表 回复 发帖

[其他工具] C语言程序贪吃蛇

[其他工具] C语言程序贪吃蛇

/************************    文本方式的贪吃蛇   ********************************/
/************************     tc2.0 运行成功    ********************************/

#include <stdio.h>
#include <stdlib.h>
#define X 21
#define Y 8
#define MID_SPEED 5           /* 中等速度 */
#define MAX_SPEED 60          /* 最大速度 */
/* 键盘码 */
#define LEFT 19200
#define RIGHT 19712
#define UP 18432
#define DOWN 20480
#define ESC 283
#define SPACE 14624
#define ENTER 7181
int a,b;                   /* 食物坐标 */
int x,y;                   /* 蛇头坐标 */
int num;     /* 蛇长 */
int room1[100][2];         /* 保存蛇各结点坐标 */
int room2[100][2];
int aspect;            /* 方向 */
int speed;              /* 速度 */
void left();
void right();
void up();
void down();
void goleft();
void goright();
void goup();
void godown();
void produce();
void getxy();
void check();
void eat();
void cut();
void set();
void mydelay();
void draw();
void start();
void go();
main()
{
    int i;
    int key;
    /* 初始化 */
    clrscr();
    start();
    printf("***\b");
    num=3;
    speed=MID_SPEED;               /* 设置初速度 */
    aspect=RIGHT;                  /* 置初始方向状态为右 */
    /* 设置初始结点坐标 */
    room1[0][0]=4;
    room1[0][1]=2;
    room1[1][0]=3;
    room1[1][1]=2;
    room1[2][0]=2;
    room1[2][1]=2;
    produce();                /* 产生第一个食物 */
    go();                     /* 行动 */
    while(1)
    {
check();                /* 检查是否与自身相碰 */
if(speed>MID_SPEED)     /* 模拟摩擦力,让速度趋于正常 */
{
     speed--;
}
go();                 /* 行动 */
/* 如果有按键,进行处理 */
if(kbhit())
{
     key=bioskey(0);              /* 接收按键 */
     if(key==LEFT)
     {
  if(x>2)
  {   /* 如果方向相反,减速 */
      if(aspect==RIGHT)      
      {
   speed--;
      }
      else
      {    /* 如果方向相同,加速*/
   if((aspect==LEFT)&&(speed<MAX_SPEED))   
     {      
                                speed+=2;
   }
   else                 /* 改变方向,重置速度 */
   {
       aspect=LEFT;
       speed=MID_SPEED;
   }
      }
  }
     }
     if(key==RIGHT)
     {
  if(x<79)
  {
      if(aspect==LEFT)
      {
   speed--;
      }
      else
      {
   if((aspect==RIGHT)&&(speed<MAX_SPEED))
   {
       speed+=2;
   }
   else
   {
       aspect=RIGHT;
       speed=MID_SPEED;
   }
      }
  }
     }
     if(key==DOWN)
     {
  if(y<24)
  {
      if(aspect==UP)
      {
   speed--;
      }
      else
      {
   if((aspect==DOWN)&&(speed<MAX_SPEED))
   {
       speed+=2;
   }
   else
   {
       aspect=DOWN;
       speed=MID_SPEED;
   }
      }
  }
     }
     if(key==UP)
     {
  if(y>2)
  {
      if(aspect==DOWN)
      {
   speed--;
      }
      else
      {
   if((aspect==UP)&&(speed<MAX_SPEED))
   {
       speed+=2;
   }
   else
   {
       aspect=UP;
       speed=MID_SPEED;
   }
      }
  }
     }
     if(key==SPACE)    /* 手动产生一个食物 */
     {
  produce();
     }
     if(key==ENTER)         /* 暂停 */
     {
  getch();
     }
   
     if(key==ESC)           /* 退出游戏 */
     {
  exit(0);
     }
}                          /* end of if */
    }                              /* end of while */
}                                  /* end of main */
/***************************************************************/
void left()
{
   int i;
   getxy();                      /*   取得当前坐标  */
   if((x==a+1)&&(y==b))          /* 遇到零星 */
   {
      eat();
   }
   else
   {
      cut();                     /* 砍掉尾巴 */
      gotoxy(x-1,y);             /* 前面加一个 */
      printf("*\b");
      set();                     /* 重置结点坐标 */
   }
}
/*********************************************/
void right()
{  int i;
   getxy();
   if(x==a-1 && y==b)
   {
       eat();
   }
   else
   {
       cut();
       gotoxy(x+1,y);
       printf("*\b");
       set();
   }
}
/*********************************************/
void down()
{
   int i;
   getxy();
   if((x==a)&&(y==b-1))
   {
       eat();
   }
   else
   {
       cut();
       gotoxy(x,y+1);
       printf("*\b");
       set();
   }
}
/**********************************************/
void up()
{
   int i;
   getxy();
   if((x==a)&&(y==b+1))
   {
       eat();
   }
   else
   {
       cut();
       gotoxy(x,y-1);
       printf("*\b");
       set();
   }
}
/***************** 产生一个食物函数********************************/
void produce()
{
   int i;
   getxy();
   randomize();
   a=random(78);
   randomize();
   b=random(23);
   gotoxy(a,b);
   printf("*");
   gotoxy(x,y);
}
/************** 取得当前坐标函数 ********************/
void getxy()
{
   x=wherex();
   y=wherey();
}
/*************** 检查是否与自己相遇函数 ********************/
void check()
{
    int i;
    getxy();
    for(i=1;i<num;i++)
    {
if(x==room1[0] && y==room1[1])
{
     exit(0);
}
    }
}
/***************** 吃食物函数 ********************/
void eat()
{
    int i;
    gotoxy(a,b);                        /* 把光标移动到零星的位置 */
    room2[0][0]=a;                      /* 把数组2的头座标定为零星的坐标 */
    room2[0][1]=b;
    for(i=0;i<num;i++)                  /* 把数组1接到数组2后面 */
    {
room2[i+1][0]=room1[0];
room2[i+1][1]=room1[1];
    }
    num++;                              /* 星数改变 */
    for(i=0;i<num;i++)                  /* 把数组2复制给数组1 */
    {
room1[0]=room2[0];
room1[1]=room2[1];
    }
    produce();                          /* 产生另一个星 */
}
/********************** 删除尾巴函数 ***************************/
void cut()
{
    gotoxy(room1[num-1][0],room1[num-1][1]);               
    printf(" ");
    gotoxy(room1[0][0],room1[0][1]);
}
/************************************************/
void set()
{
    int i;
    getxy();                          /* 数组2获取头坐标 */
    room2[0][0]=x;
    room2[0][1]=y;
    for(i=0;i<num-1;i++)              /* 连接 */
    {
room2[i+1][0]=room1[0];
room2[i+1][1]=room1[1];
    }
    for(i=0;i<num;i++)                /* 复制 */
    {
room1[0]=room2[0];
room1[1]=room2[1];
    }
}
/***************** 向右运动函数 ******************************/
void goright()
{
    while(!kbhit()&&(x<79))
    {
right();
mydelay();
    }
}
/****************** 向左运动函数 ******************************/
void goleft()
{
    while(!kbhit()&&(x>2))
    {
left();
mydelay();
    }
}
/****************** 向上运动函数 *******************************/
void goup()
{
    while(!kbhit()&&(y>2))
    {
up();
mydelay();
    }
}
/******************* 向下运动函数*********************************/
void godown()
{
    while(!kbhit()&&(y<24))
    {
down();
mydelay();
    }
}
/******************* 自定义延时函数 ******************************/
void mydelay()
{
    int i;
    for(i=0;i<60-speed;i++)
    {
delay(1000);
    }
}
/************** 画边框函数 ***********************  */
void draw()
{
    int i;
    gotoxy(1,1);
    for(i=1;i<=25;i++)
    {
printf("*\n");
    }
    gotoxy(2,1);
    for(i=1;i<79;i++)
    {
printf("*");
    }
    for(i=1;i<=25;i++)
    {
printf("*");
gotoxy(80,i);
    }
    gotoxy(1,25);
    for(i=1;i<=80;i++)
    {
printf("*");
    }
    gotoxy(X+5,25);
    printf(" Esc exit    Enter pause ");
    gotoxy(X+2,1);
    printf(" If you have no food,press Space ");
    gotoxy(2,2);
}
/****************** 运动函数 ****************************/
void go()
{
    switch(aspect)
    {
case LEFT: goleft();
break;
case RIGHT: goright();
break;
case DOWN: godown();
break;
case UP: goup();
break;
    }
}
/********************* 初始化函数 *************************/
void start()
{
    gotoxy(X,Y-3);
    printf("Welcome to play the game tanchishe");
    gotoxy(X+3,Y);
    printf("%c left  ",27);
    printf("%c right  ",26);
    printf("%c up  ",24);
    printf("%c down",25);
    gotoxy(X+7,Y+8);
    printf("Press any key to start");
    gotoxy(1,1);
    draw();
    printf("***\b");
    getch();
    clrscr();
    draw();
}
提示: 作者被禁止或删除 内容自动屏蔽
签名被屏蔽

回复 #2 22353255 的帖子

不要挖坟了!
  天地有正气,杂然赋流形。下则为河岳,上则为日星。于人曰浩然,沛乎塞苍冥。
  皇路当清夷,含和吐明庭。时穷节乃见,一一垂丹青。在齐太史简,在晋董狐笔。
  在秦张良椎,在汉苏武节。为严将军头,为嵇侍中血。为张睢阳齿,为颜常山舌。
  或为辽东帽,清操厉冰雪。或为出师表,鬼神泣壮烈。或为渡江楫,慷慨吞胡羯。
  或为击贼笏,逆竖头破裂。是气所磅礴,凛烈万古存。当其贯日月,生死安足论。
  地维赖以立,天柱赖以尊。三纲实系命,道义为之根。嗟予遘阳九,隶也实不力。
  楚囚缨其冠,传车送穷北。鼎镬甘如饴,求之不可得。阴房阗鬼火,春院闭天黑。
  牛骥同一皂,鸡栖凤凰食。一朝蒙雾露,分作沟中瘠。如此再寒暑,百疠自辟易。
  嗟哉沮洳场,为我安乐国。岂有他缪巧,阴阳不能贼。顾此耿耿在,仰视浮云白。
  悠悠我心悲,苍天曷有极。哲人日已远,典刑在夙昔。风檐展书读,古道照颜色。


近期热点,帝国全面战争。
传送门
..............关了好了
我并我不觉得我签名吓人 如果反感请快速跳过

顶了
返回列表