TIMER

Hello guys
Actually my game is like a quiz .

i wanted to insert a timer in my game so that when 30 seconds are over the
level on which the player is gets over.

how do we make or insert such a timer?
Using the standard ctime (time.h) library, take a snap of the current time when the question is started. Then in a while loop, check what the current time is and subtract from the original time to get the time elapsed.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <ctime> // Used for time_t and time()
#include <windows.h> // Used for sleep()

time_t InitialTime, CurrentTime; //Time in seconds

time(&InitialTime);

while (true)
{
  time(&CurrentTime);

  int TimeDiff = CurrentTime - InitialTime;
  cout << "Time left: " << 30 - TimeDiff << endl;

  Sleep(1000); // Wait 1000 ms before going to the next loop
}

By the way, if you want to run the timer in parallel to a cin, then you'll need to read up on threads. This will let you run two bits of code at the same time in parallel. This will prevent the timer from getting stuck as it waits for the cin.
Topic archived. No new replies allowed.