Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Time Remaining count down
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
vassosman




PostPosted: Sat Jun 14, 2008 10:59 am   Post subject: Time Remaining count down

can some body please tell me or send me to a link on how to have a countdown or time remaining on my game
thank you in advanced
Sponsor
Sponsor
Sponsor
sponsor
Tony




PostPosted: Sat Jun 14, 2008 1:12 pm   Post subject: RE:Time Remaining count down

time_allowed - (current_time - start_time) = time_remaining
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
Stove




PostPosted: Sat Jun 14, 2008 1:46 pm   Post subject: Re: Time Remaining count down

There are probably a couple ways to make a countdown timer. One way you could do it, is to make a loop with a delay, and increment a counter variable each time. Something like:

Turing:

var count : int := 10
loop
    put count
    exit when count = 0
    count -= 1
    delay (1000)
end loop


The only problem with that method is that delay() blocks the program while it waits, which means nothing else on that process will be able to run while your counter is counting down. So for example you wouldn't be able to update the screen (more than the delay value) and run the countdown at the same time. So a better method would be to use Time.Elapsed to find the amount of time (in milliseconds) that has passed each time a loop is run. So something like:

Turing:

var last, now, count : int := 10

last := Time.Elapsed

put count

loop
    now := Time.Elapsed
    if (now - last >= 1000) then
        count -= 1
        put count
        last := now
    end if
end loop


This way is non-blocking, so you can update the screen and have other things happening while the countdown runs. You can also use this method to achieve a steady frame rate on a game by having it fire a game procedure at a set interval. One last thing, just remember to use greater than or equal to (>=) and not just equal to (=) when comparing the "now" and "last" variables, because "now - last" may not always hit 1000 directly.

Anyways, hope that answers your question.

Edit: Opps, Tony beat me to it Razz
Insectoid




PostPosted: Sun Jun 15, 2008 12:53 pm   Post subject: RE:Time Remaining count down

I think a counter would be a better idea. Time.Elapsed does not account for a computer's processor speed. So on my computer at home, I could do twice as much in the same Time.Elapsed as on the crappy school computers. A counter will always be the same speed as the program.

Oh wait...stove seems to have combined the methods...Though it does not look like it will acount for lag.

Turing:

var count := 1000

loop
    %Do game stuff
    count -= 1
    exit when count = 0
end loop
Tony




PostPosted: Sun Jun 15, 2008 1:29 pm   Post subject: Re: RE:Time Remaining count down

insectoid @ Sun Jun 15, 2008 12:53 pm wrote:
Time.Elapsed does not account for a computer's processor speed.

That's kind of the point to not have your CPU bend the space-time continuum. Wink

Time.Elapsed will give you the time, as in the time on your watch. A counter will count the frames of the loop.
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
andrew.




PostPosted: Mon Jun 16, 2008 11:33 am   Post subject: RE:Time Remaining count down

Haha, space-time continuum. Anyways, yeah, use the way that Stove said. That way is better then delays because it doesn't stop the rest of your program.
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 6 Posts ]
Jump to:   


Style:  
Search: