Pygame.joystick 返回 0

Pygame.joystick returning 0s

我正在尝试构建自己的迷你库以简化 pygame 中的操纵杆使用。但是,当我尝试读取按钮和轴时,它们都是 return 0,但仅在 assign 函数中(我使用的是 Logitech attack 3)

import sys

import pygame


joysticks = []

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


# Variables for locations in list #
AXIS_PITCH = 1
AXIS_ROLL = 0
AXIS_THROTTLE = 2
BUTTON_TRIGGER = BUTTON_1 = 3
BUTTON_2 = 4
BUTTON_3 = 5
BUTTON_4 = 6
BUTTON_5 = 7




# Must be called first, initializes joysticks #
def init():
    for joy in range(pygame.joystick.get_count()):
        joysticks.append(pygame.joystick.Joystick(joy))
        joysticks[joy].init()


# Needs player count, surface to draw to, font to use, colour of text, dimesions of screen #
def assign(players, screen, font, colour, dimensions):
#     unassigned = joysticks.copy()
    toReturn = []
    for i in range(players):
        assigned = False
        while(not assigned):
            if(pygame.key.get_pressed()[pygame.K_ESCAPE]): pygame.quit(); sys.exit();
            screen.fill([0,0,0])
            outString = "Player "+str(i+1)+" pull the trigger"
            out = font.render(outString, 1, colour, None)
            screen.blit(out, (dimensions[0]/2-out.get_width()/2, dimensions[1]/2-out.get_height()/2))
            pygame.display.flip()
            for stick in joysticks:
                print(stick.get_axis(1))
                if(stick.get_button(0) == 1):
                    print("Triggered")
                    if(stick not in toReturn):
                        toReturn.append(stick)
                        assigned = True
    return(toReturn)


# Returns the values of three axis, and 5 buttons on the passed joystick
def check(joystick):
    toReturn = []
    toReturn.append(joystick.get_axis(0))
    toReturn.append(joystick.get_axis(1))
    toReturn.append(joystick.get_axis(2))
    toReturn.append(joystick.get_button(0))
    toReturn.append(joystick.get_button(1))
    toReturn.append(joystick.get_button(2))
    toReturn.append(joystick.get_button(3))
    toReturn.append(joystick.get_button(4))
    return(toReturn)

如果您需要更多代码,请询问。提前致谢!

如果您不处理事件,get_pressed()get_button()get_axis() 等函数可能无法工作 - 当您不使用 pygame.event.get() 或其他事件函数时调用 pygame.event.pump() 从系统获取信息。

定期(即在您的 while 循环中)使用 pygame.event.get()(或其他事件函数)从系统获取当前信息。