简体   繁体   English

如何在事件队列中为pygame添加按键事件

[英]How to add keypress events in event queue for pygame

I have a game made with pygame which runs perfectly okay. 我有一个用pygame制作的游戏,运行完全没问题。 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. 我想创建一个系统,它从一个文件中读取按键(它将包含按键代码按下单独的行)并将它们添加到pygame事件队列中,以便玩家代理自己移动而不会实际按下按键。

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. 在阅读了pygame文档后,我尝试创建一个新的事件对象并将其添加到队列中,但事件构造函数需要在任何地方都找不到的属性。

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. 当我实际按下'a'键时,会添加突出显示的事件。 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. 更新2:我在keydown事件的事件处理代码中添加了一些print语句。 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'. 我通过打印当我实际按下键'a'时发生的事件找到了这些值。

The Event object's first argument is its type, which is an integer between pygame.USEREVENT and pygame.NUMEVENTS (24 to 32 exclusive). Event对象的第一个参数是它的类型,它是pygame.USEREVENTpygame.NUMEVENTS之间的整数(24到32独占)。 This is used to identify your event from other events, such as pygame.KEYDOWN , pygame.MOUSEBUTTONDOWN etc. 这用于从其他事件中识别您的事件,例如pygame.KEYDOWNpygame.MOUSEBUTTONDOWN等。

The second argument is either a dictionary or keyword arguments. 第二个参数是字典或关键字参数。 These will be your event's attributes. 这些将是您的活动的属性。 The dict should contain strings as keys. dict应该包含字符串作为键。

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

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

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