Dino Game In Python With Source Code

This Dino Game In Python Code basic game requires the user to utilize only two keyboard keys to play.

To jump, hit the spacebar, and to hide, press the down arrow key.

The gaming environment in this clone version differs from the original.

The Dinosaur Game In Python is quite useful, and the concept and logic of the project are simple to grasp.

The source code is open source and free to use. Simply scroll down and click the download option.

What Is Dino Game In Python?

The Dino Game Automation Python was created with the Python module, which includes multiple platforms for multimedia programming.

A simple windowed interface with a background and sprite picture is used to play the game.

The game can be played with a keyboard ( Up Arrow Key to Move above, Down Arrow Key to Move Below).

How can we play Dinosaur Game?

The Dinosaur Game In Python, one of the most popular games, requires you to leap over all of the obstacles to win. To increase your score and win, you must control the dinosaur.

This game is also known as the T-Rex Game or the “No Internet Game,” since it is one of the secret Google games that can only be played when you don’t have access to the internet.

It is also developed in Python for this article. 

Use the Spacebar to bounce, and the Down Bolt key to cover up.

Unlike the original, this clone variant includes a few differences in the gaming conditions.

What’s the main goal of the Dino Game?

The primary goal of the game is to provide entertainment for those who enjoy this kind of gaming platform.

The main purpose of this scaled-down game is to accumulate a growing number of focuses while avoiding obstacles.

All of the game strategies are the same. The client must play this simple game using only two keyboard keys.

Project Details and Technology

Project Name:Dino Game In Python
Abstract:This Dino Game In Python is a basic Python game that attempts to provide leisure time to the users who avail the game online.
This project is also for students who need this kind of project in Python.
Language/s Used:Python (GUI Based)
Python version (Recommended):3.8 or 3.9
Type:Desktop Application
Developer:Source Code Hero
Updates:0
Dino Game In Python With Source Code – Project Information

About the Project

The Dino Game In Python With Source Code is a desktop application written in the Python programming language.

The open-source code for this Python Dino Game is free to download, and it was made for novices who wish to learn Python.

This project system makes use of a Pygame and a random module. Pygame is a cross-platform set of Python modules for creating video games.

A task record for this Dino Game contains picture documents and Python content (dino.py). The pygame library is used to create the GUI.

In terms of continuing engagement, it’s a cloned version of “T-Rex Dino Run,” a disconnected game played in Google Chrome.

How To Make Dino Game In Python?

The code given below is the full source code on How To Make Dino Game In Python

The given code below is a Python file for dino.py

import os
import sys
import pygame
import random
from pygame import *

pygame.init()

screen_size_display = (width_screen, height_screen) = (600, 150)
FPS = 60
gravity = 0.6

black_color = (0,0,0)
white_color = (255,255,255)
bg_color = (235, 235, 235)

highest_scores = 0

screen_layout_display = pygame.display.set_mode(screen_size_display)
time_clock = pygame.time.Clock()
pygame.display.set_caption("Dino Run ")

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

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

def load_sprite_sheet(
        s_name,
        namex,
        namey,
        scx = -1,
        scy = -1,
        c_key = None,
        ):
    fullname = os.path.join('resources', s_name)
    sh = pygame.image.load(fullname)
    sh = sh.convert()

    sh_rect = sh.get_rect()

    sprites = []

    sx = sh_rect.width/ namex
    sy = sh_rect.height/ namey

    for i in range(0, namey):
        for j in range(0, namex):
            rect = pygame.Rect((j*sx,i*sy,sx,sy))
            img = pygame.Surface(rect.size)
            img = img.convert()
            img.blit(sh,(0,0),rect)

            if c_key is not None:
                if c_key == -1:
                    c_key = img.get_at((0, 0))
                img.set_colorkey(c_key, RLEACCEL)

            if scx != -1 or scy != -1:
                img = pygame.transform.scale(img, (scx, scy))

            sprites.append(img)

    sprite_rect = sprites[0].get_rect()

    return sprites,sprite_rect

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)

def extractDigits(num):
    if num > -1:
        d = []
        i = 0
        while(num / 10 != 0):
            d.append(num % 10)
            num = int(num / 10)

        d.append(num % 10)
        for i in range(len(d),5):
            d.append(0)
        d.reverse()
        return d

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)

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 Dino Game In Python also includes a downloadable Project With Source Code for free, just find the downloadable source code below and click to start downloading.

By the way, if you are new to Python programming and don’t have any idea what Python IDE to use, I have here a list of the Best Python IDE for Windows, Linux, and Mac OS for you.

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

To start executing a Dino Game In Python, make sure that you have installed Python on your computer.

Steps On How To Run The Project

Time needed: 5 minutes

These are the steps on how to run Dino Game In Python with Source Code

  • Download Source Code

    First, find the downloadable source code below and click to start downloading the source code file.
    Rock Paper Scissors Game In

  • Extract File

    Next, after finished to download the file, go to the file location and right-click the file and click extract.
    customer management system in php

  • Open PyCharm

    Next, open pycharm IDE and open the project you’ve downloaded.
    Blackjack Game In Python

  • Run Project

    Next, go to the Pycharm and click the run button to start executing the project.
    Space Invaders Game In Python

Download the Source Code below!

Summary

This article is a way to enhance and develop our skills and logic ideas which is important in practicing the Python programming language which is the most well-known and most usable programming language in many companies.

Inquiries

If you have any questions or suggestions about Dino Game In Python, please feel free to leave a comment below.

Leave a Comment