在Python turtle中绘制美国国旗。

huangapple go评论89阅读模式
英文:

Drawing a USA flag in Python turtle

问题

因为我使用翻译器,英语可能不自然。

我做了一段代码来绘制美国国旗,但遇到了问题。
我完成了一行星星,但不知道如何在底部重复相同的过程。

下面是我写的代码:

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

    # 起始点
    redx = -400
    redy = 250

    # 一条红色横线
    def stripe():
        color("red")
        begin_fill()
        for i in range(2):
            forward(800)
            right(90)
            forward(38)
            right(90)
        end_fill()
    
    # 七条红色横线
    for i in range(7):
        penup()
        goto(redx, redy)
        pendown()
        stripe()
        redy= redy - 76
    
    # 蓝色矩形
    penup()
    goto(-400, 250)
    pendown()
    color("blue")
    begin_fill()
    for i in range(2):
        forward(350)
        right(90)
        forward(266)
        right(90)
    end_fill()

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

    # 一行六个星星
    def sixstar():
        x1=-380
        y1=230
        for i in range(6):
            penup()
            goto(x1, y1)
            pendown()
            star()
            x1 = x1 + 57
        
    sixstar()
    hideturtle()

运行截图

我想知道如何解决这个问题。如果你有更好的代码,请告诉我。

英文:

Because I use a translator, English can be unnatural.

I made a code to draw the American flag, but it got stuck.
I've completed a line of stars, but I don't know how to do the same on the bottom line.

Below is the code that I wrote:

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

#시작점
redx = -400
redy = 250

#빨간줄 1개
def stripe():
    color("red")
    begin_fill()
    for i in range(2):
        forward(800)
        right(90)
        forward(38)
        right(90)
    end_fill()

#빨간줄 7개
for i in range(7):
    penup()
    goto(redx, redy)
    pendown()
    stripe()
    redy= redy - 76

#파란 사각형
penup()
goto(-400, 250)
pendown()
color("blue")
begin_fill()
for i in range(2):
    forward(350)
    right(90)
    forward(266)
    right(90)
end_fill()

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



#6개짜리 별 한줄
def sixstar():
    x1=-380
    y1=230
    for i in range(6):
        penup()
        goto(x1, y1)
        pendown()
        star()
        x1 = x1 + 57
    
sixstar()
hideturtle()

Run screen

I want to know how to solve it. Also please let me know if you have any better code.

答案1

得分: 2

你可以修改sixstar()函数,以包括一个绘制多行星星的循环,修改范围以绘制尽你所愿的行数。

def sixstar():
    x1 = -380
    y1 = 230
    # 循环绘制多行星星
    for j in range(5):
        for i in range(6):
            penup()
            goto(x1, y1)
            pendown()
            star()
            x1 = x1 + 57
        # 将x1重置为起始位置
        x1 = -380
        # 向下移动到下一行
        # 根据需要修改此值以设置行之间的距离。
        y1 = y1 - 50
英文:

You can modify the sixstar() function to include a loop that draws multiple rows of stars, modify the range to draw as many rows as you wish.

def sixstar():
    x1 = - 380
    y1 = 230
    # loop for drawing multiple rows of stars
    for j in range(5):
        for i in range(6):
            penup()
            goto(x1, y1)
            pendown()
            star()
            x1 = x1 + 57
        # reset x1 to the starting position
        x1 = - 380
        # move down to the next row
        # modify this as you wish to set the distance between rows.
        y1 = y1 - 50

huangapple
  • 本文由 发表于 2023年3月31日 20:26:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/75898510.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定