Python Turtle - Code the USA Flag Tutorial

preview_player
Показать описание
Learn how to quickly draw the American flag using Python's Turtle module.

~ CODE ~

from turtle import *
setup(800, 500)
speed(0)

# Set up the start coordinates for the red stripes
stripe_x = -400
stripe_y = 250

# Draw one red stripe
def stripe():
color("firebrick")
begin_fill()
for i in range(2):
forward(800)
right(90)
forward(38)
right(90)
end_fill()

# Draw the 7 red stripes
for i in range(7):
penup()
goto(stripe_x, stripe_y)
pendown()
stripe()
stripe_y = stripe_y - 76

# Draw the blue rectangle
penup()
goto(-400, 250)
pendown()
color("navy")
begin_fill()
for i in range(2):
forward(350)
right(90)
forward(266)
right(90)
end_fill()

# Draw one star
def star():
color("white")
begin_fill()
for i in range(5):
forward(20)
right(144)
end_fill()

# Set up star coordinates
x1 = -380
y1 = 230

x2 = -350
y2 = 205

# Code for one even row of stars
def even_row():
global x1
global y1
for i in range(6):
penup()
goto(x1, y1)
pendown()
star()
x1 = x1 + 57

# Draw the 5 even rows of stars
for i in range(5):
even_row()
x1 = -380
y1 = y1 - 52

# Code for one odd row of stars
def odd_row():
global x2
global y2
for i in range(5):
penup()
goto(x2, y2)
pendown()
star()
x2 = x2 + 57

# Draw the 4 odd rows of stars
for i in range(4):
odd_row()
x2 = -350
y2 = y2 - 51

hideturtle()
Рекомендации по теме
Комментарии
Автор

first ever flag down in python thanks to you!!! Thank you for the clear guide!!

coldsynergy
Автор

How to change the flag vertically? Been trying to do this since Monday

__Najaaaa
Автор

Hi. I've been trying to replicate a flag using python turtle... I've watched a couple of yt channels about it, and your explanation works the best - so far :D - but I still have some questions though, such as how do you measure/figure out to put a certain shape (like: crest, star, etc.) at the certain exact desired and designated location of your will? I mean is there some technique to pinpoint and figure out the coordinate? Is there a software that can do the calculation of the pixel/coordinate of the picture or anything like that?... Do you have some tips or technique to do it? In this video you said that you did the math beforehand so how did you do it? I'd love to learn how you do it. Could you shed some light please? Thank you in advance.

ameiryahya
Автор

how do i implement while loop for the stars?

LeOrca
Автор

i tried to copy the code for the star, but it only fills the points, not the middle part, any idea how to fix this?

stitch
Автор

Is there any way I can calculate the coordinates?

웡-by
welcome to shbcf.ru