null
null

Reputation: 566

_getch is seeming to pause my program

The title is pretty descriptive. Basically, I am making Space Invaders in the console window in C++, it's called ASCII invaders. I seem to have everything in the game working fine except for one major issue, _getch is pausing the program. I call _kbhit to check is the player is entering player input, then if so, I use _getch to get the key and act properly. The player moves left/right just fine, but when the player presses "Space" or shoot, the program pauses until you press "Space" again, at that point it will also shoot.

Obviously the player doesn't want this in a game. So would love to know how to fix it. Here is where it is called:

/***************************************/
/*          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;
}

As a side note, if you kind of just scrolled through thinking "That's a lot of code... I don't want to deal with this." (I do that sometimes - not picking on you) please note it's not that much, I just use comments a lot and a lot of white space.

And if you want to know, here is the entire "main" function.

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;
}

I have included all of the proper libraries, I get no compile errors.

Oh yes, before I forget. Here are the constants:

/******************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;
/***********************************************/

Any help is greatly appreciated!

Upvotes: 0

Views: 816

Answers (3)

shauryachats
shauryachats

Reputation: 10405

When you call _getch() twice, the first time, it doesn't record the key, just hangs there until you press the key again, because then _getch() is assigned to the variable key. Hence, you should remove the stray _getch() in your move_player() function.

As @RetiredNinja mentioned, you do need to call _getch() twice, so you need to establish a check on the first _getch() to establish if you have pressed an alphabet key, or a special key.

Upvotes: 0

Retired Ninja
Retired Ninja

Reputation: 4925

As explained in the documentation When reading a function key or an arrow key, each function must be called twice; the first call returns 0 or 0xE0, and the second call returns the actual key code.

So, you need to check what is returned to know if you should call the function again.

Here's an example that you should be able to adapt to your needs:

#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;
}

You might also consider the ReadConsoleInput family of functions since it gives you a bit more flexibility. Here's an example from MSDN.

Upvotes: 1

shiv
shiv

Reputation: 1952

Try changing:

_getch();
int key = _getch();

to:

int key = _getch();

in your move_player function.

Upvotes: 0

Related Questions