我希望能够在按下“a”键后调用移动螺旋桨的函数(OpenGL 工作)。这是我的设置:
switch (key)
{
case 'a':
startShip = 1;
while (1 (&& startShip == 1)) {
spinPropeller();
}
}
我陷入了无限循环。我尝试过使用计时器功能,但我不确定如何正确实现它。我希望我的螺旋桨旋转并且在程序关闭之前不会停止。
编辑:程序应在检测到“q”键后关闭
最佳答案
游戏循环的工作原理如下:
while (true)
{
get_input(); // get keyboard, mouse, and joystick input
move_items(); // update the player position and all other items in the game, fire weapons, and update game state
collision_detection(); // figure out what hit what and update game state
render(); // draw your OpenGL scene
}
为了实现这一点,您的 get_input 函数或等效函数需要以非阻塞方式读取键盘状态。我认为 OpenGL 或 GLUT 有一个辅助函数来实现这一点,但实现可能是特定于平台的。
关于C中按下按键后连续调用函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40073250/