Draw Flag of USA using Python Turtle

Draw Flag of USA using Python Turtle

Introduction

Welcome everyone to violet-cat-415996.hostingersite.com . In this tutorial, we are going to draw the Flag of USA using Python Turtle. The python turtle module is very easy to understand and learn. Python beginners can easily grasp the turtle functions as we are providing the comments and a detailed explanation of the code.

Complete Code to Draw the Flag of USA using Python Turtle

#import the time and turtle module
import turtle
import time

# create a screen
scr = turtle.getscreen()
scr.title("Flag of America")
scr.bgcolor("white")

#Set the turtle object and speed of the turtle

t = turtle.Turtle()
t.speed(20)
t.penup()

# flag height and width
flag_ht = 250
flag_wth = 475

# starting points of the flag
x1 = -250
y1 = 120

# red and white stripes (total 13 stripes in flag)
stripe_ht = flag_ht/13
stripe_wdt = flag_wth
#star size
star_size = 12


def draw_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 star_shape(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()


# function to create stripes of flag
def draw_stripes():
    x = x1
    y = y1
    # draw 6 red and 7 white strips
    for stripe in range(0,6):
        for color in ["red", "white"]:
            draw_rectangle(x, y, stripe_ht, stripe_wdt, color)
            # decrease value of y by stripe_height
            y = y - stripe_ht

    # create last red stripe
    draw_rectangle(x, y, stripe_ht, stripe_wdt, 'red')
    y = y - stripe_ht


# this function create navy color square
def draw_square():
    square_ht = (7/13) * flag_ht
    square_wdt = (0.76) * flag_ht
    draw_rectangle(x1, y1, square_ht, square_wdt, 'navy')

#defining a function for drawing a 6 row star
def stars1():
    dist_of_stars = 30
    dist_bet_lines = stripe_ht + 6
    y = 112
    # create 5 rows of stars
    for row in range(0,5) :
        x = -234
        # create 6 stars in each row
        for star in range (0,6) :
            star_shape(x, y, 'white', star_size)
            x = x + dist_of_stars
        y = y - dist_bet_lines


def stars_five():
    dist_of_stars = 30
    dist_bet_lines = stripe_ht + 6
    y = 100
    # create 4 rows of stars
    for row in range(0,4) :
        x = -217
        # create 5 stars in each row
        for star in range (0,5) :
            star_shape(x, y, 'white', star_size)
            x = x + dist_of_stars
        y = y - dist_bet_lines
        
#Call all the functions
# start after 5 seconds.
time.sleep(5)
# draw 13 stripes
draw_stripes()
# draw squares 
draw_square()
# draw 30 stars, 6 * 5
stars1()
# draw 20 stars, 5 * 4
stars_five()
# hides the turtle
t.hideturtle()
scr.mainloop()

Output:

Output to Draw Flag of USA using Python Turtle

Now, let’s understand code by breaking it into sub-parts.

1. Importing the Modules

#import the time and turtle module
import turtle
import time

In our program, the import function imports the turtle and the time module which provides us with some inbuilt functions which is required while creating the program.

2. Initializing the Turtle

# create a screen
scr = turtle.getscreen()
scr.title("Flag of America")
scr.bgcolor("white")

#Set the turtle object and speed of the turtle

t = turtle.Turtle()
t.speed(20)
t.penup()

Here we have created an scr object by using the getscreen() function of the turtle. It creates the output window and, sets the title of the window and background color.

The turtle object ‘t’ is created and we have set the speed of the turtle to 20 by using the speed function. The penup() function doesn’t allow the turtle to draw anything till the next instruction.

3. Initialization of variables

# flag height and width
flag_ht = 250
flag_wth = 475

# starting points of the flag
x1 = -250
y1 = 120

# red and white stripes (total 13 stripes in flag)
stripe_ht = flag_ht/13
stripe_wdt = flag_wth
#star size
star_size = 12

Here we have initialized the height and width of the flag to 250 and 475 respectively. The x1 and y1 are starting coordinates for the flag. The flag height is divided by 13 as we want 13 strips for the flag i.e approximately 19.3 is the height of each stripe that is assigned to strip_ht. The size of each arm of the star is initialized to 12.

4. Function to create a Rectangle for the Flag of USA using Python Turtle

def draw_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()

In the function draw_rectangle() we have given the parameters x,y,height,width,color. These parameters will take the values to draw the rectangle shape accordingly. The goto(x,y) function sets the x,y location of the rectangle. The pendown() function begin’s the drawing. The color function takes the color that is filled in the shape using The begin_fill() and end_fill() function.

5. Function for creating a star shape

def star_shape(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()

Here we have created a function star_shape() which takes the x,y, color, and length parameters. The setheading (0) allows the turtle to always point to the east direction while drawing the star. Here we have created the for loop that takes 5 turns to draw a star of a specified length. The right(144) is the angle provided to generate a star shape.

6. Function for creating the stripes of the Flag of USA using Python Turtle

# function to create stripes of flag
def draw_stripes():
    x = x1
    y = y1
    # draw 6 red and 7 white strips
    for stripe in range(0,6):
        for color in ["red", "white"]:
            draw_rectangle(x, y, stripe_ht, stripe_wdt, color)
            # decrease value of y by stripe_heighFt
            y = y - stripe_ht

    # create last red stripe
    draw_rectangle(x, y, stripe_ht, stripe_wdt, 'red')
    y = y - stripe_ht

Here, the function is used to create the red and white strips of the American flag where x and y are initialized to x1 and y1 i.e. the starting position. We are drawing 6 red and 7 white strips for that we are using the for loop function and another for loop for the colors “red “ and “white”. The draw rectangle function will take the parameters from here. For each iteration, the value of y is decreased by the strip height and assigned to y.

7. Function for creating a navy color square of Flag of USA using Python Turtle

# this function create navy color square
def draw_square():
    square_ht = (7/13) * flag_ht
    square_wdt = (0.76) * flag_ht
    draw_rectangle(x1, y1, square_ht, square_wdt, 'navy')

The function draw_square() draws the square that is present on the American flag. The values for the square height and width are calculated using the flag’s height. The function draw rectangle takes the parameters including the color” navy” to create the square on the flag.

8. Function for creating the 6 stars rows

#defining a function for drawing a 6 row star
def stars1():
    dist_of_stars = 30
    dist_bet_lines = stripe_ht + 6
    y = 112
    # create 5 rows of stars
    for row in range(0,5) :
        x = -234
        # create 6 stars in each row
        for star in range (0,6) :
            star_shape(x, y, 'white', star_size)
            x = x + dist_of_stars
        y = y - dist_bet_lines

The function stars1() draws a 6-row star in a navy blue square. The distance between each star is 30 and the distance between the lines is initialized to addition on strip height and 6. The for loop function draws the 6-star rows where x location is initialized to -234. For each iteration, the location of x is increased by 30. The y points location is decreased by the value assigned to dist_bet_lines.

9. Function for creating the 5 stars rows

def stars_five():
    dist_of_stars = 30
    dist_bet_lines = stripe_ht + 6
    y = 100
    # create 4 rows of stars
    for row in range(0,4) :
        x = -217
        # create 5 stars in each row
        for star in range (0,5) :
            star_shape(x, y, 'white', star_size)
            x = x + dist_of_stars
        y = y - dist_bet_lines

The function stars_five() is similar to the above function stars1() the only change is in the location of the x and y points.

Here, we successfully completed drawing the Draw Flag of the USA using the Python Turtle module. Hope you find this article useful. For more articles on turtle keep visiting our website.


Also Read:

Share:

Author: Ayush Purawr