Python PyGame-RaspberryPi 3b和x2B;使用ps3控制器
我正在尝试使用pygame和raspberry pi来使用PlayStation 3控制器作为汽车的输入。 我用演示代码测试了控制器,一切正常。然后当我尝试在我的程序中使用它时,当操纵手柄移动时,它读取0.0作为输入。附件是我目前的代码:Python PyGame-RaspberryPi 3b和x2B;使用ps3控制器,python,pygame,raspberry-pi3,ps3,Python,Pygame,Raspberry Pi3,Ps3,我正在尝试使用pygame和raspberry pi来使用PlayStation 3控制器作为汽车的输入。 我用演示代码测试了控制器,一切正常。然后当我尝试在我的程序中使用它时,当操纵手柄移动时,它读取0.0作为输入。附件是我目前的代码: import pygame class controller: def __init__(self): pygame.init() pygame.joystick.init()
import pygame
class controller:
def __init__(self):
pygame.init()
pygame.joystick.init()
global joystick
joystick = pygame.joystick.Joystick(0)
joystick.init()
def get_value(self, axis):
value = joystick.get_axis(axis)
return value
control = controller()
val = control.get_value(0)
while True:
print(val)
我知道该测试仅适用于轴0,但所有轴的输出仍然为0.0
下面,我附上了演示代码,其中所有的值都被正确读取
import pygame, sys, time #Imports Modules
from pygame.locals import *
pygame.init()#Initializes Pygame
pygame.joystick.init()
joystick = pygame.joystick.Joystick(0)
joystick.init()#Initializes Joystick
# get count of joysticks=1, axes=27, buttons=19 for DualShock 3
joystick_count = pygame.joystick.get_count()
print("joystick_count")
print(joystick_count)
print("--------------")
numaxes = joystick.get_numaxes()
print("numaxes")
print(numaxes)
print("--------------")
numbuttons = joystick.get_numbuttons()
print("numbuttons")
print(numbuttons)
print("--------------")
loopQuit = False
while loopQuit == False:
# test joystick axes and prints values
outstr = ""
for i in range(0,4):
axis = joystick.get_axis(i)
outstr = outstr + str(i) + ":" + str(axis) + "|"
print(outstr)
# test controller buttons
outstr = ""
for i in range(0,numbuttons):
button = joystick.get_button(i)
outstr = outstr + str(i) + ":" + str(button) + "|"
print(outstr)
for event in pygame.event.get():
if event.type == QUIT:
loopQuit = True
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
loopQuit = True
# Returns Joystick Button Motion
if event.type == pygame.JOYBUTTONDOWN:
print("joy button down")
if event.type == pygame.JOYBUTTONUP:
print("joy button up")
if event.type == pygame.JOYBALLMOTION:
print("joy ball motion")
# axis motion is movement of controller
# dominates events when used
if event.type == pygame.JOYAXISMOTION:
# print("joy axis motion")
time.sleep(0.01)
pygame.quit()
sys.exit()
任何反馈都将不胜感激。代码将丢失对初始化操纵杆的引用。它需要保持与它的内部链接。请注意在下面的类中使用的
self.
。这会将引用保留在类中,从而使“self.moggle”成为类的一部分。Python类需要self.
符号(与许多(所有?)其他面向对象的语言不同)。在编辑时,我更改了一些名称以匹配Python,我希望这样可以;)
也许您没有考虑额外的代码,但是没有事件循环的PyGame程序最终会锁定
import pygame
# Window size
WINDOW_WIDTH = 300
WINDOW_HEIGHT = 300
class Controller:
""" Class to interface with a Joystick """
def __init__( self, joy_index=0 ):
pygame.joystick.init()
self.joystick = pygame.joystick.Joystick( joy_index )
self.joystick.init()
def getAxisValue( self, axis ):
value = self.joystick.get_axis( axis )
return value
### initialisation
pygame.init()
window = pygame.display.set_mode( ( WINDOW_WIDTH, WINDOW_HEIGHT ) )
clock = pygame.time.Clock()
pygame.display.set_caption( "Any Joy?" )
# Talk to the Joystick
control = controller()
# Main loop
done = False
while not done:
for event in pygame.event.get():
if ( event.type == pygame.QUIT ):
done = True
# Query the Joystick
val = control.getAxisValue( 0 )
print( "Joystick Axis: " + str( val ) )
# Update the window, but not more than 60fps
window.fill( (0,0,0) )
pygame.display.flip()
clock.tick_busy_loop(60)
pygame.quit()
代码正在丢失对初始化操纵手柄的参考。它需要保持与它的内部链接。请注意在下面的类中使用的
self.
。这会将引用保留在类中,从而使“self.moggle”成为类的一部分。Python类需要self.
符号(与许多(所有?)其他面向对象的语言不同)。在编辑时,我更改了一些名称以匹配Python,我希望这样可以;)
也许您没有考虑额外的代码,但是没有事件循环的PyGame程序最终会锁定
import pygame
# Window size
WINDOW_WIDTH = 300
WINDOW_HEIGHT = 300
class Controller:
""" Class to interface with a Joystick """
def __init__( self, joy_index=0 ):
pygame.joystick.init()
self.joystick = pygame.joystick.Joystick( joy_index )
self.joystick.init()
def getAxisValue( self, axis ):
value = self.joystick.get_axis( axis )
return value
### initialisation
pygame.init()
window = pygame.display.set_mode( ( WINDOW_WIDTH, WINDOW_HEIGHT ) )
clock = pygame.time.Clock()
pygame.display.set_caption( "Any Joy?" )
# Talk to the Joystick
control = controller()
# Main loop
done = False
while not done:
for event in pygame.event.get():
if ( event.type == pygame.QUIT ):
done = True
# Query the Joystick
val = control.getAxisValue( 0 )
print( "Joystick Axis: " + str( val ) )
# Update the window, but not more than 60fps
window.fill( (0,0,0) )
pygame.display.flip()
clock.tick_busy_loop(60)
pygame.quit()
唯一的区别似乎是这一行:
全局操纵杆
。你试过删除它吗?我试过删除全局,我得到一个错误,“NameError:全局名称‘操纵杆’没有定义唯一的区别似乎是这一行:全局操纵杆
。你试过删除它吗?我试过删除全局,我得到一个错误,”名称错误:全局名称“操纵杆”未定义谢谢您的反馈。我已更改代码以使用self。在课堂上。仅此更改,该值仍为0.0。我试图理解您提供的代码的第二部分。窗户和钟是干什么的?我只是简单地使用pygame从控制器接收一个数字和轴,它最终会告诉汽车向前和向后移动。@ShaneCourter-pygame使用一个事件模型。如果停止处理事件,程序最终将停止。你的操作系统可能会认为它“反应迟钝”。有了这个窗口,事件循环阻止了这一切的发生。太棒了,它现在开始工作了。非常感谢您的帮助。谢谢您的反馈。我已更改代码以使用self。在课堂上。仅此更改,该值仍为0.0。我试图理解您提供的代码的第二部分。窗户和钟是干什么的?我只是简单地使用pygame从控制器接收一个数字和轴,它最终会告诉汽车向前和向后移动。@ShaneCourter-pygame使用一个事件模型。如果停止处理事件,程序最终将停止。你的操作系统可能会认为它“反应迟钝”。有了这个窗口,事件循环阻止了这一切的发生。太棒了,它现在开始工作了。非常感谢你的帮助。