Hi, Hackernoon peeps! Today I will show you guys, how to build one of the most popular games that almost everyone plays while they are offline. If your guessing which game then I am talking about Google Chrome's Dino game. But, Our Dino game is a modified form of Chrome's version as its colored. Now let's go directly on to the tutorial to create a dino run game in python. Requirements: A PC with any OS installed in it. The latest version of Python which can be easily downloaded from https://www.python.org/downloads/[For installing you can check ] this blog A good Code IDE [ Prefer using VS Code or Atom] Your focus Tutorial: Firstly, open your cmd[Command Prompt] and type the following command after installing python from https://pypi.org/project/pygame/ Secondly, copy the following codes provided along with resources that you can get in the download button below. pip install pygame pip install random2 Lastly, run the python code with command.[Here, main.py is the file name]. That's it! python main.py Source Code: __author_ = import os import sys import pygame import random from pygame import * pygame.init() scr_size = (width,height) = ( , ) FPS = gravity = . black = ( , , ) white = ( , , ) background_col = ( , , ) high_score = screen = pygame.display.set_mode(scr_size) clock = pygame.time.Clock() pygame.display.set_caption( ) jump_sound = pygame.mixer.Sound( ) die_sound = pygame.mixer.Sound( ) checkPoint_sound = pygame.mixer.Sound( ) : fullname = os.path.join( , name) image = pygame.image.load(fullname) image = image.convert() colorkey is colorkey is - : colorkey = image.get_at(( , )) image.set_colorkey(colorkey, RLEACCEL) sizex != - sizey != - : image = pygame.transform.scale(image, (sizex, sizey)) (image, image.get_rect()) : fullname = os.path.join( ,sheetname) sheet = pygame.image.load(fullname) sheet = sheet.convert() sheet_rect = sheet.get_rect() templates = [] sizex = sheet_rect.width/nx sizey = sheet_rect.height/ny i range( ,ny): j range( ,nx): rect = pygame.Rect((j*sizex,i*sizey,sizex,sizey)) image = pygame.Surface(rect.size) image = image.convert() image.blit(sheet,( , ),rect) colorkey is colorkey is - : colorkey = image.get_at(( , )) image.set_colorkey(colorkey,RLEACCEL) scalex != - scaley != - : image = pygame.transform.scale(image,(scalex,scaley)) templates.append(image) sprite_rect = templates[ ].get_rect() templates,sprite_rect : retbutton_rect = retbutton_image.get_rect() retbutton_rect.centerx = width / retbutton_rect.top = height* . gameover_rect = gameover_image.get_rect() gameover_rect.centerx = width / gameover_rect.centery = height* . screen.blit(retbutton_image, retbutton_rect) screen.blit(gameover_image, gameover_rect) : number > - : digits = [] i = (number/ != ): digits.append(number% ) number = int(number/ ) digits.append(number% ) i range(len(digits), ): digits.append( ) digits.reverse() digits : .images, .rect = load_sprite_sheet( , , ,sizex,sizey,- ) .images1, .rect1 = load_sprite_sheet( , , , ,sizey,- ) .rect.bottom = int( . *height) .rect.left = width/ .image = .images[ ] .index = .counter = .score = .isJumping = False .isDead = False .isDucking = False .isBlinking = False .movement = [ , ] .jumpSpeed = .stand_pos_width = .rect.width .duck_pos_width = .rect1.width : screen.blit( .image, .rect) : .rect.bottom > int( . *height): .rect.bottom = int( . *height) .isJumping = False : . .movement[ ] = .movement[ ] + gravity . .index = elif . .index == : .counter % == : .index = ( .index + )% .counter % == : .index = ( .index + )% elif . .counter % == : .index = ( .index + )% .counter % == : .index = ( .index + )% + . .index = . .image = .images[ .index] .rect.width = .stand_pos_width .image = .images1[( .index)% ] .rect.width = .duck_pos_width .rect = .rect.move( .movement) .checkbounds() .isDead .counter % == .isBlinking == .score += .score % == .score != : pygame.mixer.get_init() != checkPoint_sound.play() .counter = ( .counter + ) : pygame.sprite.Sprite.__init_ ( , .containers) .images, .rect = load_sprite_sheet( , , ,sizex,sizey,- ) .rect.bottom = int( . *height) .rect.left = width + .rect.width .image = .images[random.randrange( , )] .movement = [- *speed, ] : screen.blit( .image, .rect) : .rect = .rect.move( .movement) .rect.right < : .kill() : pygame.sprite.Sprite.__init_ ( , .containers) .images, .rect = load_sprite_sheet( , , ,sizex,sizey,- ) .ptera_height = [height* . ,height* . ,height* . ] .rect.centery = .ptera_height[random.randrange( , )] .rect.left = width + .rect.width .image = .images[ ] .movement = [- *speed, ] .index = .counter = : screen.blit( .image, .rect) : .counter % == : .index = ( .index+ )% .image = .images[ .index] .rect = .rect.move( .movement) .counter = ( .counter + ) .rect.right < : .kill() : .image, .rect = load_image( ,- ,- ,- ) .image1, .rect1 = load_image( ,- ,- ,- ) .rect.bottom = height .rect1.bottom = height .rect1.left = .rect.right .speed = speed : screen.blit( .image, .rect) screen.blit( .image1, .rect1) : .rect.left += .speed .rect1.left += .speed .rect.right < : .rect.left = .rect1.right .rect1.right < : .rect1.left = .rect.right : pygame.sprite.Sprite.__init_ ( , .containers) .image, .rect = load_image( ,int( * / ), ,- ) .speed = .rect.left = x .rect.top = y .movement = [- * .speed, ] : screen.blit( .image, .rect) : .rect = .rect.move( .movement) .rect.right < : .kill() : .score = .tempimages, .temprect = load_sprite_sheet( , , , ,int( * / ),- ) .image = pygame.Surface(( ,int( * / ))) .rect = .image.get_rect() x == - : .rect.left = width* . .rect.left = x y == - : .rect.top = height* . .rect.top = y : screen.blit( .image, .rect) : score_digits = extractDigits(score) .image.fill(background_col) s .image.blit( .tempimages[s], .temprect) .temprect.left += .temprect.width .temprect.left = : temp_dino = Dino( , ) temp_dino.isBlinking = True gameStart = False temp_ground,temp_ground_rect = load_sprite_sheet( , , ,- ,- ,- ) temp_ground_rect.left = width/ temp_ground_rect.bottom = height logo,logo_rect = load_image( , , ,- ) logo_rect.centerx = width* . logo_rect.centery = height* . pygame.display.get_surface() == print( ) True event pygame.event.get(): event.type == pygame. True event.type == pygame. event.key == pygame.K_SPACE event.key == pygame. temp_dino.isJumping = True temp_dino.isBlinking = False temp_dino.movement[ ] = - *temp_dino.jumpSpeed temp_dino.update() pygame.display.get_surface() != screen.fill(background_col) screen.blit(temp_ground[ ],temp_ground_rect) temp_dino. screen.blit(logo,logo_rect) temp_dino.draw() pygame.display.update() clock.tick(FPS) temp_dino.isJumping == False temp_dino.isBlinking == gameStart = True : global high_score gamespeed = startMenu = False gameOver = False gameQuit = False playerDino = Dino( , ) new_ground = Ground(- *gamespeed) scb = Scoreboard() highsc = Scoreboard(width* . ) counter = cacti = pygame.sprite.Group() pteras = pygame.sprite.Group() clouds = pygame.sprite.Group() last_obstacle = pygame.sprite.Group() Cactus.containers = cacti Ptera.containers = pteras Cloud.containers = clouds retbutton_image,retbutton_rect = load_image( , , ,- ) gameover_image,gameover_rect = load_image( , , ,- ) temp_images,temp_rect = load_sprite_sheet( , , , ,int( * / ),- ) HI_image = pygame.Surface(( ,int( * / ))) HI_rect = HI_image.get_rect() HI_image.fill(background_col) HI_image.blit(temp_images[ ],temp_rect) temp_rect.left += temp_rect.width HI_image.blit(temp_images[ ],temp_rect) HI_rect.top = height* . HI_rect.left = width* . pass pygame.display.get_surface() == print( ) gameQuit = True gameOver = True event pygame.event.get(): event.type == pygame. gameQuit = True gameOver = True event.type == pygame. event.key == pygame. playerDino.rect.bottom == int( . *height): playerDino.isJumping = True pygame.mixer.get_init() != jump_sound.play() playerDino.movement[ ] = - *playerDino.jumpSpeed event.key == pygame. (playerDino.isJumping playerDino.isDead): playerDino.isDucking = True event.type == pygame. event.key == pygame. playerDino.isDucking = False c c.movement[ ] = - *gamespeed pygame.sprite.collide_mask(playerDino,c): playerDino.isDead = True pygame.mixer.get_init() != die_sound.play() p p.movement[ ] = - *gamespeed pygame.sprite.collide_mask(playerDino,p): playerDino.isDead = True pygame.mixer.get_init() != die_sound.play() len(cacti) < : len(cacti) == : last_obstacle.empty() last_obstacle.add(Cactus(gamespeed, , )) l l.rect.right < width* . random.randrange( , ) == : last_obstacle.empty() last_obstacle.add(Cactus(gamespeed, , )) len(pteras) == random.randrange( , ) == counter > : l l.rect.right < width* . : last_obstacle.empty() last_obstacle.add(Ptera(gamespeed, , )) len(clouds) < random.randrange( , ) == : Cloud(width,random.randrange(height/ ,height/ )) playerDino.update() cacti.update() pteras.update() clouds.update() new_ground.update() scb.update(playerDino.score) highsc.update(high_score) pygame.display.get_surface() != screen.fill(background_col) new_ground.draw() clouds.draw(screen) scb.draw() high_score != : highsc.draw() screen.blit(HI_image,HI_rect) cacti.draw(screen) pteras.draw(screen) playerDino.draw() pygame.display.update() clock.tick(FPS) playerDino. gameOver = True playerDino.score > high_score = playerDino.score counter% == : new_ground.speed -= gamespeed += counter = (counter + ) pygame.display.get_surface() == print( ) gameQuit = True gameOver = False event pygame.event.get(): event.type == pygame. gameQuit = True gameOver = False event.type == pygame. event.key == pygame. gameQuit = True gameOver = False event.key == pygame.K_RETURN event.key == pygame. gameOver = False gameplay() highsc.update(high_score) pygame.display.get_surface() != disp_gameOver_msg(retbutton_image, gameover_image) high_score != : highsc.draw() screen.blit(HI_image, HI_rect) pygame.display.update() clock.tick(FPS) pygame.quit() quit() : isGameQuit = introscreen() gameplay() main() _ "Tripura Coders" 600 600 60 0 6 0 0 0 255 255 255 235 235 235 0 "Dino Run by Tripura Coders" 'templates/jump.wav' 'templates/die.wav' 'templates/checkPoint.wav' def load_image ( name, sizex=- , sizey=- , colorkey=None, ) 1 1 'templates' if not None: if 1 0 0 if 1 or 1 return def load_sprite_sheet ( sheetname, nx, ny, scalex = - , scaley = - , colorkey = None, ) 1 1 'templates' for in 0 for in 0 0 0 if not None: if 1 0 0 if 1 or 1 0 return def disp_gameOver_msg (retbutton_image,gameover_image) 2 0 52 2 0 35 def extractDigits (number) if 1 0 while 10 0 10 10 10 for in 5 0 return (): class Dino def __init__ ( ,sizex=- ,sizey=- ) self 1 1 self self 'dino.png' 5 1 1 self self 'dino_ducking.png' 2 1 59 1 self 0 98 self 15 self self 0 self 0 self 0 self 0 self self self self self 0 0 self 11.5 self self self self def draw ( ) self self self def checkbounds ( ) self if self 0 98 self 0 98 self def update ( ) self if self isJumping: self 1 self 1 if self isJumping: self 0 self isBlinking: if self 0 if self 400 399 self self 1 2 else: if self 20 19 self self 1 2 self isDucking: if self 5 0 self self 1 2 else: if self 5 0 self self 1 2 2 if self isDead: self 4 if not self isDucking: self self self self self else: self self self 2 self self self self self self if not self and self 7 6 and self False: self 1 if self 100 0 and self 0 if None: self self 1 ( . . ): class Cactus pygame sprite Sprite def __init__ ( ,speed= ,sizex=- ,sizey=- ) self 5 1 1 _ self self self self 'cacti-small.png' 3 1 1 self 0 98 self self self self 0 3 self 1 0 def draw ( ) self self self def update ( ) self self self self if self 0 self ( . . ): class Ptera pygame sprite Sprite def __init__ ( ,speed= ,sizex=- ,sizey=- ) self 5 1 1 _ self self self self 'ptera.png' 2 1 1 self 0 82 0 75 0 60 self self 0 3 self self self self 0 self 1 0 self 0 self 0 def draw ( ) self self self def update ( ) self if self 10 0 self self 1 2 self self self self self self self self 1 if self 0 self (): class Ground def __init__ ( ,speed=- ) self 5 self self 'ground.png' 1 1 1 self self 'ground.png' 1 1 1 self self self self self def draw ( ) self self self self self def update ( ) self self self self self if self 0 self self if self 0 self self ( . . ): class Cloud pygame sprite Sprite def __init__ ( ,x,y) self _ self self self self 'cloud.png' 90 30 42 30 1 self 1 self self self 1 self 0 def draw ( ) self self self def update ( ) self self self self if self 0 self (): class Scoreboard def __init__ ( ,x=- ,y=- ) self 1 1 self 0 self self 'numbers.png' 12 1 11 11 6 5 1 self 55 11 6 5 self self if 1 self 0 89 else: self if 1 self 0 1 else: self def draw ( ) self self self def update ( ,score) self self for in score_digits: self self self self self self 0 def introscreen () 44 47 'ground.png' 15 1 1 1 1 20 'logo.jpg' 300 140 1 0 6 0 6 while not gameStart: if None: "Couldn't load display surface" return else: for in if QUIT: return if KEYDOWN: if or K_UP: 1 1 if None: 0 if isBlinking: if and False: def gameplay () 4 44 47 1 0 78 0 'replay_button.png' 35 31 1 'game_over.png' 190 11 1 'numbers.png' 12 1 11 11 6 5 1 22 11 6 5 10 11 0 1 0 73 while not gameQuit: while startMenu: while not gameOver: if None: "Couldn't load display surface" else: for in if QUIT: if KEYDOWN: if K_SPACE: if 0 98 if None: 1 1 if K_DOWN: if not and if KEYUP: if K_DOWN: for in cacti: 0 1 if if None: for in pteras: 0 1 if if None: if 2 if 0 40 40 else: for in last_obstacle: if 0 7 and 0 50 10 40 40 if 0 and 0 200 10 and 500 for in last_obstacle: if 0 8 46 40 if 5 and 0 300 10 5 2 if None: if 0 if isDead: if high_score: if 700 699 1 1 1 if gameQuit: break while gameOver: if None: "Couldn't load display surface" else: for in if QUIT: if KEYDOWN: if K_ESCAPE: if or K_SPACE: if None: if 0 def main () if not isGameQuit: Conclusion: Follow all the instruction which are given carefully to avoid any issue while running the Dino Run game. Also, let us know whether this blog helps you or not in the comment section below. Thank you!