In this lesson, we are going to be creating a simple “Flappy Bird” game using Python Turtle. Whether you are new to Python or have some prior experience, this project is a great way to learn more about making games and apply your knowledge in a practical and fun way.
Who is this Project For?
This is an intermediate-level project for those who are new to Python. At this point, you should already know the basics of creating functions such as lambda functions, using booleans and loops, as well as the basics of Turtle. These are the concepts that you will use and build upon as you code this project.
What Will We Learn?
This project focuses on using booleans to continually check whether the player has collided with any obstacles to run the game. The player will click on the game screen to move the ball upwards and avoid the obstacles. This game is fun and simple enough for beginner and intermediate programmers to create, so let’s get started!
Features to Consider:
When creating the game, there are a couple of concepts we need to keep in mind:
- The player can click to move the ball upwards
- When the player does not click the ball will fall towards the bottom of the screen
- If the player hits an obstacle the game stops
- The obstacles should appear randomly and move left across the screen.
This project can be broken down into 4 main steps:
- Set up libraries, variables, and lists
- Create the tap and inside functions
- Create the draw function
- Create the move function
- Write the final setup code
Step 1: Set up Libraries, Variables, and Lists:
The first thing we’ll need to do to begin the project is import all of the libraries and modules that we’ll be using. We will use the Turtle module for the graphics, the random module to create the random obstacle coordinates, and the freegames library to help move the player’s bird and the obstacles. To do this, let’s type:
Next, we’ll need to set up the “bird” as a vector with two values set to 0. This will ensure that the bird starts in the middle of the screen. Next, we’ll simply create a list named “balls”. This will be used to store the obstacles that players will need to dodge. Once this step is done, the code should be:
Step 2: Create the Tap and Inside Functions
Next, we’ll create the tap function. This will be a function that will move the bird up whenever the player clicks the screen. Start by creating a function by typing:
Now it’s time to create the code to make the bird move up when the player clicks the game screen. To do this, we will create an up command using a vector with values of 0 and 30. This will ensure that when the screen is clicked, the bird’s y-value will change by 30 pixels, making it move upwards. Then, we’ll add the command bird.move(up) to make the bird shift up 30 pixels. The code for this step will look like this:
Now it’s time to code the inside function. This is a function that will return a True value if the points on the screen are the same.
Hint: This will be used to tell if the bird has collided with any of the obstacles. The code for this function will be:
All this line of code will do is check whether point x and point y are both greater than -200 and less than 200.
Step 3: Code the Draw Function
Now that the tap and inside functions are complete it’s time to create the draw function. This function is responsible for making sure that the player and obstacles are in their proper positions. It will also change the colour of the player’s bird from green to red depending on whether the player collides with any obstacles. First, we’ll create the draw function and ensure we have a clear game screen to place the bird on by typing:
Next, it’s time to create an if-statement to check whether the player’s bird is “alive” or if they’ve hit an obstacle. To do this, the code will be:
Hint: This code will change the bird’s colour to red or green depending on whether the player has collided with an obstacle. Note that the update command at the end of the code will ensure that the bird’s position is constantly updated to ensure the game runs smoothly and as intended.
Step 4: Create the Move Function
Now it’s time to code the move function, which will make the obstacles move to the left on the game screen. We’ll start by creating the function, then adding two lines of code to change the bird and the balls’ positions: The bird will continually move down 5 pixels, and the balls will continually move left 3 pixels. The code for this step should look like this:
Hint: Note that we’ve created a for loop to ensure that all of the balls are moved continually left. If we didn’t use this loop, only one of the balls would move and we’d need to program each one individually.
Next, we’ll create an if-statement to continually create new obstacles and add them to the “balls” list. To do this, we will type:
This section of code will create new obstacles and randomly place them on the game screen for the player to dodge. The next section of code deals mainly with checking whether the player is touching the obstacles, as well as ensuring that there aren’t too many or too few balls on the screen. To complete this step, we’ll type:
What this code does is use the while loop and if-statements to check whether the balls are on the game screen and touching the player. If so, it returns the false value. The game will also end if the bird touches an obstacle or the edge of the game screen. Once this section of code is complete, it’s time to move on to set up the final lines of the project.
Step 5: Write the Final Setup Code
Finally, it’s time to set up the code that will help the game run. We’ll start by setting up the window with a width and height of 420X420 pixels. If you want the window to be bigger or smaller, adjust these values accordingly. We will also add a starting value of (370, 0), meaning that the bird will start 350 pixels away from the left edge of the game screen. Next, we’ll hide the turtle and set the tracer state to false. This means that the cursor will become invisible and the player will only see what is drawn. Additionally, the tracer state will be set to False. Next, we’ll run the up and move functions to make sure the game works. Finally, we’ll add a line of code to let the computer know that clicking on the screen should run the tap function. Once this is done, the project is finished and the code should look like this:
And now we’re done! Feel free to test your code now and see how it works. If you’re stuck or have any issues with your code, try reviewing it again either in your text editor or by looking at the code mentioned above as a reference. If you need more information, please check 40 Best Coding Games for Kids 2021