▶️Python Project: How to make THE UNITED STATES FLAG with Python

in python •  last year  (edited)

Hello everyone! In this blog I will show you how to make the United States flag with Python. It's a fun and exciting project that you can do in your spare time.

https://www.youtube.com/shorts/OrSkOEAEWm4

To get started, you will need to have Python installed on your computer. If you don't already have it, you can download it from the official Python website.

Once you have Python installed and the turtle library, copy the following code into your favorite code editor (e.g. PyCharm or VS Code) and save the file with the name of your choice, followed by the ".py" extension.

import turtle
import time

screen = turtle.getscreen()
screen.bgcolor("black")
screen.title("USA Flag")

oogway = turtle.Turtle()
oogway.speed(100)
oogway.penup()
oogway.shape("turtle")

flag_height = 250
flag_width = 475

start_x = -237
start_y = 125

stripe_height = flag_height/13
stripe_width = flag_width

star_size = 10


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


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



def draw_stripes():
    x = start_x
    y = start_y
    for stripe in range(0,6):
        for color in ["red", "white"]:
            draw_fill_rectangle(x, y, stripe_height, stripe_width, color)
            y = y - stripe_height

    draw_fill_rectangle(x, y, stripe_height, stripe_width, 'red')
    y = y - stripe_height


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
    for row in range(0,5) :
        x = -222
        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
    for row in range(0,4) :
        x = -206
        for star in range (0,5) :
            draw_star(x, y, 'white', star_size)
            x = x + gap_between_stars
        y = y - gap_between_lines


time.sleep(5)
draw_stripes()
draw_square()
draw_six_stars_rows()
draw_five_stars_rows()
oogway.hideturtle()

screen.mainloop()

The process for creating the US flag with Python using the turtle module is relatively straightforward, although it may be a bit complex for those unfamiliar with programming.

First, you import the turtle module and create a window to draw the flag. Next, you define a number of variables for the size and position of the flag, as well as for the size of the stripes and stars.

Next, two functions are defined: one to draw a rectangle filled with a given color and one to draw a five-pointed star. These functions will be used later to draw the stripes and stars on the flag.

The next step is to draw the red and white stripes on the flag. This is done by looping through the first six stripes (three red and three white) and alternating the color. A seventh red stripe is then drawn and moved down to make room for the blue square.

The blue square is then drawn, and the rectangle function is used to draw it. The size and position of the square is calculated based on the size of the flag.

Finally, the white stars are drawn on the blue square. This is done using two loops: one for the top six rows of stars (each row has six stars) and one for the bottom four rows (each row has five stars). The star function is used to draw each star in its corresponding position.

Once you have drawn all the stripes and stars, run the code and it should look like the video above.

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!