Python Bot for a Flashgame

Sun 26 August 2012
By balrok

Inspired by a Youtube-Video how to play flashgames using python I wanted to try it myself. This post is less about the code and more about the experiences I got.


Since computers are designed to help us in every day tasks.. they should also help us in gaming tasks. My first bot-attempt was for a web-based online game which was quite unsuccessful since I didn't understood the HTTP-Protocol. My next try was a bit more successful a bash-based bot for WoW which just sent some keyboard and mouse-events to the screen.. This one worked quite well but was far away from being perfect (especially on a PvP Server). So now my 3rd try..

What game

The games name is Anti-Idle. The purpose is to get levelups without doing anything (like progress quest). But since it is ANTI-Idle you can also actively do things to increase your level.. One of those things is to click a button which moves with each click.

The code

Since it is too long to post here and is still not in a good shape I publish it only on my github repo: here

The difficulties

Access to the gui isn't really supported inside python.. You can't simply take a screenshot without a bigger library and also there doesn't exist a cross-plattform mouse-api. For the screenshots I use pygtk (no reason for this choice). And for the mouse there is a great PyMouse module which wraps around various apis.

On the other hand to work in 2D was quite new for me and debugging was very hard.. I often ended up with saving all screenshots and try to identify the mistake there.. Also I noticed that I lack some math which would be quite helpful.. Like calculating the angle of the button or getting the button center if one point is known..

The code itself

I decided to make a bigger project of this (but too much work lately took all my motivation away) so some things are a bit too much abstracted.. On the other hand I wrote the code in multiple short sessions which caused the code to be quite redundant and not so well designed - sorry for that.. Maybe a cleanup follows some day :)

First I make a screenshot of the whole screen. Then I search for color #333333 which is the kongregate border around games - I then proceed to find all corners of this game area.. In all further Steps I only take a screenshot of the game-area which is probably a bit faster. Then I search the button by it's color inside this game-area and get two points inside the button-area to find later, if the button is broken or not. From the button I determine the center, click do a screenshot and everything repeats.. if the button is broken, the background color changes and since I have 2 points in the area, I notice this change and can click repair.


I was quite unhappy about how hard it is to determine positions of stuff and thought it would be somewhat easier to read the text on the screen.. I guess the way to go is to use a mix of absolute positioning to find buttons and some searching inside the image. My Idea is to have the bot farming the trees, reloading the battery, buying upgrades and maybe some more things.. But currently I'm considering if this is really worth the effort.. so maybe this will later again count as one of my unsuccessful attempts for writing a bot \^\^


I've found the cool library autopy which does keyboard,mouse,screengrabbing. With this library my code got a bit simplified (through not having 3 different dependencies). Also the code runs much faster 200 clicks before took 4 seconds and now 1 second. Although the flashgame doesn't recognize each click.