instructions

Brief description: this article will be 4399 small games “pet lianliankan classic version 2” as a test case, through the recognition of small ICONS, simulation mouse click, quickly complete pairing. It is helpful for students who are interested in learning game scripts.

Operating environment: Win10/Python3.5.

Main modules: Win32GUI (window recognition, window top operation), PIL (screen shot), NUMpy (create matrix), Operator (compare value), Pymouse (simulate mouse click).

Note:

1. If the Installation of PyMouse fails or runs incorrectly, consider installing pyHook via WHL first and then PyUserInput via PIP.

2, If (ImportError: No module named ‘Windows’) ImportError: No module named’ Windows’, change __init__.py to Windows => pymouse.windows.

The development process

General development idea: intercept the main picture of the game –> divide it into small pictures –> compare each small picture, compare the acquaintance degree of the picture, and store the number into the matrix –> perform continuous calculation on the matrix –> simulate click.




Gets the window handle and places the window at the top

Python can use the Win32GUI module to call the Windows API to operate on Windows. The FindWindow() method can be used to obtain the handle of the window. You need to pass in two arguments, the first is the parent window handle. The second parameter is the name of the window (tag title – Google Chrome). Get the handle and then set the window in front by SetForegroundWindows(), here pass in the game window report, the code is as follows:




Capture the game interface, split ICONS, compare pictures




According to the two coordinates of the upper left and lower right corner of the initialization, use imagegrab.grab () method to take a screenshot, pass in a tuple, then split the large image, cut it into small ICONS and store them in the images_list array.




Return the index if the icon is already stored in image_type_list, or append it if it doesn’t exist. Then the current length is the number of the newly added icon, as shown in the following code:




Here, hamming distance is used to judge the degree of recognition of two images. Set a threshold value of 10, if less than the threshold value, it is considered to be the same image, the specific code is as follows:




Program core – Icon connection algorithm (path finding)




The idea of the algorithm: Firstly, the path search is to find a coordinate set that can be directly connected horizontally and vertically, such as coordinate p1 (1,1) has [(0,1), (1,0)], and another coordinate p2 (1,10) has [(0,10)], and then compare the coordinate set of p1 and p2. If the coordinates in the set are also connectable, it means p1 and p2 are connectable. Obviously, (0,1) and (0,10) are on the same line and connectable, which means p1 and p2 have connectable paths, as shown in the code below:

Simple analysis of the code implementation process: When isReachable() receives two coordinate values that need to be compared, and then obtains the set of coordinates that can be connected by the two points (isRowConnect() and isColConnect()) respectively. Finally, the set is traversed and compared to see if any coordinate can be connected. If present, the two coordinates passed in are concatenable.