Posted: 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
exitwhen count =0
count -=1 delay(1000) endloop
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:
loop
now :=Time.Elapsed if(now - last >= 1000)then
count -=1 put count
last := now
endif endloop
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
Insectoid
Posted: 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 exitwhen count =0 endloop
Tony
Posted: 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.
Time.Elapsed will give you the time, as in the time on your watch. A counter will count the frames of the loop.
Posted: 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.