Coding games for kids: Python Flappy Bird Game

Table Of Content:

Introduction:

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.
python flappy bird


Main Steps:

This project can be broken down into 4 main steps:

  1. Set up libraries, variables, and lists
  2. Create the tap and inside functions
  3. Create the draw function
  4. Create the move function
  5. 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:

from random import * from turtle import *
from freegames import vector

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:

bird = vector(0, 0)
balls = []


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:

def tap(x, y):

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:

up = vector(0, 30)
bird.move(up)

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:
return -200 < point.x < 200 and -200 < point.y < 200

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:

def draw(alive):
 
clear()
goto(bird.x, bird.y)

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:

if alive:
 dot(10,'green') 
Else:
 dot(10,'red')
 update()
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.
python flappy bird

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:

def move():
 bird.y -=5
 for ball in balls:
  ball.x -= 3
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:

if randrange(10) == 0:
 y = randrange(-199, 199)
 ball = vector(199, y)
 balls.append(ball)

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:

while len(balls) > 0 and not inside(balls[0]):
    balls.pop(0)
 
if not inside(bird):
    draw(False)
    return
 
for ball in balls:
    if abs(ball - bird) < 15:
       draw(False)
       return
draw(True)
ontimer(move, 50)

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:

setup(420, 420, 0, 0)
hideturtle()
tracer(False)
up()
onscreenclick(tap)
move()
done()

python flappy bird


Project Complete!

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

Geek Team

Geekedu is an expert in Coding and Math learning. Our goal is to inspire and empower youth to use their knowledge of technology to become the influencers, inventors and innovators of the future.

Sign up and get a 60-minute free assessment class

Book A FREE Trial