Video Game Timer

12wwood

New Member
Joined
Mar 5, 2019
Hey guys, I am trying to create a game that needs to include a timer. I have had trouble trying to write the code for it and I was wandering if anyone could lend a hand. I need to be able to access the current time like its a variable and print it/use it for manipulation other variables in the code.
 

Dubbsta

Active Member
Joined
Jul 13, 2017
This is an example from the docs under mojo.apps. This should help:)

Cerberus X:
Import mojo

Class MyApp Extends App

    Method OnCreate()
   
        SetUpdateRate 60
       
    End
   
    Method OnRender()

        Local date:=GetDate()
   
        Local months:=["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"]
   
        Local day:=("0"+date[2])[-2..]
        Local month:=months[date[1]-1]
        Local year:=date[0]
        Local hour:=("0"+date[3])[-2..]
        Local min:=("0"+date[4])[-2..]
        Local sec:=("0"+date[5])[-2..] + "." + ("00"+date[6])[-3..]
       
        Local now:=hour+":"+min+":"+sec+"  "+day+" "+month+" "+year
       
        Cls
        DrawText now,DeviceWidth/2,DeviceHeight/2,.5,.5
    End

End

Function Main()

    New MyApp
   
End
 

Holzchopf

Moderator
Joined
Jul 31, 2017
Location
Bern, Switzerland
Yeah Millisecs() is the way to go then. Use a variable like tstart that you set to it when entering the playing state. Millisecs() - tstart will then give you the milliseconds the state's been active. Or tend - tstart with tend = Millisecs() on leaving the state if you only need to know the playtime afterwards.
 

Paul59

Active Member
CX Code Contributor
Joined
Dec 13, 2018
Location
UK
I am using Mojo, I have a game state called playing and i need the timer to start when that state is entered and end when the state is left.
Here's a complete example, just wait a while for the output . At some random point the game state will change to FINISHED and print the start and end times and total seconds elapsed but the app will carry on running.

Cerberus X:
Strict

Import mojo

Enumerate PLAYING, FINISHED

Class MyApp Extends App


    Field startTime:Int
    Field endTime:Int
    Field state:Int
    
    Method OnCreate:Int()
    
        state = PLAYING
        startTime = Millisecs()
        ' ensure each run is random...
        Seed = Millisecs()
        Return 0
        
    End
    

    Method OnUpdate:Int()
    
        If Rnd() > 0.995 And state <> FINISHED
            state = FINISHED
            endTime = Millisecs()
            Print("Started: " + startTime)
            Print("Ended: " + endTime)       
            Print("That's: " + (endTime - startTime) / 1000.0 + " seconds")
        Endif
        
        Return 0
    End
    

End


Function Main:Int()

    Local a:MyApp = New MyApp()
    return 0
End
 
Top Bottom