Sword and expedition automatic battle

After the last yys script, I wrote a small script for sword and expedition

Today, I just used it for a while, and I feel it's pretty good. Although it has a single function, it's still very stable. So I'll share with you a few bb codes

# coding=gbk
# Declaration decoding method

# Import related modules
import os
import cv2
import random
import time

# -------------------Settings-----------------------
class Settings():

    def __init__(self):
        # Open 1 range
        self.start1_x = [470, 630]
        self.start1_y = [1640, 1680]
        # Open 1.5 range
        self.start15_x = [470, 630]
        self.start15_y = [1440, 1515]
        # Open 2 range
        self.start2_x = [400, 600]
        self.start2_y = [1800, 1855]
        # Closing range
        self.end_x = [129, 300]
        self.end_y = [1620, 1740]

s = Settings()
# -----------------y_functions--------------------
class Y_function(object):

    # Screen capture and return to picture
    def get_screen(self):
        # Screenshots password
        cmd_get = 'adb shell screencap -p /sdcard/img000.png'
        # Send picture password
        cmd_send = 'adb pull sdcard/img000.png ./JYYZimg'
        # Screenshot and send operation
        img = cv2.imread('./JYYZimg/img000.png', 0)
        return img

    # Return similarity of comparison pictures
    def match(self, img1, template):
        """img1 Represents the image to be matched, template Representative template, Template is a small picture"""
        res = cv2.matchTemplate(img1, template, cv2.TM_CCOEFF_NORMED)
        # The. max() method returns a match like parameter
        # mexres is the matching degree return maxres is to return the matching degree for the caller to use
        maxres = res.max()

        return maxres

    # Select a random point in the range
    def get_randxy(self, x, y):
        """Create a x,y Random position in two-dimensional region,x,y List of two elements, variable range"""
        xc = random.randint(x[0], x[1])
        yc = random.randint(y[0], y[1])

        return xc, yc

    # Random selection interval
    def get_randtime(self, a, b):
        """produce a,b Random time delay between"""
        time.sleep(random.uniform(a, b))

    # Click on the screen position
    def click(self, x, y):
        """Enter two 2D lists representing the location to be clicked x Coordinates y coordinate"""
        # Click through the point to be clicked by the adb to the mobile terminal
        cmd_click = 'adb shell input tap {} {}'.format(x, y)

    def perform_click(self, x_range, y_range):
        # Call get randxy method to generate random location
        xc, yc = self.get_randxy(x_range, y_range)
        # Call the click method to click the random location
        self.click(xc, yc)

    def begin(self):
        t_start1 = cv2.imread('./JYYZimg/img01.png', 0)
        t_start15 = cv2.imread('./JYYZimg/img015.png', 0)
        t_start2 = cv2.imread('./JYYZimg/img02.png', 0)
        t_end = cv2.imread('./JYYZimg/img03.png', 0)

        return t_start1,t_start15, t_start2, t_end

    def matchT(self, t_start1,t_start15, t_start2, t_end):
        while True:
            # The get screen method takes a screenshot and saves the return img
            img1 = self.get_screen()
            # The result returned by matct is the result of the cv2.max() method
            res1 = self.match(img1, t_start1)
            res15 = self.match(img1, t_start15)
            res2 = self.match(img1, t_start2)
            res3 = self.match(img1, t_end)
            # >Greater than 0.97 means that the image matching is on the jump out loop to execute the click method! [insert the image description here] (https://img-blog.csdnimg.cn/2020011321184071.png)
            # 0.97 the higher the parameter matching degree is, the more similar it is
            if res15 > 0.97:
                self.perform_click(s.start15_x, s.start15_y)
            elif res1 > 0.97:
                self.perform_click(s.start1_x, s.start1_y)
            elif res2 > 0.97:
                self.perform_click(s.start2_x, s.start2_y)
            elif res3 > 0.97:
                self.perform_click(s.end_x, s.end_y)
                print('Wait four seconds for match failure')

if __name__ == '__main__':

    Y = Y_function()
    t_start1, t_start15, t_start2, t_end = Y.begin()
    Y.matchT(t_start1,t_start15, t_start2, t_end)

Attached picture:





Create a code level directory JYYZimg, put four diagrams into the directory corresponding to the name, and save them. If it's an adb or cv2 problem, you can go to the yys script I wrote. That blog has records

Take the cabbies. Give me a compliment

32 original articles published, 25 praised, 308 visited
Private letter follow

Tags: shell Mobile

Posted on Mon, 13 Jan 2020 10:13:03 -0500 by RobertPaul