import pygame
# 先初始化
pygame.init()
pygame.joystick.init()
# 获取手柄数量
pygame.joystick.get_count()
# 获取第一个手柄,并初始化
joystick = pygame.joystick.Joystick(0)
joystick.init()
# 退出
pygame.quit()
joystick
# 初始化
joystick = pygame.joystick.Joystick(0)
joystick.init()
# 手柄名称
joystick.get_name()
# 手柄按钮数
joystick.get_numbuttons()
# 手柄轴数
joystick.get_numaxes()
rumble
手柄震动效果设置,如果成功,返回 True。
语法:
joystick.rumble(<低频>, <高频>, <持续事件>)
for event in pygame.event.get():
if event.type == pygame.JOYBUTTONDOWN:
print("Joystick button pressed.")
if event.button == 0:
joystick = joysticks[event.instance_id]
if joystick.rumble(0, 0.7, 500):
print(f"Rumble effect played on joystick {event.instance_id}")
evenv
手柄事件。
pygame.event.wait()
pygame.event.get() # 返回 list
for event in pygame.event.get():
# 获取按钮名称编号
if event.type == pygame.JOYBUTTONDOWN:
print(f"按下按钮{event.button}") # 输出按钮编号
elif event.type == pygame.JOYBUTTONUP:
print(f"松开按钮:{event.button}")
# 获取摇杆
elif event.type == pygame.JOYAXISMOTION:
print(event.axis) # 输出摇杆值
硬件映射
XBox
摇杆
- Left Stick:左摇杆
- 0: Up -> Down
- 1: Left -> Right
- Right Stick:右摇杆
- 2: Up -> Down
- 3: Left -> Right
扳机
- Left Tigger:左扳机键
- 4: Out -> On
- Right Tigger:右扳机键
- 5: Out -> On
按钮
- 0: A
- 1: B
- 2: X
- 3: Y
- 4: View button (中间左)
- 5: Xbox button (西瓜键)
- 6: Menu button (中间右)
- 7: L. Stick In (左摇杆按下)
- 8: R. Stick In (右摇杆按下)
- 9: Left Bumper (LB 左缓冲键)
- 10: Right Bumper (RB 右缓冲键)
- 11: 上 (方向键)
- 12: 下 (方向键)
- 13: 左 (方向键)
- 14: 右 (方向键)
- 15: 共享按钮 (中间按钮)