TGC Codebase Backup



Countdown timer and Elapsed time timer by IanM

15th Sep 2003 13:58
Summary

Shows both a countdown timer and an elapsed time timer.



Description

This code contains a single function that can convert elapsed or countdown times to hours/mins/secs/hundredths. Also included is a simple example of how to pause the timers that is activated by pressing and holding the spacebar



Code
                                    ` This code was downloaded from The Game Creators
                                    ` It is reproduced here with full permission
                                    ` http://www.thegamecreators.com
                                    
                                    sync on
sync rate 0

ElapsedTime = timer()
CountdownTime = ElapsedTime + 1000000

Paused=0

do
   cls
   print GameTime(ElapsedTime)
   print GameTime(CountdownTime)

   if spacekey()
      ` Record the time that pausing started
      PauseStart=timer()

      ` Wait for unpause
      while spacekey()
      endwhile

      ` Adjust the timers by the paused time
      inc ElapsedTime, timer()-PauseStart
      inc CountdownTime, timer()-PauseStart
   endif

   sync
loop

function GameTime(BaseTime as integer)
   local TimeValue as integer
   local TextTime as string

   TimeValue=abs( timer() - BaseTime )

   ` Hundredths
   TimeValue = TimeValue / 10
   TextTime=PadNumber( TimeValue mod 100 , 2)

   ` Seconds
   TimeValue = TimeValue / 100
   TextTime=PadNumber( TimeValue mod 60, 2) + ":" + TextTime

   ` Minutes
   TimeValue = TimeValue / 60
   TextTime=PadNumber( TimeValue mod 60, 2) + ":" + TextTime

   ` Hours
   TimeValue = TimeValue / 60
   TextTime=PadNumber( TimeValue mod 60, 2) + ":" + TextTime

endfunction TextTime

function PadNumber(n as integer, Size as integer)
   local PaddedNumber as string

   PaddedNumber=str$(n)
   while len(PaddedNumber) < Size
      PaddedNumber = "0" + PaddedNumber
   endwhile
endfunction PaddedNumber