Python and mobile games: how to enjoy gaming more

Mostly it's mobile games that are just boring in some parts. Whether it's some constantly repetitive tasks or long waits for various upgrades. We can somehow speed all that up and skip the boring sections. This strategy of having to wait and perform annoying tasks to advance in the game is, in my opinion, a pretty clever way to extract money from players. In fact, you can often skip boring parts of the game simply by paying the developer. For example, you can use money to buy various accelerations that skip boring parts of the game. Paid deals are almost always worthwhile because developers make them profitable to make money. In some games, macros and various automations are disabled. The game we'll be making macros for is owned by a huge Chinese company called Lilith, and they don't care if someone cheats or not. It's called Rise of Kingdoms.

We can hardly do without a laptop or a computer. Actually, not at all... We will be working with the Python programming language, so if you don't have Python on your computer, you need to download it (ideally choose the latest version, which can be found here: https://www.python.org/downloads/. Along with Python, we will also need a package downloader for our project so we don't have to reinvent the wheel… For this purpose Pip will do, which we can download here: https://pip.pypa. io/en/stable/installation/

Now that we have Python and Pip, we should choose a suitable program to write the code. For Windows and Mac, just VS Code and if you have Linux you can use for example Vim or Nano. If you've decided on VS Code, set it up to your liking and we're good to go.

Finally, we need to install the game itself: https://rok.lilith.com/en . Create an account and go through the tutorial, which you can't avoid anyway. The tutorial explains the basic principles of the game.

Before we get started, here is a video demonstration of what we can create 🙂

Programming macros

We already have everything we need and now we can start creating the macro:
It is important to understand and take into account that the game can do something unexpected at any time and this can throw off the whole macro.

First we need to install all the packages, so we open a terminal. On Windows and Mac, this is Ctrl+Shift+; and on Linux, just open a regular terminal where your currently empty project folder is.

pip3 install pyautogui Pillow opencv-python
if this command does not work, use this command:
sudo pip3 install pyautogui Pillow opencv-python

In the folder of our project, we will create a file called main.py, for example. This file will house our first automation. One of the mechanics of the game Rise of Kingdoms is to collect raw materials by clicking on the farm or icon of the raw material, which is located above the farm.

Just click on one icon from each Resource to pick up the Resource.

We will use the installed library called pyautogui for the clicks. We can start adding code to our file like this:

In order for this macro to work properly, we need to create a folder called image and take a screenshot with the stone icon in it. After detection, a left button press will then be simulated in place of the image. If we want to click on specific coordinates on the screen, we can use the pyautogui.click() function and put the coordinates in the parentheses that we find using the program below. Just run the script and just get to the position whose coordinates we want to find out as quickly as possible. But we have to be patient, because the script will find the current coordinate of the cursor only after a time interval of three seconds has passed. We can adjust the number of seconds we want to wait by adjusting the value in the parentheses of the time.sleep(3) function, which in our case is 3.

Now that we know the coordinates we want the macro to click on, we can use them on any device, because for some reason the pyautogui library shows the same coordinates on all monitors and computers (at least the ones I've tested) . Just try it. My coordinates that the script will show when running when the cursor is at the very bottom left corner of the screen are x=1919 and y=1079. Well, you need to have a Full HD resolution (1920x1080), but this resolution should theoretically be able to be set on any device that is not a hundred years old. In short, when the computer tightens Python, it can change the resolution.

However, clicking the left button is not always enough, so the pyautogui library is capable of simulating keystrokes and even performing keyboard shortcuts. All other features can be found at pyautogui dokumentation.

Conclusion

Pyautogui along with OpenCV are great libraries that are perfect for these projects. If, after all, basic image recognition is not enough, we can train the so-called Image Recognition Model, for example. This is AI recognizing images using the images you show it, I like YOLOv8 the most, but more on that later!


Posted

in

by

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *