In: Computer Science
Draw an American Flag in Python turtle Height A=1 Width B=1.9 Blue C=0.5385 D=0.76 Star Pattern E=F=0.0538 G=H=0.0633 Strips K=0.0633 L=0.0769
Python code for the problem is provided below, please comment if any doubts:
Note: if the code indentation may lost on copying, then please refer the code screenshot to reconstruct the indentation.
Pyhton code:
import turtle
import time
# create the screen
flagScn = turtle.getscreen()
#set flag background
flagScn.bgcolor("white")
#set cursor properties
cursor = turtle.Turtle()
cursor.speed(1000)
cursor.penup()
#set the ht and wid
#convert incles to mm
height_flag = 1*250
width_of_flag = 1.9*250
#set the starting corodinates
x_co_begin = -237
y_co_begin = 125
#set the stripe sizes
height_red_strp = 0.0769*250
width_red_strp = width_of_flag
#set the size of the star
star_size = 0.0538*250
#function to create the main rectangeke
def main_rect_draw(co_x, co_y, ht, wid, color):
cursor.goto(co_x,co_y)
cursor.pendown()
cursor.color(color)
cursor.begin_fill()
cursor.forward(wid)
cursor.right(90)
cursor.forward(ht)
cursor.right(90)
cursor.forward(wid)
cursor.right(90)
cursor.forward(ht)
cursor.right(90)
cursor.end_fill()
cursor.penup()
#function to create the stars
def main_stars(co_x,co_y,color,length) :
cursor.goto(co_x,co_y)
cursor.setheading(0)
cursor.pendown()
cursor.begin_fill()
cursor.color(color)
for turn in range(0,5) :
cursor.forward(length)
cursor.right(144)
cursor.forward(length)
cursor.right(144)
cursor.end_fill()
cursor.penup()
#function to draw the strips
def strips_draw():
co_x = x_co_begin
co_y = y_co_begin
for stripe in range(0,6):
for color in ["red",
"white"]:
main_rect_draw(co_x, co_y, height_red_strp, width_red_strp,
color)
co_y = co_y -
height_red_strp
main_rect_draw(co_x, co_y, height_red_strp,
width_red_strp, 'red')
co_y = co_y - height_red_strp
#function to draw the navy square
def navy_square():
square_height = 0.5385*250;
square_width = 0.76*250;
main_rect_draw(x_co_begin, y_co_begin,
square_height, square_width, 'navy')
#function to create the main row stars
def main_stars_row():
gap_between_stars = 30
gap_between_lines = height_red_strp + 6
co_y = 112
for row in range(0,5) :
co_x = -222
for star in range (0,6)
:
main_stars(co_x, co_y, 'white', star_size)
co_x = co_x + gap_between_stars
co_y = co_y -
gap_between_lines
#function to create the pattern row stars
def pattern_stars_row():
gap_between_stars = 30
gap_between_lines = height_red_strp + 6
co_y = 100
for row in range(0,4) :
co_x = -206
for star in range (0,5)
:
main_stars(co_x, co_y, 'white', star_size)
co_x = co_x + gap_between_stars
co_y = co_y -
gap_between_lines
#draw the flag using the above functions
strips_draw()
navy_square()
main_stars_row()
pattern_stars_row()
cursor.hideturtle()
flagScn.mainloop()
Output:
Code screenshot: