first commit

This commit is contained in:
王鹏
2026-03-18 09:04:37 +08:00
commit b7719b377d
121 changed files with 116104 additions and 0 deletions

70
recorder.py Normal file
View File

@@ -0,0 +1,70 @@
import json
import math
import os
import time
from game_state import parse_game_state
# 默认配置文件名
WAYPOINTS_FILE = 'waypoints.json'
# 记录两个点之间的最小间距阈值(游戏坐标单位)
DEFAULT_MIN_DISTANCE = 0.5
class WaypointRecorder:
def __init__(self, min_distance=DEFAULT_MIN_DISTANCE, waypoints_file=None, on_point_added=None):
self.waypoints = []
self.min_distance = min_distance
self.last_pos = (0, 0)
self.waypoints_file = waypoints_file or WAYPOINTS_FILE
self.on_point_added = on_point_added # 可选回调: (pos, count) => None
def get_distance(self, p1, p2):
return math.sqrt((p1[0]-p2[0])**2 + (p1[1]-p2[1])**2)
def record(self, state):
"""state 来自 game_state.parse_game_state(),坐标字段为 x, y。"""
if state is None:
return
x, y = state.get('x'), state.get('y')
if x is None or y is None:
return
curr_pos = (float(x), float(y))
# 初始点记录
if self.last_pos == (0, 0):
self.add_point(curr_pos)
return
# 只有当移动距离超过阈值时才记录点
dist = self.get_distance(curr_pos, self.last_pos)
if dist >= self.min_distance:
self.add_point(curr_pos)
def add_point(self, pos):
self.waypoints.append(pos)
self.last_pos = pos
if self.on_point_added:
self.on_point_added(pos, len(self.waypoints))
else:
print(f"已记录点: {pos} | 当前总点数: {len(self.waypoints)}")
def save(self, path=None):
out_path = path or self.waypoints_file
with open(out_path, 'w', encoding='utf-8') as f:
json.dump(self.waypoints, f, indent=4, ensure_ascii=False)
return out_path
# 使用示例(整合进你的主循环)
if __name__ == "__main__":
recorder = WaypointRecorder()
print("开始录制模式:请在游戏内跑动。按 Ctrl+C 停止并保存。")
try:
while True:
state = parse_game_state()
if state:
recorder.record(state)
time.sleep(0.5)
except KeyboardInterrupt:
path = recorder.save()
print(f"路径已保存至 {path}")