-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauto_play.py
More file actions
155 lines (132 loc) · 4.87 KB
/
Copy pathauto_play.py
File metadata and controls
155 lines (132 loc) · 4.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
"""
2048自動遊玩主程式
整合圖像識別、遊戲邏輯和自動控制
"""
import time
from image_recognition import recognize_grid, visualize_grid
from game_logic import get_best_move, is_game_over
from capture_window import capture_window
from setup_manual_positions import MANUAL_POSITIONS
# 嘗試使用 pyautogui,如果失敗則使用 win32api
try:
from auto_control_pyautogui import send_key, wait_for_animation
print("使用 pyautogui 進行按鍵控制")
except ImportError:
from auto_control import send_key, wait_for_animation
print("使用 win32api 進行按鍵控制(如果無效,請安裝 pyautogui: pip install pyautogui)")
# 遊戲視窗標題
GAME_WINDOW_TITLE = "Doomsday: Last Survivors"
# 網格位置(需要根據實際遊戲畫面調整)
# 這些參數可以通過截圖分析來確定
GRID_X = None # 自動檢測
GRID_Y = None # 自動檢測
GRID_WIDTH = None # 自動檢測
GRID_HEIGHT = None # 自動檢測
def auto_play_once():
"""
執行一次自動遊玩循環:
1. 截圖
2. 識別網格
3. 計算最佳移動
4. 執行移動
"""
print("\n" + "="*50)
print("開始自動遊玩循環...")
# 步驟1: 截圖
print("步驟1: 截取遊戲畫面...")
if not capture_window(GAME_WINDOW_TITLE, "current_game.png"):
print("錯誤:無法截取遊戲畫面")
return False
# 步驟2: 識別網格
print("步驟2: 識別網格狀態...")
try:
# 使用手動位置進行識別
grid = recognize_grid("current_game.png", manual_positions=MANUAL_POSITIONS, debug=False)
visualize_grid(grid)
except Exception as e:
print(f"錯誤:識別網格失敗 - {e}")
return False
# 步驟3: 檢查遊戲是否結束
if is_game_over(grid):
print("遊戲結束!無法繼續移動。")
return False
# 步驟4: 計算最佳移動
print("步驟3: 計算最佳移動...")
best_move, score = get_best_move(grid)
if best_move is None:
print("錯誤:無法找到有效移動")
return False
print(f"最佳移動方向:{best_move},評分:{score}")
# 步驟5: 執行移動
print(f"步驟4: 執行移動 ({best_move})...")
if send_key(best_move):
wait_for_animation(0.5) # 等待動畫完成
return True
else:
print("錯誤:無法發送按鍵")
return False
def auto_play_loop(max_iterations=100, delay=1.0):
"""
自動遊玩主循環
max_iterations: 最大迭代次數
delay: 每次循環之間的延遲(秒)
"""
print("="*50)
print("2048自動遊玩腳本啟動")
print("="*50)
print(f"目標遊戲:{GAME_WINDOW_TITLE}")
print(f"最大迭代次數:{max_iterations}")
print(f"循環延遲:{delay}秒")
print("\n提示:按 Ctrl+C 停止")
print("="*50)
iteration = 0
consecutive_failures = 0
max_failures = 3
try:
while iteration < max_iterations:
iteration += 1
print(f"\n--- 第 {iteration} 次迭代 ---")
success = auto_play_once()
if success:
consecutive_failures = 0
else:
consecutive_failures += 1
if consecutive_failures >= max_failures:
print(f"\n連續失敗 {max_failures} 次,停止自動遊玩")
break
# 等待一段時間再進行下一次
if iteration < max_iterations:
print(f"等待 {delay} 秒後繼續...")
time.sleep(delay)
except KeyboardInterrupt:
print("\n\n用戶中斷,停止自動遊玩")
except Exception as e:
print(f"\n\n發生錯誤:{e}")
print("\n" + "="*50)
print("自動遊玩結束")
print("="*50)
def calibrate_grid_position():
"""
校準網格位置(互動式)
讓用戶手動設定網格的位置和大小
"""
print("網格位置校準模式")
print("請確保遊戲視窗可見,然後截圖...")
if capture_window(GAME_WINDOW_TITLE, "calibration.png"):
print("\n截圖已保存為 calibration.png")
print("請查看截圖,並手動設定以下參數:")
print("- GRID_X: 網格左上角的X座標")
print("- GRID_Y: 網格左上角的Y座標")
print("- GRID_WIDTH: 網格的寬度")
print("- GRID_HEIGHT: 網格的高度")
print("\n這些參數可以在 auto_play.py 中修改")
if __name__ == "__main__":
import sys
if len(sys.argv) > 1 and sys.argv[1] == "calibrate":
calibrate_grid_position()
else:
# 開始自動遊玩
# 可以調整參數:
# - max_iterations: 最大遊玩次數
# - delay: 每次移動之間的延遲(秒)
auto_play_loop(max_iterations=100, delay=1.0)