简体   繁体   English

pyglet游戏循环如何工作?

[英]How exactly does the pyglet game loop work?

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. 我已经阅读了所有可以找到的内容,并查看了pyglet上许多游戏循环的示例,但是我仍然不确定它是如何工作的或究竟发生了什么。

(These are the articles I read...) (这些是我阅读的文章...)

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

http://www.pyglet.org/doc/api/toc-pyglet.event-module.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. 而且我感觉,直到我对技巧有所了解,我才能充分利用pyglet的潜力。

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? 因此,您告诉pyglet运行并...它找到具有事件的对象(game_window),并以某种方式找到并调用附加了处理程序的函数? 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! 谢谢!

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. 当然,他们做事的方式可能有所不同,但我希望您了解基本概念。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM