🇺🇸 United States of America USA flag using python turtle

Today we are here sharing the tutorial of creating USA FLAG using Python. So let us see the video tutorial, the source code and the output of the program.

SOURCE CODE: USA FLAG USING PYTHON

import turtle

# Set up the screen
wn = turtle.Screen()
wn.title("United States Flag")

# Create a new turtle named 'flag'
flag = turtle.Turtle()
flag.speed(0)  # Fastest speed

# Define a function to draw a star
def draw_star(x, y, size):
    flag.penup()
    flag.goto(x, y)
    flag.pendown()
    flag.setheading(90)  # The turtle faces upwards
    flag.begin_fill()
    for i in range(5):
        flag.forward(size)
        flag.right(144)
    flag.end_fill()

# Define a function to draw a stripe
def draw_stripe(y, color):
    flag.penup()
    flag.goto(-200, y)
    flag.pendown()
    flag.color(color)
    flag.setheading(0)  # Set the turtle to face to the right
    flag.begin_fill()
    for _ in range(2):
        flag.forward(400)
        flag.right(90)
        flag.forward(20)
        flag.right(90)
    flag.end_fill()

# Define a function to draw the blue union
def draw_union():
    flag.penup()
    flag.goto(-200, 160)
    flag.pendown()
    flag.color("midnight blue")
    flag.setheading(0)  # Set the turtle to face to the right
    flag.begin_fill()
    for _ in range(2):
        flag.forward(150)
        flag.right(90)
        flag.forward(140)
        flag.right(90)
    flag.end_fill()

# Draw the 13 stripes
for i in range(13):
    y = 160 - i * 20
    if i % 2 == 0:
        draw_stripe(y, "firebrick")
    else:
        draw_stripe(y, "white")

# Draw the blue union
draw_union()

# Draw the 50 stars in the union
star_size = 10  # Size of the stars
star_spacing_x = 25  # Horizontal space between stars
star_spacing_y = 14  # Vertical space between stars
start_y_offset = 10  # Offset from the top of the blue union

flag.color("white")
for row in range(9):
    for col in range(6 if row % 2 == 0 else 5):
        x = -190 + col * star_spacing_x + (0 if row % 2 == 0 else star_spacing_x / 2)
        y = 150 - row * star_spacing_y - start_y_offset
        draw_star(x, y, star_size)

# Hide the turtle
flag.hideturtle()

# Keep the window open
wn.mainloop()

OUTPUT:

Also Read: Indian Flag using Python

Leave a Reply

Your email address will not be published. Required fields are marked *