-3

I want to add timer in my game.it works every 30 seconds and after any 30 seconds it should start another 30 seconds. Every player have 30 sec to play his turn and when it finished he just has 3 pack of 30 seconds to use them for playing his turn.If his packs are finished, it's the turn f another player. I don't have any idea of it, I don't know how to make timer in c or c++.

  • 3
    Possible duplicate of [C++ Cross-Platform High-Resolution Timer](https://stackoverflow.com/questions/1487695/c-cross-platform-high-resolution-timer) – Duck Dodgers Jan 22 '19 at 07:54
  • see [this](https://stackoverflow.com/questions/538609/high-resolution-timer-with-c-and-linux), [this](https://stackoverflow.com/questions/1487695/c-cross-platform-high-resolution-timer) or this [this](https://stackoverflow.com/questions/9777638/c-background-timer) – Duck Dodgers Jan 22 '19 at 07:56

2 Answers2

1

If you have boost, boost::asio library provides one solution to the problem. See for example this answer C++ Boost ASIO simple periodic timer?

darune
  • 10,480
  • 2
  • 24
  • 62
1

In addition to the answers above :) if you want a simple handy solution this may help

#include <ctime>
#include <cstdlib>
int main()
{

    clock_t startTime = clock(); //Start timer
    double secondsPassed;
    double secondsToDelay = 30;
    std::cout << "Time to delay: " << secondsToDelay << std::endl;
    bool flag = true;
    while (flag)
    {
        secondsPassed = (clock() - startTime) / CLOCKS_PER_SEC;
        if (secondsPassed >= secondsToDelay)
        {
            std::cout << secondsPassed << " seconds have passed" << std::endl;
            flag = false;
        }
    }
}
Spinkoo
  • 2,080
  • 1
  • 7
  • 23