【问题标题】:Pygame read axis value from BU0836X HID devicePygame 从 BU0836X HID 设备读取轴值
【发布时间】:2015-06-02 18:10:18
【问题描述】:

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

在计算机上,有一个 python 脚本正在运行,使用 pygame 的 joystick 模块捕获游戏杆值。

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

我遇到的唯一问题是我无法读取轴数据,这当然可以与任何标准游戏手柄完美配合。 有没有一种解决方法可以让开发板在 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)

【问题讨论】:

    标签: python pygame joystick


    【解决方案1】:

    Documentation 所述,当您没有在游戏的每一帧中对某些 Pygames 事件队列函数进行任何调用时,有时会发生此问题。

    如果您没有在主游戏中使用任何其他事件函数,您应该 调用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 
    

    希望这会有所帮助:)

    【讨论】:

    • 非常感谢!现在可以了!虽然我必须添加 pygame.init() 否则我得到一个 pygame.error: video system not initialized
    • @user3603948:我很高兴能帮助你:) 哦,是的,你是对的,pygame.init() 无论如何都是必要的。你测试了我提到的第二种方法吗?
    • 自从第一种方法有效以来,我没有测试第二种方法,但我也会很快测试它。再次感谢!
    猜你喜欢
    • 2016-10-28
    • 2016-12-07
    • 2014-01-20
    • 2011-04-08
    • 2010-12-04
    • 1970-01-01
    • 1970-01-01
    • 2020-02-29
    • 1970-01-01
    相关资源
    最近更新 更多