Skip to main content
gamedev.net
🔒 Locked

countdown timer

Started by iamanoob Dec 29, 2006 at 1:41 AM 0 replies 400+ views
Original Post
iamanoob
iamanoob
Hi, I need to implement a countdown timer in my game whereby for eg. 3 minutes counting down to 0 sec, which will end the game. May I know if there's anyone who has the source codes for it? Thanks.
haegarr
haegarr
Write a function that gets the current system time from an abtracting API (if you use one) or the underlying OS. (You haven't told us what you use, so we can't tell you how it could work in more detail.) Return the time value converted to seconds.

Before entering the game loop, invoke the above function to get the game start moment. Then inside the game loop use it again to get the current moment. Compute the difference and break the loop (i.e. stop the game) if the difference is greater than N.

Alternatively for a "real" countdown, e.g. if you want to display the remaining time, add N to the start moment value before entering the game loop. Then subtract the current moment inside the loop.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.