Xiaomi 5s WeChat jump applet python source code, 5 spython

Source: Internet
Author: User

Xiaomi 5-5 hop-on-one small program python source code, 5 spython

This article provides an example of the python source code for the skip-hop applet for your reference. The details are as follows:

The one-hop applet Xiaomi 5s source code python, which is ready for use after the environment is set up.

# Coding: utf-8import osimport sysimport subprocessimport shutilimport timeimport mathfrom PIL import Image, ImageDrawimport randomimport jsonimport re, calculate the coordinates of the pawnpiece and the midpoint coordinates of the Next Top, # obtain the long-pressed time by multiplying the distance between the two points by a time coefficient # recognize the pawnpiece: Identify the position by the color of the pawnpiece, we can find that the bottom row is probably a straight line, and traverse from the top row to the bottom row. # Compare the color (the color is compared with a range) to find all the points in the bottom row, then obtain a midpoint. # After the coordinates are obtained, let the Y axis coordinate reduce the half height of the pawnboard chassis to obtain the coordinates of the center. # identify the chessboard by the background color and the color difference of the square, starting from the position below the score, a row scans, because the top of the circular block is a line, # Square The above is probably a point, so I used the method similar to recognizing a piece to identify multiple points for the midpoint. # at this time, I got the X axis coordinates of the midpoint, at this time, we assume that the pawns are in the center of the current block. # Calculate the Y coordinate of the midpoint based on a fixed angle obtained. # Finally: calculate the distance multiplied by the coefficient based on the coordinates of the two points to obtain the long time (it seems that the distance from the X axis can be directly used) # TODO: solves the problem of location offset # TODO: check whether the distance between the two center blocks and the central axis is the same. If yes, you can determine whether the current advances are backward and facilitate correction. # TODO: some fixed values are calculated based on the specific size # TODO: directly use the X axis to simplify the logic def open_accordant_config (): screen_size = _ get_screen_size () config_file = "{path}/config/{screen_size}/config. json ". format (path = sys. path [0], screen_s Ize = 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) else: with open ('{}/config/default. json '. format (sys. path [0]), 'R') as f: print ("Load default config") return json. load (f) def _ get_screen_size (): size_str = OS. popen ('adb shell wm size '). read () m = re. search ('(\ d +) x (\ d +)', size_str) if m: widt H = m. group (1) height = m. group (2) return "{height} x {width }". format (height = height, width = width) config = open_accordant_config () # Magic Number. If this parameter is not set, execution may fail, set under_game_score_y = 300press_coefficient = 1.47 # The time coefficient of Long press according to your needs. Adjust the height of piece_base_height_1_2 = 25 #1/2 based on your actual situation, it may be necessary to adjust piece_body_width = 80 # the width of the pawnpiece, which is a little larger than the moderate width. It may be necessary to adjust # coordinate of the start point of the simulated press, to automatically repeat the game, set it to the "next game" coordinate if config. get ('swipe '): swipe = c Onfig ['swip'] else: swipe ={} swipe ['x1'], swipe ['y1 '], swipe ['x2'], swipe ['y2 '] = 320,410,320,410 screenshot_backup_dir = 'screenshot _ backups/' if not OS. path. 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 = process. stdout. read () if sys. platform = 'win32 ': SC Reenshot = screenshot. replace (B '\ r \ n', B' \ n') f = open('autojump.png ', 'wb') f. write (screenshot) f. close () def backup_screenshot (ts): # debug if not OS in case of failure. path. isdir (screenshot_backup_dir): 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) # pair Add the detailed comment draw to the debug image. 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]) and 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): # Set swipe to the global swipe_x1, swipe_y1, swipe_x2, swipe_y2 w, h = im. size left = w/2 top = 1003 * (h/1280.0) + 10 swipe_x1, swipe_y1, swipe_x2, swipe_y2 = left, top, left, topdef 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 press 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) # convert the color pattern HSV 2rgbdef HSV 2rgb (h, s, v): h = float (h) s = float (s) v = float (v) h60 = h/60.0 h60f = math. floor (h60) hi = int (h60f) % 6 f = h60-h60f p = v * (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 eli F hi = 5: r, g, B = v, p, q r, g, B = int (r * 255), int (g * 255 ), int (B * 255) return r, g, B # convert color mode 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, vdef find_piece_and_board (im): w, h = im. size piece_x_sum = 0 piece_x_c = 0 rows = 0 board_x = 0 board_y = 0 left_value = 0 left_count = 0 right_value = 0 right_count = 0 rows = 0 rows = 0 scan_x_border = int/ 8) # scan_start_y = 0 # Start y coordinate im_pixel = im. load () # in the 50px step, try to detect scan_start_y for I 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] # if it is not a solid color line, record the value of scan_start_y and prepare to jump out of the loop if pixel [0]! = Last_pixel [0] or pixel [1]! = Last_pixel [1] or pixel [2]! = Last_pixel [2]: scan_start_y = I-50 break if scan_start_y: break print ('Scan _ start_y: ', scan_start_y) # Start scanning from scan_start_y, the pawn pieces should be located in the upper part of the screen. It is tentatively set to not exceed 2/3 for I in range (scan_start_y, int (h * 2/3): for j in range (scan_x_border, w-scan_x_border ): # In terms of abscissa, it also reduces part of scanning overhead pixel = im_pixel [j, I] # based on the color of the lowest row of the pawnpiece, find the average of the vertices in the last row, 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 = piece_y_max-piece_base_height_1_2 # half of the height of the move-up chess piece chassis for I in range (int (h/3 ), int (h * 2/3): last_pixel = im_pixel [0, I] # Calculate the RGB value of the shadow and observe it through photoshop, the Shadow part is actually the brightness of the background color V multiplied by 0.7 h, s, v = rgb2hsv (last_pixel [0], last_pixel [1], last_pixel [2]) r, g, B = HSV 2rgb (h, s, v * 0.7) if from_left_find_board_y and 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] # if abs (j-piece_x) <piece_body_width: continue # small bug caused by a line when the dome is repaired. This color should be OK. if abs (pixel [0]-last_pixel [0]) is not mentioned for the time being. + abs (pixel [1]-last_pixel [1]) + abs (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, scan from left to right, find the first pixel that is different from the background color, record position # When there are three identical records in a row, a straight line is found # This line is the left edge of the target board # And the current y value is reduced by 3 to get the first pixel of the left edge # It is the left Vertex on the top for j in range (w): pixel = im_pixel [j, I] # bug where the head is higher than the next Small Grid 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])> 10) and (abs (pixel [0]-r) + abs (pixel [1]-g) + abs (pixel [2]-B)> 10): if left_value = j: left_count = left_count + 1 else: left_value = j left_count = 1 if left_count> 3: from_left_find_board_y = I-3 break # The logic is similar to the above, but the direction is from right to left # When there is occlusion, there is only one side of the block. # The two must be correct for j in range (w) [:-1]: pixel = im_pixel [j, i] # bug if abs (j-piece_x) where the head is higher than the next Small Grid <piece_body_width: continue if (abs (pixel [0]-last_pixel [0]) + abs (pixel [1]-last_pixel [1]) + abs (pixel [2]-last_pixel [2])> 10) and (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 there are many pixels on the top, the pattern is near-circular and the calculated value needs to increase, here, we tentatively increase the top width by 1/3 if board_x_c> 5: Fill = fill + board_x_c/3 from_right_find_board_y = from_right_find_board_y + board_x_c/3 # based on the actual angle, locate the coordinates close to the center of the next board. The angle here should be 30 °, the value should be tan 30 °, math. sqrt (3)/3 board_y = piece_y-abs (board_x-piece_x) * math. sqrt (3)/3 # extract two data from left and right for comparison, and select the value closer to the original algorithm if abs (board_y-from_left_find_board_y)> abs (from_right_find_board_y ): new_board_y = cancelse: new_board_y = returns if not all (board_x, board_y): return 0, 0, 0 return piece_x, piece_y, board_x, mongodump_device_info (): size_str = OS. popen ('adb shell wm size '). read () device_str = OS. popen ('adb shell getprop ro. product. model '). read () density_str = OS. popen ('adb shell wm density '). read () print ("If your script cannot work, copy the following information when reporting issue: \ n *********** \ nScreen: {size} \ nDensity: {dpi} \ nDeviceType: {type} \ nOS: {OS} \ nPython: {python} \ n **********". format (size = size_str.strip (), type = device_str.strip (), dpi = density_str.strip (), OS = sys. platform, python = sys. version) def check_adb (): flag = OS. system ('adb devices') if flag = 1: print ('Install adb and configure the environmental variable ') sys. exit () def main (): h, s, v = rgb2hsv (201,204,214) print (h, s, v) r, g, B = HSV 2rgb (h, s, v * 0.7) print (r, g, B) dump_device_info () check_adb () while True: pull_screenshot () im = Image. open ('. /autojump.png ') # obtain the position piece_x, piece_y, board_x, board_y = find_piece_and_board (im) ts = int (time. time () print (ts, piece_x, piece_y, board_x, board_y) set_button_position (im) jump (math. sqrt (board_x-piece_x) ** 2 + (board_y-piece_y) ** 2) terminate (ts, im, piece_x, piece_y, board_x, board_y) backup_screenshot (ts) time. sleep (3) # to ensure stability, wait for a while if _ name _ = '_ main _': main ()

Download source code: python source code

For more information, refer to the topic ".

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.