Timer for racing game
Blitz3D Forums/Blitz3D Beginners Area/Timer for racing game
| ||
Im writing a 2D skiing game and want and accurate timer so that i can hold the best times in a high score table,havent programmed in ages,just doing this project to get me back into programming. I want the timer to record minutes,seconds,hundreths of seconds,heres the code i have: The hundreths doesnt seem that accurate,anyone know of a better way of doing this?. Thanks for the help. oldtime=MilliSecs() oldtime1=MilliSecs() mins=0 seconds=0 hundreths=0 SetBuffer BackBuffer() Repeat Cls If MilliSecs()>oldtime1+10 Then hundreths=hundreths+1:oldtime1=MilliSecs() If MilliSecs()>oldtime+1000 Then seconds=seconds+1:oldtime=MilliSecs() If seconds=60 Then seconds=0:mins=mins+1 Locate 10,10 Print "TIME 0"+mins+":"+seconds+":"+hundreths If hundreths>99 Then hundreths=1 Flip Until False |
| ||
This is nice an solid as it doesn't rely on any if statements. The biggest problem you've got it when the player hits pause halfway through a game! That takes a bit of work! time=MilliSecs() Repeat h=(MilliSecs()-time)/10 s=h/100 h=h-(s*100) m=s/60 s=s-(m*60) Cls timer$=Right("00"+m,2)+":"+Right("00"+s,2)+":"+Right("00"+h,2) Text 0,0,timer$ Until KeyHit(1) |
| ||
I would prefer to do it by noting Millisecs() when the race starts (say, 'StartTime'), then subtract that value from Millisecs() and pass the result to a function to turn into a readable string. This accurately records minutes, seconds and thousandths of a second - you might want to modify it if all you need is hundredths:Function CalcTime$(T) If T > 59459998 TIM$ = "99:59.999" Return TIM$ EndIf S = Int(T / 1000) FS = T - (S*1000) M = S / 60 S = S - (M*60) If M > 99 M = 99 : S = 59 EndIf If M > 99 Then M = 99 If S > 59 Then S = 59 If FS > 999 Then FS = 999 MM$ = Str$(M) SS$ = Str$(S) FF$ = Str$(FS) If Len(MM) = 1 Then MM = "0" + MM If Len(SS) = 1 Then SS = "0" + SS If Len(FF) = 1 Then FF = "00" + FF If Len(FF) = 2 Then FF = "0" + FF TIM$ = MM + ":" + SS + "." + FF Return TIM$ End Function Note: The "T" value passed to the function would be "Millisecs() - StartTime". To get around the problem of pausing is easy. When you press the pause key, do "PauseStart = Millisecs()". When the player un-pauses the game, simply do "StartTime = StartTime + (Millisecs()-PauseStart)". This basically means the length of time the game was paused for, will be added onto the time recorded at the start of the race, compensating for the pause. I've used this method in 3 racing games. It works well and its very accurate. |
| ||
OK... Converted to a function. GFK, I must say, your function looks way overly complex. start_time=MilliSecs() SetBuffer BackBuffer() Repeat timer$=get_time$(MilliSecs()-start_time) Color 255,255,255 Text 0,0,timer$ Flip ; clear time Color 0,0,0 Text 0,0,timer$ Until KeyHit(1) Function get_time$(t) 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) End Function |
| ||
Thanks guys i`ll try em both out. |
| ||
GFK, I must say, your function looks way overly complex. My my, we have got our critical head on today, haven't we? I'm trying to point the guy in the right direction - I don't expect to be "bitch slapped" for posting a function that hasn't been optimised to death. :/It works. End of. |
| ||
GFK it wasn't supposed to be a bitch slapping, I was just curious if there was a need for it, that maybe you'd hit across a problem that the additional lines fixed. That's all. Sorry that you took it the wrong way. |
| ||
NP. I've had some arse-brained n00b on another Blitz site, telling me *I* don't know how LoadAnimImage works. Puts you in a mood when they argue that black is white. Not your fault (except for your unfortunate timing). Sorry. Anyway, its just a function that works. Thats why I posted it. Haven't tried yours but if it works, pazza can use whichever one makes the most sense to him. :) |