【问题标题】:PyGame code doesn't execute properly when event is executed执行事件时 PyGame 代码无法正确执行
【发布时间】:2010-12-11 10:00:23
【问题描述】:

我正在尝试制作最简单的 pythong 代码,当按下操纵杆上的按钮时会做出响应。我使用了几个不同示例的代码,但我仍然无法让它工作。当我按下触发器(或任何按钮)时,以下代码不会调度事件

import pygame

joy = []

def handleJoyEvent(e):
     if e.type == pygame.JOYBUTTONDOWN:
        str = "Button: %d" % (e.dict['button'])
        if (e.dict['button'] == 0):
            print ("Pressed!\n")
    else:
        pass

def joystickControl():
    while True:
        e = pygame.event.wait()
        if (e.type == pygame.JOYBUTTONDOWN):
            handleJoyEvent(e)

# main method
def main():
    pygame.joystick.init()
    pygame.display.init()
    for i in range(pygame.joystick.get_count()):
        myjoy = pygame.joystick.Joystick(i)
        myjoy.init()
        joy.append(myjoy)

    # run joystick listener loop
    joystickControl()

# allow use as a module or standalone script
if __name__ == "__main__":
    main()

【问题讨论】:

  • 我试过你的代码,它对我有用。因此,您的计算机识别您的操纵杆或其他东西可能存在问题。您是否在其他应用程序中成功尝试过操纵杆?

标签: python pygame


【解决方案1】:

我假设您已经尝试放弃 if 并仅打印 str? 您的操纵杆也可能无法正常工作。它在其他程序中是否有效?

如果您使用的是 linux,您可能需要安装游戏杆驱动程序。对于 Windows,请检查设备管理器。

【讨论】:

    猜你喜欢
    • 2017-05-26
    • 1970-01-01
    • 2016-08-29
    • 2020-02-29
    • 2013-08-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多