初学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以上的时候。蛇的长度显示不正常。
我打印了数组中的数值来看。发现 每一次 更新蛇的坐标时 ,每一节的坐标被赋值两次 。所以 有两节蛇被打印到同一坐标 造成蛇的长度错误。
但是我不知道这代码哪里错了。。
求高手指教。