News:

Building a 3D Ray Tracer  By stevmjon

Main Menu

Count Down / Timing Examples

Started by kevin, February 14, 2013, 10:13:34 PM

Previous topic - Next topic

kevin

Count Down / Timing Examples


   Here's a couple of examples for creating a timer that's counting down, rather than up.  

  Timer / Milliseconds version #1.

PlayBASIC Code: [Select]
   ; ------------------------------------------------------------------------
; ------------------------------------------------------------------------
; -[ Count Down Using Timer ]---------------------------------------------
; ------------------------------------------------------------------------
; ------------------------------------------------------------------------

; load a replacement for font 1..
loadfont "veradana",1,100

; Number of th seconds the count down lasts for
MaxSeconds=5


; get the starting millisecond of this loop
StartTime=timer()

EndTime =StartTime+(MaxSeconds*1000)
repeat
cls

; Get the current millisecond that this loop is running
CurrentTime=Timer()

; get number of milliseconds that have past since we started this loop
TimePast=CurrentTime-StartTime

; convert the milliseconds to seconds and invert them for the count down
SecondsLeft=MaxSeconds-(TimePast/1000)

print SecondsLeft

print Timer()

Sync
until SecondsLeft<1


print "done"
Sync
waitkey
end





   Timer / Milliseconds version #2 - Use End time

PlayBASIC Code: [Select]
   ; ------------------------------------------------------------------------
; ------------------------------------------------------------------------
; -[ Count Down Using Timer ]---------------------------------------------
; ------------------------------------------------------------------------
; ------------------------------------------------------------------------

; load a replacement for font 1..
loadfont "veradana",1,100

; Number of th seconds the count down lasts for
MaxSeconds=5


; get the starting millisecond of this loop
StartTime=timer()

EndTime =StartTime+(MaxSeconds*1000)


repeat

cls

; Get the current millisecond that this loop is running
CurrentTime=Timer()

; get number of milliseconds until we past the expect end time
TimeLeft=EndTime-CurrentTime

; convert the milliseconds to seconds and invert them for the count down
SecondsLeft=TimeLeft/1000

print SecondsLeft
print Timer()

Sync
until SecondsLeft<=0


print "done"
Sync
waitkey
end