我见过这个问题的其他解决方案说你要么需要调用 pygame.event.pump() 要么在 while 循环之外初始化操纵杆。然而,即使使用这些解决方案,我得到的操纵杆轴值仍为 0。
如果我仅取消注释pygame.display.set_mode((1, 1)),则代码将按预期工作,并且值将输出到控制台。
有没有办法仍然可以获取轴值而无需创建额外的窗口?
另外,我在 Windows 10 上运行 python 3.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\nStarting 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 …Run Code Online (Sandbox Code Playgroud)