This Dino Game in Python with Source Code 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.
The Dino Game in Python 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.
Anyway if you want 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 Dino Game in Python, make sure that you have PyCharm IDE and Pygame installed in your computer.
By the way if you are new to python programming and you don’t know what would be the the Python IDE to use, I have here a list of Best Python IDE for Windows, Linux, Mac OS that will suit for you. I also have here How to Download and Install 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.
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.
1. Importing Pygame Module
import pygame from pygame import *
In the code above, 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.
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 a 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 load image 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 game over display message in 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 cactus used in a game.
Downloadable Source Code
I have here the list of Best Python Project 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 in implementing the Dino Game in your Python projects.
Dino Game in Python is free to download the open source code and it is use for educational purposes only..
If you have any questions or suggestions about Dino Game in Python with Source Code , please feel free to leave a comment below.