标题:c语言贪吃蛇问题
只看楼主
captain2050
Rank: 2
等 级:论坛游民
帖 子:57
专家分:43
注 册:2016-7-15
结帖率:92.86%
 问题点数:0 回复次数:0 
c语言贪吃蛇问题
我在linux环境用c写的贪吃蛇,借助了ncurses,编译后可以运行,但有以下问题
问题一:无法显示食物
问题二:蛇长度错误
???????

程序代码:
#include<stdio.h>
#include<time.h>
#include<stdlib.h>
#include<ncurses.h>

struct snake
{
    int y;
    int x;
    struct snake* next;
};



struct food
{
    int y;
    int x;
};

void main()
{
    int space[22][42]={0};
    initscr();
    srand(time(NULL));
    WINDOW* win=newwin(22,42,3,6);
    int dir=3;   //1左, 2上, 3右, 4下
    struct snake* snake_head=malloc(sizeof(struct snake));
    struct snake* new_head=malloc(sizeof(struct snake));
    struct food*  food=malloc(sizeof(struct food));
    snake_head->y=5;snake_head->x=5;    //(5,5)
    new_head->y=5;new_head->x=6;        //(5,6)
    new_head->next=snake_head;snake_head->next=NULL;
    snake_head=new_head;//头指向(5,6)
    food->y=10;food->x=10;        //food初始位置(10,10)
    halfdelay(3);
    curs_set(0);            //不显示 光标
    keypad(win,TRUE);
    noecho();
    int j,i,c;
    for(i=0,j=21,c=0;c<42;c++)
        space[i][c]=space[j][c]=3;            //3:border
    for(i=0,j=41,c=1;c<22;c++)
        space[c][i]=space[c][j]=3;
    while(1)
    {
        ////////input///////////////
        setbuf(stdin,NULL);
        int ch=wgetch(win);
        if(ch==KEY_LEFT)
            dir=1;
        else if(ch==KEY_UP)
            dir=2;
        else if(ch==KEY_RIGHT)
            dir=3;
        else if(ch==KEY_DOWN)
            dir=4;
        ////////process//////////////
        new_head=malloc(sizeof(struct snake));
        if(dir==1)
        {
            new_head->y=snake_head->y;
            new_head->x=snake_head->x-1;
        }
        else if(dir==2)
        {
            new_head->y=snake_head->y-1;
            new_head->x=snake_head->x;
        }
        else if(dir==3)
        {
            new_head->y=snake_head->y;
            new_head->x=snake_head->x+1;
        }
        else if(dir==4)
        {
            new_head->y=snake_head->y+1;
            new_head->x=snake_head->x;
        }
        new_head->next=snake_head;
        snake_head=new_head;
        if((snake_head->y!=food->y)||(snake_head->x!=food->x))   //没吃到食物,保持原来长度
        {
            while(1)
            {
                new_head=new_head->next;
                if(new_head->next==NULL)
                {
                    new_head=NULL;
                    break;
                }
            }
        }
        else if((snake_head->y==food->y)&&(snake_head->x==food->x))  //如果吃到食物,就生成新的食物位置
        {
            int fod=rand()%800;
            food->y=fod/20+1;
            food->x=fod%20+1;
        }
        ///////////output///////////
        wmove(win,food->y,food->x);
        wprintw(win,"!");                //print the food
        for(int i=0;i<22;i++)
            for(int j=0;j<42;j++)
                if(space[i][j]==3)
                {
                    wmove(win,i,j);
                    wprintw(win,"#");  // print the border
                }
       

        new_head=snake_head;
        while(1)
        {
            wmove(win,new_head->y,new_head->x);
            wprintw(win,"*");            //printf the snake
            new_head=new_head->next;
            if(new_head==NULL)
            break;
        }
        if((snake_head->y==0)||(snake_head->y==22))
            break;
        if((snake_head->x==0)||(snake_head->x==42))
            break;
        new_head=snake_head->next;
        int death=1;
        while(1)
        {
            if((new_head->y==snake_head->y)&&(new_head->x==snake_head->x))
                {death=0;break;}
            new_head=new_head->next;
            if(new_head==NULL)
                break;
        }
        if(death==0)
            break;
    }
    int ch=wgetch(win);
    endwin();
}


搜索更多相关主题的帖子: struct int next NULL win 
2017-10-11 08:45



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-481263-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.211813 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved