PYGAME在没有显示的情况下不返回操纵杆轴移动 [英] Pygame not returning joystick axis movement without display

查看:0
本文介绍了PYGAME在没有显示的情况下不返回操纵杆轴移动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我见过这个问题的其他解决方案,比如您需要调用pygame.vent.pump(),或者在While循环外部初始化操纵杆。然而,即使有了这些解决方案,我还是得到了操纵杆轴值的0。

如果我只取消对pygame.display.set_mode((1, 1))的注释,则代码将按预期工作,并且值将输出到控制台。

是否有方法仍然可以在不创建额外窗口的情况下获取轴值?

另外,我在Windows 10上运行的是python3.6。

import pygame

FRAMES_PER_SECOND = 20

pygame.init()
pygame.joystick.init()

# pygame.display.set_mode((1,1))

# Used to manage how fast the screen updates.
clock = pygame.time.Clock()

xboxController = pygame.joystick.Joystick(0)
xboxController.init()


# Loop until the user presses menu button
done = False

print('Found controller
Starting loop...')
while not done:
    pygame.event.pump()
    for event in pygame.event.get():
        if event.type == pygame.JOYBUTTONDOWN and event.button == 7:
            print(f'Exiting controller loop')
            done = True

    for i in range(xboxController.get_numaxes()):
        print(f'Axis {i}: {xboxController.get_axis(i)}')

    # pygame.display.flip()

    clock.tick(FRAMES_PER_SECOND)

输出:

pygame 1.9.6
Hello from the pygame community. https://www.pygame.org/contribute.html
Found controller
Starting loop...
Axis 0: 0.0
Axis 1: 0.0
Axis 2: 0.0
Axis 3: 0.0
Axis 4: 0.0
.
.
.

推荐答案

除非您需要整个底层GL功能,否则我可能不会使用它,因为该库是用于2D/3D游戏开发的。尽管可以将其用于这些目的,但最终的问题或多或少是不可避免的。一种可能更简单的方法是使用Python的input库,它可以处理游戏手柄(操纵杆)。

from inputs import get_gamepad

while True:
    events = get_gamepad()
    for event in events:
        if event.ev_type == 'Absolute':
            if event.code == 'ABS_X':
                print(f'Left joystick x: {event.state}')
            elif event.code == 'ABS_Y':
                print(f'Left joystick y: {event.state}')
            elif event.code == 'ABS_RX':
                print(f'Right joystick x: {event.state}')
            elif event.code == 'ABS_RY':
                print(f'Right joystick y: {event.state}')

这篇关于PYGAME在没有显示的情况下不返回操纵杆轴移动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆