生活随笔
收集整理的這篇文章主要介紹了
Minecraft 1.16 简易高效的自动钓鱼脚本
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
新更新
更新了在windows版本下可以后臺掛機的開源版本 !!!!
https://github.com/onlytheworld/openfish
后續更新將直接在github上更新
正文
MC 1.16版本更新改變了釣魚機制,增加了開闊水域判定,使得之前的所有釣魚機全部失效,而現有的基于時序的釣魚機也都效率低下,因此不得不使用掛機腳本來實現高效的自動釣魚。
基本原理
打開Minecraft 游戲菜單 – 選項 – 音樂和聲音 – 顯示字幕,即可出現游戲右下角的聲音字幕,當出現 “漂浮:濺起水花” 字樣時則說明魚已上鉤,已經可以收桿了。
因此可以簡單地設計識別游戲右下角的字幕,就可以達到自動釣魚的目的。
代碼實現
腳本代碼由 python3 書寫;利用 pyautogui 控制截圖與鼠標操作;使用傳統算法進行文本所在的區域檢測,參見這里;利用cnocr項目進行文本識別。
簡單的運行腳本并切換到游戲界面扔出浮漂即可掛機釣魚。
注意:
1、Minecraft 全屏模式下禁止截圖,因此腳本一定要在窗口模式下才可以使用。
2、需要先手動定位字幕所在的大致區域以提高精度。默認的定位為1920×1080屏幕的右下角。
import pyautogui
import time
from cnocr
import CnOcr
import cv2
import numpy
as nppyautogui
.PAUSE
= 1def findfish(res
):for line
in res
:if(line
== ['浮', '漂', ':', '濺', '起', '水', '花']):return Truereturn Falsedef fish():ocr
= CnOcr
()while(1):fig
= pyautogui
.screenshot
(region
=(1775, 700, 130, 300))img
= np
.asarray
(fig
)textImg
= detect
(img
)res
= ocr
.ocr
(textImg
)print("Predicted Chars:", res
)if(findfish
(res
)):pyautogui
.click
(button
='right')pyautogui
.click
(button
='right')time
.sleep
(1)else:time
.sleep
(0.5)def detect(img
):gray
= cv2
.cvtColor
(img
, cv2
.COLOR_BGR2GRAY
) dilation
= preprocess
(gray
)x
, y
, w
, h
= findTextRegion
(dilation
)return img
[y
:y
+ h
, x
:x
+ w
]def preprocess(gray
):sobel
= cv2
.Sobel
(gray
, cv2
.CV_8U
, 1, 0, ksize
=3)_
, binary
= cv2
.threshold
(sobel
, 0, 255, cv2
.THRESH_OTSU
+ cv2
.THRESH_BINARY
)element1
= cv2
.getStructuringElement
(cv2
.MORPH_RECT
, (30, 9))element2
= cv2
.getStructuringElement
(cv2
.MORPH_RECT
, (24, 6))dilation
= cv2
.dilate
(binary
, element2
, iterations
=1)erosion
= cv2
.erode
(dilation
, element1
, iterations
=1)dilation2
= cv2
.dilate
(erosion
, element2
, iterations
=2)return dilation2
def findTextRegion(img
):contours
, _
= cv2
.findContours
(img
, cv2
.RETR_TREE
, cv2
.CHAIN_APPROX_SIMPLE
)maxArea
= 0maxContour
= 0if(len(contours
)==0):return 0,0,0,0for i
in range(len(contours
)):cnt
= contours
[i
]area
= cv2
.contourArea
(cnt
)if area
> maxArea
:maxArea
= areamaxContour
= cntx
, y
, w
, h
= cv2
.boundingRect
(maxContour
)return x
, y
, w
, h
if __name__
=='__main__':fish
()
總結
以上是生活随笔為你收集整理的Minecraft 1.16 简易高效的自动钓鱼脚本的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。