if_else高级工程师 2022-09-08 11:07 采纳率: 33.3%
浏览 18

基于linux开发的贪吃蛇游戏死机的问题

求解答问什么我的程序加了蛇身撞自己死亡的程序后运行死机了

# include <curses.h>
# include <stdlib.h>
# include <pthread.h>




struct snake
{
    int hang;
    int lie;
    struct snake *next;
};


struct snake *head = NULL;
struct snake *tail = NULL;
int key = KEY_RIGHT;
int com;
struct snake food;


void updatefood()
{
     int x = rand()%20;
     int y = rand()%20;
     
     food.hang = x;
     food.lie = y;
}


int judgesnakes(int i, int j)
{
    struct snake *p = head;
    while(p != NULL)
    {
        if(p->hang == i && p->lie == j)
        {
            return 1;
        }
            
        else
        {
            p = p->next;
        }
            
    }
    return 0;
    
}


void deleteheath()
{
    struct snake *p = head;
    head = head->next;
    free(p);
    
}

void creathead()
{
    while(head != NULL)
    {
        struct snake *p = head;
        head = head->next;
        free(p);
    }
    updatefood();
    head = (struct snake*)malloc(sizeof(struct snake));
    head->hang = 2;
    head->lie = 2;
    head->next = NULL;
    tail = head;
}

void creatnewhealth1()
{
    struct snake *new = (struct snake*)malloc(sizeof(struct snake));
    tail->next = new;
    switch(key)
    {
        case KEY_UP:
            new->hang = tail->hang - 1;
            new->lie = tail->lie;
            new->next = NULL;
            tail = new;    
            com = 1;
            break;
        case KEY_DOWN:
            new->hang = tail->hang + 1;
            new->lie = tail->lie;
            new->next = NULL;
            tail = new;    
            com = -1;
            break;
        case KEY_LEFT:
            new->hang = tail->hang;
            new->lie = tail->lie - 1;
            new->next = NULL;
            tail = new;    
            com = 2;
            break;
        case KEY_RIGHT:
            new->hang = tail->hang;
            new->lie = tail->lie + 1;
            new->next = NULL;
            tail = new;    
            com = -2;
            break; 
    }
    
}


int snakefood(int a, int b)
{
    
    
    if(food.hang == a && food.lie == b)
        return 1;
    else
        return 0;
}



int judgerefresh()
{
    struct snake *p = head;
    if(tail->lie == 20 || tail->lie == 0 || tail->hang < 0 || tail->hang == 20)
    {
        return 1;
    }
    else
    {
        while(p->next != NULL)
        {
        if(p->hang == tail->hang && p->lie == tail->lie)
            return 1;
        else
            p == p->next;
        }
    }
    if(p->next == NULL)

    {
        return 0;
    }
}


void creatnewhealth2()
{
    if(judgerefresh())
    {
        key = KEY_RIGHT;
        creathead();
        creatnewhealth1();
        creatnewhealth1();
        
    }
    else if(snakefood(tail->hang, tail->lie))
    {
        creatnewhealth1();
        updatefood();
    }
    else
    {
        deleteheath();
        creatnewhealth1();
        
    }
}




void gamechart()
{
    move(0, 0);
    int hang;
    int lie;
    for(hang = 0; hang<=20; ++hang)
    {
        if(hang == 0 || hang == 20)
        {
            for(lie = 0; lie<20; ++lie)
            {
                printw("--");
            }
            printw("\n");
        }
        else
        {
            for(lie = 0; lie<=20; ++lie)
            {
                if(lie == 0 || lie == 20)
                {
                    printw("|");
                }
                else if(judgesnakes(hang, lie))
                {
                    printw("[]");
                }
                else if(snakefood(hang, lie))
                {
                    printw("##");
                }
                
                else
                {
                    printw("  ");
                }
            }
            printw("\n");    
        }            
    }
}



void* startchart()
{
    while(1)
    {
    creatnewhealth2();
    gamechart();
    refresh();
    usleep(100000);
    }
    
}


void gamecurses()
{
   initscr();
   keypad(stdscr,1);
}



void* gamekey()
{
   int t;
   int con;
   while(1)
{
   t = getch();
   switch(t)
   {
       case KEY_UP:
            con = 1;
            break;
        case KEY_DOWN:
            con = -1;
            break;
        case KEY_LEFT:
            con = 2;
            break;
        case KEY_RIGHT:
            con = -2;
            break;
   }
   if(abs(con) == abs(com))
   {
        t = key;
   }
      
   else
   {
       switch(t)
      {
          case KEY_UP:
            key = KEY_UP;
             break;
          case KEY_DOWN:
             key = KEY_DOWN;
             break;
          case KEY_LEFT:
             key = KEY_LEFT;
             break;
          case KEY_RIGHT:
             key = KEY_RIGHT;
             break;

   }
        } 
}
}


int main()
{
   gamecurses();
   creathead();
   creatnewhealth1();
   creatnewhealth1();
   gamechart();
   noecho();
   

    pthread_t st1;
    pthread_t st2;
       
    pthread_create(&st1, NULL, gamekey, NULL);
    pthread_create(&st2, NULL, startchart, NULL);
       
    while(1);
       
  
   endwin;
   return 0;
 }

运行效果

img

加了这个判定函数后就死机了,没加之前还能运行

int judgerefresh()
{
    struct snake *p = head;
    if(tail->lie == 20 || tail->lie == 0 || tail->hang < 0 || tail->hang == 20)
    {
        return 1;
    }
    else
    {
        while(p->next != NULL)
        {
        if(p->hang == tail->hang && p->lie == tail->lie)
            return 1;
        else
            p == p->next;
        }
    }
    if(p->next == NULL)

    {
        return 0;
    }
}
  • 写回答

1条回答 默认 最新

  • 关注

    可能是死循环了,judgerefresh()函数开始的时候,增加head和tail是否为NULL的判断,否则可能会出错,然后在调用judgerefresh()的地方添加打印信息,看看在什么位置、什么情况导致的死循环。

    评论

报告相同问题?

问题事件

  • 创建了问题 9月8日

悬赏问题

  • ¥15 Apache显示系统错误3该如何解决?
  • ¥30 uniapp小程序苹果手机加载gif图片不显示动效?
  • ¥20 js怎么实现跨域问题
  • ¥15 C++dll二次开发,C#调用
  • ¥15 请教,如何使用C#加载本地摄像头进行逐帧推流
  • ¥15 Python easyocr无法顺利执行,如何解决?
  • ¥15 为什么会突然npm err!啊
  • ¥15 java服务连接es读取列表数据,服务连接本地es获取数据时的速度很快,但是换成远端的es就会非常慢,这是为什么呢
  • ¥15 vxworks交叉编译gcc报错error: missing binary operator before token "("
  • ¥15 JSzip压缩文件时如何设置解压缩密码