| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 707 人关注过本帖
标题:在写贪吃蛇代码其中有几行不懂,求高手指点!谢谢!
只看楼主 加入收藏
蜗牛在行动
Rank: 1
来 自:周口
等 级:新手上路
威 望:1
帖 子:13
专家分:6
注 册:2014-4-20
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:9 
在写贪吃蛇代码其中有几行不懂,求高手指点!谢谢!
if(c=='d')
        {
            head->y+=1;
            if(head->y>=15)
                head->y-=15;
        }
        else if(c=='a')
        {
            head->y-=1;
            if(head->y<0)
                head->y+=15;
        }
        else if(c=='w')
        {
            head->x-=1;
            if(head->x<0)
                head->x+=15;
        }
        else if(c=='s')
        {
            head->x+=1;
            if(head->x>=15)
                head->x-=15;
        }
为什么当按下a,或者d,的时候会是head->y+1或者-1;不是应该head->x变化吗?
求解释!
搜索更多相关主题的帖子: 贪吃蛇 
2014-04-21 21:05
Andrew_Lee
Rank: 7Rank: 7Rank: 7
等 级:黑侠
威 望:3
帖 子:185
专家分:626
注 册:2014-3-21
收藏
得分:0 
就这些,真的很难看懂是什么意思。
2014-04-21 21:11
蜗牛在行动
Rank: 1
来 自:周口
等 级:新手上路
威 望:1
帖 子:13
专家分:6
注 册:2014-4-20
收藏
得分:0 
回复 楼主 蜗牛在行动
大神,这是源码!求解!谢谢啦!

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <conio.h>

struct Node
{
    int x;
    int y;
    struct Node *pre;
    struct Node *next;
};

struct Food
{
    int x;
    int y;
    char c;
};

main()
{
    int a[15][15]={0};
    int i,j,t,flag=0;
    char c='d',c1='d';
    struct Food food={5,8,'A'};;
    int gameover=0;
    struct Node *head,*p,*rear,*pt;
   
    head=(struct Node *)malloc(sizeof(struct Node));
    head->x=5;
    head->y=8;
    head->pre=NULL;
    head->next=NULL;
    rear=head;

    srand((unsigned)time(NULL));
   
    while(1)
    {
        if(food.x==head->x && food.y==head->y)
        {
            p=(struct Node *)malloc(sizeof(struct Node));
            pt=head;
            while(pt->next!=NULL)
                pt=pt->next ;
            p->pre= pt;
            pt->next = p;
            p->next=NULL;
            rear=p;

            food.x=rand()%15;
            food.y=rand()%15;
            food.c=65+rand()%26;

            flag=1;
            t=0;
            while(flag==1)
            {
                if(t>5)
                    break;

                flag=0;

                pt=head;
                while(pt!=NULL)
                {
                    if(food.x==pt->x && food.y==pt->y)
                    {
                        flag=1;
                        food.x=rand()%15;
                        food.y=rand()%15;
                        break;
                    }
                    pt=pt->next;
                }
                t++;
            }
            if(t>5)     /*判断食物有没有出界!*/
            {
                if(c=='d')
                {
                    food.x=head->x+1;
                    food.y=head->y;
                    if(food.x>=15)
                        food.x-=15;
                }
                else if(c=='a')
                {
                    food.x=head->x-1;
                    food.y=head->y;
                    if(food.x<0)
                        food.x+=15;
                }
                else if(c=='w')
                {
                    food.x=head->x;
                    food.y=head->y+1;
                    if(food.y>=15)
                        food.y-=15;
                }
                else if(c=='s')
                {
                    food.x=head->x;
                    food.y=head->y-1;
                    if(food.y<0)
                        food.y+=15;
                }
            }
        }

        if(kbhit())
        {
            c1=getch();
            if(c1==27)
                break;
            
            if(c!='d' && c1=='a')
                c=c1;
            else if(c!='a' && c1=='d')
                c=c1;
            else if(c!='w' && c1=='s')
                c=c1;
            else if(c!='s' && c1=='w')
                c=c1;
        }

        pt=rear;
        while(pt!=head )
        {
            pt->x=pt->pre->x;
            pt->y=pt->pre->y;
            pt=pt->pre;
        }

        if(c=='d')
        {
            head->y+=1;
            if(head->y>=15)
                head->y-=15;
        }
        else if(c=='a')
        {
            head->y-=1;
            if(head->y<0)
                head->y+=15;
        }
        else if(c=='w')
        {
            head->x-=1;
            if(head->x<0)
                head->x+=15;
        }
        else if(c=='s')
        {
            head->x+=1;
            if(head->x>=15)
                head->x-=15;
        }

        pt=head->next;
        while(pt!=NULL)
        {
            if(head->x==pt->x && head->y==pt->y)
            {
                gameover=1;
                break;
            }
            pt=pt->next ;
        }
        if(gameover==1)
            break;
        
        system("cls");
        printf("  ───────────────\n");
        for(i=0;i<15;i++)
        {
            printf("│");
            for(j=0;j<15;j++)
            {

                flag=0;
                pt=head;
                while(pt!=NULL)
                {
                    if(i==pt->x && j==pt->y)
                    {
                        if(pt==head)
                            printf("■");
                        else
                            printf("□");
                        flag=1;
                        break;
                    }
                    pt=pt->next;
                }


                if(flag==0)
                {
                    if(i==food.x && j==food.y)
                    {
                        putchar(food.c);
                        putchar(food.c);
                        continue;
                    }
                    printf("  ");
                }
            }
            printf("│");
            putchar('\n');
        }
        printf("  ───────────────\n");
        
        _sleep(200);   
    }
    system("cls");
    if(gameover==1)
        printf("游戏结束!(Game Over!)\n\n");
    getch();
}

现在很辛苦,因为你再走上坡路!
2014-04-21 22:10
蜗牛在行动
Rank: 1
来 自:周口
等 级:新手上路
威 望:1
帖 子:13
专家分:6
注 册:2014-4-20
收藏
得分:0 
回复 2 楼 Andrew_Lee
大神,这是源码!求解!谢谢啦!

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <conio.h>

struct Node
{
    int x;
    int y;
    struct Node *pre;
    struct Node *next;
};

struct Food
{
    int x;
    int y;
    char c;
};

main()
{
    int a[15][15]={0};
    int i,j,t,flag=0;
    char c='d',c1='d';
    struct Food food={5,8,'A'};;
    int gameover=0;
    struct Node *head,*p,*rear,*pt;
   
    head=(struct Node *)malloc(sizeof(struct Node));
    head->x=5;
    head->y=8;
    head->pre=NULL;
    head->next=NULL;
    rear=head;

    srand((unsigned)time(NULL));
   
    while(1)
    {
        if(food.x==head->x && food.y==head->y)
        {
            p=(struct Node *)malloc(sizeof(struct Node));
            pt=head;
            while(pt->next!=NULL)
                pt=pt->next ;
            p->pre= pt;
            pt->next = p;
            p->next=NULL;
            rear=p;

            food.x=rand()%15;
            food.y=rand()%15;
            food.c=65+rand()%26;

            flag=1;
            t=0;
            while(flag==1)
            {
                if(t>5)
                    break;

                flag=0;

                pt=head;
                while(pt!=NULL)
                {
                    if(food.x==pt->x && food.y==pt->y)
                    {
                        flag=1;
                        food.x=rand()%15;
                        food.y=rand()%15;
                        break;
                    }
                    pt=pt->next;
                }
                t++;
            }
            if(t>5)     /*判断食物有没有出界!*/
            {
                if(c=='d')
                {
                    food.x=head->x+1;
                    food.y=head->y;
                    if(food.x>=15)
                        food.x-=15;
                }
                else if(c=='a')
                {
                    food.x=head->x-1;
                    food.y=head->y;
                    if(food.x<0)
                        food.x+=15;
                }
                else if(c=='w')
                {
                    food.x=head->x;
                    food.y=head->y+1;
                    if(food.y>=15)
                        food.y-=15;
                }
                else if(c=='s')
                {
                    food.x=head->x;
                    food.y=head->y-1;
                    if(food.y<0)
                        food.y+=15;
                }
            }
        }

        if(kbhit())
        {
            c1=getch();
            if(c1==27)
                break;
            
            if(c!='d' && c1=='a')
                c=c1;
            else if(c!='a' && c1=='d')
                c=c1;
            else if(c!='w' && c1=='s')
                c=c1;
            else if(c!='s' && c1=='w')
                c=c1;
        }

        pt=rear;
        while(pt!=head )
        {
            pt->x=pt->pre->x;
            pt->y=pt->pre->y;
            pt=pt->pre;
        }

        if(c=='d')
        {
            head->y+=1;
            if(head->y>=15)
                head->y-=15;
        }
        else if(c=='a')
        {
            head->y-=1;
            if(head->y<0)
                head->y+=15;
        }
        else if(c=='w')
        {
            head->x-=1;
            if(head->x<0)
                head->x+=15;
        }
        else if(c=='s')
        {
            head->x+=1;
            if(head->x>=15)
                head->x-=15;
        }

        pt=head->next;
        while(pt!=NULL)
        {
            if(head->x==pt->x && head->y==pt->y)
            {
                gameover=1;
                break;
            }
            pt=pt->next ;
        }
        if(gameover==1)
            break;
        
        system("cls");
        printf("  ───────────────\n");
        for(i=0;i<15;i++)
        {
            printf("│");
            for(j=0;j<15;j++)
            {

                flag=0;
                pt=head;
                while(pt!=NULL)
                {
                    if(i==pt->x && j==pt->y)
                    {
                        if(pt==head)
                            printf("■");
                        else
                            printf("□");
                        flag=1;
                        break;
                    }
                    pt=pt->next;
                }


                if(flag==0)
                {
                    if(i==food.x && j==food.y)
                    {
                        putchar(food.c);
                        putchar(food.c);
                        continue;
                    }
                    printf("  ");
                }
            }
            printf("│");
            putchar('\n');
        }
        printf("  ───────────────\n");
        
        _sleep(200);   
    }
    system("cls");
    if(gameover==1)
        printf("游戏结束!(Game Over!)\n\n");
    getch();
}

现在很辛苦,因为你再走上坡路!
2014-04-21 22:19
Andrew_Lee
Rank: 7Rank: 7Rank: 7
等 级:黑侠
威 望:3
帖 子:185
专家分:626
注 册:2014-3-21
收藏
得分:20 
回复 4 楼 蜗牛在行动
这个的确是这样的,程序里竖直方向看做X轴,将水平方向看做Y轴心的。我运行了你的程序,按下a或者d的时候,蛇是在水平方向移动(左右移动),就是说在y轴上移动的。所以应该是head-y的值在变化
2014-04-21 22:33
法海他不懂爱
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2014-4-22
收藏
得分:0 
请问设计贪吃蛇的思路是什么呀?我是新手,想学一下~谢谢
2014-04-22 10:45
蜗牛在行动
Rank: 1
来 自:周口
等 级:新手上路
威 望:1
帖 子:13
专家分:6
注 册:2014-4-20
收藏
得分:0 
回复 5 楼 Andrew_Lee
好像是的啊!我可能思想太古板了!还没试着转换思路!谢谢大神了!

现在很辛苦,因为你再走上坡路!
2014-04-22 19:32
蜗牛在行动
Rank: 1
来 自:周口
等 级:新手上路
威 望:1
帖 子:13
专家分:6
注 册:2014-4-20
收藏
得分:0 
回复 6 楼 法海他不懂爱
通过定义一个蛇头,并把蛇头用坐标表示head-x,和head->y!然后通过随机数函数产生一些随机数,定义成食物的坐标,判断蛇头的坐标和食物的坐标是否相同,相同增加节数,然后判断你按的按键来控制方向,,蛇的移动是把蛇的后一节只向前一节!

现在很辛苦,因为你再走上坡路!
2014-04-22 20:11
法海他不懂爱
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2014-4-22
收藏
得分:0 
回复 8 楼 蜗牛在行动
谢谢~
2014-04-22 22:50
鸥翔鱼游
Rank: 5Rank: 5
等 级:职业侠客
帖 子:182
专家分:323
注 册:2014-4-19
收藏
得分:0 
观摩技术性回复
2014-04-23 12:57
快速回复:在写贪吃蛇代码其中有几行不懂,求高手指点!谢谢!
数据加载中...
 
   



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

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