Pygame Event Queue

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

In my game, I check the input in different places (not only in the main loop), and sometimes I need to check the same event in different places, but when I check it, as soon as it removes it from the queue. I tried using peek() , but the problem is that I cannot get the key corresponding to the event.

 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 cannot:

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

However, I cannot use the first method because it removes an event from the queue, so I cannot check key events elsewhere in the program.
I don’t understand how queue works, so maybe I’m just mistaken, but I tried the first one in another place and only the first time I checked the event in which it was working.

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

thanks for the help

+4
source share
2 answers

I think that the best design will check events in one place - even if in the factored function or method outside the mainloop code and store all the event data notnt in other objects (as attributes) or variables.

For example, you can save a link to a Python set with all the current keys pressed, the current mouse position and button state, and pass these variables to functions and methods.

Otherwise, if you need to check only the pressed keys and the state of the mouse (and the position of the pointer), you can completely bypass events (only by saving the calls to pygame.event.pump () on mainloop). The pygame.key.get_pressed function is my favorite way to read the keyboard - it returns a sequence with as many positions as there are key codes, and each key pressed has a corresponding position set to True in this vector. (Key codes are available as constants in pygame.locals, such as K_ESC, K_a, K_LEFT, etc.).

Example:

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

The mouse module (documented at http://www.pygame.org/docs/ref/mouse.html ) allows you to get the state of the mouse without using events.

And finally, if you really want to receive events, I see that this is an opportunity to transfer events to the queue, if they will not be used, with a call to pygame.event.post - this call can be placed, for example, in else in the if / elif sequence where you check some state in the event queue.

+2
source

I don’t know if the style is good, but I just saved all the events in a variable and passed them to 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) 
0
source

Source: https://habr.com/ru/post/1391039/


All Articles