gpt4 book ai didi

C/我如何从这里到达第 2 阶段?它只是循环并直接进入第 3 阶段

转载 作者:行者123 更新时间:2023-11-30 16:27:55 28 4
gpt4 key购买 nike

这是贪吃蛇游戏的部分代码。我想做的是制作关卡(大约3个),如果分数达到一定的分数(100或200),关卡就会改变。

在这段代码中,我尝试让分数达到 100 时进入第 2 阶段。但正如我编码的那样,它只是在 main 函数中循环并直接进入 stage3。

这是主要代码:

int main(){

title();

while(1)
{
if(kbhit()) do{key=getch();} while(key==224); // Key input
Sleep(speed);

switch(key) // Apprehend the key and runs the program
{
case LEFT:
case RIGHT:
case UP:
case DOWN:
if( (dir==LEFT&&key!=RIGHT)||(dir==RIGHT&&key!=LEFT)||(dir==UP&&key!=DOWN)||
(dir==DOWN&&key!=UP)) // Need for prevent the 180 degrees rotational movement
dir=key;
key=0; // Reset the key to 0
break;

case PAUSE:
pause();
break;

case 115: // input 'S', then status() activated
if(status_on==0) status_on=1;
else status_on=0;
key=0;
break;

case ESC: // Program exits if the input value is ESC
exit(0);
}
move(dir);
if(status_on==1) status();
}

如您所见,移动函数位于 while(1) 中。

这是 move(dir) 的代码:

void move(int dir)
{
int i, j;

if(x[0]==food_x&&y[0]==food_y) // Case when it hits with food
{
score+=10;
food();
length++;
x[length-1]=x[length-2];
y[length-1]=y[length-2];
}
for (j = 0; j < num_of_bombs; j++)
{
if (x[0] == bomb_x[j] && y[0] == bomb_y[j]) // Case when it hits with bomb
{
score -= 10;

for (j = 0; j < num_of_bombs; j++)
gotoxy(MAP_ADJ_X + bomb_x[j], MAP_ADJ_Y + bomb_y[j], " "); // Delets the lastest bombs

bomb();
gotoxy(MAP_ADJ_X + x[length - 1], MAP_ADJ_Y + y[length - 1], " "); // Delets the last body of the snake
length--;
}
}
if(x[0]==0||x[0]==MAP_X-1||y[0]==0||y[0]==MAP_Y-1) // Case when it hits the wall
{
game_over();
return;
}
for(i=1;i<length;i++) // Case when it hits itself
{
if(x[0]==x[i]&&y[0]==y[i])
{
game_over();
return;
}
}

gotoxy(MAP_ADJ_X+x[length-1],MAP_ADJ_Y+y[length-1]," "); // Delets it's last one
for(i=length-1;i>0;i--) // Move the coordinates one by one
{
x[i]=x[i-1];
y[i]=y[i-1];
}
gotoxy(MAP_ADJ_X+x[0],MAP_ADJ_Y+y[0],"▣"); // Part that changes head to body. But in this code, it is unnecessery
if(dir==LEFT) --x[0];
if(dir==RIGHT) ++x[0];
if(dir==UP) --y[0];
if(dir==DOWN) ++y[0];
gotoxy(MAP_ADJ_X+x[i],MAP_ADJ_Y+y[i],"▣"); // Part that puts new head. But unnecessery in this code

if (length < 2) // If length goes down below 2, gmae is over
game_over();

switch (score)
{
case 100:
case 200:
++stage;
break;
}

switch (stage)
{
case 2:
stage2();
case 3:
stage3();
}
}

有人可以帮我吗?

最好,

兰迪

最佳答案

默认情况下,switch 语句中的情况会失败,除非包含 break。在你的代码中你有

switch(stage) 
{
case 2:
stage2();
// Here there is an implicit fall through because a break statement is missing.
case 3:
// This gets executed if stage == 3 but also if stage == 2
// because of the fall through you first go to stage2() and then immediately afterwards to stage3()
stage3();
}

解决方案是在调用 stage2() 之后添加 break;

此行为会导致许多问题,以至于 gcc 等编译器具有选项 -Wimplicit-fallthrough。使用该选项,每次您有意使用该行为时,都必须添加类似 //fall-through 的注释。

关于C/我如何从这里到达第 2 阶段?它只是循环并直接进入第 3 阶段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52567511/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com