简体   繁体   English

检查 pygame 事件队列中的特定事件

[英]Checking for specific event in pygame event queue

I want to be able to check for a user defined event in pygame pygame.USERVENT+1 to be specific.我希望能够在 pygame pygame.USERVENT+1中检查用户定义的事件。 I have my main loop where a window is filled and things are drawn to the window.我有我的主循环,其中填充了 window,并将事物绘制到 window。 I also have a for loop to check for events such as quitting or resizing the window.我还有一个 for 循环来检查诸如退出或调整 window 等事件。 Then I have a function that I call called draw_cursor() .然后我有一个称为draw_cursor()的 function 。 I am trying to check for the specific event of pygame.USEREVENT+1 in this function so I could use it to draw an object however I cannot seem to get this event without using the event queue in the main function to check if pygame.USERVENT+1 is in the event queue. I am trying to check for the specific event of pygame.USEREVENT+1 in this function so I could use it to draw an object however I cannot seem to get this event without using the event queue in the main function to check if pygame.USERVENT+1在事件队列中。

import pygame

pygame.init()

width, height = 1024, 648
window = pygame.display.set_mode((width, height), pygame.RESIZABLE)

pygame.time.set_timer(pygame.USEREVENT, 1000)

def display_cursor():
    if pygame.event.poll().type == pygame.USEREVENT+1:
        print("This should print when USERVENT is in the event queue")

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            quit()
        if event.type == pygame.VIDEORESIZE:
            width, height = event.w, event.h
            window = pygame.display.set_mode((event.w, event.h), pygame.VIDEORESIZE)
        """if event.type == pygame.USEREVENT+1:
            print("This will print when USEREVENT in the event queue")"""

    display_cursor()

    window.fill((0,0,255))

    pygame.display.flip()

The commented out part is the part that would actually print when pygame.USERVENT+1 is in the event queue.注释掉的部分是当pygame.USERVENT+1在事件队列中时实际打印的部分。 I use pygame.event.poll().type as this returns the events as a number and pygame.USERVENT+1 is number 24. I would think this would work as pyame.event.poll().type should return the number 24 when pygame.USEREVENT+1 is in the event queue.我使用pygame.event.poll().type因为这将事件作为数字返回,pygame.USERVENT+1 是数字 24。我认为这会起作用pyame.event.poll().type应该返回数字 24当pygame.USEREVENT+1在事件队列中时。 I also had a look at: https://www.pygame.org/docs/ref/event.html#pygame.event.peek to also check in a similar way if the event is in the event queue.我还查看了: https://www.pygame.org/docs/ref/event.html#pygame.event.peek也以类似的方式检查事件是否在事件队列中。 I tried if pygame.event.peek(pygame.USEREVENT+1): instead of if pygame.event.poll().type == pygame.USEREVENT+1: It still didn't print out what I wanted it to do.我试过if pygame.event.peek(pygame.USEREVENT+1):而不是if pygame.event.poll().type == pygame.USEREVENT+1:打印出 IENT+142DA30A7Z 想要什么它仍然没有。

I would like to be able to check the specific event pygame.USERVEENT+1 only in this display_cursor() function and I am not using pygame.event.get() in a for loop just to check for one event as I have found it to slow down my program to a point where the window is barely responsive.我希望能够仅在此display_cursor() function 中检查特定事件pygame.USERVEENT+1而我没有使用pygame.event.get()中的一个事件,因为它找到了一个事件。将我的程序减慢到 window 几乎没有响应的程度。 Is there a way that I could only check for user defined events in a separate function without having to pass the event object to this function to stop it from slowing down?有没有办法我只能在单独的 function 中检查用户定义的事件,而不必将事件 object 传递给这个 function 以阻止它减速? Is there also a parameter or similar function that I am missing that does exactly what I thought pygame.event.poll() or pygame.event.peek() did?是否还有我缺少的参数或类似的 function 完全符合我的想法pygame.event.poll()pygame.event.peek()

pygame.event.get() get all the messages and remove them from the queue. pygame.event.get()获取所有消息并将它们从队列中删除。 There are no more messages in the queue after pygame.event.get() has been called.调用pygame.event.get()后队列中没有更多消息。 Hence pygame.event.poll() almost always returns an event object of type pygame.NOEVENT in display_cursor .因此pygame.event.poll()几乎总是在display_cursor中返回pygame.NOEVENT类型的事件 object。

You need to call display_cursor in the event loop.您需要在事件循环中调用display_cursor The event must be an argument of the function:该事件必须是 function 的参数:

def display_cursor(event):
    if event.type == pygame.USEREVENT+1:
        print("This should print when USERVENT is in the event queue")

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            quit()
        if event.type == pygame.VIDEORESIZE:
            width, height = event.w, event.h
            window = pygame.display.set_mode((event.w, event.h), pygame.VIDEORESIZE)
        """if event.type == pygame.USEREVENT+1:
            print("This will print when USEREVENT in the event queue")"""
        display_cursor(event)

Alternatively, you can get the list of events and pass the list of events to the function:或者,您可以获取事件列表并将事件列表传递给 function:

def display_cursor(event_list ):
    for event in event_list :
        if event.type == pygame.USEREVENT+1:
            print("This should print when USERVENT is in the event queue")

while True:
    event_list = pygame.event.get()
    for event in event_list:
        if event.type == pygame.QUIT:
            pygame.quit()
            quit()
        if event.type == pygame.VIDEORESIZE:
            width, height = event.w, event.h
            window = pygame.display.set_mode((event.w, event.h), pygame.VIDEORESIZE)
        """if event.type == pygame.USEREVENT+1:
            print("This will print when USEREVENT in the event queue")"""
        
    display_cursor(event_list)

See also Faster version of pygame.event.get()另请参阅pygame.event.get() 的更快版本

If you're looking for a more nuanced approach, see Pygame: how to write event-loop polymorphically如果您正在寻找更细微的方法,请参阅Pygame:如何以多态方式编写事件循环

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

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