Some checks failed
Quality / Style (push) Has been cancelled
Quality / Typos (push) Has been cancelled
Tests / Pytest (push) Has been cancelled
Tests / Pytest (minimal install) (push) Has been cancelled
Tests / End-to-end (push) Has been cancelled
Secret Leaks / trufflehog (push) Has been cancelled
Builds / CPU (push) Has been cancelled
Builds / GPU (push) Has been cancelled
Builds / GPU Dev (push) Has been cancelled
Nightly / CPU (push) Has been cancelled
Nightly / GPU (push) Has been cancelled
147 lines
5.4 KiB
Python
147 lines
5.4 KiB
Python
import pygame
|
||
import time
|
||
|
||
pygame.init()
|
||
pygame.joystick.init()
|
||
|
||
if pygame.joystick.get_count() == 0:
|
||
print("没有检测到手柄,请插入手柄后重试。")
|
||
exit()
|
||
|
||
joystick = pygame.joystick.Joystick(0)
|
||
joystick.init()
|
||
print(f"检测到手柄: {joystick.get_name()}\n")
|
||
|
||
# 手柄按键和功能列表
|
||
button_names = [
|
||
("A 键", "button"),
|
||
("B 键", "button"),
|
||
("X 键", "button"),
|
||
("Y 键", "button"),
|
||
("左摇杆(按下)", "button"),
|
||
("右摇杆(按下)", "button"),
|
||
("LB(左肩键)", "button"),
|
||
("RB(右肩键)", "button"),
|
||
("BACK/SELECT", "button"),
|
||
("START", "button"),
|
||
("方向键 ↑", "hat_up"),
|
||
("方向键 ↓", "hat_down"),
|
||
("方向键 ←", "hat_left"),
|
||
("方向键 →", "hat_right"),
|
||
("左摇杆(上推)", "axis", 1, -1),
|
||
("左摇杆(下推)", "axis", 1, 1),
|
||
("左摇杆(左推)", "axis", 0, -1),
|
||
("左摇杆(右推)", "axis", 0, 1),
|
||
("右摇杆(上推)", "axis_guess", "up"),
|
||
("右摇杆(下推)", "axis_guess", "down"),
|
||
("右摇杆(左推)", "axis_guess", "left"),
|
||
("右摇杆(右推)", "axis_guess", "right"),
|
||
]
|
||
|
||
results = {}
|
||
|
||
print("请根据提示依次操作,操作后立即松开。\n")
|
||
|
||
for item in button_names:
|
||
name = item[0]
|
||
print(f"请操作:{name},操作后松开,等待检测...")
|
||
|
||
detected = False
|
||
start_time = time.time()
|
||
|
||
while not detected:
|
||
pygame.event.pump()
|
||
|
||
# 检查按钮
|
||
if item[1] == "button":
|
||
for i in range(joystick.get_numbuttons()):
|
||
if joystick.get_button(i):
|
||
print(f"检测到:{name} -> 按钮编号: {i}")
|
||
results[name] = f"按钮 {i}"
|
||
detected = True
|
||
while joystick.get_button(i): # 等待松开
|
||
pygame.event.pump()
|
||
break
|
||
|
||
# 检查方向键
|
||
elif item[1].startswith("hat"):
|
||
for i in range(joystick.get_numhats()):
|
||
hat = joystick.get_hat(i)
|
||
if item[1] == "hat_up" and hat[1] == 1:
|
||
print(f"检测到:{name} -> hat编号: {i}, 数值: {hat}")
|
||
results[name] = f"hat {i}, {hat}"
|
||
detected = True
|
||
elif item[1] == "hat_down" and hat[1] == -1:
|
||
print(f"检测到:{name} -> hat编号: {i}, 数值: {hat}")
|
||
results[name] = f"hat {i}, {hat}"
|
||
detected = True
|
||
elif item[1] == "hat_left" and hat[0] == -1:
|
||
print(f"检测到:{name} -> hat编号: {i}, 数值: {hat}")
|
||
results[name] = f"hat {i}, {hat}"
|
||
detected = True
|
||
elif item[1] == "hat_right" and hat[0] == 1:
|
||
print(f"检测到:{name} -> hat编号: {i}, 数值: {hat}")
|
||
results[name] = f"hat {i}, {hat}"
|
||
detected = True
|
||
if detected:
|
||
while joystick.get_hat(i) == hat:
|
||
pygame.event.pump()
|
||
break
|
||
|
||
# 检查左摇杆
|
||
elif item[1] == "axis":
|
||
axis_id = item[2]
|
||
direction = item[3]
|
||
val = joystick.get_axis(axis_id)
|
||
if direction == -1 and val < -0.7:
|
||
print(f"检测到:{name} -> 轴编号: {axis_id}, 数值: {val:.2f}")
|
||
results[name] = f"axis {axis_id}, {val:.2f}"
|
||
detected = True
|
||
elif direction == 1 and val > 0.7:
|
||
print(f"检测到:{name} -> 轴编号: {axis_id}, 数值: {val:.2f}")
|
||
results[name] = f"axis {axis_id}, {val:.2f}"
|
||
detected = True
|
||
if detected:
|
||
# 不强制等待回中,立即进入下一步
|
||
break
|
||
|
||
# 猜测右摇杆的轴编号
|
||
elif item[1] == "axis_guess":
|
||
print("当前所有轴数值如下(请推动右摇杆到指定方向):")
|
||
for axis_id in range(joystick.get_numaxes()):
|
||
print(f" axis {axis_id}: {joystick.get_axis(axis_id):.2f}", end="; ")
|
||
print()
|
||
# 下面等待你推动,检测哪个轴发生较大变化
|
||
# 只检测一次
|
||
max_diff = 0
|
||
target_axis = None
|
||
target_val = None
|
||
base = [joystick.get_axis(i) for i in range(joystick.get_numaxes())]
|
||
time.sleep(0.1)
|
||
for _ in range(15):
|
||
pygame.event.pump()
|
||
for axis_id in range(joystick.get_numaxes()):
|
||
now = joystick.get_axis(axis_id)
|
||
diff = abs(now - base[axis_id])
|
||
if diff > 0.6 and diff > max_diff:
|
||
max_diff = diff
|
||
target_axis = axis_id
|
||
target_val = now
|
||
time.sleep(0.02)
|
||
if target_axis is not None:
|
||
print(f"检测到:{name} -> 轴编号: {target_axis}, 数值: {target_val:.2f}")
|
||
results[name] = f"axis {target_axis}, {target_val:.2f}"
|
||
detected = True
|
||
else:
|
||
print("未检测到明显变化,请重试。")
|
||
time.sleep(0.5)
|
||
continue
|
||
|
||
time.sleep(0.25)
|
||
|
||
print("\n=== 检测结果(请复制以下内容发给我) ===")
|
||
for k, v in results.items():
|
||
print(f"{k}: {v}")
|
||
|
||
print("全部完成!")
|