Ticker

6/recent/ticker-posts

Advertisement

Drawing flag of USA using python turtle

Flag of USA using python

 Hey everyone today we're going to  learn about how to draw Flag Of United States Of America by using in python turtle library. The turtle library from python is perfect library for starting programmers. It can be used  to draw shape, create designs, create awesome animations and you can even create mini-games with it. It is already preinstalled in python so you dont have to install anything. So, let get started:


Source code:

import turtle

import time

# for create a screen 

screen = turtle.Screen()



# set a background color

screen.bgcolor("white")



# set a drawer

drawer = turtle.Turtle()



# set the drawer speed


drawer.speed(50)

drawer.penup()

# create  a shape of cursor

drawer.shape("turtle")

# set a flag height ang weight

f_width = 475

f_height = 250

# set a starting point


start_x = -237

start_y = 125

# for creating the red and stripes of flag

stripe_height = f_height/13

stripe_width = f_width

# length of one arm of star

str_size = 10

# create a function to draw a rectangles.

def draw_fill_rect(x, y, height, width, color):

drawer.goto(x, y)

drawer.pendown()

drawer.color(color)

drawer.begin_fill()

drawer.forward(width)

drawer.right(90)

drawer.forward(height)

drawer.right(90)

drawer.forward(width)

drawer.right(90)

drawer.forward(height)

drawer.right(90)

drawer.end_fill()

drawer.penup()

def draw_star(x, y, color, length):

drawer.goto(x, y)

drawer.setheading(0)

drawer.pendown()

drawer.begin_fill()

drawer.color(color)

for turn in range(0, 5):

drawer.forward(length)

drawer.right(144)

drawer.forward(length)

drawer.right(144)

drawer.end_fill()

drawer.penup()







# create a function to create 13 stripe of flag.

def draw_stripe():

x = start_x

y = start_y



# create for loop to draw  6 white and 6 red stripes

for i in range(0, 6):

for color in ["red", "white"]:

draw_fill_rect(x, y, stripe_height, stripe_width, color)

y = y - stripe_height



# create a last red stripe

draw_fill_rect(x, y, stripe_height, stripe_width, "red")

y = y - stripe_height



# create a function to draw navy color square


def  draw_square():

square_height = (7/13) * f_height

square_width = (0.76) * f_height

draw_fill_rect(start_x, start_y, square_height, square_width, "navy")

def draw_six_star_rows():

gap_betwn_stars = 30

gap_betwn_lines = stripe_height + 6

y = 112



# create a for loop to draw 5 star in row.

for row in range(0, 5):

x = -222

# create six star in each row.

for star in range (0,6):

draw_star(x, y, "white", str_size)

x = x + gap_betwn_stars

y = y - gap_betwn_lines



def draw_five_star_rows():

gap_betwn_stars = 30

gap_betwn_lines = stripe_height + 6

y = 100



# create a four star


for row in range(0, 4):

x = -206

# create a 5 star in each row.

for star in range(0, 5):

draw_star(x, y, "white", str_size)

x = x + gap_betwn_stars

y = y - gap_betwn_lines



# start after 0.5 sec

time.sleep(0.5)

# draw 13 stripes


draw_stripe()

# draw square

draw_square()

#for  drawing 30 stars, 6 * 5

draw_six_star_rows()

# 5 * 4, total 20 stars and sum of 30 + 20 = 50 represents the 50 states of USA

draw_five_star_rows()

# hide the drawer cursor

drawer.ht()

# for holding the mainscreen.

screen.mainloop()


output:





If you find this tutorial is helpful for you then please share with your friends. I'll be back with another tutorial soon. Stay tuned, stay happy.

Post a Comment

0 Comments