from PIL import Image import numpy as np import random import time import them def get_screen_shot(): """Take a screenshot of the phone screen,""" os.system('adb shell screencap -p /sdcard/screen.png') # Get a screenshot of the current section of the phone os.system('adb pull /sdcard/screen.png') # Download the current screenshot to the current folder def find_piece_and_board(img_path): """Accept an image and find the coordinates of the chessboard""" img = Image.open(img_path) # Get image size w,h = img.size # w 1080 h 1920 # Get the pixel matrix of the image img_pixel = img.load() start_y = None # Scan with a step size of 50 to improve efficiency and determine the boundary first for i in range(int(h/3),int(h*2/3),50): first_pixel = img_pixel[0,i] # Loop through other points in this line, if it is not a solid color, it means that other chessboards are encountered for j in range(1, w): pixel = img_pixel[j,i] if pixel[0] != first_pixel[0] or pixel[1] !=first_pixel[1] or pixel[2] != first_pixel[2]: start_y = i - 50 if start_y: break left = 0 right = 0 piece_y_max = 0 # find chess pieces for i in range(start_y, int(h*2/3)): flag = True for j in range(int(w/8),int(w*7/8)): # To reduce overhead, remove 1/8 from left and right pixel = img_pixel[j, i] # According to the color of the last row of the pieces, find if (50 < pixel[0] < 60) and (53 < pixel[1] < 63) and (95 <pixel[2] <110): if flag: left = j flag = False right = j piece_y_max = max(i,piece_y_max) piece_x = (left + right) // 2 # y is to find the bottom point piece_y = piece_y_max - 20 piece = (piece_x, piece_y) # Limit the chessboard scan abscissa if piece_x < w/2: board_x_start = piece_x + 380 board_x_end = 1000 else: board_x_start = 0 board_x_end = piece_x + 38 # find the board flag = True for i in range(start_y, int(h * 2 / 3)): first_pixel = img_pixel[0, i] for j in range(board_x_start, board_x_end): pixel = img_pixel[j, i] if abs(pixel[0] - first_pixel[0]) + abs(pixel[1] - first_pixel[1]) + abs(pixel[2] - first_pixel[2]) > 10: if flag: left = j right = j flag = False else: right = j if not flag: break board_x = (left + right) // 2 top_point = img_pixel[board_x, i] board_y = 0 # Start to find the position 274 pixels down # 274 The height of the largest board for k in range(i+274,i,-1): pixel = img_pixel[board_x, k] if abs(pixel[0] - top_point[0]) + abs(pixel[1] - top_point[1]) + abs(pixel[2] - top_point[2]) < 15: board_y = k break board_y = (board_y + i) // 2 board = (board_x, board_y) return piece, board def get_distance(piece, board): """Calculate distance""" x1 = piece[0] y1 = piece[1] x2 = board[0] y2 = board[1] distance = ((x1-x2)**2+(y1-y2)**2)**0.5 return distance def jump(distance): press_time = distance * 1.392 point = (random.randint (815, 923), random.randint (1509, 1658)) cmd = 'adb shell input swipe {x1} {y1} {x2} {y2} {time}'.format( x1=point[0], y1=point[1], x2=point[0]+ random.randint(1,3), y2=point[1] + random.randint(1,3), time = int(press_time) ) os.system(cmd) def run(): print("Please adjust the phone to usb debugging and start the game") flag = input("Please enter y to start the game:") if flag=='y': while 1 : get_screen_shot() img_path = 'screen.png' piece, board = find_piece_and_board(img_path) print(str(piece) + "--->" + str(board)) distance = get_distance(piece, board) jump(distance) time.sleep (random.randrange (1,3)) else: return if __name__ == '__main__': run()
Based on pixel analysis, find the center of the pawn and the center of the next grid, and then use the number of pixels to jump, which is not particularly stable