Explore

Turtle Programs

Draw American Flag In Python With Code

Draw American Flag In Python

Want to draw american flag in python then you are at the right place today in this tutorial I will show you how to draw american flag in python programming with code so read this guide till the end.

We have created many tutorials on drawing things with python. You can check them out and you will find them interesting and useful. 

To draw us flag in python, we will use the python turtle module to draw the usa flag in python. I will provide you with the code of this program, so follow up with me till the end.

Code To Draw American Flag In Python


import turtle
import time

# create a screen
screen = turtle.getscreen()

# set background color of screen
screen.bgcolor("white")

# set tile of screen
screen.title("American Flag")

t = turtle.Turtle()
# set the cursor/turtle speed. Higher value, faster is the turtle
t.speed(100)
t.penup()

# decide the shape of cursor/turtle
t.shape("turtle")

# flag height to width ratio is 1:1.9
flag_height = 250
flag_width = 475

# starting points
# start from the first quardant, half of flag width and half of flag height
start_x = -237
start_y = 125

# For red and white stripes (total 13 stripes in flag), each strip width will be flag_height/13 = 19.2 approx
stripe_height = flag_height/13
stripe_width = flag_width

# length of one arm of star
star_size = 10


def draw_fill_rectangle(x, y, height, width, color):
    t.goto(x,y)
    t.pendown()
    t.color(color)
    t.begin_fill()
    t.forward(width)
    t.right(90)
    t.forward(height)
    t.right(90)
    t.forward(width)
    t.right(90)
    t.forward(height)
    t.right(90)
    t.end_fill()
    t.penup()

def draw_star(x,y,color,length) :
    t.goto(x,y)
    t.setheading(0)
    t.pendown()
    t.begin_fill()
    t.color(color)
    for turn in range(0,5) :
        t.forward(length)
        t.right(144)
        t.forward(length)
        t.right(144)
    t.end_fill()
    t.penup()


# this function is used to create 13 red and white stripes of flag
def draw_stripes():
    x = start_x
    y = start_y
    # we need to draw total 13 stripes, 7 red and 6 white
    # so we first create, 6 red and 6 white stripes alternatively    
    for stripe in range(0,6):
        for color in ["red", "white"]:
            draw_fill_rectangle(x, y, stripe_height, stripe_width, color)
            # decrease value of y by stripe_height
            y = y - stripe_height            

    # create last red stripe
    draw_fill_rectangle(x, y, stripe_height, stripe_width, 'red')
    y = y - stripe_height


# this function create navy color square
# height = 7/13 of flag_height
# width = 0.76 * flag_height
# check references section for these values
def draw_square():
    square_height = (7/13) * flag_height
    square_width = (0.76) * flag_height
    draw_fill_rectangle(start_x, start_y, square_height, square_width, 'navy')


def draw_six_stars_rows():
    gap_between_stars = 30
    gap_between_lines = stripe_height + 6
    y = 112
    # create 5 rows of stars
    for row in range(0,5) :
        x = -222
        # create 6 stars in each row
        for star in range (0,6) :
            draw_star(x, y, 'white', star_size)
            x = x + gap_between_stars
        y = y - gap_between_lines


def draw_five_stars_rows():
    gap_between_stars = 30
    gap_between_lines = stripe_height + 6
    y = 100
    # create 4 rows of stars
    for row in range(0,4) :
        x = -206
        # create 5 stars in each row
        for star in range (0,5) :
            draw_star(x, y, 'white', star_size)
            x = x + gap_between_stars
        y = y - gap_between_lines

# start after 5 seconds.
time.sleep(5)
# draw 13 stripes
draw_stripes()
# draw squares to hold stars
draw_square()
# draw 30 stars, 6 * 5
draw_six_stars_rows()
# draw 20 stars, 5 * 4. total 50 stars representing 50 states of USA
draw_five_stars_rows()
# hide the cursor/turtle
t.hideturtle()
# keep holding the screen until closed manually
screen.mainloop()

Above is the python code to draw the American flag in python. First create a new folder for this project, open it in a code editor of your choice, create a python file and paste the above python code.

As I have said I have used the turtle module to draw the US flag, so do you have to install the turtle library? No, you don’t have to install the turtle library because it comes installed when you set up python on your computer.

But still if you get any errors like turtle module is missing then you can use the below command to install turtle module.


pip install turtle

It will install the module on your computer. Now you can run the program on your computer and see the american flag drawing in python to run, or you can use an online python compiler.  

It will open a new window and it will start drawing the american flag below is the image output of the program

American flag python output

As you can see, It successfully draws the american flag in python. I hope you were able to run the program successfully without any errors.

Summary

This was a program to draw american flag in python. I hope you found this python tutorial helpful and useful. Do share this guide with your friends or anyone who needs it.

Here are more python drawing tutorials for you:

I hope you found what you were looking for from this python tutorial. If you want more python tutorials like this, do join our Telegram channel for future updates.

Thanks for reading, have a nice day 🙂