标题:初学C 写了贪吃蛇 出现奇怪问题 请高手指教
取消只看楼主
testarosa
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2013-1-13
结帖率:100%
已结贴  问题点数:20 回复次数:2 
初学C 写了贪吃蛇 出现奇怪问题 请高手指教
先上代码:
/* snake */
#include<stdio.h>

int main()
{
    int x = 1;
    int y = 1;
    char key = 0;
    char x_snake[30] = {12};
    char y_snake[30]= {6};
    char length = 4;
    char length_count = 4;
    char close = 1;
   
   
   
    for(;close; )
    {
        clrscr();
        /* clear screen and show new update for snake */
        gotoxy(29, 2);
        printf("Wellcome to snake world!");
        gotoxy(1, 5);
        for(y = 5; y <= 21; y++)
        {
            if((y == 5) || (y == 21))
            {
                for(x = 1; x <= 80; x++)
                {
                    if((x >= 11) && (x <= 70))
                    {
                        printf("%c", 0xCD);
                    }
                    else
                    {
                        printf(" ");
                    }
                }
               
            }
            else
            {
                for(x = 1; x <= 80; x++)
                {
                    if(( x == 11) || (x == 70))
                    {
                        printf("%c", 0xBA);
                    }
                    else
                    {
                        printf(" ");
                    }
                }
               
            }
        }
        /* print a box as limit of the world */

   
   
        gotoxy(22, 23);
        printf("x_snake = %d          y_snake = %d", x_snake[0], y_snake[0]);
        
        
        for( length_count = length; length_count > 0 ; )
        {
                x_snake[length_count] = x_snake[length_count - 1];
                y_snake[length_count] = y_snake[length_count - 1];
                gotoxy(x_snake[length_count], y_snake[length_count]);
                printf("*");
                length_count--;
                /*gotoxy(x_snake[length_count], y_snake[length_count]);*/
        }
        gotoxy(15, 22);
        printf("%d  %d    ", x_snake[0], y_snake[0] );
        printf("%d  %d    ", x_snake[1], y_snake[1] );
        printf("%d  %d    ", x_snake[2], y_snake[2] );
        printf("%d  %d    ", x_snake[3], y_snake[3] );
        printf("%d  %d    ", x_snake[4], y_snake[4] );
        printf("%d  %d    ", x_snake[5], y_snake[5] );
        
        
        /*print snake with update */

        key = getch();
        if(key == 0x4b)
        {
            x_snake[0] = x_snake[0] == 12 ? 69 : --x_snake [0];
        }        
        if(key == 0x4d)
        {
            x_snake[0] = x_snake[0] == 69 ? 12 :++x_snake[0];
        }
        if(key == 0x48)
        {
            y_snake[0] = y_snake[0] == 6 ? 20 : --y_snake[0];
        }
        if(key == 0x50)
        {
            y_snake[0] = y_snake[0] == 20? 6 : ++y_snake[0];
            
        }
        /* contorl for snake */
   
    }
    return 0;
}

这代码执行的问题是。。当我length为4以上的时候。蛇的长度显示不正常。
我打印了数组中的数值来看。发现 每一次 更新蛇的坐标时 ,每一节的坐标被赋值两次 。所以 有两节蛇被打印到同一坐标 造成蛇的长度错误。
但是我不知道这代码哪里错了。。

求高手指教。
搜索更多相关主题的帖子: update screen 贪吃蛇 include 
2013-01-13 12:25
testarosa
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2013-1-13
得分:0 
回复 5楼 peach5460
是的 你说的很对。我这个只是初步实现了控制。死亡准备下一步慢慢做进去
。。但是。。蛇的数量不对。。头痛了2天了。。不知道哪里除了问题 ,能帮我看看吗?
2013-01-14 21:30
testarosa
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2013-1-13
得分:0 
回复 4楼 辉阳
多谢~~能知道哪里出错就好了~
2013-01-14 21:31



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




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

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