| |
| from inference.game_controller import GameController |
| from inference.action_mapper import ActionMapper |
| import time |
| import os |
|
|
| def test_all_actions(): |
| """测试所有动作映射""" |
| ctrl = GameController() |
| mapper = ActionMapper(ctrl) |
| |
| print("=" * 50) |
| print("王者荣耀AI控制测试 - 全面测试") |
| print("=" * 50) |
| |
| |
| print("\n[1/4] 测试移动控制...") |
| moves = [ |
| "move_up", "move_down", "move_left", "move_right", |
| "move_upleft", "move_upright", "move_downleft", "move_downright" |
| ] |
| |
| for move in moves: |
| print(f" 执行: {move}") |
| mapper.execute(move) |
| time.sleep(0.5) |
| |
| |
| print("\n[2/4] 测试战斗技能...") |
| combat_actions = ["attack", "skill_damage", "skill_control"] |
| |
| for action in combat_actions: |
| print(f" 执行: {action}") |
| mapper.execute(action) |
| time.sleep(0.8) |
| |
| |
| print("\n[3/4] 测试战术动作...") |
| tactical_actions = ["recall", "heal", "summoner", "enhance", "upgrade"] |
| |
| for action in tactical_actions: |
| print(f" 执行: {action}") |
| mapper.execute(action) |
| time.sleep(0.8) |
| |
| |
| print("\n[4/4] 测试截图功能...") |
| try: |
| img = ctrl.screenshot() |
| timestamp = time.strftime("%Y%m%d_%H%M%S") |
| filename = f"screenshot_{timestamp}.png" |
| img.save(filename) |
| print(f" 截图已保存: {filename}") |
| |
| |
| w, h = ctrl.get_screen_size() |
| print(f" 屏幕尺寸: {w}x{h}") |
| |
| |
| print(f" 图片尺寸: {img.size}") |
| print(f" 图片模式: {img.mode}") |
| |
| except Exception as e: |
| print(f" 截图失败: {e}") |
| |
| print("\n" + "=" * 50) |
| print("测试完成!") |
| print("=" * 50) |
|
|
| def test_single_action(): |
| """交互式测试单个动作""" |
| ctrl = GameController() |
| mapper = ActionMapper(ctrl) |
| |
| print("\n=== 交互式测试模式 ===") |
| print("可用动作:") |
| print(" 移动: up, down, left, right, upleft, upright, downleft, downright") |
| print(" 战斗: attack, damage, control") |
| print(" 战术: recall, heal, summoner, enhance, upgrade") |
| print(" 其他: screenshot, quit") |
| print("-" * 40) |
| |
| while True: |
| cmd = input("\n请输入动作: ").strip().lower() |
| |
| if cmd == 'quit': |
| print("退出测试") |
| break |
| elif cmd == 'screenshot': |
| try: |
| img = ctrl.screenshot() |
| filename = "manual_screenshot.png" |
| img.save(filename) |
| print(f"截图已保存: {filename}") |
| except Exception as e: |
| print(f"截图失败: {e}") |
| elif cmd in ['up', 'down', 'left', 'right', 'upleft', 'upright', 'downleft', 'downright']: |
| action = f"move_{cmd}" |
| print(f"执行: {action}") |
| mapper.execute(action) |
| elif cmd in ['attack', 'damage', 'control']: |
| if cmd == 'damage': |
| action = 'skill_damage' |
| elif cmd == 'control': |
| action = 'skill_control' |
| else: |
| action = cmd |
| print(f"执行: {action}") |
| mapper.execute(action) |
| elif cmd in ['recall', 'heal', 'summoner', 'enhance', 'upgrade']: |
| print(f"执行: {cmd}") |
| mapper.execute(cmd) |
| else: |
| print(f"未知动作: {cmd}") |
| |
| time.sleep(0.3) |
|
|
| def test_with_delay(): |
| """带延迟的循环测试(用于观察)""" |
| ctrl = GameController() |
| mapper = ActionMapper(ctrl) |
| |
| print("\n=== 自动循环测试模式 ===") |
| print("将依次执行所有动作,每个动作间隔1秒") |
| print("按 Ctrl+C 停止\n") |
| |
| actions = [ |
| |
| "move_up", "move_down", "move_left", "move_right", |
| "move_upleft", "move_upright", "move_downleft", "move_downright", |
| |
| "attack", "skill_damage", "skill_control", |
| |
| "recall", "heal", "summoner", "enhance", "upgrade" |
| ] |
| |
| try: |
| for i, action in enumerate(actions, 1): |
| print(f"[{i}/{len(actions)}] {action}") |
| mapper.execute(action) |
| time.sleep(1) |
| |
| print("\n循环测试完成") |
| except KeyboardInterrupt: |
| print("\n用户中断测试") |
|
|
| if __name__ == "__main__": |
| print("选择测试模式:") |
| print("1. 全面测试(一次执行所有动作)") |
| print("2. 交互式测试(手动输入动作)") |
| print("3. 循环测试(自动循环,可观察)") |
| |
| choice = input("\n请选择 (1/2/3): ").strip() |
| |
| if choice == '1': |
| test_all_actions() |
| elif choice == '2': |
| test_single_action() |
| elif choice == '3': |
| test_with_delay() |
| else: |
| print("无效选择,运行全面测试") |
| test_all_actions() |