Search code examples
pythonpyglet

Is there a function for telling time in pyglet?


I would like to be able to somehow tell the time in pyglet, either by the actual clock time or time since the program was launched kind of like pygames pygame.time.get_ticks().

The pyglet wiki says:

from pyglet import clock
while True:
    dt = clock.tick()
    # ... update and render ...
    print 'FPS is %f' % clock.get_fps()

The dt value returned gives the number of seconds (as a float) since the last “tick”.

The get_fps function averages the framerate over a sliding window of approximately 1 second. (You can calculate the instantaneous framerate by taking the reciprocal of dt).

But im not sure what they mean with "instantaneous framerate" and i dont know what reciprocal is.


Solution

  • You can use python's time module. e.g.

    time.time() 
    

    Return the time in seconds since the epoch as a floating point number.

    or

    time.perf_counter()
    

    Return the value (in fractional seconds) of a performance counter, i.e. a clock with the highest available resolution to measure a short duration. It does include time elapsed during sleep and is system-wide.


    In PyGlet there is the pyglet.clock module

    e.g. the FPS can be determined by

    pyglet.clock.get_fps()
    

    See also Keeping track of time