Course Content

I automated Dinosaur Game in Chrome

In today’s tutorial, we will learn how to “Automated Dinosaur Game in Chrome” by using python programming by importing some modules like pyautogui and image processing. PyAutoGUI let the Python program control the mouse and keyboard to automate interactions with other applications.

Prerequisite: The prerequisite of this project is the basic knowledge of python.

Dinosaur Game is one of the most popular game, where your goal is to jump over all boundaries to win the game. You have to control dinosaur to get more scores and win! This game is also known as T-Rex Game, or the NO INTERNET GAME, as it is one of the hidden Google games which originally can only be activated when you have no internet connection.

For this project, I am using Virtual Studio Code IDE. This tutorial is a part of python series for absolute beginners. If you have not watched my python tutorial, then click on the link below and start learning!

https://www.codewithharry.com/videos/python-tutorials-for-absolute-beginners-0

If you have ever tried a programming challenge, then you know they are fun. Accepting a coding challenge is an excellent way to improve your coding skills. If you like my work, make sure to check out my courses. Stay safe and keep yourself up to date with codewithharry.

 

Code as described/written in the video

import pyautogui # pip install pyautogui
from PIL import Image, ImageGrab # pip install pillow
# from numpy import asarray
import time

def hit(key):
    pyautogui.keyDown(key)
    return

def isCollide(data):
    # Draw the rectangle for birds
    for i in range(300, 415):
        for j in range(410, 563):
            if data[i, j] < 100:
                hit("down")
                return

    for i in range(300, 415):
        for j in range(563, 650):
            if data[i, j] < 100:
                hit("up")
                return
    return

if __name__ == "__main__":
    print("Hey.. Dino game about to start in 3 seconds")
    time.sleep(2)
    # hit('up') 

    while True:
        image = ImageGrab.grab().convert('L')  
        data = image.load()
        isCollide(data)
            
        # print(asarray(image))
        '''
        # Draw the rectangle for cactus
        for i in range(275, 325):
            for j in range(563, 650):
                data[i, j] = 0
        
        # Draw the rectangle for birds
        for i in range(250, 300):
            for j in range(410, 563):
                data[i, j] = 171

        image.show()
        break
      '''

Comments(14)

sagark 3 months, 1 week ago
Very well done harry bai mja aa gaya
suvam_roy 3 months, 1 week ago
there is a problem in the source code...in my case while running the code the dino always remains in down position
Ahtasham 2 months ago
Replace .keyDown() method by .keypress() method , then it will work
Sulaiman 3 months, 1 week ago
Regarding chrome dino. 
Great project sir. With reference to your code i made for my pc. Problem I am facing is dino collides after 400 or 500 score. Is it that computer is getting slow to check and jump or  i  have placed the box for checking the pixels too close to dino that till it jumps it is getting collided. This is happening after score of 400 or 500. Sometimes even goes till 700. Another major problem is this is not working when night mode comes. Code is missinterpretating the night black with black of cactus and bird. I believe this program is also possible without using down key. And to my surprise it works. Just needs to jump above the bird when bird is at mid and low height. And leave as it is when bird is at top height. There are around three heights.
But weird thing is your program does not fail in night mode. While my with almost same code just changed numbers doesnt work.
What is your active email. I emailed you once maybe you dont use it. The one from youtube channel description.
Please help.
shiny 2 months, 2 weeks ago
did u find the soln??
Ahtasham 2 months ago
import pyautogui
import time
from PIL import Image, ImageGrab
def hit(key):
    pyautogui.press(key)

def check_day():
    for i in range(250,260):
        for j in range(100,120):
            if data[i,j]>150:
                return "day"
            else:
                return "night"

def collide_1(data):
    for i in range(310,340):
        for j in range(400,450):
            if data[i,j]<100:
                return True
    return False

def collide_2(data):
    for i in range(310,340):
        for j in range(400,450):
            if data[i,j]>100:
                return True
    return False


time.sleep(3)
hit("space")
while True:
    image=ImageGrab.grab().convert("L")
    data=image.load()
    if check_day()=="day":
        if collide_1(data):
            hit("up")
    if check_day()=="night":
        if collide_2(data):
            hit("up")

This is the code for night mode also.
sunilcbpur 3 months, 1 week ago
You are awesome , you can share dinosoar game with separate article
maheshmeena 3 months, 1 week ago
Hello Harry bhai mene ye game ko automate krne ke liy program bnaya, program run hora h screenshots sab sahi hora h but ye game nahi khel rha..mene ye mac-book me bnaya h....please suggest me kya kru.
@aggarwal 2 months, 2 weeks ago
Harry bhai ye work nahi kar raha program. I have tried my best to solve it but it is not working properly. And instead of blindly copying your code I made my own by adjusting the position of rectangles for birds and cactus. Dino jumps 1 second after it get collide with cactus.Please see the program and then tell me. I have made this program ------
import pyautogui
from PIL import ImageGrab
import time


def hit(key):
    pyautogui.press(key)
    return


def is_collide(data):
    # Rectangle for birds
    for i in range(225, 265):
        for j in range(275, 378):
            if data[i, j] < 100:
                hit('down')
                return

    # Rectangle for cactus
    for i in range(225, 265):
        for j in range(380, 455):
            if data[i, j] < 100:
                hit('up')
                return
    return


if __name__ == "__main__":
    print("Hey...  Dino game is about to start in 3 seconds...")
    time.sleep(3)

    while True:
        image = ImageGrab.grab().convert('L')
        data = image.load()
        is_collide(data)
'''
        # Rectangle for cactus
        for i in range(225, 265):
            for j in range(380, 455):
                data[i, j] = 0

        # Rectangle for birds
        for i in range(225, 265):
            for j in range(275, 378):
                data[i, j] = 100

        image.show()
        break
'''
@aggarwal 2 months, 2 weeks ago
Ye kya hua saara code ek line me convert ho gaya. Saari indentation ki watt lag gayi.
@aggarwal 2 months, 2 weeks ago
I had adjusted this according to my laptop's screen
@aggarwal 2 months, 2 weeks ago
Aur harry bhai ek aur baat ------- agar ye jump bhi kar rha hai to sirf ek baar
suhaibarbi 2 months, 1 week ago
Harry bhai yeh smjh nae aaya k jab night time aata to yeh work kese kar raha ha jab k humnay isko black pe jump ya duck ki command de hoe h
Ahtasham 2 months ago
import pyautogui
import time
from PIL import Image, ImageGrab
def hit(key):
    pyautogui.press(key)

def check_day():
    for i in range(250,260):
        for j in range(100,120):
            if data[i,j]>150:
                return "day"
            else:
                return "night"

def collide_1(data):
    for i in range(310,340):
        for j in range(400,450):
            if data[i,j]<100:
                return True
    return False

def collide_2(data):
    for i in range(310,340):
        for j in range(400,450):
            if data[i,j]>100:
                return True
    return False


time.sleep(3)
hit("space")
while True:
    image=ImageGrab.grab().convert("L")
    data=image.load()
    if check_day()=="day":
        if collide_1(data):
            hit("up")
    if check_day()=="night":
        if collide_2(data):
            hit("up")

Resources

No downloadable resources for this video. If you think you need anything, please post it in the QnA!

Course Announcements

Any Course related announcements will be posted here