简体   繁体   English

Pygame 事件队列

[英]Pygame event queue

I would like to know if there is a way of using poll() or get() without removing the events from the queue.我想知道是否有一种使用poll()get()而不从队列中删除事件的方法。

In my game, I check input at different places (not only in the main loop) and sometimes I need to check the same event at different places but when i check it once it removes it from the queue.在我的游戏中,我在不同的地方(不仅在主循环中)检查输入,有时我需要在不同的地方检查相同的事件,但是一旦它从队列中删除它,当我检查它时。 I tried using peek() but the problem is that I can't get the key corresponding to the event done.我尝试使用peek()但问题是我无法获得与完成的事件对应的密钥。

while 1:
    event = pygame.event.poll()
    if event.type == KEYDOWN:
        return event.key
    else:
        pass

#works but removes event from the queue

This can get the key corresponding to the event but with peek() it can't:这可以获得与事件对应的键,但使用peek()则不能:

pygame.event.peek(pygame.KEYDOWN).key 
#dosent work

However I can't use the first method because removes the event from the queue so I can't check key events elsewhere in the program.但是我不能使用第一种方法,因为从队列中删除了事件,所以我无法检查程序中其他地方的关键事件。
I don't understand well how the queue works so maybe I'm just mistaking but I tried the first one at different location and only the first time i checked the event it worked.我不太了解queue是如何工作的,所以也许我只是弄错了,但我在不同的位置尝试了第一个,并且只是第一次检查它起作用的事件。

My goal is to check events in different classes in my game.我的目标是在我的游戏中检查不同课程中的事件。

Thanks for your help谢谢你的帮助

I think a better design would be to check events in a single place - even if in a factored out function or method outside the mainloop code, and keep all the relevnt event data in other objetcts (as attributes) or variables. 我认为更好的设计是在单个位置检查事件-即使在mainloop代码之外的分解函数或方法中,也将所有相关事件数据保留在其他objetcts(作为属性)或变量中。

For example, you can keep a reference to a Python set with all current pressed keys, current mouse position and button state, and pass these variables around to functions and methods. 例如,您可以使用所有当前按下的键,当前的鼠标位置和按钮状态来保留对Python集合的引用,并将这些变量传递给函数和方法。

Otherwise, if your need is to check only for keys pressed and mouse state (and pointer posistion) you may bypass events entirely (only keeping the calls to pygame.event.pump() on the mainloop). 否则,如果您只需要检查按键和鼠标状态(以及指针指向),则可以完全绕开事件(仅在mainloop上保留对pygame.event.pump()的调用)。 The pygame.key.get_pressed function is my favorite way of reading the keyboard - it returns a sequence with as many positions as there are key codes, and each pressed key has its correspondent position set to True in this vector. pygame.key.get_pressed函数是我最喜欢的读取键盘的方法-它返回一个序列,该序列的位置与键码一样多,并且每个按下的键在此向量中的对应位置都设置为True (The keycodes are available as constants in pygame.locals, like K_ESC, K_a, K_LEFT, and so on). (键码可以在pygame.locals中作为常量使用,例如K_ESC,K_a,K_LEFT等)。

Ex: 例如:

if pygame.key.get_pressed()[pygame.K_ESCAPE]:
     pygame.quit()

The mouse module (documented in http://www.pygame.org/docs/ref/mouse.html ) allows you to get the mouse state without consuming events as well. 鼠标模块(在http://www.pygame.org/docs/ref/mouse.html中记录 )允许您在不消耗事件的情况下获取鼠标状态。

And finally, if you really want to get events, the possibility I see is to repost events to the Queue if they are not consumed, with a call to pygame.event.post - this call could be placed, for example at the else clause in an if/elif sequence where you check for some state in the event queue. 最后,如果您真的想要获取事件,那么我看到的可能性是,如果事件未被消耗,则将事件重新发布到Queue中,并调用pygame.event.post可以将其放置在例如else子句中以if / elif顺序,在其中检查事件队列中的某些状态。

I don't know if it is good style, but what I did was just saving all the events in a variable and passing it to the objects that used their own event queues to detect "their" events. 我不知道这是否是一种好的样式,但是我所做的只是将所有事件保存在变量中,然后将其传递给使用自己的事件队列检测“其”事件的对象。

while running:
        events = pygame.event.get()
        for event in events:
            if event.type == pygame.QUIT:
                running = False

        self.allS.update(events)

and in the update method: 并在更新方法中:

for event in events:
    print("Player ", event)

As far as I can tell there is no one 'right' way to do this, but one option is to save all the events into a variable.据我所知,没有一种“正确”的方法可以做到这一点,但一种选择是将所有事件保存到一个变量中。 Then you can access them as many times as you want.然后,您可以根据需要多次访问它们。

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

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