Automating Mouse Clicks. A Lazy (Smart) Way to Play Clicker Games.

Recently, I came across a Python package developed by Al Sweigart called PyAutoGUI (https://pyautogui.readthedocs.io/en/latest/index.html). It is a very interesting package as it allows the user to automate common functions like keyboard and mouse actions, obtain the coordinates of a certain pixel on your screen and its RGB colour, among other functions. Not sure how helpful this will be, but this gave me an idea to apply a simple Python code for ‘clicker games’.

A ‘clicker game’, or sometimes called ‘incremental game’, is one where the player performs a simple action (e.g. clicking on the screen repeatedly) to earn some in-game currency that could be used for upgrades that in turn generate more in-game currency.

There would be the initial phase where the player is required to click manually to get the first amount of in-game currency before purchasing that first upgrade to automate future earnings (albeit slowly…). Thus, why not use this Python package to make that a little easier? We will use the ‘Cookie Clicker’ game (https://orteil.dashnet.org/cookieclicker/) as an example in this post.

We will first access the website and then fixed the position of the browser window on the desktop. Next, the pixel coordinate of the point you wish to click has to be determine. The following code does just that and it can be saved as ‘pixel coordinate.py’, for example.

Line 1 imports the PyAutoGUI package, and Line 2 imports the time module that will be used to introduce some delay between each readout.
Line 6 maps the screen resolution to pyautogui. A typical laptop should have a screen resolution of 1366 by 768, but do check your display settings to confirm.
Line 10 to 16 uses a while loop to return the pixel coordinate of where the mouse cursor is pointing, with a delay of 2.5 seconds between each readout. A try-except conditional is used to break out of this while loop such that the code could be stopped by raising a KeyboardInterrupt error, after the desired pixel coordinate info has been obtained.

Once the above code is running, just bring up the browser window with the ‘Cookie Clicker’ game and position the mouse cursor over the point that you wish to click. Note down the pixel coordinates shown on the IPython console. As the mouse cursor is moved, the new pixel coordinate will be output.

Since the cookie icon is so huge, it is unlikely that any inaccuracy with the pixel coordinate would create a problem, unless the position of the browser window is changed significantly.

Next, another Python code would be required to move the mouse cursor to the required pixel coordinate and call the left-click action repeatedly. This could be achieved with the short code as shown below – this is the code that will effect the automated clicking.

Line 10 and 11 imports the required Python modules.
Line 12 to 14 uses a for loop to achieve that repetitive left-click action on the mouse. In this case, the loop is repeated 50 times.
Line 13 moves the mouse cursor to the desired position (x-y coordinates of 320, 500 in this case), and calls the click action.
Line 14 then introduces a slight pause in between the clicks.

The number of automated clicks (i.e number of for loop iterations, Line 12) can be changed to suit the player’s need accordingly.

Note: The PyAutoGUI module has an in-built safety feature such that should the code goes into an endless loop, you can push the mouse cursor to the top-left corner of the screen and the code would be forced to terminate.

Is there any way to make the above codes more concise and elegant? Feel free to comment.

Leave a comment