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 “enter“.Naming 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.

Frequently Asked Questions

How do you play this Python Chrome Dino clone?

Press Spacebar to make the dinosaur jump over incoming cacti. Press Down Arrow to duck under flying birds. The game scrolls continuously and speeds up over time. Score is calculated as distance traveled. Hitting any obstacle ends the run. This is a single-player endurance score game, the goal is to beat your high score, not reach an end state. Save SCORE_FILE on game over to persist the high score across runs.

What Python version do I need to run this game?

Python 3.8 or newer works. We recommend Python 3.11 or 3.12 because Pygame ships pre-built wheels for those versions (faster pip install, no compilation errors). Python 3.13 may need a slightly older Pygame release because some wheels lag the latest CPython. Verify your version with python –version on Windows or python3 –version on Mac/Linux.

How do I install Pygame for this project?

Run pip install pygame in your terminal (Windows: open Command Prompt, Mac/Linux: open Terminal). On Windows, if pip is not recognized, try python -m pip install pygame instead. On Linux, you may need sudo apt install python3-pygame if pip fails. Verify with python -c “import pygame; print(pygame.version.ver)”.

Can I use this Python game as my BSIT capstone project?

On its own, no, most Philippine BSIT panels expect a full system with users, data, reports, and a real-world problem. A single Pygame game is too narrow for capstone scope. BUT, you can use this game as ONE module inside a larger capstone (e.g. a gamified learning system, a math practice tool for elementary students with this game as the reward layer, or an arcade-style POS for an internet cafe). Pair the game with a Django backend, a database, and analytics for a defensible capstone.

Can I package this game as a standalone .exe to distribute?

Yes, use PyInstaller. Run pip install pyinstaller then pyinstaller –onefile –windowed your_game.py from the project folder. PyInstaller bundles Python and Pygame into a single .exe (Windows) or .app (Mac) that runs without Python installed on the target machine. Include asset files (images, sounds) with –add-data “assets;assets”. Output goes to the dist/ folder.

Where do I get help if the game does not run?

Check the top 3 most common failures: (1) Pygame is not installed correctly, re-run pip install pygame. (2) The game cannot find an asset file (image/sound), make sure you run python from the project folder so relative paths resolve. (3) Wrong Python version, this code expects Python 3.8+; older Python 2.x or 3.6 raises syntax errors. If you still hit a wall, drop a comment on this article with the exact error message, our team monitors comments daily.

Leave a Comment