Dino Game is developed in Python Programming Language and it is a desktop application. This Dino Game in Python is free to download the open source code and it is created for the beginners who wants to learn python. This project system used a Pygame and Random module. Pygame is a cross-platform set of Python modules designed for writing video games.
This Dino Game has a task record contains picture documents and a python content (dino.py). GUI utilizes the pygame library. Discussing the ongoing interaction, it is a cloned adaptation of a disconnected game played in google chrome naming “T-Rex Dino Run”.
The principle goal of this scaled down game is to score an ever increasing number of focuses without being moved by any snags. All the playing strategies are the equivalent. The client needs to play this basic game utilizing two Keyboard keys.
Spacebar to bounce and Down bolt key to cover up. In contrast with the first one, this clone form has a few changes in the gaming condition.
Before you start on how to create Dino Game in Python, make sure that you have PyCharm IDE and Pygame installed in your computer.
This are the steps on How to Create Dino Game in Python.
- Step 1: Create a project name.
First when you finished installed the Pycharm IDE in your computer, open it and then create a “project name” after creating a project name click the “create” button.
- Step 2: Create a python file.
Second after creating a project name, “right click” your project name and then click “new” after that click the “python file“.
- Step 3: Name your python file.
Third after creating a python file, Name your python file after that click “enter“
- Step 4: The actual code.
This is the actual coding on how to create Dino Game in Python, and you are free to copy this code and download the full source code given below.
Importing Pygame Module
In the code below, which is pygame library is an open-source module for the Python programming language specifically intended to help you make games and other multimedia applications. Pygame can run across many platforms and operating systems.
import pygame
from pygame import *
Importing Random Module
In the code below. which is for the random() function, which generates random numbers between 0 and 1.(Python Source Code for Dino Game)
import random
This module is for the Sound
In the code given below, which is for the function of sound used in a gameplay.
jump_sound = pygame.mixer.Sound('resources/jump.wav')<br>die_sound = pygame.mixer.Sound('resources/die.wav')<br>checkPoint_sound = pygame.mixer.Sound('resources/checkPoint.wav')
This module is for the load image
In the code given below, which is for the function of load image and colors.(Python Source Code for Dino Game
def load_image(
name,
sx=-1,
sy=-1,
colorkey=None,
):
fullname = os.path.join('resources', name)
img = pygame.image.load(fullname)
img = img.convert()
if colorkey is not None:
if colorkey == -1:
colorkey = img.get_at((0, 0))
img.set_colorkey(colorkey, RLEACCEL)
if sx != -1 or sy != -1:
img = pygame.transform.scale(img, (sx, sy))
return (img, img.get_rect())
This module is for the game over
In the code given below, which is for the function of game over display message in screen window.
def gameover_display_message(rbtn_image, gmo_image):
rbtn_rect = rbtn_image.get_rect()
rbtn_rect.centerx = width_screen / 2
rbtn_rect.top = height_screen * 0.52
gmo_rect = gmo_image.get_rect()
gmo_rect.centerx = width_screen / 2
gmo_rect.centery = height_screen * 0.35
screen_layout_display.blit(rbtn_image, rbtn_rect)
screen_layout_display.blit(gmo_image, gmo_rect)
This module is for the class dino
In the code given below, which is for the function of class dino. Dino is a character used in a game, dino can jump, blink and duck and it can move.
class Dino():
def __init__(self, sx=-1, sy=-1):
self.imgs, self.rect = load_sprite_sheet('dino.png', 5, 1, sx, sy, -1)
self.imgs1, self.rect1 = load_sprite_sheet('dino_ducking.png', 2, 1, 59, sy, -1)
self.rect.bottom = int(0.98 * height_screen)
self.rect.left = width_screen / 15
self.image = self.imgs[0]
self.index = 0
self.counter = 0
self.score = 0
self.jumping = False
self.dead = False
self.ducking = False
self.blinking = False
self.movement = [0,0]
self.jumpSpeed = 11.5
self.stand_position_width = self.rect.width
self.duck_position_width = self.rect1.width
def draw(self):
screen_layout_display.blit(self.image, self.rect)
def checkbounds(self):
if self.rect.bottom > int(0.98 * height_screen):
self.rect.bottom = int(0.98 * height_screen)
self.jumping = False
def update(self):
if self.jumping:
self.movement[1] = self.movement[1] + gravity
if self.jumping:
self.index = 0
elif self.blinking:
if self.index == 0:
if self.counter % 400 == 399:
self.index = (self.index + 1)%2
else:
if self.counter % 20 == 19:
self.index = (self.index + 1)%2
elif self.ducking:
if self.counter % 5 == 0:
self.index = (self.index + 1)%2
else:
if self.counter % 5 == 0:
self.index = (self.index + 1)%2 + 2
if self.dead:
self.index = 4
if not self.ducking:
self.image = self.imgs[self.index]
self.rect.width = self.stand_position_width
else:
self.image = self.imgs1[(self.index) % 2]
self.rect.width = self.duck_position_width
self.rect = self.rect.move(self.movement)
self.checkbounds()
if not self.dead and self.counter % 7 == 6 and self.blinking == False:
self.score += 1
if self.score % 100 == 0 and self.score != 0:
if pygame.mixer.get_init() != None:
checkPoint_sound.play()
self.counter = (self.counter + 1)
This module is for the class cactus
In the code given below, which is for the function of cactus used in a game.(Python Source Code for Dino Game)
class Cactus(pygame.sprite.Sprite):
def __init__(self, speed=5, sx=-1, sy=-1):
pygame.sprite.Sprite.__init__(self,self.containers)
self.imgs, self.rect = load_sprite_sheet('cactus-small.png', 3, 1, sx, sy, -1)
self.rect.bottom = int(0.98 * height_screen)
self.rect.left = width_screen + self.rect.width
self.image = self.imgs[random.randrange(0, 3)]
self.movement = [-1*speed,0]
def draw(self):
screen_layout_display.blit(self.image, self.rect)
def update(self):
self.rect = self.rect.move(self.movement)
if self.rect.right < 0:
self.kill()
This module is for the class birds
In the code given below, which is for the function of birds used in a game.
class birds(pygame.sprite.Sprite):<br> def __init__(self, speed=5, sx=-1, sy=-1):<br> pygame.sprite.Sprite.__init__(self,self.containers)<br> self.imgs, self.rect = load_sprite_sheet('birds.png', 2, 1, sx, sy, -1)<br> self.birds_height = [height_screen * 0.82, height_screen * 0.75, height_screen * 0.60]<br> self.rect.centery = self.birds_height[random.randrange(0, 3)]<br> self.rect.left = width_screen + self.rect.width<br> self.image = self.imgs[0]<br> self.movement = [-1*speed,0]<br> self.index = 0<br> self.counter = 0<br><br> def draw(self):<br> screen_layout_display.blit(self.image, self.rect)<br><br> def update(self):<br> if self.counter % 10 == 0:<br> self.index = (self.index+1)%2<br> self.image = self.imgs[self.index]<br> self.rect = self.rect.move(self.movement)<br> self.counter = (self.counter + 1)<br> if self.rect.right < 0:<br> self.kill()<br>
This module is for the class ground
In the code given below, which is for the function of ground used in a game.
class Ground():<br> def __init__(self,speed=-5):<br> self.image,self.rect = load_image('ground.png',-1,-1,-1)<br> self.image1,self.rect1 = load_image('ground.png',-1,-1,-1)<br> self.rect.bottom = height_screen<br> self.rect1.bottom = height_screen<br> self.rect1.left = self.rect.right<br> self.speed = speed<br><br> def draw(self):<br> screen_layout_display.blit(self.image, self.rect)<br> screen_layout_display.blit(self.image1, self.rect1)<br><br> def update(self):<br> self.rect.left += self.speed<br> self.rect1.left += self.speed<br><br> if self.rect.right < 0:<br> self.rect.left = self.rect1.right<br><br> if self.rect1.right < 0:<br> self.rect1.left = self.rect.right
This module is for the class cloud
In the code given below, which is for the function of cloud used in a game
class Cloud(pygame.sprite.Sprite):
def __init__(self,x,y):
pygame.sprite.Sprite.__init__(self,self.containers)
self.image,self.rect = load_image('cloud.png',int(90*30/42),30,-1)
self.speed = 1
self.rect.left = x
self.rect.top = y
self.movement = [-1*self.speed,0]
def draw(self):
screen_layout_display.blit(self.image, self.rect)
def update(self):
self.rect = self.rect.move(self.movement)
if self.rect.right < 0:
self.kill()
This module is for the class scoreboard
In the code given below, which is for the function of scoreboard used in game.(Python Source Code for Dino Game)
class Scoreboard():
def __init__(self,x=-1,y=-1):
self.score = 0
self.scre_img, self.screrect = load_sprite_sheet('numbers.png', 12, 1, 11, int(11 * 6 / 5), -1)
self.image = pygame.Surface((55,int(11*6/5)))
self.rect = self.image.get_rect()
if x == -1:
self.rect.left = width_screen * 0.89
else:
self.rect.left = x
if y == -1:
self.rect.top = height_screen * 0.1
else:
self.rect.top = y
def draw(self):
screen_layout_display.blit(self.image, self.rect)
def update(self,score):
score_digits = extractDigits(score)
self.image.fill(bg_color)
for s in score_digits:
self.image.blit(self.scre_img[s], self.screrect)
self.screrect.left += self.screrect.width
self.screrect.left = 0
This module is for the gameplay
In the code given below, which is for the function of main system the gameplay.(Python Source Code for Dino Game)
def gameplay():
global highest_scores
gp = 4
s_Menu = False
g_Over = False
g_exit = False
gamer_Dino = Dino(44,47)
new_grnd = Ground(-1*gp)
score_boards = Scoreboard()
highScore = Scoreboard(width_screen * 0.78)
counter = 0
cactusan = pygame.sprite.Group()
smallBird = pygame.sprite.Group()
skyClouds = pygame.sprite.Group()
last_end_obs = pygame.sprite.Group()
Cactus.containers = cactusan
birds.containers = smallBird
Cloud.containers = skyClouds
rbtn_image,rbtn_rect = load_image('replay_button.png',35,31,-1)
gmo_image,gmo_rect = load_image('game_over.png',190,11,-1)
t_images,t_rect = load_sprite_sheet('numbers.png',12,1,11,int(11*6/5),-1)
ado_image = pygame.Surface((22,int(11*6/5)))
ado_rect = ado_image.get_rect()
ado_image.fill(bg_color)
ado_image.blit(t_images[10],t_rect)
t_rect.left += t_rect.width
ado_image.blit(t_images[11],t_rect)
ado_rect.top = height_screen * 0.1
ado_rect.left = width_screen * 0.73
while not g_exit:
while s_Menu:
pass
while not g_Over:
if pygame.display.get_surface() == None:
print("Couldn't load display surface")
g_exit = True
g_Over = True
else:
for event in pygame.event.get():
if event.type == pygame.QUIT:
g_exit = True
g_Over = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
if gamer_Dino.rect.bottom == int(0.98 * height_screen):
gamer_Dino.jumping = True
if pygame.mixer.get_init() != None:
jump_sound.play()
gamer_Dino.movement[1] = -1*gamer_Dino.jumpSpeed
if event.key == pygame.K_DOWN:
if not (gamer_Dino.jumping and gamer_Dino.dead):
gamer_Dino.ducking = True
if event.type == pygame.KEYUP:
if event.key == pygame.K_DOWN:
gamer_Dino.ducking = False
for c in cactusan:
c.movement[0] = -1*gp
if pygame.sprite.collide_mask(gamer_Dino,c):
gamer_Dino.dead = True
if pygame.mixer.get_init() != None:
die_sound.play()
for p in smallBird:
p.movement[0] = -1*gp
if pygame.sprite.collide_mask(gamer_Dino,p):
gamer_Dino.dead = True
if pygame.mixer.get_init() != None:
die_sound.play()
if len(cactusan) < 2:
if len(cactusan) == 0:
last_end_obs.empty()
last_end_obs.add(Cactus(gp,40,40))
else:
for l in last_end_obs:
if l.rect.right < width_screen*0.7 and random.randrange(0, 50) == 10:
last_end_obs.empty()
last_end_obs.add(Cactus(gp, 40, 40))
if len(smallBird) == 0 and random.randrange(0,200) == 10 and counter > 500:
for l in last_end_obs:
if l.rect.right < width_screen*0.8:
last_end_obs.empty()
last_end_obs.add(birds(gp, 46, 40))
if len(skyClouds) < 5 and random.randrange(0,300) == 10:
Cloud(width_screen, random.randrange(height_screen / 5, height_screen / 2))
gamer_Dino.update()
cactusan.update()
smallBird.update()
skyClouds.update()
new_grnd.update()
score_boards.update(gamer_Dino.score)
highScore.update(highest_scores)
if pygame.display.get_surface() != None:
screen_layout_display.fill(bg_color)
new_grnd.draw()
skyClouds.draw(screen_layout_display)
score_boards.draw()
if highest_scores != 0:
highScore.draw()
screen_layout_display.blit(ado_image, ado_rect)
cactusan.draw(screen_layout_display)
smallBird.draw(screen_layout_display)
gamer_Dino.draw()
pygame.display.update()
time_clock.tick(FPS)
if gamer_Dino.dead:
g_Over = True
if gamer_Dino.score > highest_scores:
highest_scores = gamer_Dino.score
if counter%700 == 699:
new_grnd.speed -= 1
gp += 1
counter = (counter + 1)
if g_exit:
break
while g_Over:
if pygame.display.get_surface() == None:
print("Couldn't load display surface")
g_exit = True
g_Over = False
else:
for event in pygame.event.get():
if event.type == pygame.QUIT:
g_exit = True
g_Over = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
g_exit = True
g_Over = False
if event.key == pygame.K_RETURN or event.key == pygame.K_SPACE:
g_Over = False
gameplay()
highScore.update(highest_scores)
if pygame.display.get_surface() != None:
gameover_display_message(rbtn_image, gmo_image)
if highest_scores != 0:
highScore.draw()
screen_layout_display.blit(ado_image, ado_rect)
pygame.display.update()
time_clock.tick(FPS)
pygame.quit()
quit()
Complete Source Code of Dino Game in Python
import os<br>import sys<br>import pygame<br>import random<br>from pygame import *<br><br>pygame.init()<br><br>screen_size_display = (width_screen, height_screen) = (600, 150)<br>FPS = 60<br>gravity = 0.6<br><br>black_color = (0,0,0)<br>white_color = (255,255,255)<br>bg_color = (235, 235, 235)<br><br>highest_scores = 0<br><br>screen_layout_display = pygame.display.set_mode(screen_size_display)<br>time_clock = pygame.time.Clock()<br>pygame.display.set_caption("Dino Run ")<br><br>jump_sound = pygame.mixer.Sound('resources/jump.wav')<br>die_sound = pygame.mixer.Sound('resources/die.wav')<br>checkPoint_sound = pygame.mixer.Sound('resources/checkPoint.wav')<br><br>def load_image(<br> name,<br> sx=-1,<br> sy=-1,<br> colorkey=None,<br> ):<br><br> fullname = os.path.join('resources', name)<br> img = pygame.image.load(fullname)<br> img = img.convert()<br> if colorkey is not None:<br> if colorkey == -1:<br> colorkey = img.get_at((0, 0))<br> img.set_colorkey(colorkey, RLEACCEL)<br><br> if sx != -1 or sy != -1:<br> img = pygame.transform.scale(img, (sx, sy))<br><br> return (img, img.get_rect())<br><br>def load_sprite_sheet(<br> s_name,<br> namex,<br> namey,<br> scx = -1,<br> scy = -1,<br> c_key = None,<br> ):<br> fullname = os.path.join('resources', s_name)<br> sh = pygame.image.load(fullname)<br> sh = sh.convert()<br><br> sh_rect = sh.get_rect()<br><br> sprites = []<br><br> sx = sh_rect.width/ namex<br> sy = sh_rect.height/ namey<br><br> for i in range(0, namey):<br> for j in range(0, namex):<br> rect = pygame.Rect((j*sx,i*sy,sx,sy))<br> img = pygame.Surface(rect.size)<br> img = img.convert()<br> img.blit(sh,(0,0),rect)<br><br> if c_key is not None:<br> if c_key == -1:<br> c_key = img.get_at((0, 0))<br> img.set_colorkey(c_key, RLEACCEL)<br><br> if scx != -1 or scy != -1:<br> img = pygame.transform.scale(img, (scx, scy))<br><br> sprites.append(img)<br><br> sprite_rect = sprites[0].get_rect()<br><br> return sprites,sprite_rect<br><br>def gameover_display_message(rbtn_image, gmo_image):<br> rbtn_rect = rbtn_image.get_rect()<br> rbtn_rect.centerx = width_screen / 2<br> rbtn_rect.top = height_screen * 0.52<br><br> gmo_rect = gmo_image.get_rect()<br> gmo_rect.centerx = width_screen / 2<br> gmo_rect.centery = height_screen * 0.35<br><br> screen_layout_display.blit(rbtn_image, rbtn_rect)<br> screen_layout_display.blit(gmo_image, gmo_rect)<br><br>def extractDigits(num):<br> if num > -1:<br> d = []<br> i = 0<br> while(num / 10 != 0):<br> d.append(num % 10)<br> num = int(num / 10)<br><br> d.append(num % 10)<br> for i in range(len(d),5):<br> d.append(0)<br> d.reverse()<br> return d<br><br>class Dino():<br> def __init__(self, sx=-1, sy=-1):<br> self.imgs, self.rect = load_sprite_sheet('dino.png', 5, 1, sx, sy, -1)<br> self.imgs1, self.rect1 = load_sprite_sheet('dino_ducking.png', 2, 1, 59, sy, -1)<br> self.rect.bottom = int(0.98 * height_screen)<br> self.rect.left = width_screen / 15<br> self.image = self.imgs[0]<br> self.index = 0<br> self.counter = 0<br> self.score = 0<br> self.jumping = False<br> self.dead = False<br> self.ducking = False<br> self.blinking = False<br> self.movement = [0,0]<br> self.jumpSpeed = 11.5<br><br> self.stand_position_width = self.rect.width<br> self.duck_position_width = self.rect1.width<br><br> def draw(self):<br> screen_layout_display.blit(self.image, self.rect)<br><br> def checkbounds(self):<br> if self.rect.bottom > int(0.98 * height_screen):<br> self.rect.bottom = int(0.98 * height_screen)<br> self.jumping = False<br><br> def update(self):<br> if self.jumping:<br> self.movement[1] = self.movement[1] + gravity<br><br> if self.jumping:<br> self.index = 0<br> elif self.blinking:<br> if self.index == 0:<br> if self.counter % 400 == 399:<br> self.index = (self.index + 1)%2<br> else:<br> if self.counter % 20 == 19:<br> self.index = (self.index + 1)%2<br><br> elif self.ducking:<br> if self.counter % 5 == 0:<br> self.index = (self.index + 1)%2<br> else:<br> if self.counter % 5 == 0:<br> self.index = (self.index + 1)%2 + 2<br><br> if self.dead:<br> self.index = 4<br><br> if not self.ducking:<br> self.image = self.imgs[self.index]<br> self.rect.width = self.stand_position_width<br> else:<br> self.image = self.imgs1[(self.index) % 2]<br> self.rect.width = self.duck_position_width<br><br> self.rect = self.rect.move(self.movement)<br> self.checkbounds()<br><br> if not self.dead and self.counter % 7 == 6 and self.blinking == False:<br> self.score += 1<br> if self.score % 100 == 0 and self.score != 0:<br> if pygame.mixer.get_init() != None:<br> checkPoint_sound.play()<br><br> self.counter = (self.counter + 1)<br><br>class Cactus(pygame.sprite.Sprite):<br> def __init__(self, speed=5, sx=-1, sy=-1):<br> pygame.sprite.Sprite.__init__(self,self.containers)<br> self.imgs, self.rect = load_sprite_sheet('cactus-small.png', 3, 1, sx, sy, -1)<br> self.rect.bottom = int(0.98 * height_screen)<br> self.rect.left = width_screen + self.rect.width<br> self.image = self.imgs[random.randrange(0, 3)]<br> self.movement = [-1*speed,0]<br><br> def draw(self):<br> screen_layout_display.blit(self.image, self.rect)<br><br> def update(self):<br> self.rect = self.rect.move(self.movement)<br><br> if self.rect.right < 0:<br> self.kill()<br><br>class birds(pygame.sprite.Sprite):<br> def __init__(self, speed=5, sx=-1, sy=-1):<br> pygame.sprite.Sprite.__init__(self,self.containers)<br> self.imgs, self.rect = load_sprite_sheet('birds.png', 2, 1, sx, sy, -1)<br> self.birds_height = [height_screen * 0.82, height_screen * 0.75, height_screen * 0.60]<br> self.rect.centery = self.birds_height[random.randrange(0, 3)]<br> self.rect.left = width_screen + self.rect.width<br> self.image = self.imgs[0]<br> self.movement = [-1*speed,0]<br> self.index = 0<br> self.counter = 0<br><br> def draw(self):<br> screen_layout_display.blit(self.image, self.rect)<br><br> def update(self):<br> if self.counter % 10 == 0:<br> self.index = (self.index+1)%2<br> self.image = self.imgs[self.index]<br> self.rect = self.rect.move(self.movement)<br> self.counter = (self.counter + 1)<br> if self.rect.right < 0:<br> self.kill()<br><br><br>class Ground():<br> def __init__(self,speed=-5):<br> self.image,self.rect = load_image('ground.png',-1,-1,-1)<br> self.image1,self.rect1 = load_image('ground.png',-1,-1,-1)<br> self.rect.bottom = height_screen<br> self.rect1.bottom = height_screen<br> self.rect1.left = self.rect.right<br> self.speed = speed<br><br> def draw(self):<br> screen_layout_display.blit(self.image, self.rect)<br> screen_layout_display.blit(self.image1, self.rect1)<br><br> def update(self):<br> self.rect.left += self.speed<br> self.rect1.left += self.speed<br><br> if self.rect.right < 0:<br> self.rect.left = self.rect1.right<br><br> if self.rect1.right < 0:<br> self.rect1.left = self.rect.right<br><br>class Cloud(pygame.sprite.Sprite):<br> def __init__(self,x,y):<br> pygame.sprite.Sprite.__init__(self,self.containers)<br> self.image,self.rect = load_image('cloud.png',int(90*30/42),30,-1)<br> self.speed = 1<br> self.rect.left = x<br> self.rect.top = y<br> self.movement = [-1*self.speed,0]<br><br> def draw(self):<br> screen_layout_display.blit(self.image, self.rect)<br><br> def update(self):<br> self.rect = self.rect.move(self.movement)<br> if self.rect.right < 0:<br> self.kill()<br><br>class Scoreboard():<br> def __init__(self,x=-1,y=-1):<br> self.score = 0<br> self.scre_img, self.screrect = load_sprite_sheet('numbers.png', 12, 1, 11, int(11 * 6 / 5), -1)<br> self.image = pygame.Surface((55,int(11*6/5)))<br> self.rect = self.image.get_rect()<br> if x == -1:<br> self.rect.left = width_screen * 0.89<br> else:<br> self.rect.left = x<br> if y == -1:<br> self.rect.top = height_screen * 0.1<br> else:<br> self.rect.top = y<br><br> def draw(self):<br> screen_layout_display.blit(self.image, self.rect)<br><br> def update(self,score):<br> score_digits = extractDigits(score)<br> self.image.fill(bg_color)<br> for s in score_digits:<br> self.image.blit(self.scre_img[s], self.screrect)<br> self.screrect.left += self.screrect.width<br> self.screrect.left = 0<br><br><br>def introduction_screen():<br> ado_dino = Dino(44,47)<br> ado_dino.blinking = True<br> starting_game = False<br><br> t_ground,t_ground_rect = load_sprite_sheet('ground.png',15,1,-1,-1,-1)<br> t_ground_rect.left = width_screen / 20<br> t_ground_rect.bottom = height_screen<br><br> logo,l_rect = load_image('logo.png',300,140,-1)<br> l_rect.centerx = width_screen * 0.6<br> l_rect.centery = height_screen * 0.6<br> while not starting_game:<br> if pygame.display.get_surface() == None:<br> print("Couldn't load display surface")<br> return True<br> else:<br> for event in pygame.event.get():<br> if event.type == pygame.QUIT:<br> return True<br> if event.type == pygame.KEYDOWN:<br> if event.key == pygame.K_SPACE or event.key == pygame.K_UP:<br> ado_dino.jumping = True<br> ado_dino.blinking = False<br> ado_dino.movement[1] = -1*ado_dino.jumpSpeed<br><br> ado_dino.update()<br><br> if pygame.display.get_surface() != None:<br> screen_layout_display.fill(bg_color)<br> screen_layout_display.blit(t_ground[0], t_ground_rect)<br> if ado_dino.blinking:<br> screen_layout_display.blit(logo, l_rect)<br> ado_dino.draw()<br><br> pygame.display.update()<br><br> time_clock.tick(FPS)<br> if ado_dino.jumping == False and ado_dino.blinking == False:<br> starting_game = True<br><br>def gameplay():<br> global highest_scores<br> gp = 4<br> s_Menu = False<br> g_Over = False<br> g_exit = False<br> gamer_Dino = Dino(44,47)<br> new_grnd = Ground(-1*gp)<br> score_boards = Scoreboard()<br> highScore = Scoreboard(width_screen * 0.78)<br> counter = 0<br><br> cactusan = pygame.sprite.Group()<br> smallBird = pygame.sprite.Group()<br> skyClouds = pygame.sprite.Group()<br> last_end_obs = pygame.sprite.Group()<br><br> Cactus.containers = cactusan<br> birds.containers = smallBird<br> Cloud.containers = skyClouds<br><br> rbtn_image,rbtn_rect = load_image('replay_button.png',35,31,-1)<br> gmo_image,gmo_rect = load_image('game_over.png',190,11,-1)<br><br> t_images,t_rect = load_sprite_sheet('numbers.png',12,1,11,int(11*6/5),-1)<br> ado_image = pygame.Surface((22,int(11*6/5)))<br> ado_rect = ado_image.get_rect()<br> ado_image.fill(bg_color)<br> ado_image.blit(t_images[10],t_rect)<br> t_rect.left += t_rect.width<br> ado_image.blit(t_images[11],t_rect)<br> ado_rect.top = height_screen * 0.1<br> ado_rect.left = width_screen * 0.73<br><br> while not g_exit:<br> while s_Menu:<br> pass<br> while not g_Over:<br> if pygame.display.get_surface() == None:<br> print("Couldn't load display surface")<br> g_exit = True<br> g_Over = True<br> else:<br> for event in pygame.event.get():<br> if event.type == pygame.QUIT:<br> g_exit = True<br> g_Over = True<br><br> if event.type == pygame.KEYDOWN:<br> if event.key == pygame.K_SPACE:<br> if gamer_Dino.rect.bottom == int(0.98 * height_screen):<br> gamer_Dino.jumping = True<br> if pygame.mixer.get_init() != None:<br> jump_sound.play()<br> gamer_Dino.movement[1] = -1*gamer_Dino.jumpSpeed<br><br> if event.key == pygame.K_DOWN:<br> if not (gamer_Dino.jumping and gamer_Dino.dead):<br> gamer_Dino.ducking = True<br><br> if event.type == pygame.KEYUP:<br> if event.key == pygame.K_DOWN:<br> gamer_Dino.ducking = False<br> for c in cactusan:<br> c.movement[0] = -1*gp<br> if pygame.sprite.collide_mask(gamer_Dino,c):<br> gamer_Dino.dead = True<br> if pygame.mixer.get_init() != None:<br> die_sound.play()<br><br> for p in smallBird:<br> p.movement[0] = -1*gp<br> if pygame.sprite.collide_mask(gamer_Dino,p):<br> gamer_Dino.dead = True<br> if pygame.mixer.get_init() != None:<br> die_sound.play()<br><br> if len(cactusan) < 2:<br> if len(cactusan) == 0:<br> last_end_obs.empty()<br> last_end_obs.add(Cactus(gp,40,40))<br> else:<br> for l in last_end_obs:<br> if l.rect.right < width_screen*0.7 and random.randrange(0, 50) == 10:<br> last_end_obs.empty()<br> last_end_obs.add(Cactus(gp, 40, 40))<br><br> if len(smallBird) == 0 and random.randrange(0,200) == 10 and counter > 500:<br> for l in last_end_obs:<br> if l.rect.right < width_screen*0.8:<br> last_end_obs.empty()<br> last_end_obs.add(birds(gp, 46, 40))<br><br> if len(skyClouds) < 5 and random.randrange(0,300) == 10:<br> Cloud(width_screen, random.randrange(height_screen / 5, height_screen / 2))<br><br> gamer_Dino.update()<br> cactusan.update()<br> smallBird.update()<br> skyClouds.update()<br> new_grnd.update()<br> score_boards.update(gamer_Dino.score)<br> highScore.update(highest_scores)<br><br> if pygame.display.get_surface() != None:<br> screen_layout_display.fill(bg_color)<br> new_grnd.draw()<br> skyClouds.draw(screen_layout_display)<br> score_boards.draw()<br> if highest_scores != 0:<br> highScore.draw()<br> screen_layout_display.blit(ado_image, ado_rect)<br> cactusan.draw(screen_layout_display)<br> smallBird.draw(screen_layout_display)<br> gamer_Dino.draw()<br><br> pygame.display.update()<br> time_clock.tick(FPS)<br><br> if gamer_Dino.dead:<br> g_Over = True<br> if gamer_Dino.score > highest_scores:<br> highest_scores = gamer_Dino.score<br><br> if counter%700 == 699:<br> new_grnd.speed -= 1<br> gp += 1<br><br> counter = (counter + 1)<br><br> if g_exit:<br> break<br><br> while g_Over:<br> if pygame.display.get_surface() == None:<br> print("Couldn't load display surface")<br> g_exit = True<br> g_Over = False<br> else:<br> for event in pygame.event.get():<br> if event.type == pygame.QUIT:<br> g_exit = True<br> g_Over = False<br> if event.type == pygame.KEYDOWN:<br> if event.key == pygame.K_ESCAPE:<br> g_exit = True<br> g_Over = False<br><br> if event.key == pygame.K_RETURN or event.key == pygame.K_SPACE:<br> g_Over = False<br> gameplay()<br> highScore.update(highest_scores)<br> if pygame.display.get_surface() != None:<br> gameover_display_message(rbtn_image, gmo_image)<br> if highest_scores != 0:<br> highScore.draw()<br> screen_layout_display.blit(ado_image, ado_rect)<br> pygame.display.update()<br> time_clock.tick(FPS)<br><br> pygame.quit()<br> quit()<br><br>def main():<br> isGameQuit = introduction_screen()<br> if not isGameQuit:<br> gameplay()<br><br>main()
Show some ❤️ by starring some of the repositories! Downloadable Source Code : GITHUB