键盘按键从 pyautogui 迁移到 pydirectinput
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
import math
|
||||
import time
|
||||
import logging
|
||||
import pyautogui
|
||||
import pydirectinput
|
||||
from player_position import PlayerPosition
|
||||
|
||||
# 游戏朝向约定:正北=0°,正西=90°,正南=180°,正东=270°(逆时针递增)
|
||||
@@ -81,9 +81,9 @@ class PlayerMovement:
|
||||
f"[转向 {attempt + 1}] 当前 {current_heading:.1f}° → 目标 {target_heading:.1f}°,"
|
||||
f"按 '{key}' {turn_duration:.2f}s(需转 {angle_to_turn:.1f}°)"
|
||||
)
|
||||
pyautogui.keyDown(key)
|
||||
pydirectinput.keyDown(key)
|
||||
time.sleep(turn_duration)
|
||||
pyautogui.keyUp(key)
|
||||
pydirectinput.keyUp(key)
|
||||
time.sleep(0.15) # 等待游戏刷新朝向
|
||||
|
||||
self.logger.warning(f"转向失败:超过最大尝试次数 {max_attempts}")
|
||||
@@ -96,9 +96,9 @@ class PlayerMovement:
|
||||
duration: 移动时间(秒)
|
||||
"""
|
||||
self.logger.info(f"向前移动 {duration:.2f}s")
|
||||
pyautogui.keyDown('w')
|
||||
pydirectinput.keyDown('w')
|
||||
time.sleep(duration)
|
||||
pyautogui.keyUp('w')
|
||||
pydirectinput.keyUp('w')
|
||||
|
||||
def _escape_stuck(self):
|
||||
"""卡死脱困组合拳:后退 + 随机转向 + 跳跃。
|
||||
@@ -109,23 +109,23 @@ class PlayerMovement:
|
||||
self.logger.warning("检测到角色卡死,执行脱困动作")
|
||||
|
||||
# 1. 松开前进键,后退 0.8s
|
||||
pyautogui.keyUp('w')
|
||||
pyautogui.keyDown('s')
|
||||
pydirectinput.keyUp('w')
|
||||
pydirectinput.keyDown('s')
|
||||
time.sleep(0.8)
|
||||
pyautogui.keyUp('s')
|
||||
pydirectinput.keyUp('s')
|
||||
|
||||
# 2. 随机左转或右转 0.3~0.5s
|
||||
turn_key = random.choice(['a', 'd'])
|
||||
turn_dur = random.uniform(0.3, 0.5)
|
||||
pyautogui.keyDown(turn_key)
|
||||
pydirectinput.keyDown(turn_key)
|
||||
time.sleep(turn_dur)
|
||||
pyautogui.keyUp(turn_key)
|
||||
pydirectinput.keyUp(turn_key)
|
||||
|
||||
# 3. 跳跃(按空格)同时向前冲 0.5s
|
||||
pyautogui.keyDown('w')
|
||||
pyautogui.press('space')
|
||||
pydirectinput.keyDown('w')
|
||||
pydirectinput.press('space')
|
||||
time.sleep(0.5)
|
||||
pyautogui.keyUp('w')
|
||||
pydirectinput.keyUp('w')
|
||||
|
||||
self.logger.info("脱困动作完成,重新开始前进")
|
||||
|
||||
@@ -172,7 +172,7 @@ class PlayerMovement:
|
||||
return False
|
||||
|
||||
# 按住 w 开始持续前进
|
||||
pyautogui.keyDown('w')
|
||||
pydirectinput.keyDown('w')
|
||||
# 卡死检测:记录最近 STUCK_CHECK_COUNT 次坐标,用于判断是否停滞
|
||||
recent_positions = []
|
||||
try:
|
||||
@@ -204,7 +204,7 @@ class PlayerMovement:
|
||||
if moved < self.STUCK_MOVE_THRESHOLD:
|
||||
self._escape_stuck()
|
||||
recent_positions.clear()
|
||||
pyautogui.keyDown('w')
|
||||
pydirectinput.keyDown('w')
|
||||
|
||||
# x 和 y 都在容差范围内即视为到达
|
||||
if abs(dx) <= position_tolerance and abs(dy) <= position_tolerance:
|
||||
@@ -213,7 +213,7 @@ class PlayerMovement:
|
||||
|
||||
# 接近目标时松开 w 停下,再做一次静止判定,避免冲过头后死循环
|
||||
if distance <= position_tolerance * 2:
|
||||
pyautogui.keyUp('w')
|
||||
pydirectinput.keyUp('w')
|
||||
time.sleep(0.2)
|
||||
final_pos = self.player_position.get_position_with_retry()
|
||||
if final_pos is not None:
|
||||
@@ -223,7 +223,7 @@ class PlayerMovement:
|
||||
self.logger.info(f"已到达目标位置 ({target_x}, {target_y})")
|
||||
return True
|
||||
# 静止后仍未到达,重新按住 w 继续前进
|
||||
pyautogui.keyDown('w')
|
||||
pydirectinput.keyDown('w')
|
||||
|
||||
# 每隔 HEADING_CHECK_INTERVAL 次读一次朝向,减少 OCR 频率
|
||||
if iteration % self.HEADING_CHECK_INTERVAL != 0:
|
||||
@@ -239,12 +239,12 @@ class PlayerMovement:
|
||||
|
||||
if abs_diff > self.COARSE_TURN_THRESHOLD:
|
||||
# 偏差过大:停步原地粗修正,再重新按住 w
|
||||
pyautogui.keyUp('w')
|
||||
pydirectinput.keyUp('w')
|
||||
self.logger.info(f"偏差 {abs_diff:.1f}° 过大,停步原地修正")
|
||||
if not self.turn_to_heading(required_heading):
|
||||
self.logger.warning("修正转向失败,移动中止")
|
||||
return False
|
||||
pyautogui.keyDown('w')
|
||||
pydirectinput.keyDown('w')
|
||||
|
||||
elif abs_diff > self.ANGLE_TOLERANCE:
|
||||
# 小幅偏差:边走边转,同时按住 w 和转向键
|
||||
@@ -254,15 +254,15 @@ class PlayerMovement:
|
||||
self.logger.info(
|
||||
f"边走边转:按 '{turn_key}' {turn_time:.2f}s(偏差 {abs_diff:.1f}°)"
|
||||
)
|
||||
pyautogui.keyDown(turn_key)
|
||||
pydirectinput.keyDown(turn_key)
|
||||
time.sleep(turn_time)
|
||||
pyautogui.keyUp(turn_key)
|
||||
pydirectinput.keyUp(turn_key)
|
||||
else:
|
||||
# 方向已对齐,让角色多走一段再重新检测,减少 OCR 频率
|
||||
time.sleep(1)
|
||||
|
||||
finally:
|
||||
pyautogui.keyUp('w')
|
||||
pydirectinput.keyUp('w')
|
||||
|
||||
self.logger.warning(f"移动失败:超过最大迭代次数 {max_iterations}")
|
||||
return False
|
||||
|
||||
Reference in New Issue
Block a user