0

我试图让玩家 lightcycle 继续向一个方向移动而不会停止,直到玩家按下按钮将其移动到另一个方向。我不确定如何使用 kbhit 做到这一点,所以请给我一些建议!谢谢。

void Lightcycle(){
    if (kbhit()) {// get user key input
    char GetCh = getch(); // GetCh equal to the button the user presses
    if (GetCh == 'w'){PlayerX = PlayerX - 1;}
    else if (GetCh == 's'){PlayerX = PlayerX +1;}
    else if (GetCh == 'd'){PlayerY = PlayerY +1;}
    else if (GetCh == 'a'){PlayerY = PlayerY - 1;}
}// end kbhit
}// end function
4

1 回答 1

2

我想您需要一个名为的全局变量direction并对其进行更改,例如:

if (GetCh == 'w'){direction=1;}
else if (GetCh == 's'){direction=2;}
else if (GetCh == 'd'){direction=3;}
else if (GetCh == 'a'){direction=4;}

然后你需要在你的游戏循环中的某个地方,不断地处理玩家的移动,比如:

while(gameRunning){
    // Random code handling game goes here
    ...
    if (direction== 1){PlayerX = PlayerX - 1;}
    else if (direction== 2){PlayerX = PlayerX +1;}
    else if (direction== 3){PlayerY = PlayerY +1;}
    else if (direction== 3){PlayerY = PlayerY - 1;}
    ...
    // Other code handling game goes here
}
于 2013-11-18T00:00:52.440 回答