Random fight Westward Journey 2 automatic coin collection script

Article catalogue

  • preface
  • I Slide Click & screenshot
  • II Picture comparison
  • 3, Character recognition
  • 4, Linkage with Excel
  • summary

preface

Random fighting is just an old Netease abandon game.

Well, although, as an amateur coin dealer who enjoys a certain popularity in the Android 2 theater, I don't have 100 numbers in my head every day, and several fixed operations such as repeated recharge and collection really disgust me. Therefore, I spent all my life learning to write a python program for automatic coin collection in the past few days of winter vacation, realizing a series of small functions from automatic coin collection to automatic recording on Excel. This move also successfully led me to the road of automatic farming, and since then I have liberated my hands and lived a happy life (dog head).

The ultimate goal of this script is to receive money and record. After decomposition, it only needs to realize the following functions: sliding click, intercepting pictures, picture comparison, character recognition and operating Excel tables.

Let me introduce how to solve it one by one.  

1, Slide Click & screenshot

In fact, this step is relatively simple. At first, I used the module PyMouse to operate the lightning simulator directly. Later, I found that it was too pull: first, I had to fix the resolution of the lightning simulator and the relative position on the desktop. Each position must be the same program to be effective. Secondly, I couldn't hit LOL during operation, You can't even operate the mouse. Obviously, this does not meet my requirements. So I looked for his method and discovered the new world --adb of the lightning simulator, which directly called the internal command line of thunder and lightning, and realized the purpose operation easily.     

After installing the lightning simulator, you can find ldconsole under the installation directory Exe and ld Exe two command line programs. The ld command is used to execute adb Command, while ldconsole Exe is a heavyweight. It can control the setting of simulator parameters, including imei, serial number, mobile phone number, resolution and so on. It can also detect and control the startup and shutdown of the simulator. It can even simulate input, key press, sliding and other operations.  

The following is from python controlled lightning simulator_ Self cultivation of a programmer - CSDN blog_ python lightning simulator Several simple functions written by classes in:

def touch(index: int, x: int, y: int):  # click
    Dnconsole.touch(index, x, y, 0)
    time.sleep(0.5)


def touch_01(index: int, x: int, y: int, z: int, w: int):  # Long press
    Dnconsole.touch(index, x, y, 0)
    for i in range(0, z):
        Dnconsole.touch(index, x, y, 0)
        time.sleep(w)


def swipe(index: int, x1: int, y1: int, x2: int, y2: int):  # slide
    A = [x1, y1]
    B = [x2, y2]
    Dnconsole.swipe(index, A, B)
    time.sleep(1.5)

Although the screenshot function is simple, there is no ready-made one. I fumbled to come up with a function:

def screen(index: int):
    cmd = "adb -s 127.0.0.1:5559 shell screencap -p /sdcard/Pictures/Screenshots/two.png"
    os.popen(cmd)
    time.sleep(0.5)

-p is followed by the address where the screenshot is saved and - s is followed by the serial number of the simulator. You can get it by entering adb devices in cmd.

 

In fact, there is another small problem with screenshots, that is, partial screenshots. For example, in the figure below, I only want the local figure of the number of coins. How to realize it?

In fact, it's very simple. It's ok to cut it again based on this screenshot, as follows:

import cv2
file_path = 'D:/PycharmProjects/ldxd.UE/Screenshots/two.png'  #Picture address
im = cv2.imread(file_path)
im = im[91:113, 766:808]   #Clipping range
save_path_file = os.path.join('D:/PycharmProjects/ldxd.UE/Screenshots/currency.png')
cv2.imwrite(save_path_file, im)

The demonstration effect is as follows:

Ah ~ it's troublesome to transmit video. Forget it...

2, Picture comparison

The purpose of this function is to judge what interface the game is currently in. If there is a notice bar and there is a 6-yuan package in it, what should be executed at this time and what should be executed if it is another one. It's easy to realize this function because there are many related resources in the station. I found a hash difference comparison, input the two picture addresses and output the difference value. The smaller the value, the more similar it is. The effect of personal test is OK. reference resources python compares the similarity of two pictures_ Xiaoqiang's blog - CSDN blog_ python image contrast similarity

def dHash(img):
    # Zoom 8 * 8
    img = cv2.resize(img, (9, 8), interpolation=cv2.INTER_CUBIC)
    # Convert grayscale image
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    hash_str = ''
    # If the first pixel of each row is greater than the last pixel, it is 1, and the opposite is 0. A hash is generated
    for i in range(8):
        for j in range(8):
            if gray[i, j] > gray[i, j + 1]:
                hash_str = hash_str + '1'
            else:
                hash_str = hash_str + '0'
    return hash_str


# Hash value comparison
def cmpHash(hash1, hash2):
    n = 0
    # If the hash length is different, - 1 will be returned, indicating an error in parameter transmission
    if len(hash1) != len(hash2):
        return -1
    # Ergodic judgment
    for i in range(len(hash1)):
        # If they are not equal, n count + 1, and N is the similarity finally
        if hash1[i] != hash2[i]:
            n = n + 1
    return n


# Hash difference
def valunhash(img1, img2):  # img picture address
    img1 = cv2.imread(img1)
    img2 = cv2.imread(img2)
    hash1 = dHash(img1)
    hash2 = dHash(img2)
    n = cmpHash(hash1, hash2)
    # print('difference hash algorithm similarity: '+ str(n))
    return n

3, Character recognition

In order to identify "6 yuan" (in fact, can be compared with pictures) and "coin number", I use the Baidu Intelligent Cloud character recognition module, personal feel nice. I won't repeat the usage method. I found a tutorial for your reference and study. If you don't understand, you can ask again in the comment area. Identify the text on the picture through Baidu ai_ Column of yunwu009 - CSDN blog_ How does ai recognize the text on the picture

from aip import AipOcr


#The following is obtained from Baidu intelligent cloud.                                                                                                       
APPP_ID = '  '
API_KEY = '  '
SECRET_KEY = ' '
client = AipOcr(APPP_ID, API_KEY, SECRET_KEY)

# Character recognition
def character(filepath):#Enter picture address
    i = open(filepath, 'rb')
    img = i.read()
    message = client.basicGeneral(img)
    string = '';
    for i in message.get('words_result'):
        string += i.get('words')
    return string

4, Linkage with Excel

The realization of this function is everywhere in the station. I use openpyxl module. The purpose is to record each trumpet in the corresponding form after receiving the currency for subsequent viewing. The schematic diagram is as follows:

b = int(b)
workbook = openpyxl.load_workbook("three.xlsx")  #Open Excel file
worksheet = workbook.worksheets[0]               #Load first table
worksheet.cell(j, 2, b)                          #Fill in the box in row j-1 and column 2 with b
workbook.save(filename="three.xlsx")             #Save and close

 

summary

It is not difficult to find that although there are many steps, they are relatively simple, and the combination effect is very good. After learning this other function, you can develop it yourself (such as cutting skill ~ ~) (I haven't cut skill). Criticism and correction are welcome.

Partial source code display:

import os
import time
import cv2
import openpyxl
from aip import AipOcr
from ldconsole import Dnconsole

APPP_ID = ' '
API_KEY = ' '
SECRET_KEY = '  '
client = AipOcr(APPP_ID, API_KEY, SECRET_KEY)


# Character recognition
def character(filepath):
    i = open(filepath, 'rb')
    img = i.read()
    message = client.basicGeneral(img)
    string = '';
    for i in message.get('words_result'):
        string += i.get('words')
    return string


# screenshot
def screen(index: int):
    if index == 1:
        cmd = "adb -s 127.0.0.1:5557 shell screencap -p /sdcard/Pictures/Screenshots/one.png"
        os.popen(cmd)
    if index == 2:
        cmd = "adb -s 127.0.0.1:5559 shell screencap -p /sdcard/Pictures/Screenshots/two.png"
        # cmd = "adb -s emulator-5558 shell screencap -p /sdcard/Pictures/Screenshots/two.png"
        os.popen(cmd)
    if index == 3:
        cmd = "adb -s 127.0.0.1:5561 shell screencap -p /sdcard/Pictures/Screenshots/three.png"
        os.popen(cmd)
    if index == 4:
        cmd = "adb -s 127.0.0.1:5557 shell screencap -p /sdcard/Pictures/Screenshots/four.png"
        os.popen(cmd)
    time.sleep(0.5)


# Difference sensing algorithm
def dHash(img):
    # Zoom 8 * 8
    img = cv2.resize(img, (9, 8), interpolation=cv2.INTER_CUBIC)
    # Convert grayscale image
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    hash_str = ''
    # If the first pixel of each row is greater than the last pixel, it is 1, and the opposite is 0. A hash is generated
    for i in range(8):
        for j in range(8):
            if gray[i, j] > gray[i, j + 1]:
                hash_str = hash_str + '1'
            else:
                hash_str = hash_str + '0'
    return hash_str


# Hash value comparison
def cmpHash(hash1, hash2):
    n = 0
    # If the hash length is different, - 1 will be returned, indicating an error in parameter transmission
    if len(hash1) != len(hash2):
        return -1
    # Ergodic judgment
    for i in range(len(hash1)):
        # If they are not equal, n count + 1, and N is the similarity finally
        if hash1[i] != hash2[i]:
            n = n + 1
    return n


# Hash difference
def valunhash(img1, img2):  # img picture address
    img1 = cv2.imread(img1)
    img2 = cv2.imread(img2)
    hash1 = dHash(img1)
    hash2 = dHash(img2)
    n = cmpHash(hash1, hash2)
    # print('difference hash algorithm similarity: '+ str(n))
    return n


def touch(index: int, x: int, y: int):
    Dnconsole.touch(index, x, y, 0)
    time.sleep(0.5)


def swipe(index: int, x1: int, y1: int, x2: int, y2: int):
    A = [x1, y1]
    B = [x2, y2]
    Dnconsole.swipe(index, A, B)
    time.sleep(1.5)


def select(index: int):
    touch(index, 542, 435)  # Click 6 yuan
    time.sleep(3.5)
    touch(index, 660, 493)  # Click pay
    time.sleep(25)
    touch(index, 265, 106)  # View bulletin board
    time.sleep(0.5)
    touch(index, 265, 106)  # View bulletin board
    time.sleep(0.5)
    touch(index, 541, 430)  # Click to receive
    time.sleep(1)
    touch(index, 135, 238)  # Click blank
    touch(index, 432, 107)  # Click Liancheng ceremony
    touch(index, 486, 443)  # Click to receive
    time.sleep(1)
    touch(index, 135, 238)  # Click blank
    touch(index, 321, 45)
    touch(index, 532, 207)  # Click to receive
    touch(index, 135, 238)  # Click blank
    touch(index, 135, 238)  # Click blank


address_01 = 'D:/PycharmProjects/ldxd.UE/Screenshots/one.png'
address_02 = 'D:/PycharmProjects/ldxd.UE/Screenshots/two_2.png'
x = [429, 670]
y = [108, 178, 253, 328, 394, 467]


# Check whether it flashes back
def check(index: int):
    touch(index, 644, 196)
    Dnconsole.invokeapp(index, 'com.netease.ldxy.ewan.x7sy')
    time.sleep(1)
    screen(index)
    if index == 2:
        a = valunhash('D:/PycharmProjects/ldxd.UE/Screenshots/two.png',
                      'D:/PycharmProjects/ldxd.UE/Screenshots/compare.png')
    if index == 1:
        a = valunhash('D:/PycharmProjects/ldxd.UE/Screenshots/one.png',
                      'D:/PycharmProjects/ldxd.UE/Screenshots/compare.png')
    if a < 5:
        time.sleep(5)
        touch(index, 381, 437)  # Confirm agreement
        time.sleep(30)
        touch(index, 755, 121)  # Last login
        time.sleep(5)
        touch(index, 481, 459)  # Click OK


# Zone changing function
def handoff(index: int, number: int, i: int):
    if i <= 16:
        touch(index, 572, 398)
        time.sleep(1.5)
        touch(index, 248, 340)  # Select 2 Theater
        if i > 11:
            Dnconsole.swipe(2, [545, 466], [545, 28], 10100)
            time.sleep(0.8)
        a = x[i % 2]
        b = y[(int(i / 2)) % 6]
        touch(index, a, b)
        touch(index, 478, 454)  # get into

        touch(index, 478, 454)  # get into


def screen_small(index: int):
    if index == 2:
        file_path = 'D:/PycharmProjects/ldxd.UE/Screenshots/two.png'
        im = cv2.imread(file_path)
        im = im[91:113, 766:808]
        save_path_file = os.path.join('D:/PycharmProjects/ldxd.UE/Screenshots/currency.png')
        cv2.imwrite(save_path_file, im)
    if index == 1:
        file_path = 'D:/PycharmProjects/ldxd.UE/Screenshots/one.png'
        im = cv2.imread(file_path)
        im = im[91:113, 766:808]
        save_path_file = os.path.join('D:/PycharmProjects/ldxd.UE/Screenshots/currency.png')
        cv2.imwrite(save_path_file, im)


def rent(index: int):
    time.sleep(1.8)
    screen(index)
    time.sleep(2)
    if index == 2:
        has = valunhash(address_02, 'D:/PycharmProjects/ldxd.UE/Screenshots/goinx7_02.png')
    if index == 1:
        has = valunhash(address_01, 'D:/PycharmProjects/ldxd.UE/Screenshots/goinx7_02.png')
    if has < 4:
        select(index)
        # touch(index, 97, 37)  # Click on the Avatar
        # touch(index, 700, 431)  # Exit the game

    if 4 <= has <= 14:
        touch(index, 135, 238)  # Click on the blank bar
        # touch(index, 97, 37)  # Click on the Avatar
        # touch(index, 700, 431)  # Exit the game
    if has > 14:
        touch(index, 266, 106)  # Click on the notice bar
        time.sleep(0.5)
        screen(index)
        screen_small_02(2)
        if index == 2:
            has = valunhash(address_02, 'D:/PycharmProjects/ldxd.UE/Screenshots/two_2.reference.png')
        if index == 1:
            has = valunhash(address_01, 'D:/PycharmProjects/ldxd.UE/Screenshots/goinx7_02.png')
        if 3 < has <= 14:
            touch(index, 135, 238)  # Click on the blank bar
            # touch(index, 97, 37)  # Click on the Avatar
            # touch(index, 700, 431)  # Exit the game
        if has < 4:
            select(index)
            # touch(index, 97, 37)  # Click on the Avatar
            # touch(index, 700, 431)  # Exit the game


def check_01(j):
    index = 2;
    touch(index, 135, 238)  # Click blank
    touch(index, 326, 110)  # Click zhenbaoxuan
    time.sleep(1)
    screen(2)
    time.sleep(0.5)
    screen_small(index)
    a = valunhash('D:/PycharmProjects/ldxd.UE/Screenshots/currency.png',
                  'D:/PycharmProjects/ldxd.UE/Screenshots/sam.png')
    if a < 3:
        workbook = openpyxl.load_workbook("one.xlsx")
        worksheet = workbook.worksheets[0]
        worksheet.cell(j + 1, 2, 1)
        workbook.save(filename="one.xlsx")
    else:
        b = character('D:/PycharmProjects/ldxd.UE/Screenshots/currency.png')
        if b == ' ':
            workbook = openpyxl.load_workbook("one.xlsx")
            worksheet = workbook.worksheets[0]
            worksheet.cell(j + 2, 2, 'error')
            workbook.save(filename="one.xlsx")
        else:
            b = int(b)
            workbook = openpyxl.load_workbook("one.xlsx")
            worksheet = workbook.worksheets[0]
            worksheet.cell(j + 2, 2, b)
            workbook.save(filename="one.xlsx")
    touch(index, 97, 37)  # Click on the Avatar
    touch(index, 700, 431)  # Exit the game


def screen_small_02(index: int):
    if index == 2:
        file_path = 'D:/PycharmProjects/ldxd.UE/Screenshots/two.png'
        im = cv2.imread(file_path)
        im = im[261:370, 346:748]
        save_path_file = os.path.join('D:/PycharmProjects/ldxd.UE/Screenshots/two_2.png')
        cv2.imwrite(save_path_file, im)
    if index == 1:
        file_path = 'D:/PycharmProjects/ldxd.UE/Screenshots/one.png'
        im = cv2.imread(file_path)
        im = im[261:370, 346:748]
        save_path_file = os.path.join('D:/PycharmProjects/ldxd.UE/Screenshots/one_2.png')
        cv2.imwrite(save_path_file, im)


'''
workspace workspace workspace workspace
workspace workspace workspace workspace
workspace workspace workspace workspace
workspace workspace workspace workspace
workspace workspace workspace workspace
workspace workspace workspace workspace                                 
workspace workspace workspace workspace
workspace workspace workspace workspace
'''
for j in range(0, 17):
    index = 2
    time.sleep(1)
    handoff(index, 1, j)
    rent(index)
    check_01(j)

For the determination of coordinates, pick can be used.

Keywords: Python Android OpenCV Game Development

Added by prowley on Sat, 29 Jan 2022 15:24:47 +0200