A.I Project
A.I Project
A.I Project
Technology
BUBT
Committed to Academic Excellence
Project Name.
Dino Gaming Bot
Course Title: Artificial Intelligence Lab
Code: CSE 319
Submitted To
Submitted By
Name ID Program: B.Sc.Engg. in
MD. Ashrafuzzaman ID:16173103136 CSE
Intake: 36
MD. Nazirul Islam ID:16173103041 Section: 01
1|Page
TABLE OF CONTENTS
2 BACKGROUND
2.1 Existing Systems 9
2.2 Problem Analysis 10
2.3 Supporting Theory 10
3 PROPOSED MODEL
3.1 Feasibility Analysis 12
3.2 Requirement Analysis 12
3.3 System Architecture 12
3.4 System Design 14
3.5 Implementation 14
4 IMPLEMENTATION AND TESTING
4.2 Result Analysis 17
4.3 Application Outcome 17
5 CONSTRAINTS AND ALTERNATIVES 18
2|Page
DECLARATION
We declare that this project and the work presented in it is our own and has been
generated by us as the result of our own original research. Sometimes I got help
from our honorable teacher and we also got help from internet.
3|Page
DEDICATION
Dedicate to our parents and siblings for all their love, support and
inspiration.
4|Page
ACKNOWLEDGEMENTS
Any attempt at any level cannot be satisfactorily completed without the support
and guidance of learned people. We would like to express our immense gratitude
to all BUBT teachers on computer programming for their constant support and
motivation that has encouraged us to come up with this project.
We are profoundly grateful to Dr. M. Firoz Mridha his expert guidance and
continuous encouragement throughout to see that this project rifts its target since
its commencement to its completion.
5|Page
ABSTRACT
Keywords
Deep Q-Learning; MLP; Feature Extraction with OpenCV
6|Page
Chapter 1 Introduction
1.1 Introduction
T-Rex run game is a game which we are sure every human with a phone or a
laptop with Google Chrome and no internet connection has played. For those of
you who do not know, just turn off your internet connection on your phone or
laptop and open any website using Google Chrome and you’ll be able to play T-
Rex run! The basic objective of the game is to jump or duck and dodge the
obstacles and keep your Dino alive. However, being humans we might get bored
or might get distracted and probably loose the game. In order to beat this game to
the fullest, we require someone with extreme concentration and someone who
cannot get distracted with anything in this world, someone like a BOT!
The Easter Egg is a dinosaur game, a simple infinite runner game that features
the Lonely T-Rex. Somehow got trapped in a desert, the T-Rex runs and has to
avoid obstacles like cactus and pterodactyls, with the game speeding up as
user’s progress further.
The controls are basic: using the spacebar to start the game when the dinosaur
first appears, and to jump when playing the game. To duck, they can use the
down arrow. For users on mobile can start the game by tapping the dinosaur (in
Android or iOS).
The goal of the game is to survive as long as they can, through days and nights
(the colors invert after users reach certain milestones) or at least until the
internet starts working again.
Chrome first released the game in September 2014. But since Google struggled
to make it work on certain platforms - particularly on older Android devices, the
developers did some tweaks and rewrites, and completed it by December that
year.
If ever Chrome users want to play the game when the internet is on, they don't
have to switch to Airplane Mode or disconnect their internet connection. They
can type in chrome://dino in Chrome's address bar to launch the game, anytime
they want.
This will take them to an arcade mode in Chrome, where they can play as the
Lonely T-Rex in full-window mode.
7|Page
The developers boast that the game can last for almost an eternity. Or 17 million
years to be exact. This is roughly how long the T-Rex was alive on Earth.
Dino Run Game project is composed purely in Python. The task documents
consist of image documents as well as a python script (main.py). GUI utilizes the
pygame library. Speaking about the gameplay, it is a cloned version of an offline
game played in google chrome calling “T-Rex Dino Run”. The main objective of
this mini-game is to rack up increasingly more point without being touched by
any kind of challenges. All the playing methods coincide. The customer needs to
play this straightforward game utilizing two Keyboard keys. Spacebar to leap as
well as Down arrowhead key to hide. In contrast to the initial one, this clone
variation has some modifications in the video gaming environment
Our main motive was to build an system that can act according to the
environment. And this is an era of modern tools and components. We use PIL
package to process our image and make a decision according to the environment.
In this game we have to jump when an object come, but if we think in real life we
can use it in a self-driving car. We just have to modify something and give the
idea of an environment to our bot and it will work accordingly. Our main motive
was it to take a baby step to do a big thing in future.
8|Page
Chapter 2 Background
9|Page
2.2 Problem Analysis
The basic logic what we will be following here in building the bot is to mimic
how a human plays the game. A human sees the obstacle and tries to time the
jump of the Dino to avoid the obstacle. For our bot, we will be doing the same.
Basically, here we take a screenshot of the T-Rex game screen and check the
position of a tree or a bird obstacle in that image and if the obstacle is close
enough to the Dino (defined using a threshold defined in the code), we ask the
Dino to jump.
The first step is to take the screen shot of the web page with the game and locate
the exact pixel values of the Dino. We find the top and bottom corner pixel values
of the Dino. This can be done by importing the image into paint.net and finding
the pixel values. Also, if you notice, the position of the Dino is constant and only
the obstacles are moving towards it. Hence we take this approach of creating an
action on the Dino based on its static position with respect to the obstacles moving
forward towards it. The below figure shows how we can get the pixel positions
of the Dino figure, using the rulers and the grid view. Usually the sum of all pixel
values in an image adds up to a number, and if the image does not change, the
sum always remains constant. We keep this in mind and I’ll explain why this is
important in the next few paragraphs.
Similarly, we take a screenshot of the game when a tree is present, a bird is present
and replay button is present and note down its pixel locations. This is not much
required as of now, but in the future can be helpful
I will be building this bot in python using image processing libraries. To start
with, you will be required to install Python 3.6 or above on your PC or laptop.
Here are the list of libraries you’ll be required to install —
PIL — PIL Library is used for taking screenshots of the screen and
applying image processing on it
10 | P a g e
ImageGrab — ImageGrab is a Python module that helps to capture the
contents of the system.
NumPy — This article will help you get acquainted with the widely used
array-processing library in Python, NumPy.
We then load our prerequisites into our code by the following snippet —
import time
from PIL import ImageGrab, ImageOps
import pyautogui
from numpy import *
11 | P a g e
Chapter 3 Proposed Model
The desire output of this project was defeat the game. But for our limited
resources and time it is not possible to defeat the game. We just built a system
to play the game as humans. So this system is not feasible.
PC
Paint.net
T-Rex Game
Snipping Tool
Google Chrome
Pycharm Community Edition 2019.3
12 | P a g e
Incremental model is also known as successive version model. First, a simple
working system implementing only a few basic features is built and then that is
delivered to the user.
In incremental model requirements of Software are first broken down into several
modules that can be incrementally constructed and delivered. At any time, the
plan is made just for the next increment and not for any kind of long term plans.
Therefore, it is easier to modify the version as per the need of the customer.
Development Team first undertakes to develop core features (these do not need
services from other features) of the system.
Once the core features are fully developed, then these are refined to increase
levels of capabilities by adding new functions in Successive versions. Each
incremental version is
13 | P a g e
3.4 System Design
3.5 Implementation
There are 2 different objects in this game — Dino and replay button if you crash.
You can use the pixel location values which I have used for each of these
characters. I have used a class definition hold onto these values for making it
simple. This is how to looks —
class Cordinates():
replayBtn = (341,386)
dinasaur = (370,390)
The next step is to actually write a logic for this code. We create different
functions for different processes or objectives.
First we create a function which would capture every frame or take screen shot
of every frame on the game in order to process it. Think it like how humans see
the game while playing it, the bot also needs to see the game using screenshots.
This is shown in the following snippet.
def imageGrab():
box = (190, 365,
280, 419)
image = ImageGrab.grab(box)
grayimage = ImageOps.grayscale(image)
a = array(grayimage.getcolors())
print(a.sum())
return a.sum()
14 | P a g e
In the above function imagegrab(), we get the coordinates of the Dino and create
a box around it. Basically the box is a rectangle with 4 pixel location values of
each corner, with the Dino inside the box. The box has some open space on right
side of the Dino. We then take a screen shot of the box and the box acts like our
logic for creating a jump or duck action. The basic logic here is that if a tree or a
bird comes inside the box, the Dino is asked to jump or duck respectively.
But bots actually cannot see what’s happening in the screenshot or precisely
inside the box. Bots can only see and read pixel. Hence, here as hinted in the
previous section, we calculate the sum of all pixels within the box. As the image
of the Dino and the box is constant and Dino is not moving, the sum of pixels in
the box always returns a constant. But if a tree or bird enters the box, the sum
Value actually increases or decreases based on the pixel values, and that is when
The bot gets a hint to either jump or duck. The below image displace the
screenshot or how the box would look.
Once we have the image of the Dino and the box, we convert the box into gray
scale to increase up our processing as color is not an important factor here as we
are just bothered with calculating the sum of the pixel values in the box.
The next function we write is the jump function, which is basically asks the bot
to jump when a tree appears. Puautogui library is used to simulate keyboard
controls which enables the bot to interact with the game.
def pressSapce():
pyautogui.keyDown('space')
print("jump")
time.sleep(0.18)
pyautogui.keyUp('space')\
15 | P a g e
The next function we write is to restart the game. If by chance the bot fails (very
low chance), it should automatically restart without human aid. We use the replay
button coordinates and simulate a click on that location. Here is the code snippet
for it.
def restartGame():
pyautogui.click(Cordinates.replayBtn)
Once done with all our functions, we are ready to write our main function and run
it. The below snippet shows the main function.
def main():
restartGame()
while True:
if (imageGrab() != 5107):
pressSapce()
time.sleep(0.1)
main()
16 | P a g e
Chapter 4 Implementation and Testing
SCORE
Score
600
521
500
390
400
300
187
200
120
100
0
1st Time 2nd Time 3rd Time 4th Time
17 | P a g e
Chapter 5 Constraints and Alternatives
For building this project we use the T-Rex Runner game from their website. Our
main motive is take an image from the monitor and process it. So we have to
calculate the grayscale value of the image, and make a decision how the keyboard
will work. So we have to place the game screen at a constant position every time.
We face problems to doing this. But after some time we overcome it and now
Every time our position of the screen is same.
When we take an image form our screen using imagegrab function we have to
find out all the needed pixel point to perfectly calculate the value by which out
bot will take decision. For this we use an online platform point.net, we have to
upload an image and note all the points of dinosaur tree and the box. But it is not
perfect because we don't know the exact value of the points. So we have to take
point many time and give it a try. The more we become close to the exact point
the more we come close to the desire output.
18 | P a g e
Chapter 6 Conclusion
6.1 Introduction
Several approaches were used to achieve the AI that can play Chrome Offline
Dinosaur Game. For the feature extraction based algorithm, computer version
methods can recognize the T-Rex and obstacles from the images. Carefully
designed feature extraction algorithms can successfully abstract the state and AI
built upon them can improve its performance significantly compared with naive
baseline. MLP learned from online training can strengthen the AI further for it
refines the parameters automatically by experience. However, feature-extraction
based algorithm have their limits and cannot outperform the human experts. For
end-to-end Deep-Q learning method, our result shows that it can successfully play
the game by learning straightly from the pixels without feature extraction, and is
much stronger than the feature-based method. Finally, specially designed training
method can help us overcome the training difficulties caused by the properties of
our game, which further improves our AI’s performance and helps achieve super-
human results.
In our project, neither MLP nor Deep Q-learning handles velocity well enough.
In Deep Q-learning, we notice velocity makes great impact over the jumping
position selection. During the process of training agent with different acceleration
and velocity, we observe that the agent tends to use policies incorrectly fitting the
current velocity when the relative speed changes. Two possible explanations are
as follows. Firstly, we simplify the computation in neural networks by resizing
the raw game image into 80×80 pixels. In this way, the edge of the obstacles
would be obscure which negatively influenced the prediction. Another problem
is that we use four images in stacking to infer the relative velocity based on their
differences. If more channels are added in the game image, more deviation would
be detected in both MLP and Deep Q-learning in order to capture the change of
velocity.
19 | P a g e