Skip to main content
 首页 » 编程设计

python之Xbox 360 振动隆隆声

2024年11月01日9cmt

我设法找到了一个启动 xbox 360 Controller 以发出隆隆声(振动)的脚本,但我无法将其关闭。有没有办法将其设置为 5 秒后隆隆声停止?

import ctypes 
 
# Define necessary structures 
 class XINPUT_VIBRATION(ctypes.Structure): 
    _fields_ = [("wLeftMotorSpeed", ctypes.c_ushort), 
                ("wRightMotorSpeed", ctypes.c_ushort)] 
 
xinput = ctypes.windll.xinput1_1  # Load Xinput.dll 
 
# Set up function argument types and return type 
XInputSetState = xinput.XInputSetState 
XInputSetState.argtypes = [ctypes.c_uint, ctypes.POINTER(XINPUT_VIBRATION)] 
XInputSetState.restype = ctypes.c_uint 
 
# Now we're ready to call it.  Set left motor to 100%, right motor to 50% 
# for controller 0 
vibration = XINPUT_VIBRATION(65535, 32768) 
XInputSetState(0, ctypes.byref(vibration)) 
 
# You can also create a helper function like this: 
def set_vibration(controller, left_motor, right_motor): 
    vibration = XINPUT_VIBRATION(int(left_motor * 65535), int(right_motor * 65535)) 
    XInputSetState(controller, ctypes.byref(vibration)) 
 
# ... and use it like so 
set_vibration(0, 0.5, 0.5,) 

谢谢

请您参考如下方法:

看起来您已经通过此脚本获得了所需的一切。 set_vibration 辅助函数接受 3 个输入参数:

  • Controller ID(在您的情况下为 0)
  • 左电机振动从 0(关闭)到 1.0(完全打开)缩放 - 您已使用 0.5 将其设置为 50%
  • 正确的电机振动(也为 0 - 1.0)

所以要将它设置为仅以 50% 的功率振动 5 秒钟,请尝试以下方法:

import time 
set_vibration(0, 0.5, 0.5) 
time.sleep(5) 
set_vibration(0, 0, 0) 

授予这一切只是通过检查你的脚本,并没有经过测试