人生苦短,我用PythonPython自动流程化高效率工作

用Python来自动玩放置类游戏,就是你了《剑与远征》

2020-03-10  本文已影响0人  Never肥宅

有个朋友拉我玩《剑与远征》,但是这种放置类游戏来回点开始战斗好累噢。。。。。。
就琢磨用python实现一下自动化操作
毕竟几年之前搞过跳一跳的adb连接然后自己跳
基本思路就是模板匹配,把开始游戏等几个游戏按键截取下来,然后过几秒就给手机截屏,对比是哪个按键,在哪里,模拟点击对应的位置,就可以让他自己推图了,等打不过了我再来给英雄升级。


按键图 按键图
#!/usr/bin/env python
# -*- encoding: utf-8 -*-
'''
@File    :   AutoSandE.py
@Time    :   2020/03/10 13:09:38
@Author  :   Jumpingknight 
@Version :   1.0
@Contact :   jumpingknight@tju.edu.cn
'''
# here put the import lib
import os 
import cv2
import random
import time
import re
class AutoGame():
    def __init__(self,path):
        self.devicesIP = "192.168.0.100"
        self.path = path
        os.system('cd '+self.path)
        resolution = os.popen("adb shell wm size").read()
        self.width = int(re.findall("\d*x",os.popen("adb shell wm size").read())[0][0:-1])#宽度
        self.height = int(re.findall("x\d*",os.popen("adb shell wm size").read())[0][1:])#宽度
        self.imgpath = "/sdcard/screenimg.png"
        self.template = [] 
        self.template.append(cv2.imread( path + "/img1.jpg"))
        self.template.append(cv2.imread( path + "/img2.jpg"))
        self.template.append(cv2.imread( path + "/img3.jpg"))
        self.method = ['cv2.TM_CCOEFF', 'cv2.TM_CCOEFF_NORMED', 'cv2.TM_CCORR',
           'cv2.TM_CCORR_NORMED', 'cv2.TM_SQDIFF', 'cv2.TM_SQDIFF_NORMED']

    def sendCmd(self,msg):#发送cmd指令,返回cmd的输出
        return os.popen(msg).read()

    def screenShot(self):#截取屏幕
        self.sendCmd("adb shell screencap -p " + self.imgpath)
    def screenDelete(self):#删除屏幕图像
        self.sendCmd("adb shell rm /"  + self.imgpath)

    def getScreen(self):
        self.screenShot()
        img = cv2.imread(self.imgpath)
        self.screenDelete()
        return img

    def matchDegree(self,img,template):#匹配度
        return cv2.matchTemplate(img,template,cv2.TM_CCOEFF_NORMED).max()
    
    def analogPlay(self):
        while True:
            img = self.getScreen()
            for tem in self.template:
                w = tem.width
                h = temp.height
                for meth in self.method:
                    m = eval(meth)
                    r = cv2.matchTemplate(img,tem,m)
                    min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(r)
                    if method in [cv2.TM_SQDIFF, cv2.TM_SQDIFF_NORMED]:
                        top_left = min_loc
                    else:
                        top_left = max_loc
                    ######此段参考https://zhuanlan.zhihu.com/p/62643151
                    
                    #######加个随机偏移怕被认成机器人,虽然不知道被不被禁止
                    clickpoint = [top_left[0] + w/2 + random.randrange(-w/5*10,w/5*10)/10,top_left[1]+h/2 + random.randrange(-h/5*10,h/5*10)/10]
                    ####
            self.clickScreen(clickpoint)#点击
            time.sleep(3 + random.randrange(-10,10)/10)#给人家歇会
            


    def clickScreen(self,point):
        os.system('adb shell input tap {} {}'.format(point[0],point[1]))
游戏图
上一篇下一篇

猜你喜欢

热点阅读