I want to implement a countdown timer in a game,
but need some help figuring out how to do it properly.

I know that this is probably a very regular question that has been asked a thousand times, but I've been trying out some methods I found through searching, but can't get them to work for whatever reason.

Basically, I want a timer (probably 120 seconds or so) to countdown, and if the player hasn't completed the objective before the timer reaches zero, then the level will restart.

I think I can figure out the rest of it, but I just need some help getting an actual timer up and counting.

I assumed I would use whatever countdown function, while printing the current time to the screen with:

textprintf_ex(screen, font, 300, 0, RED, BLACK, "Time: %d", time_left);

Help please? Thanks :)

Recommended Answers

All 4 Replies

I want to implement a countdown timer in a game,
but need some help figuring out how to do it properly.

I know that this is probably a very regular question that has been asked a thousand times, but I've been trying out some methods I found through searching, but can't get them to work for whatever reason.

Basically, I want a timer (probably 120 seconds or so) to countdown, and if the player hasn't completed the objective before the timer reaches zero, then the level will restart.

I think I can figure out the rest of it, but I just need some help getting an actual timer up and counting.

I assumed I would use whatever countdown function, while printing the current time to the screen with:

textprintf_ex(screen, font, 300, 0, RED, BLACK, "Time: %d", time_left);

Help please? Thanks :)

Yes this has already been covered. Here is the link.

Have Fun :)

I want to implement a countdown timer in a game,
but need some help figuring out how to do it properly.

I know that this is probably a very regular question that has been asked a thousand times, but I've been trying out some methods I found through searching, but can't get them to work for whatever reason.

Basically, I want a timer (probably 120 seconds or so) to countdown, and if the player hasn't completed the objective before the timer reaches zero, then the level will restart.

I think I can figure out the rest of it, but I just need some help getting an actual timer up and counting.

I assumed I would use whatever countdown function, while printing the current time to the screen with:

textprintf_ex(screen, font, 300, 0, RED, BLACK, "Time: %d", time_left);

Help please? Thanks :)

If you don't want to mess with processes, interrupts, hooks, and threads, you can do a relatively easy one with simple signals. Here's an example I gave someone, though it may be a bit too limited for your needs.

http://www.daniweb.com/forums/thread269274.html

Java makes it a bit easier than regular old C++ with its Timer class and Action Listeners, I think. Basic C++ provides signals and that's about it. Beyond that it is OS-specific and you may want to go beyond the regular old portable C++.

Depending on your OS and IDE, I wouldn't be surprised if there may be stuff already written for, say, Microsoft Visual Studio, or you can try Boost or any of a whole bunch of stuff out there that may be already written, similar to Java. I'm not sure though.

In the real world, all that timer stuff is written using multiple processes, multiple threads, and inter-process communication, where even fairly simple stuff gets non-trivial really fast.

What is your experience level?

What is your experience level?

Fairly low, fairly experienced... ? Leaning more toward fairly low haha..
I'm gonna check out the link you gave, so hopefully it will help me accomplish what I need. I don't need anything fancy, just a basic countdown with a hard-coded time limit. If the player hasn't accomplished the objective by the time it reaches zero, I want the game to be over.

Thanks for the quick responses. It is appreciated!

Thinking more, I really doubt either my link or IceMan's is going to do the job for you. I think you are probably looking at two processes: one for the game itself, one for the timer, and the timer will have the sleep commands and will signal the game process when the countdown is complete. I can't see a way to do it using a single thread in a single process.

Are you using Allegro? I've never used it, so no help there, but they might have something written that does what I am talking about where you don't have to handle this multiprocessing yourself; it's done for you. Here's a link that may be promising.

http://alleg.sourceforge.net/latestdocs/en/alleg005.html

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.