【问题标题】:Turtle graphics, draw a star?乌龟图形,画星星?
【发布时间】:2014-10-14 08:56:11
【问题描述】:

我要画一个实心星,如:

到目前为止我有这个代码:

def draw_star(size,color):
    count = 0
    angle = 144
    while count <= 5:
        turtle.forward(size)
        turtle.right(angle)
        count += 1
    return

draw_star(100,"purple")

我想用函数传递的任何颜色填充星号。我该怎么做?

【问题讨论】:

  • 您遇到了完全一致的错误。一侧将被绘制两次

标签: python turtle-graphics


【解决方案1】:

要获得一个 5 角星,您应该为每边画 2 条线。角度需要加到 72 (360/5)

import turtle
def draw_star(size, color):
    angle = 120
    turtle.fillcolor(color)
    turtle.begin_fill()

    for side in range(5):
        turtle.forward(size)
        turtle.right(angle)
        turtle.forward(size)
        turtle.right(72 - angle)
    turtle.end_fill()
    return

draw_star(100, "purple")

angle的不同值进行实验,得到你想要的形状

【讨论】:

  • 我喜欢这个解决方案的外观,但我对size 参数有疑问——它与什么有关?我认为size 是一个圆的半径,例如,星星应该放在其中。然而在这个实现中,图像的大小会随着anglesize 的变化而变化,因此很难正确调整大小。
  • 我在 turtle end_fill 文档中添加了一条注释,根据操作系统图形、重叠的性质和重叠的数量,重叠区域可能会或可能不会被填充。
【解决方案2】:

turtle documentation中搜索“填写”:

def draw_star(size,color):
    count = 0
    angle = 144
    turtle.fillcolor(color)
    turtle.begin_fill()
    for _ in range(5):
        turtle.forward(size)
        turtle.right(angle)
    turtle.end_fill()

draw_star(100,"purple")

注意return 是多余的,通过像这样对循环进行编码,它不会绘制两次轮廓。

【讨论】:

  • 在某些系统上,这显示了@KonradRudolph 在他的回答中描述的“交替”填充问题。
【解决方案3】:
def draw_star(size, color):
...:     turtle.reset()
...:     turtle.color(color)
...:     turtle.fillcolor(color)
...:     turtle.begin_fill()
...:     turtle.lt(260)
...:     for _ in range(5):
...:         turtle.fd(size)
...:         turtle.lt(170)
...:         turtle.fd(size)
...:         turtle.rt(100)
...:     turtle.end_fill()
...:     
...:draw_star(size, color)

【讨论】:

  • 这看起来不像 OP 的示例明星。 fillcolor() 调用与前面的.color() 调用是多余的。请在您的回答中解释此解决方案对现有讨论的添加。
【解决方案4】:

turtle.fill 进行实验。但是,如果您只是在代码中使用它而不做进一步更改,您将获得“交替”填充:

您需要调整您的例程以绘制没有相交线的星星的轮廓(可以通过在两个角度之间交替来完成),或者分别填充星星的内部(通过追踪内接多边形)。

【讨论】:

  • 不给我交替填充。
  • @poke 绝对应该,看看我的截图。 Eric 答案中的代码也是如此。
  • 好吧,我既使用了 OP 的代码,在开头添加了 turtle.fill(True),也使用了 Eric 的代码,都没有给我这个结果。它们都显示了穿过星星的线条,但填充已完成。
  • @poke 这很有趣:这可能取决于系统如何实现海龟图形。我正在使用 OS X 及其 libtk。我假设 TK 在不同系统上实现此操作的方式肯定有所不同。
  • 可能。我在 Windows 上,尝试了 Python 2.7 和 3.4。
【解决方案5】:
def create_star (pointer, color_mix, central_point_x_value,\
                    central_point_y_value, length, height):
    travel_distance = length * .223
    pointer.home() # resets the orientation of the pointer
    #   without reseting everything
    pointer.penup()

    pointer.goto (central_point_x_value,central_point_y_value)
    pointer.left(90)
    pointer.forward(height) #move to the top of the star
    pointer.left(18) # must straighten the direction to match the iteration needs
    #draw Star , going counterclockwise
    # starting at the top and ending at the top of each outside triangle
    pointer.pendown()
    pointer.fillcolor (color_mix)
    pointer.begin_fill()
    count = 5
    while count != 0:
        #draw the star
        pointer.left(144)
        pointer.forward(travel_distance)
        pointer.right(72)
        pointer.forward(travel_distance)

        count -=1
    pointer.end_fill()

【讨论】:

  • 你能解释一下你的参数吗?如果我在屏幕中间创建一个半径为 50 的圆,然后执行以下操作:create_star(turtle.Turtle(), "purple", 0, 0, 100, 100) 我期待屏幕中间有一颗星星“填满”我的圆——相反,我得到一颗只有我一半大小的星星坐在我的圈子上方的圈子。
【解决方案6】:

如果你在 Windows 上,你可能会逃脱:

turtle.color("purple")
turtle.begin_fill()
turtle.circle(100, extent=720, steps=5)
turtle.end_fill()

但是,这段代码有两个问题:它与您的插图中的 style 星号不同;它在所有 Python turtle/tkinter 实现上的工作方式都不一样(有些只显示部分填充):

这是一个使用 stamping 而不是 drawing 的替代实现,应该可以解决这两个问题:

STAR_SIZE = 100

EXPANSION = 1.2
TRANSLATION = STAR_SIZE * EXPANSION / 4

turtle.hideturtle()
turtle.color("purple")
turtle.shape("triangle")
turtle.turtlesize(STAR_SIZE * EXPANSION / 20)

for _ in range(5):
    turtle.right(72)
    turtle.forward(TRANSLATION)
    turtle.stamp()
    turtle.backward(TRANSLATION)

【讨论】:

    【解决方案7】:

    我做得很快,这很容易更正。随意评论一个更好的解决方案:)

    import turtle 
    
    x = turtle.Turtle()
    x.speed(0)
    
     def draw_star(length, angle):
    
     for i in range(5): #the star is made out of 5 sides
      x.forward(length)
      x.right(angle)
    
    
    for i in range(1):
     x.color("purple") #if  you want outline of the star choose .color("purple" + "outline color")
     x.begin_fill()
     draw_star(100, 144) #144 is the perfect angle for a star
     x.end_fill()
    

    【讨论】:

      【解决方案8】:

      不妨试试这个:

      turtle.write("★", font=("Arial", 40, "normal"))
      

      这只是写给乌龟,所以这可能无济于事,但你可以试试。

      【讨论】:

        【解决方案9】:
        from turtle import *
        hideturtle()
        
        def draw_star(sidesize, points, alfacorner, color):
            betacorner = 360/points+alfacorner
            fillcolor(color)
            begin_fill()
            for x in range(points*2):
                forward(sidesize)
                if x % 2 == 0:
                    left(180-alfacorner)
                else:
                    right(180-betacorner)
            end_fill()
        
        
        draw_star(70, 8, 90, 'grey')
        draw_star(90, 5, 72, 'yellow')
        draw_star(120, 5, 36, 'red')
        draw_star(65, 6, 60, 'darkblue')
        draw_star(80, 4, 45, 'darkviolet')
        draw_star(80, 3, 30, 'greenyellow')
        
        exitonclick()
        

        【讨论】:

          【解决方案10】:

          这应该可行,请随时提出任何问题!

          def draw_star(size,color):
              count = 0
              angle = 144
              turtle.color(color)
              turtle.begin_fill()
              while count <= 5:
                  turtle.forward(size)
                  turtle.right(angle)
                  count += 1
              turtle.end_fill()
              return
          
          draw_star(100,"purple")
          

          【讨论】:

            【解决方案11】:
                    ...:def draw_star(size):
                    ...:     turtle.reset()
                    ...:     turtle.color("silver")
                    ...:     turtle.fillcolor("yellow")
                    ...:     turtle.begin_fill()
                    ...:     turtle.lt(260)
                    ...:     for _ in range(5):
                    ...:         turtle.fd(size)
                    ...:         turtle.lt(170)
                    ...:         turtle.fd(size)
                    ...:         turtle.rt(100)
                    ...:     turtle.end_fill()
                    ...:
            

            draw_star(在这里放一个尺寸)

            【讨论】:

            • ...: 是什么意思?
            猜你喜欢
            • 2020-10-13
            • 2014-12-14
            • 1970-01-01
            • 2022-12-30
            • 2022-01-20
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2016-10-02
            相关资源
            最近更新 更多