键盘按键从 pyautogui 迁移到 pydirectinput
This commit is contained in:
@@ -5,7 +5,7 @@ import math
|
||||
import random
|
||||
import time
|
||||
import logging
|
||||
import pyautogui
|
||||
import pydirectinput
|
||||
from stuck_handler import StuckHandler
|
||||
|
||||
# 转向与死区常量(度)
|
||||
@@ -73,9 +73,9 @@ class CoordinatePatrol:
|
||||
self.logger.info(
|
||||
f">>> 未上马,先按 {self.mount_key} {self.mount_hold_sec:.1f}s 上马"
|
||||
)
|
||||
pyautogui.keyDown(self.mount_key)
|
||||
pydirectinput.keyDown(self.mount_key)
|
||||
time.sleep(self.mount_hold_sec)
|
||||
pyautogui.keyUp(self.mount_key)
|
||||
pydirectinput.keyUp(self.mount_key)
|
||||
self._next_mount_allowed = time.time() + self.mount_retry_after_sec
|
||||
return False
|
||||
|
||||
@@ -235,16 +235,16 @@ class CoordinatePatrol:
|
||||
# --- [平滑移动核心:边走边转控制层] ---
|
||||
|
||||
# A. 始终保持前进动力
|
||||
pyautogui.keyDown("w")
|
||||
pydirectinput.keyDown("w")
|
||||
|
||||
# B. 转向决策逻辑
|
||||
if abs_diff <= self.angle_deadzone_deg:
|
||||
if random.random() < self.random_jump_prob: # 频率建议设低,约每 200 帧跳一次
|
||||
self.logger.info(">>> 随机跳跃:模拟真人并辅助脱困")
|
||||
pyautogui.press("space")
|
||||
pydirectinput.press("space")
|
||||
# 在死区内:绝对直线行驶,松开所有转向键
|
||||
pyautogui.keyUp("a")
|
||||
pyautogui.keyUp("d")
|
||||
pydirectinput.keyUp("a")
|
||||
pydirectinput.keyUp("d")
|
||||
|
||||
elif abs_diff > self.angle_threshold_deg:
|
||||
# 超出阈值:执行平滑脉冲转向
|
||||
@@ -252,24 +252,24 @@ class CoordinatePatrol:
|
||||
other_key = "a" if angle_diff > 0 else "d"
|
||||
|
||||
# 确保不会同时按下左右键
|
||||
pyautogui.keyUp(other_key)
|
||||
pydirectinput.keyUp(other_key)
|
||||
|
||||
# 获取针对平滑移动优化的短脉冲时长
|
||||
duration = self._turn_duration_from_angle(abs_diff)
|
||||
|
||||
# 关键:执行短促转向脉冲
|
||||
pyautogui.keyDown(turn_key)
|
||||
pydirectinput.keyDown(turn_key)
|
||||
# 这里的 sleep 极短 (建议 < 0.1s),不会造成移动卡顿
|
||||
time.sleep(duration)
|
||||
pyautogui.keyUp(turn_key)
|
||||
pydirectinput.keyUp(turn_key)
|
||||
|
||||
self.last_turn_end_time = time.time()
|
||||
# self.logger.debug(f"修正航向: {turn_key} | 角度差: {angle_diff:.1f}°")
|
||||
|
||||
else:
|
||||
# 在死区与阈值之间:为了平滑,不进行任何按键动作,仅靠 W 前进
|
||||
pyautogui.keyUp("a")
|
||||
pyautogui.keyUp("d")
|
||||
pydirectinput.keyUp("a")
|
||||
pydirectinput.keyUp("d")
|
||||
|
||||
return
|
||||
|
||||
@@ -306,8 +306,8 @@ class CoordinatePatrol:
|
||||
if dist < threshold:
|
||||
# 到点后保持平滑过渡:不松开 W,只松开左右修正键
|
||||
# 避免 patrol 连续多点时出现“刹一下再走”的抖动。
|
||||
pyautogui.keyUp("a")
|
||||
pyautogui.keyUp("d")
|
||||
pydirectinput.keyUp("a")
|
||||
pydirectinput.keyUp("d")
|
||||
self.reset_stuck()
|
||||
return True
|
||||
|
||||
@@ -319,37 +319,37 @@ class CoordinatePatrol:
|
||||
# --- 平滑移动核心逻辑 ---
|
||||
|
||||
# 只要没到终点,始终保持前进 W 键按下
|
||||
pyautogui.keyDown("w")
|
||||
pydirectinput.keyDown("w")
|
||||
|
||||
# 4. 根据偏角决定转向动作
|
||||
if abs_diff <= self.angle_deadzone_deg:
|
||||
if random.random() < self.random_jump_prob: # 频率建议设低,约每 200 帧跳一次
|
||||
self.logger.info(">>> 随机跳跃:模拟真人并辅助脱困")
|
||||
pyautogui.press("space")
|
||||
pydirectinput.press("space")
|
||||
# 在死区内,确保转向键松开,直线前进
|
||||
pyautogui.keyUp("a")
|
||||
pyautogui.keyUp("d")
|
||||
pydirectinput.keyUp("a")
|
||||
pydirectinput.keyUp("d")
|
||||
elif abs_diff > self.angle_threshold_deg:
|
||||
# 超出阈值:执行平滑脉冲转向
|
||||
turn_key = "d" if angle_diff > 0 else "a"
|
||||
other_key = "a" if angle_diff > 0 else "d"
|
||||
|
||||
# 松开反方向键
|
||||
pyautogui.keyUp(other_key)
|
||||
pydirectinput.keyUp(other_key)
|
||||
|
||||
# 获取针对平滑移动优化的短脉冲时长
|
||||
duration = self._turn_duration_from_angle(abs_diff)
|
||||
# 关键:执行短促转向脉冲
|
||||
pyautogui.keyDown(turn_key)
|
||||
pydirectinput.keyDown(turn_key)
|
||||
# sleep 极短 (建议 < 0.1s),不会造成移动卡顿
|
||||
time.sleep(duration)
|
||||
pyautogui.keyUp(turn_key)
|
||||
pydirectinput.keyUp(turn_key)
|
||||
|
||||
self.last_turn_end_time = time.time()
|
||||
else:
|
||||
# 在死区与阈值之间:为了平滑,不进行任何按键动作,仅靠 W 前进
|
||||
pyautogui.keyUp("a")
|
||||
pyautogui.keyUp("d")
|
||||
pydirectinput.keyUp("a")
|
||||
pydirectinput.keyUp("d")
|
||||
|
||||
return False
|
||||
|
||||
@@ -405,9 +405,9 @@ class CoordinatePatrol:
|
||||
ok = self._ensure_mounted(state)
|
||||
if not ok:
|
||||
return False
|
||||
pyautogui.keyDown(str(takeoff_key))
|
||||
pydirectinput.keyDown(str(takeoff_key))
|
||||
time.sleep(float(takeoff_hold_sec))
|
||||
pyautogui.keyUp(str(takeoff_key))
|
||||
pydirectinput.keyUp(str(takeoff_key))
|
||||
return False
|
||||
|
||||
# 计算距离
|
||||
@@ -419,23 +419,23 @@ class CoordinatePatrol:
|
||||
if f_signal < 10:
|
||||
# 退化:直接按 mount_key 一次并等待
|
||||
self.logger.info(">>> 飞行阶段检测:未上马,先按 %s", self.mount_key)
|
||||
pyautogui.press(self.mount_key)
|
||||
pydirectinput.press(self.mount_key)
|
||||
time.sleep(float(mount_wait_sec))
|
||||
return False
|
||||
|
||||
# --- 2. 起飞阶段(兼容你的阈值写法:100~200) ---
|
||||
if 100 < f_signal < 200:
|
||||
self.logger.info(">>> 起飞:按住 %s %.1f 秒", takeoff_key, float(takeoff_hold_sec))
|
||||
pyautogui.keyDown(str(takeoff_key))
|
||||
pydirectinput.keyDown(str(takeoff_key))
|
||||
time.sleep(float(takeoff_hold_sec))
|
||||
pyautogui.keyUp(str(takeoff_key))
|
||||
pydirectinput.keyUp(str(takeoff_key))
|
||||
return False
|
||||
|
||||
# --- 3. 巡航与降落判定(兼容你的阈值写法:>240) ---
|
||||
if f_signal > 240:
|
||||
if dist > float(arrival_threshold):
|
||||
# A. 始终按住 W 前进
|
||||
pyautogui.keyDown("w")
|
||||
pydirectinput.keyDown("w")
|
||||
|
||||
# B. 修正方向(使用与本模块一致的朝向计算)
|
||||
target_heading = self.get_target_heading_deg((float(curr_x), float(curr_y)), (float(target_x), float(target_y)))
|
||||
@@ -444,8 +444,8 @@ class CoordinatePatrol:
|
||||
|
||||
# 在死区内:确保转向键松开,减少左右抖动
|
||||
if abs_diff <= float(turn_deadzone_deg):
|
||||
pyautogui.keyUp("a")
|
||||
pyautogui.keyUp("d")
|
||||
pydirectinput.keyUp("a")
|
||||
pydirectinput.keyUp("d")
|
||||
return False
|
||||
|
||||
# 防止“上一个修正刚结束,下一帧立刻反向再修正”造成摆头
|
||||
@@ -456,8 +456,8 @@ class CoordinatePatrol:
|
||||
|
||||
key = "d" if angle_diff > 0 else "a"
|
||||
other_key = "a" if key == "d" else "d"
|
||||
pyautogui.keyUp(other_key)
|
||||
pyautogui.keyDown(key)
|
||||
pydirectinput.keyUp(other_key)
|
||||
pydirectinput.keyDown(key)
|
||||
|
||||
# 修正脉冲时长随角度误差变化,避免固定 0.05s 过冲/欠冲
|
||||
duration = self._turn_duration_from_angle(abs_diff)
|
||||
@@ -467,17 +467,17 @@ class CoordinatePatrol:
|
||||
safe_duration = min(safe_duration, 0.04)
|
||||
|
||||
time.sleep(safe_duration)
|
||||
pyautogui.keyUp(key)
|
||||
pydirectinput.keyUp(key)
|
||||
setattr(self, "_last_simple_flight_turn_time", now)
|
||||
return False
|
||||
|
||||
# C. 到达目标,执行降落
|
||||
pyautogui.keyUp("w")
|
||||
pydirectinput.keyUp("w")
|
||||
self.logger.info(">>> 到达目标,执行降落:按住 %s %.1f 秒", land_key, float(land_hold_sec))
|
||||
pyautogui.keyDown(str(land_key))
|
||||
pydirectinput.keyDown(str(land_key))
|
||||
time.sleep(float(land_hold_sec))
|
||||
pyautogui.keyUp(str(land_key))
|
||||
pyautogui.press(dismount_key)
|
||||
pydirectinput.keyUp(str(land_key))
|
||||
pydirectinput.press(dismount_key)
|
||||
self.logger.info(">>> 降落完成:已按 %s", dismount_key)
|
||||
return True
|
||||
|
||||
@@ -532,10 +532,10 @@ class CoordinatePatrol:
|
||||
"""只按下指定键,抬起其他移动键。"""
|
||||
for k in ("w", "a", "d"):
|
||||
if k == key:
|
||||
pyautogui.keyDown(k)
|
||||
pydirectinput.keyDown(k)
|
||||
else:
|
||||
pyautogui.keyUp(k)
|
||||
pydirectinput.keyUp(k)
|
||||
|
||||
def stop_all(self):
|
||||
for key in ["w", "a", "s", "d"]:
|
||||
pyautogui.keyUp(key)
|
||||
pydirectinput.keyUp(key)
|
||||
|
||||
Reference in New Issue
Block a user