How To Make A Simple Game Bot

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


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…


… or to click around:, 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:

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 ='./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


# 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:


Now you have your first game bot :)

Also, full working code is below:

Programming is a stressful career

If you plan to work as a software engineer in the long run make sure you don’t overwork yourself.

From personal experience I know it can literally “transform” your career.

How did that happen you ask?

Well, if you continually push yourself more and more… way beyond what is comfortable… eventually you’ll burnout completely.

And then there’s no going back.

So make sure you take care of yourself and your health because no deadline is really that urgent or important.

If you want to know more about my journey through programmer burnout then check out the blog post on the link above.

Wish you all the best,
Senior Developer Content Writer




Senior Developer Technology Blog Post Writer

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

7 Ways to Save ERP Implementation Costs and Reduce Risks

Importing your csv into elasticsearch using logstash

Perseverance — How I got a Full Stack Developer internship at Generation Blockchain.

Authorization callbacks in Swift using Combine

colab with pyspark

Face Recognizing Using Python and AWS.

Software Is Only A Tool

Energy-saving HDPE Solid Wall Pipe High-speed Extrusion Machine

Energy-saving HDPE Solid Wall Pipe High-speed Extrusion Machine

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store


Senior Developer Technology Blog Post Writer

More from Medium

Starting a project and app in Django

How do consumer businesses benefit from API usage?

Forex, Small Businesses, Trading, Crypto, Market Data

How to build a simple blog with your mobile phone using Django

The 10 Steps to Create an Online Store for Beginners

The 10 Step by Step Guide to Create an Online Store For Beginners