简体   繁体   中英

How to add keypress events in event queue for pygame

I have a game made with pygame which runs perfectly okay. I want to create a system which reads keys to press from a file (which will contain codes of keys to press in separate lines) and adds them to the pygame event queue so that the player agent moves on its own without the keys actually being pressed.

After reading the pygame docs I have tried to create a new event object and add it to the queue but the event constructor requires attributes which I could not find anywhere.

Does anyone know which attributes are needed to create an instance of an event or if there is another better approach for what I am trying to do?

UPDATE: I have successfully added my events to the queue however they do not seem to work even though they are completely identical to the ones created automatically. I am printing the event queue below. Highlighted events are added when I actually press the 'a' key. As you can see my events (the ones above) do not trigger the rest of the events as they should.

UPDATE 2: I added some print statements to the event handling code for the keydown events. These are only executed if I actually press the key on the keyboard and just seem to ignore the events I raise from the code.
input_list = [pg.K_RETURN, pg.K_a, pg.K_s] if self.cursor.state == c.PLAYER1: self.cursor.rect.y = 358 if keys[pg.K_DOWN]: print("down") self.cursor.state = c.PLAYER2 for input in input_list: if keys[input]: print("button") self.reset_game_info() self.done = True elif self.cursor.state == c.PLAYER2: self.cursor.rect.y = 403 if keys[pg.K_UP]: print("up") self.cursor.state = c.PLAYER1

I am creating my events like so:
pg.event.post(pg.event.Event(pg.KEYDOWN, {'mod': 0, 'scancode': 30, 'key': pg.K_a, 'unicode': 'a'}))
I found these values by printing the event which happens when I actually press the key 'a'.

The Event object's first argument is its type, which is an integer between pygame.USEREVENT and pygame.NUMEVENTS (24 to 32 exclusive). This is used to identify your event from other events, such as pygame.KEYDOWN , pygame.MOUSEBUTTONDOWN etc.

The second argument is either a dictionary or keyword arguments. These will be your event's attributes. The dict should contain strings as keys.

Here's a short example demonstrating how it's used:

import pygame
pygame.init()

event1 = pygame.event.Event(pygame.USEREVENT, {"greeted": False, "jumped": 10, "ID": 1})
event2 = pygame.event.Event(pygame.USEREVENT, greeted=True, jumped=200, ID=2)

pygame.event.post(event1)
pygame.event.post(event2)

for event in pygame.event.get():
    if event.type == pygame.QUIT:
        raise SystemExit
    elif event.type == pygame.USEREVENT:
        print("Player:", event.ID, "| Greeted:", event.greeted, "| Jumped:", event.jumped)
    elif event.type == pygame.USEREVENT + 1:
        print("Player:", event.ID, "| Greeted:", event.greeted, "| Jumped:", event.jumped)

This will output:

Player: 1 | Greeted: False | Jumped: 10
Player: 2 | Greeted: True | Jumped: 200

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