马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 pipixia434 于 2023-1-18 03:02 编辑
运行后可以正常检测出xbox手柄 但是一直在
while not self.isQuit:
for event_ in pygame.event.get():
try:
self.Update(self)
不进入if 判断
就是说第一个程序可以进到elif event_.type == pygame.JOYBUTTONDOWN or event_.type == pygame.JOYBUTTONUP:
第二个code反而进不去了
用这个程序可以跑
import pygame
# 模块初始化
pygame.init()
pygame.joystick.init()
# 若只连接了一个手柄,此处带入的参数一般都是0
joystick = pygame.joystick.Joystick(0)
# 手柄对象初始化
joystick.init()
done = False
clock = pygame.time.Clock()
while not done:
for event_ in pygame.event.get():
# 退出事件
if event_.type == pygame.QUIT:
done = True
# 按键按下或弹起事件
elif event_.type == pygame.JOYBUTTONDOWN or event_.type == pygame.JOYBUTTONUP:
buttons = joystick.get_numbuttons()
# 获取所有按键状态信息
for i in range(buttons):
button = joystick.get_button(i)
print("button " + str(i) +": " + str(button))
# 轴转动事件
elif event_.type == pygame.JOYAXISMOTION:
axes = joystick.get_numaxes()
# 获取所有轴状态信息
for i in range(axes):
axis = joystick.get_axis(i)
print("axis " + str(i) +": " + str(axis))
# 方向键改变事件
elif event_.type == pygame.JOYHATMOTION:
hats = joystick.get_numhats()
# 获取所有方向键状态信息
for i in range(hats):
hat = joystick.get_hat(i)
print("hat " + str(i) +": " + str(hat))
joystick_count = pygame.joystick.get_count()
pygame.quit()
知乎上看到的一个类似的
但是写进class 就只能进不去if判断
一直在try 语句
不执行 elif event_.type == pygame.JOYBUTTONDOWN or event_.type == pygame.JOYBUTTONUP:import sys, os, threading, pygame, time, math
# axis0 0-L-R
# axis1 0-U-D
# button9 0
# axis3 1-L-R
# axis4 1-U-D
# button10 1
# axis2 LT
# axis5 RT
# button4 LD
# button5 RD
# button6 SELECT
# button7 START
# button0 A
# button1 B
# button3 X
# button4 Y
# hat0(0,0) = hat0(number,number) LEFT =-1 RIGHT=1 UP=1 DOWN =-1
class JoystickKey:
def __init__(self):
super(JoystickKey, self).__init__()
self.A = 0
self.B = 1
self.X = 3
self.Y = 4
self.LB = 6
self.RB = 7
self.LT = 8
self.RT = 9
self.SELECT = 10
self.START = 11
self.LEFTAXISBT = 12
self.RIGHTAXISBT = 13
self.C = 14
self.Z = 15
self.M1 = 16
self.M2 = 17
self.M3 = 2
self.M4 = 5
self.LEFT_AXIS = 0 or 1
self.RIGHT_AXIS = 2 or 3
self.LEFT_AXIS_RIGHT_LEFT = 0
self.LEFT_AXIS_UP_DOWN = 1
self.RIGHT_AXIS_RIGHT_LEFT = 2
self.RIGHT_AXIS_UP_DOWN = 3
self.LT_AXIS = 5
self.RT_AXIS = 4
class XboxController(threading.Thread):
def __init__(self):
super(XboxController, self).__init__()
self.isQuit = False
self.startTime = time.time()
self.joystickKey = JoystickKey()
pygame.init()
pygame.joystick.init()
self.clock = pygame.time.Clock()
self.joystick_count = pygame.joystick.get_count()
if self.joystick_count == 0:
pass
self.joystick = pygame.joystick.Joystick(0)
self.joystick.init()
self.joystick_name = self.joystick.get_name()
print(self.joystick_name)
def get_runtime(self):
return abs(self.startTime - time.time())
def _map_(self, value, in_minVal=0, in_maxVal=1, out_minVal=0, out_maxVal=255):
a = in_maxVal - in_minVal
b = out_maxVal - out_minVal
c = a / b
d = in_minVal + (value / c)
return int(d)
def run(self):
try:
self.Start(self)
except:
print('start not defined')
while not self.isQuit:
for event_ in pygame.event.get(): # 获取事件
if event_.type == pygame.QUIT:
self.isQuit = True
elif event_.type == pygame.JOYBUTTONDOWN or event_.type == pygame.JOYBUTTONUP:
self.buttons = self.joystick.get_numbuttons()
for _key in range(self.buttons):
key_status = self.joystick.get_button(_key)
tmp_key = None
if key_status == 1:
tmp_key = _key
try:
self.KeyDown(self, _key)
except:
print("KeyDown Error!")
pass
elif key_status == 0 and tmp_key == _key:
try:
self.KeyUp(self, _key)
except:
print("KeyUp Error!")
pass
elif event_.type == pygame.JOYHATMOTION:
hats = self.joystick.get_numhats()
for i in range(hats):
hat = self.joystick.get_hat(i)
try:
self.Hat(self, hat)
except:
pass
pass
elif event_.type == pygame.JOYAXISMOTION:
axis = self.joystick.get_numaxes()
for i in range(axis):
axis = self.joystick.get_axis(i)
try:
self.Axis(self, i, axis)
except:
pass
pass
try:
self.Update(self)
except:
print('update not defined')
try:
# pygame.quit()
pygame.joystick.quit()
self.Quit(self)
except:
print('quit not defined')
return
# def console_clear():
# if os.name == 'posix':
# _ = os.system('clear')
# else:
# _ = os.system('cls')
if __name__ == '__main__':
def Update(content):
print('process update! %s'%(xboxController.get_runtime()))
return
def Start(content):
# print('process started!')
return
def Quit(content):
# print('process quit!')
return
def KeyDown(content, key):
# print('按键按下 %s \r\n'%(str(key)))
if (key == content.joystickKey.A):
print('hello world')
return
def KeyUp(content, key):
# print('按键释放 %s \r\n'%(str(key)))
return
def Hat(content, hat):
(y, x) = hat
print('x = %s,y = %s' % (x, y))
return
def Axis(content, axis_index, axis):
if (axis != 0.0):
if axis_index == content.joystickKey.RIGHT_AXIS:
print('RIGTH_AXIS')
elif axis_index == content.joystickKey.LEFT_AXIS:
print('LEFT_AXIS')
print('axis index is %s ,value is %s' % (axis_index, content._map_(axis)))
return
xboxController = XboxController()
xboxController.isQuit = False
# 注册方法
xboxController.Update = Update
xboxController.Start = Start
xboxController.Quit = Quit
xboxController.KeyDown = KeyDown
xboxController.KeyUp = KeyUp
xboxController.Hat = Hat
xboxController.Axis = Axis
# 启动线程
xboxController.start()
|