Skip to main content

Python Turtle Example: Flappy Bird Development in under 10 mins!

Python Turtle Example | Develop Flappy Bird Clone in 10 mins

In the following tutorial, we have used following Python libraries:

1. random: To generate obstacles at random position.
2. turtle: To help provide directions to the player.
3. vector from freegames: To 2-dimensional vectors.

Before developing the game, here is a screenshot of what you'll be developing in 10 mins.

Python Turtle Example
Python Turtle Example
Steps to create Python Flappy Bird Game:

1. Open Pycharm and install 'freegames' package by going to Files > Settings > Project Interpreter.
2. Click on '+' sign on right and search for 'freegames' package.
3. Import packages in your project by writing following lines of code:

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

4. There are two variables required in the game, one for Bird object and another for an array of obstacles:

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

5. Create function for the player response on screen tap:

def tap(x, y):
    "Move bird up in response to screen tap."    up = vector(0, 40)
    bird.move(up)

6. If the player is on screen this function will return true and Game will remain in the play mode 

def inside(point):
    "Return True if point on screen."    return -200 < point.x < 200 and -200 < point.y < 200

7. The following function will keep game playing by drawing balls and change the bird color to red if it touches any ball object. (Ball is the obstacle here)

def draw(alive):
    "Draw screen objects."    clear()

    goto(bird.x, bird.y)

    if alive:
        dot(10, 'green')
    else:
        dot(10, 'red')

    for ball in balls:
        goto(ball.x, ball.y)
        dot(20, 'black')

    update()

8. The following function will move balls near to the bird and update their positions:

def move():
    "Update object positions."    bird.y -= 5
    for ball in balls:
        ball.x -= 3
    if randrange(10) == 0:
        y = randrange(-199, 199)
        ball = vector(199, y)
        balls.append(ball)

    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)

9. Now call the following functions from turtle library to start the game and instantiate player bird and balls:

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

10. You can find complete 67 lines of code here:

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

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

def tap(x, y):
    "Move bird up in response to screen tap."    up = vector(0, 40)
    bird.move(up)

def inside(point):
    "Return True if point on screen."    return -200 < point.x < 200 and -200 < point.y < 200
def draw(alive):
    "Draw screen objects."    clear()

    goto(bird.x, bird.y)

    if alive:
        dot(10, 'green')
    else:
        dot(10, 'red')

    for ball in balls:
        goto(ball.x, ball.y)
        dot(20, 'black')

    update()

def move():
    "Update object positions."    bird.y -= 5
    for ball in balls:
        ball.x -= 3
    if randrange(10) == 0:
        y = randrange(-199, 199)
        ball = vector(199, y)
        balls.append(ball)

    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)

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

Once created, Run the game and share your experiences below.

You can see some more Python tutorials here:




























Comments

Popular posts from this blog

How To Open PDF File in Android Application Example Included

Want to know How To Open PDF File in Android Application Example. To open a PDF File in Android Application, your app can take help from Free Android library available on Github.

Here are few apps made from PDF Library you can look at, click here to know more.

By using Android Library you are not only leveraging the hard work from another developer but also making it easier and quicker to develop an android app.
How To Open PDF File in Android Application Example  In this tutorial series, we will teach you how to open a pdf file in Android Application Example included with the help of Android PDFViewer library developed by Barteksc.

Before I proceed further, here is the screenshot of the kind of PDF Viewer that’ll be created using Android Studio.



 How To Open PDF File in Android Application Example

The app will open a specified pdf file which will be copied in the assets folder of your Android studio project. I've used bitcoin.pdf file, you can use any of your choices.
Software F…

Open Weather Map API Example | Create Android Weather App

In this tutorial blog, using open weather map API example, we will create an Android weather app. The Android App will able to access Open weather map website server and access weather details of particular location.

The App will look similar to the image shown below. It will fetch real time weather of the location provided in MainActivity.Java file.


Software / Files used:Android Studio (Version 3.x) Files Edited / Created:activity_main.xmlMainActivity.JavaWeather.JavaAndroidManifest.xml Steps Involved in Open Weather Map API Example: Here are the Steps you need to follow on ‘Open Weather map API Example’ to create Android Weather App:

1. Open the Open Weather map API link provided here. Sign up for Current Weather Data API key here.

2. Once the key is received, save it to notepad or on stick notes.

3. Open Android Studio and create a new Project with name, ‘FIMC Weather App’, we will select an empty activity in the wizard and wait for Gradle build.



4. Open AndroidManifest.xml…