Live up to the time, the creation of non-stop, this article is participating in 2021 year-end summary essay contest
preface
Use Python to automatically jump a jump, without further ado ~
Let’s have a good time
The development tools
Python version: 3.6.4
Related modules:
The ADB module;
OpenCV module;
And some modules that come with Python.
Environment set up
Install Python and add it to the environment variables. PIP installs the required related modules.
Python+ADB+OpenCv, implement “hop” automation
ADB
The ADB tool is the Android Debug Bridge
ADB is a command line window for interacting with emulators or real devices from the computer side
It is similar to the Appium that Little F came into contact with before
ADB installation is simple: unpack the installation package and add the path to your system’s environment variables
Then use Python’s OS module to execute ADB commands
def get_screenshot() :
# Capture your phone's screen
os.system('adb shell /system/bin/screencap -p /sdcard/screencap.png')
Upload files or folders from the emulator to your computer
os.system('adb pull /sdcard/screencap.png screencap.png')
def jump(distance) :
# Set the pressing time to 1.35
press_time = int(distance * 1.35)
Generate random cell phone screen simulation touch points to prevent invalid results
# generate random integer (0-9), final value (0-90)
rand = random.randint(0.9) * 10
Adb long press operation, i.e. on the phone screen ((320-410),(410-500)) coordinate director press press_time ms
cmd = ('adb shell input swipe %i %i %i %i ' + str(press_time)) % (320 + rand, 410 + rand, 320 + rand, 410 + rand)
Adb command output
print(cmd)
Execute adb command
os.system(cmd)
Copy the code
Implementation to beat
Check the game end screen first
Determine if you need to end the program
# Game end template image
temp_end = cv2.imread('end.jpg'.0)
def game_over(img) :
""" template match, detect whether to end the program """
# If a template with the word "play one more game" is matched in the screenshot, the loop is terminated
res_end = cv2.matchTemplate(img, temp_end, cv2.TM_CCOEFF_NORMED)
if cv2.minMaxLoc(res_end)[1] > 0.95:
print('Game over! ')
return True
Copy the code
Template matching schematic diagram
When the maximum matrix value returned is greater than 0.95, the word “play another game” must appear in the original image
The game ends, and so does the program
The template matching code for little checkers is as follows
It is mainly to obtain the position of small checkers, that is, the starting position parameters of “jump a jump”
# Read the little checkers template image
temple = cv2.imread('temple.png'.0)
Get the height and width of the little checkers template image
th, tw = temple.shape[:2]
def get_start(img) :
""" Template match, get the position parameter of the start of a jump (little checkers) """
# Use standard correlation coefficient matching,1 for perfect match,-1 for bad match, and 0 for no correlation at all
result = cv2.matchTemplate(img, temple, cv2.TM_CCOEFF_NORMED)
# Use the function minMaxLoc to determine the maximum and minimum values of the matching result matrix (val) and their positions (LOC).
min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(result)
# Get the center position parameter of little checkers
return max_loc[0] + 47, max_loc[1] + 208
Copy the code
The results of
Through the edge detection of OpenCV to obtain the end position of “hop”
def get_end(img) :
Edge detection to obtain the position parameters of the end of a jump (box) ""
# Gaussian blur
img_rgb = cv2.GaussianBlur(img, (5.5), 0)
# Edge detection
canny_img = cv2.Canny(img_rgb, 1.10)
Get the height and width of the edge detection image
H, W = canny_img.shape
The height of the first vertex
y_top = np.nonzero([max(row) for row in canny_img[400: []])0] [0] + 400
The width of the first vertex
x_top = int(np.mean(np.nonzero(canny_img[y_top])))
# Jump over the little white circle and walk through it
y_bottom = y_top + 80
for row in range(y_bottom, H):
ifcanny_img[row, x_top] ! =0:
y_bottom = row
break
# Get the center of the square
x_center, y_center = x_top, (y_top + y_bottom) // 2
return x_center, y_center
Copy the code
Schematic diagram of edge detection
The main program
# Loop until the game fails
for i in range(10000) :# Move the screenshot from your Android phone to the current folder on your computer
get_screenshot()
# Read the screenshot image
img = cv2.imread('screencap.png'.0)
# Game over
if game_over(img):
break
Get the starting position argument
x_start, y_start = get_start(img)
Get the end position parameter
x_end, y_end = get_end(img)
# Draw the starting position, a circle
cv2.circle(img, (x_start, y_start), 10.255, -1)
# Draw the end position, a circle
img_end = cv2.circle(img, (x_end, y_end), 10.255, -1)
# Save images
cv2.imwrite('end.png', img_end)
# Calculate the straight line distance between the starting point and the ending point, check three strands four strings five
distance = (x_start - x_end) ** 2 + (y_start - y_end) ** 2
distance = distance ** 0.5
Set the press duration based on the distance obtained
jump(distance)
time.sleep(1.3)
Copy the code