Last time we talked about PyUserInput, a library that simulates mouse and keyboard clicks.
This game is known for exploding liver, why not write a simple script to free your hands. The goal is to be able to click a button when it should be clicked, in order to achieve the goal of auto-completion. There are two ways to determine the click position.
- Manually determine the corresponding key position;
- By collecting images of keys and comparing them with current screenshots, you can locate keys.
The first method has obvious drawbacks — it requires too much manual work. The second method is relatively intelligent. Let’s look at the second method in detail.
Screen capture
In order to capture the current state of the game in real time, a screen capture is needed, and ImageGrab in PIL library is used here. Obtain the size of the entire screen by simulating the mouse and take a full-screen screenshot.
Def screen_shot(self): X,Y= ImageGrab ((0,0,X,Y) img.save('now.png')) def screen_shot(self): X,Y= ImageGrab ((0,0,X,Y) img.save('now.png')Copy the code
The picture than
The OpenCV library is used for image comparison, and the key material is compared with the current real-time screen shots. If the similarity is greater than the specified threshold, the material is judged to exist in the current screen, and the location of the material in the current screen is returned.
def get_location(self,template_pic): img=cv2.imread('now.png') template=cv2.imread(template_pic) x=template.shape[1] y=template.shape[0] res=cv2.matchTemplate(img,template,cv2.TM_CCOEFF_NORMED) loc=np.where(res>=self.threshold) if len(loc[0])! = 0: self.flag2=True loc_x=loc[1][0] loc_y=loc[0][0] scale_x=(loc_x,(loc_x+x)) scale_y=(loc_y,(loc_y+y)) return scale_x,scale_yCopy the code
The coordinate range is returned in order to be able to randomly select the key area for subsequent click.
Click on the simulation
In the specified range of random selection click, more close to the real operation.
def random_click_1(self,scale_x,scale_y): X = random. Randint (scale_x [0], scale_x [1]) y = random. The randint (scale_y [0], scale_y [1]) self. M.m ove (x, y) self. M.c lick (x, y, 1, 1)Copy the code
The program logic
Depending on the task that needs to be done, we can easily write the corresponding logical code using the above technique of getting the location of a given button and randomly clicking it.
PS: this article is only for technical exchange, do not use it for illegal purposes!!