簡體   English   中英

在C中按鍵后連續調用函數

[英]Call a function continuously after key is pressed in C

我希望能夠在按“ 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對此具有輔助功能,但實現方式可能是特定於平台的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM