English 中文(简体)
D. 热梅事件
原标题: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.

在我的游戏中,我检查了不同地点(不仅是主要圈子)的投入,有时,我需要在不同地点检查同一事件,而当它从问讯中除去时检查。 我尝试使用<代码>peek(),但问题是,我拿到了与所发生的事件相应的钥匙。

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

#works but removes event from the queue

这可以取得与活动相应的钥匙,但可使用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.

My goal is to check events in different classes in my game.

感谢您的帮助

最佳回答

我认为,更好的设计是在一个地方检查事件,即便是在主机代码之外计算功能或方法,并在其他喷气机(作为属性)或变数中保存所有相关的事件数据。

例如,你可以经常提及一个装有目前所有压力钥匙、目前湿度位置和纽扣状态的“灰色”装置,并将这些变量环绕到功能和方法上。

否则,如果你只需要检查压力和湿度状态的钥匙(和点疏松)的话,你就可以绕过事件(只把打上大通风的电话放在主路上)。 <代码>pyega.key.get_pressed function is my favorite way of Read the key板 - it re-一个序列 with as well as there are keycode, and each pressed key has itspondent status set to True. (如K_ESC、K_a、K_LEFT等,关键编码作为常数提供。)

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

模块(载于) 允许你在没有耗尽的情况下进入 mo状态。

最后,如果你真的想去做事情的话,我看到的是,如果他们不消费的话,会把事件重新贴在凯内,并打电话到<条码>pyega.event.post——例如可在<条码>上发出这一呼吁。 条款,如果是,可以按顺序,在出现问题时,由你检查某个州。

问题回答

我不知道这是否是好的风格,但我确实只是把所有事件都保存在一个变数中,并将其传递给使用自己的事件点探测“他们”事件的物体。

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

        self.allS.update(events)

以及更新方法:

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

只要我能够说,没有一种正确的方式来做到这一点,但一种选择是将所有事件都变数。 然后,你可以像你所希望的那样,与他们接触。





相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签