
-----------------------------------
vassosman
Sat Jun 14, 2008 10:59 am

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

-----------------------------------
Tony
Sat Jun 14, 2008 1:12 pm

RE:Time Remaining count down
-----------------------------------
time_allowed - (current_time - start_time) = time_remaining

-----------------------------------
Stove
Sat Jun 14, 2008 1:46 pm

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:


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:


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 :P

-----------------------------------
Insectoid
Sun Jun 15, 2008 12:53 pm

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.


var count := 1000

loop
    %Do game stuff
    count -= 1
    exit when count = 0
end loop


-----------------------------------
Tony
Sun Jun 15, 2008 1:29 pm

Re: RE:Time Remaining count down
-----------------------------------
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.

-----------------------------------
andrew.
Mon Jun 16, 2008 11:33 am

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.
