How can I make a time condition?

I'm trying to create a ping pong game and I need to say, "If the user doesn't press spacebar within 500 milliseconds, say" You've failed! ". And if not, send the ball flying to the other end.

The problem is that I cannot use a function like Sleep()

, in the event that the following expressions:

if (Sleep(500)) {cout << "You lost!"}

      

Is there any timing function I could use in an if statement?

+3


source to share


2 answers


Not.

You are programming at a lower level than a language with simple expressions to define such rules.


Some ideas

Basically you will need:

  • Set the timer to 500ms
  • Configure a handler for keystrokes
  • Ask your timer expiration handler to say "you lost" if the boolean flag is set, otherwise "send the ball to the net".
  • To have your handler toggle this boolean flag if a key press Space

At the most basic level, you can achieve this:



  • directly with two worker threads, or
  • roughly just "waiting" activity keystrokes ( select

    , poll

    , epoll

    ) with the parameter timeout (and make sure that you do not reset a timeout if he was pressed any other key instead) or
  • with your operating system, using a POSIX timer, for example (although be careful, this will take you to a C compatible platform and that's probably not where you end up wanting to end up).

Typically, to do something "right," we embed this logic in functionality provided by some engine that implements an "event loop" (especially common in games or in high I / O applications).

Further information is going to take the book to explain it adequately, and thus it is not appropriate for this environment. However, at least now you know what topics to study in the existing material.


Potentially interesting anecdote

I once wrote a small scripting language for my application so that I could just define neat high-level rules and the "compiler" would use my event loop to implement them. This made my application very easy to maintain and extend, which was important at the time because the rules became quite complex and the application was broad. It is not uncommon for computer game engines to embed their own scripting capabilities (say, using the "Lua" language) to achieve the same goal.

+3


source


It's good to start thinking about your game design in general from the very beginning, as it helps you solve such problems during the design phase and not further into development.

I'm not sure which library you are using, but in sfml it looks something like this:

// Clock holds the current time.
// When we call Restart it returns the elapsed time since the last restart.
// With the current time and the start time we can find the elapsed time.    
sf::Clock clock;
sf::Time maxTime = sf::milliseconds(500);
while(window.isOpen())
{
    clock.Restart();
    while(window.PollEvents(sf::Event e))
    { 
        if(e.type == sf::Event::KeyPressed)
        {
            if(e.key.code == UP || e.key.code == DOWN || e.key.code == AnyOfOurOtherKeys)
            {
                maxTime = sf::milliseconds(500);
            }
            if(e.key.code == UP)
            {
                // Handle each individual key
            }
            if(e.key.code == DOWN)
            {
                // Handle each individual key
            }
            etc..
        }
        maxTime -= clock.Restart();
        if(maxTime < sf::milliseconds(0))
        {
            std::cout << "Game Over, no input detected for 500 ms" << std::endl;
        }
    }

      

Regardless of the library you can always use std::chrono::high_resolution_clock

, std::chrono::milliseconds

and std::chrono::duration

to achieve the same results.



When you do your game loop you want to get startLoopTime and then at the end of the game loop you want to use endLoopTime. Then you can:

totalLoopTime = endLoopTime - startLoopTime;
maxTime -= totalLoopTime;

      

When you process input, you need a system where, if a key with a function is used, you set the maxTime to 500ms. You can alternatively set bool to true when the key is pressed and then run a check for bool to restart maxTime.

+2


source







All Articles