首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >游戏Joystick.get_axis()总是返回零

游戏Joystick.get_axis()总是返回零
EN

Stack Overflow用户
提问于 2016-10-02 13:34:07
回答 1查看 7.3K关注 0票数 5

我买了一个罗技Gamepad F310来远程控制机器人。我在Linux (第18版,Sarah)上使用Pygame (版本1.9.1,2.7.11+)。

作为检查操纵杆功能的一个简单测试,我编写了这个简短的脚本,输出不同轴的值:

代码语言:javascript
复制
import pygame

pygame.display.init()
pygame.joystick.init()
pygame.joystick.Joystick(0).init()

# Get the name of the joystick and print it
JoyName = pygame.joystick.Joystick(0).get_name()
print "Name of the joystick:"
print JoyName

# Get the number of axes
JoyAx = pygame.joystick.Joystick(0).get_numaxes()
print "Number of axis:"
print JoyAx

# Print the values for the axes
pygame.event.pump()
print pygame.joystick.Joystick(0).get_axis(0)
print pygame.joystick.Joystick(0).get_axis(1)
print pygame.joystick.Joystick(0).get_axis(2)
print pygame.joystick.Joystick(0).get_axis(3)

无论运行脚本时轴位于哪个位置,我总是得到以下输出:

代码语言:javascript
复制
Name of the joystick:
Logitech Gamepad F310
Number of axis:
6
SDL_JoystickGetAxis value:0:
0.0
SDL_JoystickGetAxis value:0:
0.0
SDL_JoystickGetAxis value:0:
0.0
SDL_JoystickGetAxis value:0:
0.0

我理解输出"SDL_joystick.Joystick(0).getAxis值“是因为开发人员在编译源代码时保留了调试标志。但是,我不明白为什么脚本返回所有轴值的0,而不是0位置。

我用jstest-gtk软件包测试了操纵杆。使用这个包,它可以顺利地工作。我的代码中有错误吗?

我在windows中运行了代码,在那里运行得很顺利。知道它为什么不能在Linux发行版下工作吗?

EN

回答 1

Stack Overflow用户

发布于 2019-10-31 16:22:33

从问题的编辑中提取的.

在最初的代码中(没有在上面发布),我有一个while循环,但是我犯了一个新手错误,将pygame.event.pump()行放在while循环之外。我现在要学习如何使用这个模块的代码工作得很好。

代码语言:javascript
复制
import pygame

pygame.display.init()
pygame.joystick.init()
pygame.joystick.Joystick(0).init()

# Prints the joystick's name
JoyName = pygame.joystick.Joystick(0).get_name()
print "Name of the joystick:"
print JoyName
# Gets the number of axes
JoyAx = pygame.joystick.Joystick(0).get_numaxes()
print "Number of axis:"
print JoyAx

# Prints the values for axis0
while True:
        pygame.event.pump()
        print pygame.joystick.Joystick(0).get_axis(0)

我确实花了一些时间来调整和重新编译joystik.c源代码,以去掉"SDL_joystick.Joystick(0).getAxis值:0:“打印输出。显然,这个问题已经知道了好几年了,但是Ubuntu/Mint并没有解决这个问题。

票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39817549

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档