Pygame 从 BU0836X HID 设备读取轴值

Pygame read axis value from BU0836X HID device

我有一个 BU0836X 操纵杆接口板,我想用它来读取模拟操纵杆值。 http://www.leobodnar.com/shop/index.php?main_page=product_info&products_id=180

计算机上有一个 python 脚本 运行 使用 pygame 的操纵杆模块捕获操纵杆值。

但是,可以从板上获取所有信息,例如名称、轴数、按钮数等。 随附的校准工具也可以正常工作。

我遇到的唯一问题是我无法读取轴数据,这当然可以与任何标准游戏手柄完美配合。 是否有解决方法可以在 pygame 环境中启动电路板并 运行?

这是我用来测试的超级简单的脚本:

import pygame

pygame.joystick.init()

while True:

   joystick_count = pygame.joystick.get_count()

   for i in range(joystick_count):
        joystick = pygame.joystick.Joystick(i)
        joystick.init()
        name = joystick.get_name()
        axes = joystick.get_numaxes()
        hats = joystick.get_numhats()
        button = joystick.get_numbuttons()
        joy = joystick.get_axis(0)
        print (name,joy)

输出为:

('BU0836X Interface', 0.0)

有时当您不在每个帧[=35]中调用某些Pygame事件队列函数时,有时会发生此问题=] 你的游戏,如 Documentation 所述。

如果您在主游戏中没有使用任何其他事件函数,您应该调用pygame.event.pump() 允许 Pygame 处理内部操作,例如操纵杆信息。

尝试以下更新代码:

while True:
   pygame.event.pump() #allow Pygame to handle internal actions
   joystick_count = pygame.joystick.get_count()

   for i in range(joystick_count):
        joystick = pygame.joystick.Joystick(i)
        joystick.init()

        name = joystick.get_name()
        axes = joystick.get_numaxes()
        hats = joystick.get_numhats()
        button = joystick.get_numbuttons()

        joy = joystick.get_axis(0)

        print(name, joy)

替代方法 等待游戏杆(例如JOYAXISMOTION)生成的事件,方法是使用pygame.event.get() 函数例如:

while True:
    #get events from the queue
    for event in pygame.event.get(): 
        if event.type == pygame.QUIT:
            pygame.quit()

        if event.type == pygame.JOYAXISMOTION and event.axis == 0:
            print(event.value)

   joystick_count = pygame.joystick.get_count()

   for i in range(joystick_count):
        joystick = pygame.joystick.Joystick(i)
        joystick.init() #event queue will receive events from your Joystick 

希望对您有所帮助:)