Editor

Pakistan Flag in Python Turtle

Intermediate · Flags · 92 lines · draws in about 13 seconds

This program draws the flag of Pakistan by its official construction, making the crescent by covering a white disc with a slightly shifted green one.

Pakistan Flag in Python Turtle: the finished drawing
import math
import turtle
# The flag is 3 units wide for every 2 units high
WIDTH = 540
HEIGHT = 360
WHITE_BAND = WIDTH / 4
GREEN = "#01411C"
LEFT = -WIDTH / 2
RIGHT = WIDTH / 2
TOP = HEIGHT / 2
screen = turtle.Screen()
screen.title("Flag of Pakistan")
screen.bgcolor("#dde6ee")
t = turtle.Turtle()
t.speed(6)
def rectangle(x, y, width, height, color):
"""Draw a filled rectangle whose top-left corner is at (x, y)."""
t.penup()
t.goto(x, y)
t.setheading(0)
t.pendown()
t.color(color)
t.begin_fill()
for side in range(2):
t.forward(width)
t.right(90)
t.forward(height)
t.right(90)
t.end_fill()
def disc(x, y, radius, color):
"""Draw a filled circle centred at (x, y)."""
t.penup()
t.goto(x, y - radius)
t.setheading(0)
t.pendown()
t.color(color)
t.begin_fill()
t.circle(radius, steps=90)
t.end_fill()
def on_diagonal(distance):
"""The point `distance` from the centre of the green part, towards the top-right corner."""
t.penup()
t.goto(centre_x, 0)
t.setheading(t.towards(RIGHT, TOP))
t.forward(distance)
return t.xcor(), t.ycor()
# White band at the flagpole side, dark green for the rest
rectangle(LEFT, TOP, WHITE_BAND, HEIGHT, "white")
rectangle(LEFT + WHITE_BAND, TOP, WIDTH - WHITE_BAND, HEIGHT, GREEN)
# The crescent and the star sit on the line from the centre of the
# green part to the top-right corner
centre_x = LEFT + WHITE_BAND + (WIDTH - WHITE_BAND) / 2
half_diagonal = math.dist((centre_x, 0), (RIGHT, TOP))
# Crescent: a white disc, then a green disc whose centre is 13/20 of the
# height from the corner. The white that is still showing is the crescent.
disc(centre_x, 0, HEIGHT * 3 / 10, "white")
x, y = on_diagonal(half_diagonal - HEIGHT * 13 / 20)
disc(x, y, HEIGHT * 11 / 40, GREEN)
# Star: its centre is 1/5 of the height up the line, and one point aims
# at the corner. on_diagonal() leaves the turtle there, facing the corner.
on_diagonal(HEIGHT / 5)
radius = HEIGHT / 10
edge = radius * 0.7265
t.forward(radius)
t.right(162)
t.pendown()
t.color("white")
t.begin_fill()
for point in range(5):
t.forward(edge)
t.left(72)
t.forward(edge)
t.right(144)
t.end_fill()
t.hideturtle()
turtle.done()

How the code works

Points on the diagonal

on_diagonal() goes to the centre of the green part, turns to face the top-right corner with towards(), and moves distance forward. It returns where the turtle ends up and leaves it facing the corner.

def on_diagonal(distance):
    """The point `distance` from the centre of the green part, towards the top-right corner."""
    t.penup()
    t.goto(centre_x, 0)
    t.setheading(t.towards(RIGHT, TOP))
    t.forward(distance)
    return t.xcor(), t.ycor()

A crescent from two discs

The white disc has a radius of 3/10 of the height. A green disc with a radius of 11/40 of the height, centred 13/20 of the height from the corner, covers most of it and leaves a crescent.

disc(centre_x, 0, HEIGHT * 3 / 10, "white")
x, y = on_diagonal(half_diagonal - HEIGHT * 13 / 20)
disc(x, y, HEIGHT * 11 / 40, GREEN)

A star aimed at the corner

The star's centre is 1/5 of the height up the diagonal and its points reach 1/10 of the height out. The turtle goes out to the point facing the corner, turns right 162 degrees and traces the outline.

on_diagonal(HEIGHT / 5)
radius = HEIGHT / 10
edge = radius * 0.7265
t.forward(radius)
t.right(162)
t.pendown()
t.color("white")
t.begin_fill()
for point in range(5):
    t.forward(edge)
    t.left(72)
    t.forward(edge)
    t.right(144)
t.end_fill()

Make it yours

Change the code above and press Run. Some ideas:

  • Change GREEN in disc(x, y, HEIGHT * 11 / 40, GREEN) to "orange" to see the disc that cuts out the crescent.
  • Change on_diagonal(HEIGHT / 5) to on_diagonal(HEIGHT / 3) to move the star towards the corner.
  • Change WIDTH = 540 and HEIGHT = 360 to 450 and 300: every part is worked out from them, so the whole flag shrinks.

What it uses

Every turtle command the site supports is listed on Python turtle online.