【问题标题】:Call a function continuously after key is pressed in C在C中按下键后连续调用一个函数
【发布时间】:2016-10-16 17:31:20
【问题描述】:

我希望能够在按下“a”键后调用移动螺旋桨的函数(OpenGL 工作)。这是我设置的:

switch (key)
{
    case 'a':
        startShip = 1;
        while (1 (&& startShip == 1)) {
            spinPropeller();
        }
}

我陷入了无限循环。我尝试过使用计时器功能,但我不确定如何正确实现它。我希望我的螺旋桨旋转并且在程序关闭之前不会停止。

编辑:程序应在检测到“q”键后关闭

【问题讨论】:

  • 我认为在case 'a'之后应该有一个:
  • 只要我们没有看到,是什么(以及如何)改变了startShip 的值,就很难判断出什么问题。
  • 我试图证明按下键 'a' 后,startship 将值更改为 1,显示程序已准备好移动,并且螺旋桨将继续旋转
  • 另外,我不知道这行:while(1 (&& startShip == 1 )) 是否为您编译,可能不是因为它会被解释为函数,所以我将其更改为:while(1 && (startShip == 1 ))
  • 我看到 startShip = 1; 但它会永远保持为 1,您将无法输入新密钥,因为您的代码只会执行 spinPropeller。如果你想停止它,你必须等待 while 循环中的新密钥。

标签: c function loops call


【解决方案1】:

游戏循环的工作方式如下:

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 对此有帮助函数,但实现可能是特定于平台的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-23
    • 2014-10-17
    • 2018-10-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多