Easily create game bots with AutoIt
For this guide, we will detail how to create a bot for the game ‘Cookie Clicker’.
This guide was designed for total beginners to learn some programming concepts in a fun manner. For experienced programmers, the guide should provide a springboard for building projects with the AutoIt scripting language.
Prerequisite:
AutoIt is a Windows scripting language.
That means if you use macOS or Linux, this guide won’t work for you. If you want to do similar automation with those operating systems, try Selenium.
Step 1 — Install AutoIt
Download the full AutoIt package from the official website: https://www.autoitscript.com/site/autoit/downloads/
Here is a direct link.
This will contain the necessary packages to run AutoIt, and also a program SciTE where we will write the code.
Step 2 — Learn basics in SciTE editor
First, you should now have a program ‘SciTE Script Editor’. Open it.
Let’s create a new document. Select ‘File -> New’. Now we should have our new document.
We can name and save it with ‘File -> Save’, or CTRL+S. Let’s name it cookie_clicker_bot. The SciTE program will add the file extension ‘.au3’, which is used for AutoIT files.
Next, we want to launch the game.
In the SciTE menu, go to ‘Help’. This is at the end of the same top section as ‘File’. In the search box, search ‘ie’. With these tools, we can easily control an Internet Explorer window programmatically.
Find the example for ‘_IENavigate’. This shows how to navigate web pages.
; Create a browser window and navigate to a website,
; wait 5 seconds and navigate to another
; wait 5 seconds and navigate to another#include <IE.au3>Local $oIE = _IECreate("www.autoitscript.com")
Sleep(5000)
_IENavigate($oIE, "http://www.autoitscript.com/forum/index.php?")
Sleep(5000)
_IENavigate($oIE, "http://www.autoitscript.com/forum/index.php?showforum=9")
Copy this example to your file ‘cookie_clicker_bot’, and save. To run your program from the editor, hit F5.
Hopefully you got to see AutoIt in action :).
Next, let’s change to actually use the website of the game we are going to be botting. The website we want to go to is ‘https://orteil.dashnet.org/cookieclicker/’.
Before looking at the code below, try to update the code yourself.
#include <IE.au3>Local $oIE = _IECreate("https://orteil.dashnet.org/cookieclicker/")
When I run the above code, Internet Explorer starts in a small window. To launch a visible screen at max size, update the code to the following:
#include <IE.au3>Local $oIE = _IECreate("https://orteil.dashnet.org/cookieclicker/")
$HWND = _IEPropertyGet($oIE, "hwnd")
WinSetState($HWND, "", @SW_MAXIMIZE)
_IEAction($oIE, "visible")
_IELoadWait($oIE)
The code is starting to get more complicated. In order to clean it up, let’s create a function. Function’s allow us to define some piece of logic, and then call the function instead of all the code inside of it. Try it out:
#include <IE.au3>Func gotoCookieClicker()
Local $url = "https://orteil.dashnet.org/cookieclicker/"
Local $oIE = _IECreate($url)
$HWND = _IEPropertyGet($oIE, "hwnd")
WinSetState($HWND, "", @SW_MAXIMIZE)
_IEAction($oIE, "visible")
_IELoadWait($oIE)
EndFuncgotoCookieClicker()
Notice how first we create, or define the function. Then after, we call the function as ‘gotoCookieclicker()’.
In the next step, we will actually start to bot the game.
Step 3 — Learn to click on cookies
If you don’t know the game, it’s simple. You get points for clicking on the large cookie on the left, and you can purchase upgrades on the right.
Our bot will function by submitting repeated clicks to the cookie on the left side of the screen, and then spending those points in the store on the right.
In the ‘Help’ section, search for ‘Mouse Click’. You may find the following code in an example:
; Double click at the x, y position of 0, 500.
MouseClick($MOUSE_CLICK_LEFT, 0, 500, 2)
Try adding that to your code. It should click, probably not where you want though. We will use another program to solve this.
Open the program ‘AutoIt Window Info’. We will use this tool to find the position where we want to click.
Select the ‘Mouse Section’
Using the ‘Finder Tool’, drag the crosshair onto the target location.
We can use the X,Y coordinates given by the Finder Tool, and use that in our MouseClick function call.
Here is an example code that worked for my screen:
#include <IE.au3>Func gotoCookieClicker()
Local $url = "https://orteil.dashnet.org/cookieclicker/"
Local $oIE = _IECreate($url)
$HWND = _IEPropertyGet($oIE, "hwnd")
WinSetState($HWND, "", @SW_MAXIMIZE)
_IEAction($oIE, "visible")
_IELoadWait($oIE)EndFuncgotoCookieClicker()MouseClick($MOUSE_CLICK_LEFT, 275, 475)
One thing to note is that different size screens would have the game elements in different locations. To solve this and make the script portable across systems, you can use relative positioning, apposed to the absolute positioning we use here. You can research that outside this lesson :).
Well that’s cool, we can click one time. What if we want to click a bunch of times?
We could do the following:
#include <IE.au3>Func gotoCookieClicker()
Local $url = "https://orteil.dashnet.org/cookieclicker/"
Local $oIE = _IECreate($url)
$HWND = _IEPropertyGet($oIE, "hwnd")
WinSetState($HWND, "", @SW_MAXIMIZE)
_IEAction($oIE, "visible")
_IELoadWait($oIE)EndFuncgotoCookieClicker()MouseClick($MOUSE_CLICK_LEFT, 275, 475)
MouseClick($MOUSE_CLICK_LEFT, 275, 475)
MouseClick($MOUSE_CLICK_LEFT, 275, 475)
MouseClick($MOUSE_CLICK_LEFT, 275, 475)
MouseClick($MOUSE_CLICK_LEFT, 275, 475)
While this works okay, what if we wanted to do 10, 100, 1000 of these? Our code would be hard to read. We can use a technique called a ‘for loop’ :).
For $i = 1 to 10
MouseClick($MOUSE_CLICK_LEFT, 275, 475)
EndFunc
I prefer to add a slight delay to mouse clicks, to reduce glitches. The ideal delay time can vary depending on internet connection.
#include <IE.au3>Func gotoCookieClicker()
Local $url = "https://orteil.dashnet.org/cookieclicker/"
Local $oIE = _IECreate($url)
$HWND = _IEPropertyGet($oIE, "hwnd")
WinSetState($HWND, "", @SW_MAXIMIZE)
_IEAction($oIE, "visible")
_IELoadWait($oIE)
EndFuncgotoCookieClicker()For $i = 1 to 10
MouseClick($MOUSE_CLICK_LEFT, 275, 475)
sleep(100)
Next
We can very easily increase the number of times it will click. Now that we have the bot running, we need to program an important feature. The ability to kill the bot.
Add the following code to your script:
HotKeySet("{F1}", "myExit")Func myExit()
exit
EndFunc
Make sure that the ‘HotKeySet’ is at the top of the file. Now, you should be able to hit F1 to exit the bot.
What if we want it to continue to run until we tell it to stop? We can use a while loop for this.
The following example adds a while loop around the for loop.
HotKeySet("{F1}", "myExit")
#include <IE.au3>Func gotoCookieClicker()
Local $url = "https://orteil.dashnet.org/cookieclicker/"
Local $oIE = _IECreate($url)
$HWND = _IEPropertyGet($oIE, "hwnd")
WinSetState($HWND, "", @SW_MAXIMIZE)
_IEAction($oIE, "visible")
_IELoadWait($oIE)
EndFuncgotoCookieClicker()While 1
For $i = 1 to 10
MouseClick($MOUSE_CLICK_LEFT, 275, 475)
sleep(100)
Next
WEndFunc myExit()
exit
EndFunc
With just this, we now have a bot that will generate cookies forever!
In the next and final step, we will improve the bots logic so that it can generate cookies much faster.
Step 4 — Cookie Clicking Automaton
In order to improve the performance of the bot, we will have it spend the generated points in the in game store.
We add a function that achieves this.
HotKeySet("{F1}", "myExit")
#include <IE.au3>Func gotoCookieClicker()
Local $url = "https://orteil.dashnet.org/cookieclicker/"
Local $oIE = _IECreate($url)
$HWND = _IEPropertyGet($oIE, "hwnd")
WinSetState($HWND, "", @SW_MAXIMIZE)
_IEAction($oIE, "visible")
_IELoadWait($oIE)EndFuncgotoCookieClicker()While 1
For $i = 1 to 100
MouseClick($MOUSE_CLICK_LEFT, 275, 475)
sleep(100)
Next
spendCookies()
WEndFunc spendCookies()
MouseClick($MOUSE_CLICK_LEFT, 1630, 515) ; Click on cheapest item in store
sleep(100)
MouseClick($MOUSE_CLICK_LEFT, 1750, 620) ; Buy Cursor
sleep(100)
MouseClick($MOUSE_CLICK_LEFT, 1750, 680) ; Buy Grandma
sleep(100)
MouseClick($MOUSE_CLICK_LEFT, 1750, 745) ; Buy Farm
sleep(100)
MouseClick($MOUSE_CLICK_LEFT, 1750, 810) ; Buy Mine
sleep(100)
MouseClick($MOUSE_CLICK_LEFT, 1750, 870) ; Buy Factory
sleep(100)
EndFuncFunc myExit()
exit
EndFunc
After clicking 100 times, we spend the cookie points in the function spendCookies(). It should be noted that as you progress through the game, new clickable elements get added that can be further added to the above code.
To clean up the code, let’s put the rest of the logic into functions:
HotKeySet("{F1}", "myExit")
#include <IE.au3>
Opt("MouseClickDelay", 100) ;100 millisecondsFunc gotoCookieClicker()
Local $url = "https://orteil.dashnet.org/cookieclicker/"
Local $oIE = _IECreate($url)
$HWND = _IEPropertyGet($oIE, "hwnd")
WinSetState($HWND, "", @SW_MAXIMIZE)
_IEAction($oIE, "visible")
_IELoadWait($oIE)
EndFuncFunc generateCookies()
For $i = 1 to 100
MouseClick($MOUSE_CLICK_LEFT, 275, 475)
Next
EndFuncFunc spendCookies()
MouseClick($MOUSE_CLICK_LEFT, 1630, 515) ; Click on cheapest item in store
MouseClick($MOUSE_CLICK_LEFT, 1750, 620) ; Buy Cursor
MouseClick($MOUSE_CLICK_LEFT, 1750, 680) ; Buy Grandma
MouseClick($MOUSE_CLICK_LEFT, 1750, 745) ; Buy Farm
MouseClick($MOUSE_CLICK_LEFT, 1750, 810) ; Buy Mine
MouseClick($MOUSE_CLICK_LEFT, 1750, 870) ; Buy Factory
EndFuncFunc runBot()
gotoCookieClicker() While 1
generateCookies()
spendCookies()
WEnd
EndFuncFunc myExit()
exit
EndFuncrunBot()
A few changes can be seen:
- We added two new functions, generateCookies() and runBot().
- The bots code gets executed at that last line, runBot()
- The Opt(“MouseClickDelay”, 100) line allowed us to get rid of many sleep calls
That’s all for this project. I hope you were able to learn more about the AutoIt tool. — Ashton Shears