Analysis of Automatic Code Execution and Analysis of WeChat code

Source: Internet
Author: User

Automatic Code Analysis and code analysis

The code analysis is automatically executed during a hop. The manual version is not mentioned here. Image Recognition and coordinate computing jump may cause a pain in your hands. This section mainly analyzes the automatic version, which is only available on Android. Here we only introduce the Android version.

Overall Structure

The overall structure of the script is concise, as shown in.

The mobile phone is connected to the PC. The PC runs the adb command on the mobile game interface. The PC copies the game interface to the PC using the adb command. The PC uses python to process the image (opencv used in the first version, currently, the pixel rgb values are read directly). Obtain the position of the chess piece, obtain the position of the next checker, and calculate the distance from the next hop to calculate the compression time t based on the experience value; the adb command can be used to simulate the press time t to achieve the jump of the pawn. Repeat 1 ~ 4. You can automatically execute the "one-Hop" game. Android mobile phone screen coordinates

Code profiling Main Function Code
Def main (): # Get device information dump_device_info () # Check adb check_adb () # main Loop while True: # Use adb and copy the image back to PC pull_screenshot () # load images into memory im = Image. open ('. /autojump.png ') # obtain the position piece_x, piece_y, board_x, board_y = find_piece_and_board (im) of the pawn and board # print the debugging information ts = int (time. time () print (ts, piece_x, piece_y, board_x, board_y) # Set the position of the press to [next round, in this case, you can automatically start set_button_position (im) # calculate the distance from the next hop, convert it to time based on experience, and send it to the device model through adb. Jump (math. sqrt (board_x-piece_x) ** 2 + (board_y-piece_y) ** 2) # To debug the debugging interface. And the debugging is performed to back up save_debug_creenshot (ts, im, piece_x, piece_y, board_x, board_y) backup_screenshot (ts) # In order to ensure the stability, wait for a while. sleep (random. uniform (1, 1.1 ))

Run the adb command and copy it back to the PC. Here, the adb command is used directly.

def pull_screenshot():    os.system('del autojump.png')    os.system('adb shell screencap -p /sdcard/autojump.png')    os.system('adb pull /sdcard/autojump.png .')
Coordinate Calculation

Find_piece_and_boardThe function is the core code. Here we mainly do two things: one is to calculate the position of a piece, and the other is to calculate the position of the next board. The main code is as follows.

1. Search for the coordinates of the pawns

# Here we are simply looking for a range. In fact, it is not a good choice, but it is a waste of time to traverse the entire screen. The author has done two things #1. first look for the screen 1/3 ~ 2/3 range, top-down #2. find the point that is different from the background color and stop it. It means that the pixel points starting from this height may be the pawns or the base 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 #0 1 2 in the pixel array is RGB, if there is a different one, the point is not the background color. if pixel [0]! = Last_pixel [0] or pixel [1]! = Last_pixel [1] or pixel [2]! = Last_pixel [2]: # Return 50 pixels up to avoid the above height being different from the top pixel scan_start_y = I-50 break # Jump out of the loop if scan_start_y: break # The author starts to traverse from top to bottom # scan down from scan_start_y. The pawns should be located in the upper part of the screen. It is tentatively set to be no more than 2/3 for I in range (scan_start_y, int (h * 2/3): # Remove a certain boundary from the abscissa and start traversing. This saves some time for j in range (scan_x_border, w-scan_x_border ): # retrieve the coordinate point pixel = im_pixel [j, I] # Here is the lowest line of the chess piece, which is determined by color, the range of RGB is obtained by the author in advance # based on the color of the lowest row of the pawnpiece, find the average of those points in the last row. This color should be OK, if (50 <pixel [0] <60) and (53 <pixel [1] <63) and (95 <pixel [2] <110 ): # The sum of the X-coordinate values of pixels, because the pawns are symmetric, the average of these abscissa is the central position of the pawnpiece. piece_x_sum + = j piece_x_c + = 1 # position of the lowest point of the pawnpiece. piece_y_max = max (I, piece_y_max, if not all (piece_x_sum, piece_x_c): return 0, 0, 0, 0 # obtain the abscissa piece_x = piece_x_sum/piece_x_c # the lowest point of the pawnpiece is not the center position of the pawnpiece. A value must be compensated, this value is half the height of the pawn chassis. piece_y = piece_y_max-piece_base_height_1_2

2. Find the coordinates of the next hop Chassis

# Similarly, you can narrow down the search scope and only search for the screen between 1/3 and ~ For I in range (int (h/3), int (h * 2/3) within the range of 2/3: # Take the coordinates on the boundary as the initial value of the Last coordinate. The function of this variable is to determine whether the pixel changes. if the change changes, it enters the base pixel last_pixel = im_pixel [0, I] # if the coordinates are calculated, it jumps out of the loop if board_x or board_y: break # is symmetric With the Search Component type and the base. The average value of the pixel horizontal coordinate is the center board_x_sum = 0 board_x_c = 0 # Of the base, horizontal scan for j in range (w): # retrieve a 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 # the RGB value of the pixel has changed, it indicates that the base pixel area is entered # A small bug caused by a line when the dome is repaired. The color judgment should be OK, if abs (pixel [0]-last_pixel [0]) + abs (pixel [1]-last_pixel [1]) + abs (pixel [2]-last_pixel [2])> 10: board_x_sum + = j board_x_c + = 1 # Calculate the abscissa of the base if board_x_sum: board_x = board_x_sum/board_x_c # the next base is in the 30-degree direction of the current base. Therefore, the ordinate coordinates of the base can be calculated based on the abscissa calculated above # calculated 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 # if one of the values is null, if not all (board_x, board_y) is returned: return 0, 0, 0, 0
Skip

If you know the coordinates of the current and next hops, you can calculate the distance between the two points.

math.sqrt((board_x - piece_x) ** 2 + (board_y - piece_y) ** 2)

Then pass the value to the Skip function.

# This function calculates the press time def jump (distance): press_time = distance * press_coefficient press_time = max (press_time, 200) Based on experience values) # Set 200 MS to be the smallest press time press_time = int (press_time) # Use adb transmission to simulate pressing command 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)

All in all, the code is simple and easy to read, thanks to the original author's selfless dedication, https://github.com/wangshub/wechat_jump_game

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.