Question

I've read all that I can find and looked at many examples of a game loop on pyglet, but I'm still not sure HOW it's working or what exactly is happening.

(These are the articles I read...)

http://www.pyglet.org/doc/programming_guide/the_application_event_loop.html

http://www.pyglet.org/doc/api/toc-pyglet.event-module.html

I understand that the basic structure is something like this (this is just an example):

INITIALIZE GAME WINDOW
game_window = pyglet.window.Window(800, 600)

ATTACH EVENT HANDLERS
@game_window.event
def on_draw():
    game_window.clear()
    player_sprite.draw()

START PYGLET
pyglet.app.run()

I know it all works in practice, but I don't quite understand it. And I feel like until I have a good grip on the mechanics, I won't be able to use pyglet to its full potential.

So you tell pyglet to run and... it finds the objects that have events (the game_window) and it somehow finds and calls those functions that you attached handlers to? How does it know which scope/namespace to find them in? Does it just scan your entire code until it finds where you put the event handlers? Does it loop over them? How does it know where to start and stop the loop? How does it work???

Thank you!

Was it helpful?

Solution

Let's play:

class Window:
    def __init__(self, x, y):
        global app
        app = self
    def event(self, func):
        self.what_todo = func
    def run(self):
        self.what_todo()

>>> game_window = Window(800, 600)
>>>
>>> @game_window.event
... def on_draw():
...     print("I am drawing!")
...
>>> app.run()
I am drawing!
>>>

Of course, they probably are doing things a bit differently, but I hope you get the basic idea.

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