AFrogCat
AFrogCat

Reputation: 56

pyglet.clock.tick is not working

I am working with pyglet and I find strange what does pyglet.clock.tick() return.

pyglet.clock.tick() is not working properly, the returned value is different from the elapsed time.

main.py:

import pyglet

window = pyglet.window.Window()

def update(*args):
    pass

@window.event
def on_draw():
    dt = pyglet.clock.tick()
    print(dt)

    # rendering

pyglet.clock.schedule_interval(update, 1.0 / 60.0)
pyglet.app.run()

output:

0.00023519893125589775
0.00023904764104010212
0.0006876361481085169
0.00020056054319805838
0.00024161344756334913
0.00023306075915385804
0.00038358807515770366
0.00021253430697143472
0.00021167903813035238
0.00019927763993665693
0.00024204108198366825

Upvotes: 2

Views: 1032

Answers (1)

fewquestions
fewquestions

Reputation: 96

You have used the clock twice per frame.

The dt is passed to the update function. Do not call the pyglet.clock.tick() in the update if you use pyglet.clock.schedule_interval, it will return the elapsed time since the last schedule_interval.

This is how your current time measuring works:

0s                     1/60s                   2/60s                   3/60s
|-----------------------|-------|---------------|-------|---------------|
                        <your dt>               <your dt>

The code should be:

import pyglet

window = pyglet.window.Window()

def update(dt):
    print(dt)

    # rendering

pyglet.clock.schedule_interval(update, 1.0 / 60.0)
pyglet.app.run()

Upvotes: 3

Related Questions