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.
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.
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
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:
- The bird falls and hits the sea. In this case, you must check if the elevation of the bird is above a certain level.
- 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.
- 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
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!