How To Make A Simple Game Bot

Make your first bot in 70 lines of code in Python

CyberBotMachines
4 min readJul 28, 2021

Before we begin — you must realize this tutorial is like creating an agent of the Matrix: super-human capabilities unmatched by any other person.

BUT

If you want to be like Neo and bend the rules of the Matrix then you will use only this one line:

Runner.prototype.gameOver = () => {}

You enter this one line into the browser console and you’re done. Basically, this code makes it impossible for a game to end — no matter how many obstacles you hit.

Now let’s continue with the tutorial.

We’re going to build a super simple bot that will play dinosaur jumping game in Chrome browser.

Default dinosaur game on Chrome when you’re offline

What are we going to use?

  • Python 3
  • a couple of simple libraries
  • about 70 lines of code

Also don’t forget to turn off your internet connection in order for dinosaur to appear in Chrome :)

Doing something with our BOT

In order for this to be any kind of bot it has to be able to do something without our input. So let’s give him arms and legs :)

First let’s import a library that gives us the ability to press keys and click around:

import pyautogui

If this library isn’t available on your machine then install it simply by:

pip3 install pyautogui

(The same process is for any other library that you don’t have installed)
Then you can use this library to press keys…

pyautogui.keyDown('space')
pyautogui.keyUp('space')

… or to click around:

pyautogui.click((960, 230))

The numbers inside are coordinates of the location you want to click on your screen.

Awesome! Now our bot has ability to do stuff on your computer.

Overall BOT logic

This is how the bot is going to work — we can distinguish 3 parts:

  1. “seeing” the screen
  2. detecting if there is an obstacle in front of the dinosaur
  3. jumping if obstacle was detected

1) “Seeing” the screen

We won’t be really watching the screen but we’ll take a screenshot and then analyze the image to detect the obstacle. And we’re going to do it many times per second.

import mss

Then you can save the screenshot with:

with mss.mss() as sct:
sct.shot(output='screen.png')

2) Detecting an obstacle

First we are going to load the image we previously saved so Python can work with it.

We need a library to load and manipulate images:

from PIL import Image, ImageOps

And now we load the image:

frame = Image.open('./screen.png')

Now we don’t actually need an entire image. We just want to detect if there is an obstacle in the small portion of the image that is located in front of the dinosaur. So we’ll just select that small portion:

origin_x = 730  # top left corner of the image section
origin_y = 200 # top left corner of the image section
size_x = 150 # section width
size_y = 50 # section height
# this is the selected image section
image_section = frame.crop((origin_x, origin_y, origin_x + size_x, origin_y + size_y))

Ok so how to recognize we have a cactus or a bird in front of us? If we really went that path we’d need some artificial intelligence with computer vision. That would definitely not fit into 70 lines of code. The solution is to be smart about it.

What can we generally say about the small section of the image in front of the dinosaur if there is an obstacle in front of it?

Image section that the bot is actually “looking” at

We could say that this entire image is dimmer / darker (on average) when there is an obstacle present in that frame.
And we will use this conclusion in our favor. So whenever this section of the image becomes darker we’ll say the obstacle is in front of us.

First we need to simplify our problem by turning our image into it’s colorless version:

gray_image_section = ImageOps.grayscale(image_section)

We’ll import one library that will help us calculate the average pixel value:

import numpy as np

Then:

# turn image into an array of numbers (0-255)
image_pixel_values = np.array(gray_image_section.getdata())
# get average value of those numbers
image_color_average = np.mean(image_pixel_values)

Just for reference: pure black color will have value of 0 and pure white value will have value of 255. In our example when there is no obstacle the average value is 251.

3) Jump if obstacle is detected

When obstacle is present the average value sometimes falls as low as 218 but not always. Sometimes it’s just 244.

So for simplicity we’ll allow some margin of error and if the average value falls below 248 we’ll jump.

if image_color_average < 248:
pyautogui.keyDown('space')
pyautogui.keyUp('space')

Congratulations

Now you have your first game bot :)

Also, full working code is below:

Wish you all the best,
Will
Senior Developer & SEO Algo Specialist

FYI — if you want to build a startup then learn to get FREE web traffic to your website. Because without web traffic you don’t have a startup — you have a dream.

--

--