引入:
接着上篇的博文,今天我们讲如何实现助人为乐
前期准备:
如何获取图片中指定文字的坐标?
我的思路是截取一个小区域,再根据小区域左上角的坐标获取中央坐标
例如:
获取坐上角的x和y坐标,测量x到红点的横向距离x1,在测量y点到红点的纵向距离y1
可以得出红点的坐标(x+x1,y+y1)
复制代码,运行
X1 = 1132 X2 = 1536 time.sleep(3) screenshot(X1, 622, X2, 676,helpedName1)#截图mousemove_click(X1 + 200, Y + 30)
截取了一小区域,然后获得了中心坐标,这里的200和30是自己测量出来的
如果有更好更直接的获取图片中指定文字的坐标的方法可以下方评论告诉我,我也学习学习
开始制作外挂:
详细步骤之前的博文1和博文2已经说过
详细代码如下:
imageGrabUtil.py(截图工具)
#coding=utf-8from PIL import ImageGrab
# 参数说明
# 第一个参数 开始截图的x坐标
# 第二个参数 开始截图的y坐标
# 第三个参数 结束截图的x坐标
# 第四个参数 结束截图的y坐标
def screenshot(x1,y1,x2,y2,filename):bbox = (x1, y1, x2, y2)im = ImageGrab.grab(bbox)# 参数 保存截图文件的路径im.save('E:\\python_project\\Asktao_Automation\\resource\\'+filename+'.png')
baiduAipUtil.py(文字识别工具)
我这里百度云的APPP_ID、API_KEY、SECRET_KEY保存在config.ini
#coding=utf-8
from aip import AipOcrimport re#百度文字识别
# !/usr/bin/env python
# -*- coding:utf-8 -*- import ConfigParser import os os.chdir("E:\python_project\Asktao_Automation\util") cf = ConfigParser.ConfigParser() cf.read("config.ini") secs = cf.sections() APPP_ID = cf.get("baiduAip","APPP_ID") API_KEY = cf.get("baiduAip","API_KEY") SECRET_KEY = cf.get("baiduAip","SECRET_KEY") client = AipOcr(APPP_ID,API_KEY,SECRET_KEY) def characterRecognition(filePath): i = open(filePath,'rb') img = i.read() message = client.basicGeneral(img); #print(message.get('words_result')) string = ''; for i in message.get('words_result'): print(i.get('words')) string += i.get('words') return string
config.ini
[baiduAip]
APPP_ID = APPP_ID
API_KEY = API_KEY
SECRET_KEY = SECRET_KEY
joyByHelpingPeople.py
我只做了冯喜来和杨镖头的部分,其他人物的可以用同样的方法做出来,先走完流程,再写代码
#coding=utf-8 import autopy import time import win32api import win32con from imageGrabUtil import screenshot from baiduAipUtil import characterRecognition import sys defaultencoding = 'utf-8' if sys.getdefaultencoding() != defaultencoding:reload(sys)sys.setdefaultencoding(defaultencoding)#助人为乐 #time.sleep(5) win32api.keybd_event(18,0,0,0) #alt键位码是18 win32api.keybd_event(9,0,0,0) #tab键位码是9 time.sleep(0.5) win32api.keybd_event(13,0,0,0) #enter键位码是13win32api.keybd_event(18,0,win32con.KEYEVENTF_KEYUP,0) #释放按键 win32api.keybd_event(9,0,win32con.KEYEVENTF_KEYUP,0) win32api.keybd_event(13,0,win32con.KEYEVENTF_KEYUP,0) time.sleep(2)def mousemove_click(x,y):autopy.mouse.smooth_move(x, y)autopy.mouse.click()#帮助冯喜来 def fengXiLai(Y):mousemove_click(X1+200,Y+30) #鼠标移到按钮中央,点击冯喜来按钮for i in range(1,4,1):mousemove_click(1448,380)time.sleep(150) #打强盗、无名剑客平均时间150smousemove_click(1515, 258)#跳过对话mousemove_click(1515, 258)time.sleep(15)mousemove_click(1448, 380)# 白邦芒处领赏#帮助杨镖头 def yangBiaoTou(Y):mousemove_click(X1 + 200, Y + 30)for i in range(1,5,1):#和张老板对话,传递心意,找张老板,找玄武mousemove_click(1448, 380)#点任务栏 和张老板对话time.sleep(15)mousemove_click(1515, 258) # 跳过对话mousemove_click(1448, 380) # 点任务栏 寻找窃贼time.sleep(45)#和窃贼对战时间45smousemove_click(1515, 258) # 跳过对话for i in range(1,5,1):#向张老板复命,月老,莲花姑娘,张老板mousemove_click(1448, 380) # 点任务栏 向张老板复命time.sleep(15)mousemove_click(1515, 258) # 跳过对话mousemove_click(771, 203) # 活动的坐标mousemove_click(1359,503) #前往的坐标time.sleep(15)#从天墉城城中心/其他地图走到白帮忙花费15smousemove_click(1336,649) #领赏#为了获取图片中指定文字的坐标,这里需要分开截图 helpedName1 = "helpedName1" X1 = 1132 X2 = 1536 time.sleep(3) screenshot(X1, 622, X2, 676,helpedName1)#截图judgehelpedNameStr1 = characterRecognition('E:\\python_project\\Asktao_Automation\\resource\\'+helpedName1+'.png')#文字识别helpedName2 = "helpedName2"screenshot(X1, 690, X2, 741,helpedName2)#截图judgehelpedNameStr2 = characterRecognition('E:\\python_project\\Asktao_Automation\\resource\\'+helpedName2+'.png')#文字识别 if '冯喜来' in judgehelpedNameStr1:fengXiLai(622) elif '冯喜来' in judgehelpedNameStr2:fengXiLai(690) elif '杨镖头' in judgehelpedNameStr1:yangBiaoTou(622) elif '杨镖头' in judgehelpedNameStr2:yangBiaoTou(690)
至此,回合制手游(如问道)的所有任务的脚本都可以用这三篇教程的所讲方法去做
若有更简洁的方法,欢迎指出
三篇教程的所有代码请点击这里获取