this example, we share the WeChat jump small program Python source code for you. The details are as follows:

WeChat jump program Python 5S source code python, after building environment, pro test is available.

 coding: import OS import # UTF-8 sys import subprocess import shutil import time import math from PIL import Image, ImageDraw import random import JSON import re # = = = = #: each core idea falls steadily after the screenshot, screenshot and calculate the pieces according to the coordinates of the next block top surface point coordinates, # according to the distance of two points multiplied by a coefficient obtained by the time long time # recognition rates: by colours to identify the location, through the screenshots found the bottom line is probably a straight line on the way down from the line traversal, # color (color with an interval to find the following comparison) that line of all points, and then find a point, after half # for good let the Y axis to reduce the height of the chassis so as to obtain the coordinate pieces # Identification Center On board: color box and color to do, starting from below the fraction, a line scan, because the circular block on the top is a line above # square is probably a bit, so I used the similar recognition rates approach recognized by several points for point, this # when the X axis of the midpoint of the block, then assume now pawn in the current block center, # according to a fixed point of access through the screenshots to launch the midpoint of the Y coordinate # finally: according to the two coordinates is multiplied by the coefficient to obtain the long distance according to the time (seems to be the direct use of X axis distance) # TODO: to solve the problem of # offset positioning TODO: look at the two block center to center distance is the same, if you rely on this to determine what the current ahead of or behind, for some fixed value root # correction TODO: According to the specific size calculation # TODO: screenshots of the direct use of X def open_accordant_config (simplified logic axis): screen_size = _get_screen_size (config_file) = "{path}/config/{screen_size}/config.json".Format (path=sys.path[0], screen_size=screen_size) if os.path.exists (config_file): with open (config_file,'r') as f: print ("Load config file from.Format (config_file) {}" return json.load (f) with ('{}/config/default.json'.format) else: open (sys.path[0]),'r' f: (as) print "Load default config") return json.load (f) def (_get_screen_size): size_str = os.popen ('adb shell WM size'(.Read) = re.search (m)' (d+) x (d+ '), size_str if m: width (m.group) = 1) height = m.group (2) return "{height}x{width}" (.Format height=height, wid Th=width) config = open_accordant_config (Magic Number) #, not set may not perform properly, please according to the screenshots from top to bottom to set under_game_score_y = 300 press_coefficient = 1.47 # according to long time coefficient, please attach your own piece base according to the actual situation of regulation of piece_base_height_1_2 = 25 # 1/2, may need to adjust the piece_body_width = 80 # piece width than in the screenshot to a little larger safe starting point may be adjusted, coordinate # simulation on the need of automatic repeat game set to "another" coordinate if config.get ('swipe'): swipe = config['swipe'] else: swipe = swipe['y1'], swipe['x2'], swipe['x1'] {}, = swipe['y2'] 320, 410, 320, 410 screenshot_backup_dir ='screenshot_backups/' if not os.pa Th.isdir (screenshot_backup_dir): os.mkdir (screenshot_backup_dir) def (pull_screenshot): process = subprocess.Popen ('adb shell screencap -p', shell=True, stdout=subprocess.PIPE) screenshot (if) = process.stdout.read sys.platform = ='win32': screenshot = screenshot.replace (b'rn', b'n') f = open ('autojump.png','wb') f.write (screenshot) f.close (backup_screenshot (TS) def in order to #): when is it convenient for the failure of debug (screenshot_backup_dir): if not os.path.isdir os.mkdir (screenshot_backup_dir) shutil.copy ('autojump.png','{}{}.png'.format (screenshot_backup_dir, TS) DEF) save_debug_creenshot (TS, Im, piece_x, piece_y, board_x, board_y): draw = ImageDraw.Draw (IM) # of debug pictures and detailed notes (draw.line (piece_x, piece_y) + (board_x, board_y), fill=2, width=3)  draw.line((piece_x, 0, piece_x, im.size[1]), fill=(255, 0, 0))  draw.line((0, piece_y, im.size[0], piece_y), fill=(255, 0, 0))  draw.line((board_x, 0, board_x, im.size[1]), fill=(0, 0, 255))  draw.line((0, board_y, im.size[0], board_y), fill=(0, 0, 255))  draw.ellipse((piece_x - 10, piece_y - 10, piece_x + 10, piece_y + 10), fill=(255, 0, 0))  draw.ellipse((board_x - 10, board_y - 10, board_x + 10, board_y + 10), fill=(0, 0, 255))  del draw  im.save('{}{}_d.png'.format(screenshot_backup_dir, ts))   def set_button_position(im):  # 将swipe设置为 `再来一局` 按钮的位置  global swipe_x1, swipe_y1, swipe_x2, swipe_y2  w, h = im.size  left = w / 2  top = 1003 * (h / 1280.0) + 10  swipe_x1, swip E_y1, swipe_x2, swipe_y2 = left, top, left, top def jump (distance): if distance < 400: distance = 0.9 * distance + 50 else: distance = 0.85 * distance + 80 press_time = distance * press_coefficient press_time = max (press_time, 200) # set 200 ms is the smallest of the pressing time press_time = int (press_time) CMD ='adb shell input swipe {x1} {y1} {x2} {y2} {duration}'.format (x1=swipe['x1'], y1=swipe['y1'], x2=swipe['x2'], y2=swipe['y2'], duration=press_time) print (CMD) os.system (CMD) hsv2rgb def hsv2rgb # converting color mode (h, s, V): H = float (H) s = float (s = float (V) V) h60 = H / h60f = 60 math.floor (h60) hi = int (h60f)% 6 F = h60 - h60f = V * P (1 - s) q = V (1 - f * * s) t = V * (1 - (1 - F) * s)   r, g, b = 0, 0, 0  if hi == 0: r, g, b = v, t, p  elif hi == 1: r, g, b = q, v, p  elif hi == 2: r, g, b = p, v, t  elif hi == 3: r, g, b = p, q, v  elif hi == 4: r, g, b = t, p, v  elif hi == 5: r, g, b = v, p, q  r, g, b = int(r * 255), int(g * 255), int(b * 255)  return r, g, b  # 转换色彩模式rgb2hsv def rgb2hsv(r, g, b):  r, g, b = r/255.0, g/255.0, b/255.0  mx = max(r, g, b)  mn = min(r, g, b)  df = mx-mn  if mx == mn:   h = 0  elif mx == r:   h = (60 * ((g-b)/df) + 360) % 360  elif mx == g:   h = (60 * ((b-r)/df) + 120) % 360  elif mx == b:   h = (60 * ((r-g)/df) + 240) % 360  if mx == 0:   s = 0  else:   s = df/mx  v = mx  return h, s, v   def find_piece_and_board(im):  w, h = im.size   piece_x_sum = 0  piece_x_c = 0  piece_ Y_max = 0 board_x = 0 board_y = 0 left_value = 0 left_count = 0 right_value = 0 right_count = 0 from_left_find_board_y = 0 from_right_find_board_y = 0 scan_x_border = int (w / 8) initial y coordinates im_pixel=im.load boundary scan_start_y = # scanning 0 pieces # scanning (50px) # to try to detect scan_start_y for I step. In range (int (H / 3), int (h*2 /3), 50): last_pixel = im_pixel[0, i] for J in range (1, w): pixel=im_pixel[j, i] # is not a solid line, record the value of scan_start_y, ready to jump out of the cycle of if pixel[0]! = last_pixel[0] or = last_pixel[1] or pixel[2] pixel[1]!! = last_pixel[2]: scan_start_y = I - 50 break if scan_start_y: break print ('scan_start_y:, scan_start_y) # from scan_start_y To scan pieces should be located in the top half of the screen, there is no more than 2/3 for I in is range (scan_start_y, int (h * 2 / 3)): for J in range (scan_x_border, W - scan_x_border): # abscissa also reduced the part scanning overhead of pixel = im_pixel[j, i] # according to minimum for pieces of color judgment, to find the last line of the mean value of the point, the color of this should be OK, temporarily proposed by if (50 < pixel[0] < 60) and (53 < pixel[1] < 63) and (95 < pixel[2] < 110): piece_x_sum = J piece_x_c = 1 piece_y_max = max (I, piece_y_max) if not all ((piece_x_sum, piece_x_c)): return 0, 0, 0, 0 piece_x = piece_x_sum / piece_x_c = piece_y_max - piece_y piece_base_height_1_2 # up half the height of the for I chassis pieces in Range (int (H / 3), int (h * 2 / 3)): last_pixel = im_pixel[0, i] # calculate the shadow value of RGB observed by Photoshop, the shadow is actually part of the background color of the lightness V multiplied by 0.7. H, s, v = rgb2hsv (last_pixel[0], last_pixel[1], R, last_pixel[2]) g, B = hsv2rgb (h, s, V if from_left_find_board_y and * 0.7) from_right_find_board_y: break if not board_x: board_x_sum = 0 board_x_c = 0 for J in range (W): pixel = im_pixel[j, i] # cut off head is higher than the small lattice under a condition of bug if ABS (J - piece_x) < piece_body_width: continue; # cut off a line leading to the dome of the small bug, the color judgment should be OK, temporarily proposed by if (pixel[0] ABS last_pixel[0]) + ABS (pixel[1] - last_pixel[1]) + A BS (pixel[2] - last_pixel[2]) > 10: board_x_sum = J board_x_c = 1 if board_x_sum: = board_x board_x_sum / board_x_c else: # continue searching, scanning from left to right, find the first pixels of different colors and background, record the location # when there are 3 consecutive identical records, said the discovery of a straight line. # this line is on the left top left edge of target board # and the current y value minus the first pixel # 3 left edge is at the top of the for J in range (W): pixel = im_pixel[j, i] # cut off head is higher than the small lattice under a condition of bug if ABS (J piece_x piece_body_width: continue (if) < ABS (pixel[0] - last_pixel[0]) + ABS (pixel[1] - last_pixel[1]) + ABS (pixel[2] - last_pixel[2]) > And (10) ABS (pixel[0] - R) + ABS (pixel[1] - G) + ABS (pixel[2] - b) > 10): if left_value left_count = left_count+1 else: = = j: left_value = J left_count = 1 if left_count > 3: from_left_find_board_y = I - 3 break # logic follow surface is similar but the direction from the right. To the left # when there is occlusion, there will be only one block # out two must have a j in range is on for (W) [:: -1]: pixel = im_pixel[j, i] # cut off head is higher than the small lattice under a condition of bug if ABS (J - piece_x) < piece_body_width: continue if (ABS (pixel[0] - last_pixel[0]) + ABS (pixel[1] - last_pixel[1]) + ABS (pixel[2] - last_pixel[2]) > and (10) ABS (pixel[0] - R) + ABS (pixel[1] - G + (ABS) Pixel[2] - B >); 10): if right_value = = j: right_count = left_count + 1 else: right_value = J right_count = 1 if right_count > 3: from_right_find_board_y = I - 3 break # if the top pixel is more, that pattern is nearly round, calculated the value of the corresponding need to increase, increasing the top tentative here


This concludes the body part