Tutorial Playlist

Python Tutorial for Beginners


The Best Tips for Learning Python

Lesson - 1

How to Install Python on Windows?

Lesson - 2

Top 10 Python IDEs in 2020: Choosing The Best One

Lesson - 3

A Beginner’s Guide To Python Variables

Lesson - 4

Understanding Python If-Else Statement

Lesson - 5

Python Numbers: Integers, Floats, Complex Numbers

Lesson - 6

Python Strings | Simplilearn Python Tutorial

Lesson - 7

The Basics of Python Loops

Lesson - 8

Python For Loops Explained With Examples

Lesson - 9

Introduction to Python While Loop

Lesson - 10

Everything You Need to Know About Python Arrays

Lesson - 11

All You Need To Know About Python List

Lesson - 12

How to Easily Implement Python Sets and Dictionaries

Lesson - 13

A Handy Guide to Python Tuples

Lesson - 14

Everything You Need to Know About Python Slicing

Lesson - 15

Python Regular Expression (RegEX)

Lesson - 16

Learn A to Z About Python Functions

Lesson - 17

Objects and Classes in Python: Create, Modify and Delete

Lesson - 18

Python OOPs Concept: Here's What You Need to Know

Lesson - 19

An Introduction to Python Threading

Lesson - 20

Getting Started With Jupyter Network

Lesson - 21

PyCharm Tutorial: Getting Started with PyCharm

Lesson - 22

The Best NumPy Tutorial for Beginners

Lesson - 23

The Best Python Pandas Tutorial

Lesson - 24

An Introduction to Matplotlib for Beginners

Lesson - 25

The Best Guide to Time Series Analysis In Python

Lesson - 26

An Introduction to Scikit-Learn: Machine Learning in Python

Lesson - 27

A Beginner's Guide To Web Scraping With Python

Lesson - 28

Python Django Tutorial: The Best Guide on Django Framework

Lesson - 29

Top 10 Reason Why You Should Learn Python

Lesson - 30

10 Cool Python Project Ideas For Beginners in 2021

Lesson - 31

The Best Ideas for Python Automation Projects

Lesson - 32

12 Tips On How To Become a Python Developer

Lesson - 33

The Best Guide for RPA Using Python

Lesson - 34

Comprehending Web Development With PHP vs. Python

Lesson - 35

The Best Tips for Learning Python - REMOVE

Lesson - 36

The Best Way to Learn About Box and Whisker Plot

Lesson - 37

An Interesting Guide to Visualizing Data Using Python Seaborn

Lesson - 38

The Complete Guide to Data Visualization in Python

Lesson - 39

Everything You Need to Know About Game Designing With Pygame in Python

Lesson - 40

The Complete Simplified Guide to Python Bokeh

Lesson - 41

Top 50 Python Interview Questions and Answers in 2020

Lesson - 42
Everything You Need to Know About Game Designing With Pygame in Python

Have you ever wondered how games are created and the process involved in designing and programming a game from scratch? While it is a long, arduous process, Python has made it easy. With the help of the module Pygame in python, you can design and create games in the blink of an eye. With various inbuilt libraries specifically made to help in game design, pygame in Python makes it easy for beginners to learn a complex field. 

In this tutorial titled ‘Everything you need to know about Pygame in Python’, you will learn how to work with pygame. The major components involved in programming a game and how to make a game with pygame.

Python Training Course

Learn Data Operations in PythonExplore Course
Python Training Course

What Is Pygame?

Pygame is a cross-platform python software that was made specifically for video game designing. It also includes graphics, visuals, and sounds that can be used to enhance the game being designed.

It contains various libraries that work with images and sounds and can create graphics for games. It simplifies the whole game designing process and makes it easy for beginners who want to develop games.                                   


Figure 1: Pygame Logo

The process of installing pygame in Python is straightforward. It can be done with the pip command, and the module can be imported into your program with the help of the import command.


Figure 2: Pygame Installation 


Figure 3: Importing Pygame               

What Are the Components of Pygame? 

To design a game with Pygame in python, you need to know about four main components of Pygame:

  • Pygame.init(): This is used to initialize all imported pygame modules. Using this, you can call all pygame modules at once. This will import all the necessary modules in pygame into your program.

  • Display Surface: A black screen that acts as a canvas for you to design your game on. All the game components will appear on this screen.                           


Figure 4: Pygame Display

  • Game Loop: A game loop is a piece of code used to run the main logic of the game. It helps you set the rules of the game and does three main things : 

  • Handle events 
  • Updates game events
  • Draw game state to screen

  • Pygame.quit(): Does the opposite of the init() function and completely deactivates the pygame library. It initializes all modules which have been imported. It does not quit the game, however. 

Free Course: Programming with Python

Learn the Basics of Programming with PythonEnroll Now
Free Course: Programming with Python

How to Make a Flappy Bird Game With Pygame?

You will now see how to create a flappy bird game with pygame in Python. To start, you will import all the necessary modules.             


Figure 5: Importing necessary modules

To design a flappy bird game, you do not need any data. You need to download the background, pipe, flappy bird, and score images. 

First, initialize your display window. You do this by running the pygame.display() function. This will open a blank screen to include your graphics and design and run your game. You can set the size of this screen by using the set_mode method. You will also have to set the frames/ second parameter and the elevation of the game window for the bottom of the screen. Then, you will read in the pipe, background, bird, and sea-level images.                


Figure 6: Creating Display Window

Now, define the game logic. This dictates the various parameters involved in the game and the functionalities which your game will have. It also dictates the rules of the game and defines how the game should be played.

You need to start off by defining the score, ground height, and the vertical and horizontal height of the bird. You then need to create two lists that contain the x and y coordinates of the pipes which will appear at the bottom and top of the screen.                 


Figure 7: Main Game Function

In your game, the pipes will move from right to left of the screen. The bird will move up and down in the same x-coordinates. So the x-coordinates of the pipes change, and the y-coordinates of the bird will change. Now, set the velocity of our pipes and our bird. The bird acceleration will describe the speed at which the bird will free fall when the user stops pressing the enter key.                         


Figure 8: Speed Parameters

Now, you will check the keystrokes and set the logic that defines the keystrokes which will enable the game to start. If the user enters quit or key down or the escape key, the program will close. If the user presses the space or up key, the game will start, and the bird will move with a predefined velocity. If the game is over or the bird crashes, the window will return to its original state.


Figure 9: Bird Motion logic

Now, set the logic which will keep track of your score. The score will increase by one every time the bird passes a pipe, and the score gets printed out in the game program. If the user hasn’t pressed the enter key for a while and the birk flapped parameter is false, then the bird will free fall with an increase in its speed. This increase in speed is equal to the velocity of the bird plus the acceleration parameter, which was set at the start. 


Figure 10: Score update

You have programmed the movement of the bird. Now program the movement of the pipes. The pipes will move along the x-axis only at a constant velocity. Furthermore, every time the pipes are in the last five units of the x-axis, a new tube will be created and added to the pipes list. The old pipes which are at the leftmost corner of the screen will be popped from the list.     


Figure 11: Pipe motion

Free Course: Python for Beginners

Master the fundamentals of PythonEnroll Now
Free Course: Python for Beginners

Now, go ahead and add your graphics to your display window. You will have to display the background, pipes, bird, and sea level on your display. You must also search for the number image corresponding to your score and display it on your screen's upper right-hand corner. Every time the score changes, the display window will get updated and reload everything exactly how it was, except with a new score image displayed. 


Figure 12: Setting up graphics on display

Now, you have to program the conditions which check if the game is over or not. Three main conditions could cause the game to end:

  1. The bird falls and hits the sea. In this case, you must check if the elevation of the bird is above a certain level.
  2. The bird hits one of the pipes at the top of the screen. In this case, you check if the bird's position is the same as the position of the up_pipe that it is crossing. 
  3. The bird hits one of the pipes at the bottom of the screen. In this case, you must check if the bird's position is the same as the position of the down_pipe that it is crossing.


Figure 13: Game over logic

Next, define a function that will create pipes of varying heights. This is done by importing the pipe image into the game and getting its height. Then, generating a random size for it and offsetting the image by that amount.


Figure 14: Create Pipes

You have now defined the game logic. Now, define the game loop, which will call the game logic, and run it until the end conditions are met. You must define the main class of your game and import all the necessary images.


Figure 15: Main Game class

Data Science with Python

The Ultimate Ticket To Top Data Science Job RolesExplore Course
Data Science with Python

You should display a welcome message and set the game's name on the top of your display window, along with instructions on starting the game.                


Figure 16: Game messages

You then need to check to see the keys which are being pressed. If the user has pressed the quit keys, then the game ends, and the window closes. On the other hand, the game starts if the space key is pressed and the flappygame() function gets called.


Figure 17: Game start

These are the messages which will be displayed on the console. The game score will also get displayed on the console as and when it gets updated.                      


Figure 18: Console Updates

The below screen shows what the starting screen of your game looks like. 


Figure 19: Main game window

Learn data operations in Python, strings, conditional statements, error handling, and the commonly used Python web framework Django with the Python Training course.


In this Pygame in Python tutorial, you first looked into the pygame module in Python and learned how to install it and import it into your programs. You then looked at the basic components of pygame that are required to make a game with pygame in Python. You then designed a flappy bird with pygame in Python and got hands-on experience with pygame. 

Python today is one of the most sought-after programming languages used for a variety of applications - from software and game development to data science and much more. If you are keen on pursuing a career in the area, Simpilearn’s Python training certification program should be your next step. A comprehensive training program offering you work-ready skills in Python, this can be the gamechanger your career needs. Explore and start now.

You can check out the video tutorial for pygame on Simplilearn's youtube channel. On the other hand, if you need any clarifications on this Pygame tutorial, share them with us by commenting down below and we will have our experts review them at the earliest!

Happy learning!

About the Author


Simplilearn is one of the world’s leading providers of online training for Digital Marketing, Cloud Computing, Project Management, Data Science, IT, Software Development, and many other emerging technologies.

View More
  • Disclaimer
  • PMP, PMI, PMBOK, CAPM, PgMP, PfMP, ACP, PBA, RMP, SP, and OPM3 are registered marks of the Project Management Institute, Inc.