首页 > 用户发贴区 > 编程问题提问区 > 贪食蛇程序求助
2007
06-25

    写了个文本模式下的贪食蛇,但是却不会执行,编译会通过.不知道是哪里出错,拿出来请大家斧正!


   /*头文件*/
#include <stdio.h>
#include <stdlib.h>
#include <bios.h>
/*宏定义*/
#define M 20
#define LEFT  0x4b00
#define RIGHT 0x4d00
#define UP  0×4800
#define DOWN  0×5000
#define left 11
#define width 60
#define top 4
#define height 20


/*函数声明*/
void initial();/*初始化得到开始的数据*/
void display();/*静态的画面*/
int judge_death();/*,要根据下一步,判断是否死亡*/
int judge_win();/*判断是否赢*/
int judge_eaten();/*判断是否吃到了目标*/
void make_object();/*产生一个目标,并且打印出来*/
void save_tail();/*在改变之前将尾巴保存*/
void save_head();/*在改变前保存蛇头*/
void change();/*先改变数组头个元素的值,以方便下面的判断*/
void change1();/*如果吃到了的改变*/
void change2();/*没有吃到的改变,注意尾巴的变化*/
void print1();/*吃到的话打印*/
void print2();/*没吃到的话打印*/
void getkey();/*得到键盘的返回值*/
void overgame();/*结束游戏,根据参数决定显示*/


/*全局变量定义*/
int terminate=0;
int arr[25][80]={0};


struct   b
{
  int x[M];
  int y[M];
  int length;
  int tail_x;
  int tail_y;
  int head_x;
  int head_y;
  int direction;
}snake;
struct  a
{
   int x;
   int y;
}object;


/*函数原型*/
int main()
{   clrscr();
    initial();
    display();
    getchar();
    while(terminate==0)
    {
      save_tail();
      save_head();
      getkey();
      change();
      if(judge_death()==1)
    {terminate=1;break;
    }
       else if(judge_win()==1)
       {
     terminate=2;break;
       }
       else if (judge_eaten()==1)
       {
     change1();
     print1();
     make_object();
       }
       else if(!judge_eaten())
       {
     change2();
     print2();
       }
      
       delay(240);
    }
    overgame();
    return 0;
}


void initial()
{
     int line,column;
     int i;
     snake.length=3;
     randomize();
     do
      {object.x=rand()%width+left;
       object.y=rand()%height+top;
       snake.x[0]=rand()%width+left;
       snake.y[0]=rand()%height+top;
      }while (object.x==snake.x[0]&&object.y==snake.y[0]);
     if (snake.x[0]-left<=snake.length)   snake.x[0]+=snake.length;
     if (snake.y[0]-top<=snake.length)    snake.y[0]+=snake.length;
     snake.x[1]=snake.x[0]-1;
     snake.x[2]=snake.x[1]-1;
     snake.y[1]=snake.y[0];
     snake.y[2]=snake.y[0];
     arr[snake.y[0]][snake.x[0]]=1;
     arr[snake.y[1]][snake.x[1]]=1;
     arr[snake.y[2]][snake.x[2]]=1;
     arr[object.y][object.x]=2;


     snake.direction=rand()%3+2;
}
void display()
{
     int j;
     int gx,gy;
     gotoxy(20,2);
     printf(“welcome to snake game”);
     for(gx=left;gx<=left+width;gx++)
 {
   gotoxy(gx,top);printf(“-”);
   gotoxy(gx,top+height);printf(“-”);
 }
     for(gy=top;gy<=top+height;gy++)
       {
   gotoxy(left,gy);printf(“|”);
   gotoxy(left+width,gy);printf(“|”);
       }
     gotoxy(object.x,object.y);
     printf(“\013″);
     for(j=0;j<snake.length;j++)
     {
 gotoxy(snake.x[j],snake.y[j]);
 printf(“\002″);
     }


}


void save_tail()
{
     snake.tail_x=snake.x[snake.length-1];
     snake.tail_y=snake.y[snake.length-1];
}


void save_head()
{
     snake.head_x=snake.x[0];
     snake.head_y=snake.y[0];
}


void change()
{
     switch(snake.direction)
     {
       case 1:
       case LEFT:snake.x[0]-=1; break;
       case 2:
       case UP: snake.y[0]-=1;break;
       case 3:
       case RIGHT: snake.y[0]+=1;break;
       case 4:
       case DOWN: snake.  x[0]+=1;break;
     }
}


int judge_death()
{
    int flag=0;
    if(arr[snake.y[0]][snake.x[0]]==1)
       flag=1;
    else
       if((snake.x[0]>=left+width)||(snake.x[0]<=left))
 flag=1;
 else
   if((snake.y[0]>=top+height)||(snake.y[0]<=top))
     flag=1;


    if(flag)  return 1;
     else return 0;
}


int judge_win()
{
   if(snake.length>=7)
            return 1;
   else return 0;
}


int judge_eaten()
{
    if((snake.x[0]==object.x)&&(snake.y[0]==object.y))
      return 1;
      else return 0;
}


void change1()
{
     int i;
     snake.length++;
     for(i=snake.length-2;i>=1;i–)
     {
       snake.x[i+1]=snake.x[i];
       snake.y[i+1]=snake.y[i];
     }
     snake.x[1]=snake.head_x;
     snake.y[1]=snake.head_y;
     for(i=0;i<snake.length;i++)
     {
 arr[snake.y[i]][snake.x[i]]=1;
     }
}


void print1()
{
    gotoxy(snake.x[0],snake.y[0]);
    printf(“\002″);
}


void make_object()
{
  int flag1=0;
  do
  {object.x=rand()%width+left+1;
   object.y=rand()%height+top;
   if(arr[object.y][object.x]==1)
       flag1=1;
     else if((object.x==left)||(object.x>=left+width))
       flag1=1;
 else if((object.y==top)||(object.y>=top+height))
   flag1=1;
  }while(flag1==1);
  arr[object.y][object.x]=2;
  gotoxy(object.x,object.y);
  printf(“\013″);
}


void change2()
{
     int k;
     for(k=snake.length-2;k>=1;k–)
     {
 snake.x[k+1]=snake.x[k];
 snake.y[k+1]=snake.y[k];
     }
     snake.x[1]=snake.head_x;
     snake.y[1]=snake.head_y;
     for(k=0;k<snake.length;k++)
     {
       arr[snake.y[k]][snake.x[k]]=1;
     }
}


void print2()
{
     gotoxy(snake.x[0],snake.y[0]);
     printf(“\002″);
     gotoxy(snake.tail_x,snake.tail_y);
     printf(” “);
}


void getkey()
{
  int temp;
  int bkey;
  temp=snake.direction;
  if(bioskey(1)!=0)
  {
    bkey=bioskey(0);
    if ((bkey==LEFT)||(bkey==RIGHT)||(bkey==UP)||(bkey==DOWN))            
    snake.direction=bkey;
  }
    else snake.direction=temp;
   
}


void overgame()
{
    clrscr();
    if(terminate==1)
    {
       gotoxy(14,10);
       printf(“you lose it!”);
    }
    else if(terminate==2)
    {
       gotoxy(14,10);
       printf(“you win!”);
    }
}


 


 


 


 


 


 


 


 


 


 


 


 






贪食蛇程序求助》有 1 条评论

  1. xstar 说:

    代码看到了,问题应该出在输入和动作保持方面,没有TC,有些东西验证不了!
    输入方面的流程是:
    key_input() {
        if( 有按键动作 ) {
            取得指定键值; //取得键值后立即清除缓存
            赋给Snake.Direction;
         }
    }

    动作方面的流程是:
    move ( Snake.Direction )
    {
        switch( Snake.Direction ) {
        case KEY_UP:
                 checkstate();
                 ….
                 break;
        case KEY_DOWN;
                 checkstate();
                 ….
                 break;
        ….
        }
    }

    检查状态方面的流程:
    checkstate()
    {
        if( 碰壁 ) {
            return -1;
        }

        if( 普通格子 ) {
           // 该格子画蛇头
           // 清除原先的蛇尾
        } else { //是食物
           // 该格子上画蛇头
        }
        return 1;
    }

    判断输赢:
    if( checkstate() != 1 ) {
        // 输
    }

    if ( snake.len >= 7 )  {
        // 赢
    }

留下一个回复