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.

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
GREENindisc(x, y, HEIGHT * 11 / 40, GREEN)to"orange"to see the disc that cuts out the crescent. - Change
on_diagonal(HEIGHT / 5)toon_diagonal(HEIGHT / 3)to move the star towards the corner. - Change
WIDTH = 540andHEIGHT = 360to450and300: every part is worked out from them, so the whole flag shrinks.
What it uses
- functions that return values
- towards and setheading
- begin_fill and end_fill
Every turtle command the site supports is listed on Python turtle online.