Ask Your Question

Revision history [back]

Force cv waitKey regardless of keypressing

Hey Girls and Boys,

if have the following problem... I wan't to create a pong game in opencv. The bar should be controlled via the eye movement laterly. For now I control it with the keyboard and that's the matter.

I use the waitkey function to detect if the player moves the bar to the right or left, simultaneously the time from waitkey is needed by imshow.

What happens now is, that if the player hits a key, the game accelerates cause waitkey doesn't wait its time anymore. Unfortunately I need a fixed wait time regardless if a key is pressed or not for a constant game speed.

Heres the critical code part:

while (keypressed != 'q')
        cv::Mat output = blank.clone();
        cv::imshow("PongGame", output);

        keypressed = cv::waitKey(50);
        //std::cout << "key: " << (int)keypressed << std::endl;

        if(keypressed == 'g')
            mybar.position.x = mybar.position.x -10;
        if(keypressed == 'j')
            mybar.position.x = mybar.position.x +10;
        myball.position = myball.position + cv::Point2i(myball.xVelocity, myball.yVelocity);

Any Ideas?