Dino Game in Python with Source Code

This Dino Game in Python with Source Code is created for beginners who want 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.

The Dino Game in Python has a task record containing picture documents and 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 named “T-Rex Dino Run”.

The principal 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 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.

Anyway, if you want to level up your knowledge in programming especially games in Python, try this new article I’ve made for you Code For Game in Python: Python Game Projects With Source Code.

Before you start on how to create a Dino Game in Python, make sure that you have PyCharm IDE and Pygame installed on your computer.

By the way, if you are new to Python programming and don’t know what Python IDE is and its usage, I have here a list of the Best Python IDE for Windows, Linux, and Mac OS that will suit you.

I also have here How to Download and Install the Latest Version of Python on Windows.

Steps on How to Create Dino Game in Python with Source Code

Time needed: 5 minutes

These are the steps on how to create a Dino Game in Python with Source Code

  • Step 1: Create a project name.

    When you finished installing the Pycharm IDE on your computer, open it and then create a “project name”. After creating a project name click the “create” button.creating project name in Dino Game in python with Source Code

  • Step 2: Create a python file.

    After creating a project name, “right click” your project name and then click “new” After that click the “python file“.creating python file name in Dino Game in python with Source Code

  • Step 3: Name your python file.

    After creating a Python file, Name your Python file after that click “enterNaming python file name in Dino Game in python with Source Code

  • Step 4: The actual code.

    This is the actual coding on how to create a Dino Game in Python, and you are free to copy this code and download the full source code given below.

Code Explanations

1. Importing Pygame Module

Code:

import pygame
from pygame import *

Explanation:

The code above, which is the 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.

2. Importing Random Module

Code:

import random

Explanation:

In the code above. which is for the random() function, which generates random numbers between 0 and 1.

3. This module is for the Sound

Code:

jump_sound = pygame.mixer.Sound('resources/jump.wav')
die_sound = pygame.mixer.Sound('resources/die.wav')
checkPoint_sound = pygame.mixer.Sound('resources/checkPoint.wav')

Explanation:

In the code given above, which is for the function of sound used in gameplay.

4. This module is for the load image

Code:

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())

Explanation:

In the code given above, which is for the function of loading images and colors.

5. This module is for the game over

Code:

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)

Explanation:

In the code given above, which is for the function of the game over display message in the screen window.

6. This module is for the class Cactus

Code:

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()

Explanation:

In the code given above, which is for the function of a cactus used in a game.

Downloadable Source Code 

I have here the list of Best Python Projects with Source code free to download for free, I hope this can help you a lot.

Summary

That’s how you create Dino Game in Python in your projects. You can always expand and try different ways of implementing the Dino Game in your Python projects.

Dino Game in Python is free to download the open source code and it is used for educational purposes only.

Inquiries

If you have any questions or suggestions about the Dino Game in Python with Source Code, please feel free to leave a comment below.

Leave a Comment