【问题标题】:Pygame event not workingPygame 事件不起作用
【发布时间】:2019-01-27 02:28:02
【问题描述】:

这是我正在制作的游戏的代码,我设法将第一个文本写入屏幕,然后它设法打印事件,但它不会运行代码的第二部分。我假设if event.type == pygame.K_RETURN: 不起作用。我要么做错了什么,要么它不起作用。

while True:   
    for event in pygame.event.get():
        #print(event)
        if event.type==pygame.KEYDOWN:
            print("key pressed")
            if event.type == pygame.K_RETURN:
                print(event)
                print("enter key pressed")
                screen.fill(blue)
                textSurf, textRect = text_objects(t2, smallText)
                textRect.center = ((700),(100))
                screen.blit(textSurf, textRect)
                pygame.display.update()
    pygame.display.update()

【问题讨论】:

    标签: python python-3.x pygame


    【解决方案1】:

    是的,if event.type == pygame.K_RETURN: 应该是 if event.key == pygame.K_RETURN:

    pygame.KEYDOWNpygame.KEYUP 事件类型有一个 key 属性,用于检查按下了哪个键。

    您可以在文档中找到事件类型及其属性的列表: http://www.pygame.org/docs/ref/event.html

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多