我是C语言的初学者,尝试制作一个简单的贪吃蛇游戏时,遇到了指针在另一个函数中使用时值改变的问题。我使用指针来增加我的蛇的长度,使用了3个指针。虽然对于贪吃蛇游戏来说,这可能是一个非常新手的算法,但我觉得我已经接近成功了,但我无法弄清楚出了什么问题。我为蛇本身、每个部分的x坐标和y坐标都使用了3个指针。我不妨发一下我的代码的一部分。
#include<stdio.h>
#include<dos.h>
#include<conio.h>
printer(int *forgoodness,int *y,char *lang,int tx,int ty, int *x)
{
int h=*forgoodness-1,g=0;
这里的值发生了变化,不知何故 x 复制了 y 的 6 个值。
/* for(;g<15;g++)
{
printf("%i",x[g]);
}*/
for(;h>=0;h--)
{
gotoxy(x[h],y[h]);
printf("%c",lang[h]);
}
gotoxy(tx,ty);
printf(" ");
}
main()
{
int transferx=1,x=1,transfery=1,ch,game=0,dir;
int *transx, *transy, *numel;
int tempsx,g=0,tempsy,forex,j=0,*totalel;
char *snake;
int *snakey, *snakex;
如果我明确赋值,它可以很好地工作,但我不能使用它来让我的蛇变长:
//int snakex[15]={26,27,28,29,30,31,32,33,34,35,36,37,38,39,40};
//int snakey[15]={13,13,13,13,13,13,13,13,13,13,13,13,13,13,13};
clrscr();
*totalel=0;
*numel=14;
forex=26;
snake= "***************";
这是我所做的,我在函数 printer 运行之前检查了这些值,这些值都是正确的。
for(;j<15;j++)
{
snakey[j]=13;
snakex[j]=forex;
*totalel=j;
forex++;
}
printer(numel,snakey,snake,transferx,transfery,snakex);
我希望你能帮助我完成这个任务。谢谢。