c++ - _getch 似乎暂停了我的程序

标签 c++ io msdn

标题描述性很强。基本上,我在 C++ 的控制台窗口中制作 Space Invaders,它被称为 ASCII 入侵者。我似乎让游戏中的所有东西都运行良好,除了一个主要问题,_getch 正在暂停程序。我调用 _kbhit 检查玩家是否正在输入玩家输入,如果是,我使用 _getch 获取 key 并正确执行。播放器可以左右移动,但是当播放器按下“Space”或射击时,程序会暂停,直到您再次按下“Space”,此时它也会射击。

显然,玩家不希望在游戏中出现这种情况。所以很想知道如何修复它。这是它的名字:

/***************************************/
/*          move_player();             */
/*      Calculates player movement     */
/*Returns weather or not toQuitTheGame */
/***************************************/
bool move_player()
{
    bool ret = false;
    //If a key is pressed
    if (_kbhit())
    {
        //Get key
        _getch();
        int key = _getch();
        //Check is the player moves left
        if (key == LEFT && object_handle.player_obj.x > 0)
        {
            //Move left
            object_handle.player_obj.x -= 1;
        }

        //Check is the player moves right
        if (key == RIGHT && object_handle.player_obj.x < 79)
        {
            //Move right
            object_handle.player_obj.x += 1;
        }

        //Check is the player is shooting
        if (key == SHOOT)
        {
            //shoot by creating the bullet above the player
            object_handle.bullet_obj.x = object_handle.player_obj.x;
            object_handle.bullet_obj.y = object_handle.player_obj.y - 1;
            object_handle.bullet_obj.active = true;
        }

        //Weather or not to kill the program
        if (key == VK_ESCAPE)
        {
            ret = true;
        }
        else
        {
            ret = false;
        }
    }
    return ret;
}

作为旁注,如果您只是滚动浏览并想着“代码太多了……我不想处理这个。” (我有时会这样做 - 不是在挑剔你)请注意它并没有那么多,我只是经常使用评论和大量的空白。

如果您想知道,这里是整个“主要”功能。

int main()
{
    bool quit = false;
    object_handle.set_info();

    //Display main manu
    menu();

    //Main game loop
    while(!quit)
    {
        //Update past object variables
        object_handle.xy_update();

        //Calculate player movements
        quit = move_player();

        //Calculate bullet movements
        handle_objects();

        //Finally, update the screen
        screen_update();

        //DEBUG CODE
        debug();

        //Display score/stats
        stats();

        //Wait a given time before continuing the loop
        Sleep(INTERVAL);
    }
    cout << "\n\n\n\n\nBye" << endl;
    Sleep(1000);
    return 0;
}

我已经包含了所有正确的库,没有出现编译错误。

哦,是的,在我忘记之前。以下是常量:

/******************CONSTANTS********************/
const int ENEMY_COUNT   = 15;
const int INTERVAL      = 250;
const int LEFT          = 75;
const int RIGHT         = 77;
const int SHOOT         = VK_SPACE;
const int BULLET_SPEED  = 8;
/***********************************************/

非常感谢任何帮助!

最佳答案

the documentation 中所述读取功能键或方向键时,每个功能必须调用两次;第一次调用返回 0 或 0xE0,第二次调用返回实际的键码。

因此,您需要检查返回的内容以了解是否应再次调用该函数。

这是一个您应该能够适应您的需求的示例:

#include <conio.h>
#include <iostream>

int main()
{
    std::cout << "Type stuff, press x to quit.\n";
    int count = 0;
    for(;;)
    {
        //To show the loop isn't blocked
        if((++count % 1000) == 0)
        {
            std::cout << ".";
        }
        if(_kbhit())
        {
            int key = _getch();
            if(key == 0 || key == 0xE0)
            {
                key = _getch();
            }
            std::cout << "\nKeycode: " << key << "\n";
            if(key == 'x')
            {
                break;
            }
        }
    }
    return 0;
}

您还可以考虑 ReadConsoleInput 函数系列,因为它为您提供了更多的灵 active 。这是一个 example from MSDN .

关于c++ - _getch 似乎暂停了我的程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28122738/

相关文章:

c++ - 在显式实例化的情况下,类模板的成员函数是否可以内联?

c++ - 尝试使用 prelude 设置 ggtags 和 flycheck 时错误的 emacs 配置

c - 类似于 Linux 的 C 库中的 SetFileLength() 的函数

c# - Int64(长整型)和线程安全

c# - 在 C# 程序集的上下文中,激活作用域指的是什么?

c# - System.DirectoryServices.ActiveDirectory 和 Microsoft.ActiveDirectory 之间的区别

c++ - 使用 std::qualifier 修复奇怪的编译器错误

C++ 防止复制成员数据

haskell - 为什么使用 fromMaybe 提取 IO (Maybe Bool) 执行两个 IO 操作

c++ - 使用C++将不规则文件读入二维数组