![]() |
VOOZH | about |
In this article, we are going to see how to make a flappy bird game in Pygame.
We all are familiar with this game. In this game, the main objective of the player is to gain the maximum points by defending the bird from hurdles. Here, we will build our own Flappy Bird game using Python.
We will be using Pygame(a Python library) to create this Flappy Bird game. Pygame is an open-source library that is designed for making video games. it helps us to create fully functional games and multimedia programs in python.
First of all, You have to install the Pygame library using the command:-
pip install pygame
Step 1: In this first step, we have to import libraries.
After that, we have to set the height and width of the screen to which the game will be played. Now we have to define some images which we shall use in our game like pipes as hurdles, birds images, and also a background image of the flappy bird game.
Step 2: After declaring game variables and importing libraries we have to initialize the Pygame
The used file can be downloaded from here.
Initialize the program using pygame.init() and set the caption of the window. Here pygame.time.Clock() will be used further in the main loop of the game to alter the speed the bird. Load the images from the system in pygame using pygame.image.load().
Step 3: Initialize the position of the bird and starting the game loop
Initialize the position of bird and sea level to the ground. Adding conditions in the loop defines the game conditions. The variable horizontal and vertical is used to set the position of the bird. We have to run the program until the user stops or exits (using sys.exit())if the program so, we create an infinite while loop.
Step 4: Create a function that generates a new pipe of random height
First of all, we have to fetch the height of the pipe using getheight() function. After this generates a random number between 0 to a number (such that the height of the pipe should be adjustable to our window height). After that, we create a list of dictionaries that contains coordinates of upper and lower pipes and return it.
Step 5: Now we create a GameOver() function which represents whether the bird has hit the pipes or fall into the sea.
According to my thought, three conditions lead to a situation of the game over. if the difference between our elevation and a certain height is less than vertical it means the bird has crossed its boundaries resulting in a game over and if the bird hits any of the lower and upper pip then this will also lead to game over condition.
Step 6: Now we will be creating our main function (flappygame()) that will do the following things:
Initialize the variables and creating two pipes by createPipe() function. Create two lists first is of lower pipes and the other is of lower pipes. Defining the bird velocity, minimum bird velocity, maximum bird velocity, and pipes velocity. Handle the key events using pygame.event.get() and checking for the game is over or not if it is over return from the function. Updating the score and blit game images such as background, pipe, and bird on the window.
Output: