简体   繁体   中英

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. I have my main loop where a window is filled and things are drawn to the window. I also have a for loop to check for events such as quitting or resizing the window. Then I have a function that I call called draw_cursor() . 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.

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. 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. 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. 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.

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. 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? 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?

pygame.event.get() get all the messages and remove them from the queue. There are no more messages in the queue after pygame.event.get() has been called. Hence pygame.event.poll() almost always returns an event object of type pygame.NOEVENT in display_cursor .

You need to call display_cursor in the event loop. The event must be an argument of the 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:

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()

If you're looking for a more nuanced approach, see Pygame: how to write event-loop polymorphically

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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