Automating Telegram Games
We all know the feeling when someone breaks your hard-earned highscore as if it was nothing. In this blog post, I want to show you how you can hack the game logic of the popular Telegram game ‘Lumberjack’ using Python and be the best among your friends for good.
What is Lumberjack?
Lumberjack is one out of three small browser games which you can play using the “@gamebot” function in the messenger App Telegram. The game logic is quite simple. You just have to avoid being hit by a tree branch by clicking on the left or right arrow key. For each tree part you are cutting down you earn one point and some additional time. A time bar on the top mid displays the time which is left for taking a move. The more points you have, the faster the time runs down. The goal is to cut down more tree branches than any other person. The sky is the limit but the time will kill you anyway.
[rl_gallery id=”356″]
Automating the game logic
Despite the game logic itself being simple, writing an automatic bot is not. You will see that there are some limitations e.g. the browser, the game speed and motion problems which can lead to several difficulties along the way. There are probably many approaches how to hack such a game, but in the following I would like to introduce a technique that was the most promising to me.
The first point, if you want to automate any game, is to analyze it properly. Therefore you should check the general game logic and all included components. For Lumberjack this can be done by simply taking a screenshot of the game and all of its components. Afterwards we check e.g. the pixel tree branch color, the color of the clouds (background) and one pixel on the player to determine his start position.
What you may already noticed is that the tree branch distance is always the same. For me it was exactly 100 pixel on a split screen with Full HD resolution (1920×1080), so left side the game and on the right side my IDE. The following image gives a rough impression on how such a game analysis could look like. What’s not mentioned here is that in case there is a tree branch switch from left to right or vise versa you can cut at least two pieces. This means a direct switch from one side to the other with just one cut does not exist.
With all this information we can write a simple Python program, based on Python 3.8, with less than 60 lines of code that automates the whole process and leads to an outstanding highscore (currently 607 points, with the Chrome browser).
The first thing what we do is analyzing the next moves of the player. This happens by simply checking all possible positions of branches on one side. What we save is where the player should move or more precisely the arrow key which should be pressed to avoid the tree branch. After the execution of the sequence of key combinations, we start over by checking again the pixel positions of the tree branches. This process is followed until we reach the desired score which has been entered via command line (default 1337).
There are two variables which can still be adjusted: the SLEEP_TIME
and KEY_PRESS_INTERVAL
. SLEEP_TIME
pauses the automation so that we are able to check the pixel positions of the tree branches. KEY_PRESS_INTERVAL
handles the duration between to key clicks. Both can be adapted to get a better score and make the process much faster, however lowering the sleep time can lead to problems. One example is that the game animation might be too slow which results in a bad behavior by checking the tree branch pixels. Since the pixel can not be checked properly a wrong move may be performed leading to game over. The best way is to try it out by yourself or use a Neural Network to analyze the perfect values for your SLEEP_TIME
and KEY_PRESS_INTERVAL
.
That’s all about the Lumberjack automation. If you are interested in more check out our Projects page or subscribe to our newsletter. Thanks for reading and stay tuned for new upcoming Blog entries.
Cheers,
Christoph Kreisl
CodevemberTeam 2020
All links collected:
Telegram Gamebot: https://t.me/gamebot
Source code on Gitlab: https://gitlab.com/-/snippets/2012503