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.
- 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“.
- Step 3: Name your python file.
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 a Dino Game in Python, and you are free to copy this code and download the full source code given below.
1. Importing Pygame Module
import pygame from pygame import *
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
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
jump_sound = pygame.mixer.Sound('resources/jump.wav')
die_sound = pygame.mixer.Sound('resources/die.wav')
checkPoint_sound = pygame.mixer.Sound('resources/checkPoint.wav')
In the code given above, which is for the function of sound used in gameplay.
4. This module is for the load image
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())
In the code given above, which is for the function of loading images and colors.
5. This module is for the game over
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)
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
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()
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.
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.
If you have any questions or suggestions about the Dino Game in Python with Source Code, please feel free to leave a comment below.