pyautogui撰寫自動化腳本自動打怪

2023/09/30閱讀時間約 10 分鐘

如果對此有興趣的朋友可以跟我一起討論研究^^

工業革命以來自動化一直是人類很重視的一個領域

尤其繁忙的現代人

那種可以寫成程序的工作就交給自動化吧

那我這個遊戲是

https://nfl.lager.com.tw/event/nfl_ob/index.html

童話online

簡單來說就是一個old school的回合制戰鬥遊戲

裡面有一個角色叫做"闇術師"

有一個技能可以在非戰鬥狀態時直接招喚怪物出來

進入戰鬥狀態

進入戰鬥狀態之後一般來說我會選擇"黑洞爆發"

這個全體技能把怪物全部殺掉

所以整個邏輯大概如下

1.操作滑鼠點擊招怪技能圖案

2.進入戰鬥狀態後操作滑鼠點擊黑洞爆發圖案再點擊場上怪物

3.重複步驟2直到退出戰鬥狀態

ok邏輯釐清了再來就是程式碼

import pyautogui
from PIL import Image, ImageChops
import time
from PIL import Image

duration_n = 0.8

def 招怪():
while True:
print('正在招怪程序')
# 获取屏幕截图
screenshot = pyautogui.screenshot()

# 在屏幕截图中查找图案位置
img = Image.open('招怪.PNG')
location = pyautogui.locateOnScreen(img)

# 如果找到了图案
if location is not None:
# 计算图案中心点坐标
center_x, center_y = pyautogui.center(location)

# 点击图案中心点
pyautogui.click(center_x, center_y,duration=duration_n)

# 等待一段时间,以便观察周围像素的变化(可以根据需要调整等待时间)
time.sleep(1)

# 获取新的屏幕截图
new_screenshot = pyautogui.screenshot()

# 比较两个截图的不同之处
diff = ImageChops.difference(screenshot, new_screenshot)

# 如果有明显的不同(像素不同)
if diff.getbbox():
print("跳出招怪程序")
break

上面註解很清楚

程序會根據招怪.PNG這圖案在螢幕上搜索

搜索到了之後會計算图案中心点坐标

然後点击图案中心点

後面26行以後主要是判斷是否進入戰鬥狀態

我的作法是

# 等待一段时间,以便观察周围像素的变化(可以根据需要调整等待时间)

之後比對新的螢幕截圖跟舊的螢幕截圖是否有差異

# 如果有明显的不同(像素不同)
if diff.getbbox():
print("跳出招怪程序")
break

再來定義戰鬥函數

先宣告三種怪物的圖片

和攻擊技能黑洞爆發的圖片

避免干擾所以圖像匹配都使用模糊比對

接著進入while 迴圈

持續搜索三種怪物的圖像並找到位置

然後先點擊攻擊技能再點擊該怪物位置

這過程反覆循環直到螢幕上不再出現怪物

def 戰鬥():

# 定义怪物图像文件的路径
monster_A_path = Image.open('怪物A.PNG')
monster_B_path = Image.open('怪物B.PNG')
monster_C_path = Image.open('怪物C.PNG')
黑洞爆發_path = Image.open('黑洞爆發.PNG')

# 定義模糊比對參數
confidence_threshold = 0.8

# 循环直到屏幕上不再出现(A, B, C)怪物图像
while True:

# 搜索怪物A图像
monster_a_location = pyautogui.locateOnScreen(monster_A_path,confidence=confidence_threshold)
# 搜索怪物B图像
monster_b_location = pyautogui.locateOnScreen(monster_B_path,confidence=confidence_threshold)
# 搜索怪物C图像
monster_c_location = pyautogui.locateOnScreen(monster_C_path,confidence=confidence_threshold)

# 如果找到怪物A图像
if monster_a_location is not None:
# 点击D图像
d_location = pyautogui.locateOnScreen(黑洞爆發_path,confidence=confidence_threshold)
if d_location is not None:
d_x, d_y = pyautogui.center(d_location)
pyautogui.click(d_x, d_y,duration=duration_n)
time.sleep(0.5) # 等待一段时间,避免连续点击造成问题
# 点击怪物A图像
a_x, a_y = pyautogui.center(monster_a_location)
pyautogui.click(a_x, a_y,duration=duration_n)
time.sleep(0.5) # 等待一段时间,避免连续点击造成问题

# 如果找到怪物B图像
elif monster_b_location is not None:
# 点击D图像
d_x, d_y = pyautogui.center(pyautogui.locateOnScreen(黑洞爆發_path,confidence=confidence_threshold))
pyautogui.click(d_x, d_y,duration=duration_n)
time.sleep(0.5) # 等待一段时间,避免连续点击造成问题
# 点击怪物B图像
b_x, b_y = pyautogui.center(monster_b_location)
pyautogui.click(b_x, b_y,duration=duration_n)
time.sleep(0.5) # 等待一段时间,避免连续点击造成问题

# 如果找到怪物C图像
elif monster_c_location is not None:
# 点击D图像
d_x, d_y = pyautogui.center(pyautogui.locateOnScreen(黑洞爆發_path,confidence=confidence_threshold))
pyautogui.click(d_x, d_y,duration=duration_n)
time.sleep(0.5) # 等待一段时间,避免连续点击造成问题
# 点击怪物C图像
c_x, c_y = pyautogui.center(monster_c_location)
pyautogui.click(c_x, c_y,duration=duration_n)
time.sleep(0.5) # 等待一段时间,避免连续点击造成问题

else:
# 如果屏幕上不再出现(A, B, C)怪物图像,退出循环
break

後續發現沒辦法正常練功

主要問題出現於滑鼠點擊跟鍵盤操作的時候沒有反應

後來跟朋友討論發現

06:30 rickyyu press都改成 keydown +keyup
06:30 rickyyu click說錯
06:30 rickyyu pyautogui 的click 功能在我那個遊戲根本不適用
06:31 rickyyu https://github.com/skywalker0803r/autohitmonster/blob/main/play2.py
06:32 rickyyu 說錯鍵盤都改用keydown keyup 
06:32 rickyyu 滑鼠改用mousedown mouseup

以下是完整的可執行無bug的新程式碼:

https://github.com/skywalker0803r/autohitmonster/blob/main/play2.py


人工智能工作經驗跟研究
留言0
查看全部
發表第一個留言支持創作者!