Question

I am testing out pyglet for usage in a larger project, and apparently pyglet recommends/wants you to use it's own loop (with pyglet.app.run())

This is a something I don't want, for reasons of compatibility of other packages and also to not have to rewrite the entire program structure.

Here I have prototype code stuck together from different parts and tutorials and docs. It runs for 5-15 iterations and then just freezes, not printing anything and also not doing any draw updates.

from __future__ import division, print_function

import sys

import pyglet

window = pyglet.window.Window(800, 800, resizable=True)
window.set_caption('Pyglet Testing')
window.flip()

image = pyglet.resource.image('Sprites/scout.png')

def draw(dt):
    image.blit(700-dt, 400)

while not window.has_exit:
    dt = pyglet.clock.tick()
    window.dispatch_events()
    window.clear()
    draw(dt)
    window.flip()
    print(dt)

My suspicion is that I have done nothing to catch events and handle them, so at a certain point it just overflows with events and blocks the whole thing. I couldn't understand how to do this however, and getting overflowed with events in under 1 second seems a bit much.

Any help?

Was it helpful?

Solution

Basically what you are doing is sending as many image.blit(...) commands to the window, until the pc probably can't handle it anymore.

For instance, if you change your code like this:

add this code:

import time  
from time import sleep  

change code:

    def draw(dt):    
        image.blit(700-dt, 400)  
        sleep(0.1)             #insert this line  

When executing the modified code, you will notice that it does not freeze and that the output dt is around 0.11 seconds, which is the number of seconds since the last "tick" = the time slept (0.1 second) + the remainder time (clear window, display new frame ...)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top