生活随笔
收集整理的这篇文章主要介绍了
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 简易高效的自动钓鱼脚本的全部内容,希望文章能够帮你解决所遇到的问题。
如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。