Pygame事件队列 [英] Pygame event queue

查看:435
本文介绍了Pygame事件队列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有一种使用 poll() get()而不删除来自队列的事件。



在我的游戏中,我检查不同地方的输入(不仅在主循环中),有时我需要在不同的地方检查相同的事件,但是当我检查一旦它从队列中删除它。我尝试使用 peek(),但问题是我无法获得与完成的事件相对应的密钥。

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

#works但是从队列中删除事件

这可以获得与事件相对应的密钥,但使用 peek()它不能:

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

但是我不能使用第一种方法,因为从队列中删除事件,所以我无法检查该程序其他地方的关键事件。

我不明白如何队列的作品,所以也许我只是错误,但我尝试了第一个在不同的位置,只有我第一次检查它的工作。



我的目标是在我的游戏中检查不同类的事件。



感谢您的帮助

解决方案

我认为更好的设计是在一个地方检查事件 - 即使在mainloop代码之外的一个分解出的函数或方法,并将所有相关事件数据保存在其他对象(作为属性)或变量中。



例如,您可以保留对所有当前按下的键,当前鼠标位置和按钮状态的Python集的引用,并将这些变量传递给函数和方法。



否则,如果您的需要是只检查按键和鼠标状态(和指针位置),您可以完全绕过事件(仅在mainloop上保留对pygame.event.pump()的调用)。 pygame.key.get_pressed 函数是我最喜欢阅读键盘的方式 - 它返回一个与键码一样多的位置的序列,每个按下的键都有其对应的在此向量中设置为 True 的位置。 (键盘代码可用作pygame.locals中的常量,如K_ESC,K_a,K_LEFT等)。



Ex:

 如果pygame.key.get_pressed()[pygame.K_ESCAPE]:
pygame.quit()
/ pre>

鼠标模块(记录在 http://www.pygame.org/docs/ref/mouse.html )允许您获取鼠标状态,而不消耗事件。



最后,如果你真的想要获得事件,我看到的可能性是将事件转发到队列,如果它们没有被使用,调用 pygame.event.post - 可以放置此调用,例如在if / elif序列中的 else 子句中,您可以检查事件队列中的某些状态。


I would like to know if there is a way of using poll() or get() without removing the events from the queue.

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.

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:

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.

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.

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.

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). 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. (The keycodes are available as constants in pygame.locals, like K_ESC, K_a, K_LEFT, and so on).

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.

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.

这篇关于Pygame事件队列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆