Code archives/Algorithms/Race Timer Functions

This code has been declared by its author to be Public Domain code.

Download source code

Race Timer Functions by Rob Farley2003
This set of functions allows you to deal with a timer.

Sounds easy I know, but having written a race game I know it's a pain in the arse!

Basically this has 3 little functions, reset_clock(), start_stop_clock() and get_clock()

The usage is shown in the example below.

So the race starts... reset_clock(): start_stop_clock()

The game gets paused... start_stop_clock()

The game restarts... start_stop_clock()

The game ends... start_stop_clock()

In the game loop... timer$=get_clock()

Easy as that!

Oh and if you want to check if the clock is running or not t_clock_mode=0 is stopped and 1 is running.

Let me know if this is of use, credit me if you use it... it's nice to isn't it!
; Race timer functions by Rob Farley
; 2003 Mentalillusion
; http://www.mentalillusion.co.uk
; rob@mentalillusion.co.uk

Graphics 400,300,0,2


; globals necessary for the timer to work
Global t_clock=0
Global t_offset=0
Global t_total_offset=0
Global t_clock_mode=0



SetBuffer BackBuffer()

; reset clock function to zero the clock
reset_clock()


Repeat									; main loop


; get the current time and put it into a variable
timer$=get_clock$()

Color 255,255,255
Text 200,0,timer$,True
Text 200,40,"Space to Stop/Start, R to reset clock",True

Flip

; clock control
If KeyHit(57) Then start_stop_clock()	;start/stop clock on spacebar
If KeyHit(19) Then reset_clock()		;reset clock if you hit R


Cls

Until KeyHit(1)							;until you hit ESC




Function get_clock$()
	If t_clock_mode=1 Then t=MilliSecs()-t_clock-t_total_offset
	If t_clock_mode=0 Then t=t_offset-t_clock-t_total_offset
	h=t/10
	s=h/100
	h=h-(s*100)
	m=s/60
	s=s-(m*60)
	
	Return Right("00"+m,2)+":"+Right("00"+s,2)+":"+Right("00"+h,2)
	; return the clock in MM:SS:HH format

End Function

Function reset_clock()
	t_clock=MilliSecs()
	t_offset=MilliSecs()
	t_total_offset=0
End Function

Function start_stop_clock()
	If t_clock_mode=0
		t_clock_mode=1
		t_total_offset=t_total_offset+MilliSecs()-t_offset
		Else
		t_clock_mode=0
		t_offset=MilliSecs()
		EndIf
End Function

Comments

None.

Code Archives Forum