【问题标题】:Why is turtle not filling in a section of a closed shape为什么乌龟没有填充封闭形状的一部分
【发布时间】:2021-10-05 02:19:28
【问题描述】:

我正在尝试用海龟填充一个形状,但是当我这样做时,一小部分最终没有被填充。我该如何解决这个问题?我试着让它上升的线越过空白区域开始的点,但这没有用。正如您可能从我的代码中看到的那样,我对 python 还是很陌生。这是我的代码...

import turtle

turtl = turtle.Turtle()
turtle.screensize(852, 508, 'white')
turtl.penup()
turtl.fillcolor("royal blue")      #Every closed shape after this untill endfill() will be filled with royal blue
turtl.begin_fill()

for i in range(1):
    turtl.hideturtle()
    turtl.penup()
    turtl.setx(-213)
    turtl.sety(-75)      #Moves the turtle to the correct place to start the "plus"
    turtl.right(180)
    turtl.pendown()
    turtl.forward(427)      #Makes the first line going left
    turtl.right(90)
    turtl.forward(150)      #Width of plus
    turtl.right(90)
    turtl.forward(427)
    turtl.left(90)
    turtl.forward(253)      #Going to top of plus
    turtl.right(90)
    turtl.forward(150)      #Width of top of plus
    turtl.right(90)
    turtl.forward(253)      #Going back down from right side of top of plus
    turtl.left(90)
    turtl.forward(697)      #Going to the right side of the screen
    turtl.right(90)
    turtl.forward(150)      #Width of plus / Start of bottom half of plus
    turtl.right(90)
    turtl.forward(697)      #Going back left to to the bottom bit of the plus
    turtl.left(90)
    turtl.forward(253)     #Going to the bottom part of plus
    turtl.right(90)
    turtl.forward(150)
    turtl.right(90)
    turtl.forward(253)      #Traveling back up to center bar

turtl.end_fill()

print(turtl)
turtle.mainloop()

【问题讨论】:

    标签: python turtle-graphics python-turtle


    【解决方案1】:

    您应该在 begin_fill 之前将海龟移动到起始位置,因为它似乎将此移动 setx(), sety() 视为线 - 即使您使用 penup() - 并且这条线会在图中创建白色三角形。

    import turtle
    
    turtl = turtle.Turtle()
    
    turtle.screensize(852, 508, 'white')
    
    turtl.penup()
    turtl.setx(-213)
    turtl.sety(-75)      #Moves the turtle to the correct place to start the "plus"
    turtl.right(180)
    turtl.pendown()
    
    turtl.fillcolor("royal blue")      #Every closed shape after this untill endfill() will be filled with royal blue
    turtl.begin_fill()
    
    for i in range(1):
        turtl.hideturtle()
        turtl.forward(427)      #Makes the first line going left
        turtl.right(90)
        turtl.forward(150)      #Width of plus
        turtl.right(90)
        turtl.forward(427)
        turtl.left(90)
        turtl.forward(253)      #Going to top of plus
        turtl.right(90)
        turtl.forward(150)      #Width of top of plus
        turtl.right(90)
        turtl.forward(253)      #Going back down from right side of top of plus
        turtl.left(90)
        turtl.forward(697)      #Going to the right side of the screen
        turtl.right(90)
        turtl.forward(150)      #Width of plus / Start of bottom half of plus
        turtl.right(90)
        turtl.forward(697)      #Going back left to to the bottom bit of the plus
        turtl.left(90)
        turtl.forward(253)     #Going to the bottom part of plus
        turtl.right(90)
        turtl.forward(150)
        turtl.right(90)
        turtl.forward(253)      #Traveling back up to center bar
    
    turtl.end_fill()
    
    turtle.mainloop()
    

    【讨论】:

      【解决方案2】:

      首先,我认为您拥有的for 循环是不必要的,因为它有一个range(1),这意味着它只会运行一次,这与没有for 相同环形。关于您的问题,只需将setx(x)sety(y) 替换为单个goto(x,y),如下所示:

      原文:

      for i in range(1):
          turtl.hideturtle()
          turtl.penup()
          turtl.setx(-213)
          turtl.sety(-75)
          turtl.right(180)
      

      修订:

      for i in range(1):
          turtl.hideturtle()
          turtl.penup()
          turtl.goto(-213,-75)
          turtl.right(180)
      

      【讨论】:

        【解决方案3】:

        已经为您提供了两个可行的解决方案(每个 +1),所以我想解决您代码中的另一个问题:不要使用 screensize(),它不能满足您的要求。请改用setup()。下面的示例用法以及使用 stamping 而不是 drawing 绘制图形的完全不同的方式:

        from turtle import Screen, Turtle
        
        WIDTH, HEIGHT = 852, 508
        
        THICKNESS = 150
        
        CURSOR_SIZE = 20
        
        screen = Screen()
        screen.setup(WIDTH, HEIGHT)
        
        turtle = Turtle()
        turtle.hideturtle()
        turtle.shape('square')
        turtle.color("royal blue")
        turtle.penup()
        
        turtle.setx(-WIDTH/6)
        
        turtle.shapesize(HEIGHT/CURSOR_SIZE, THICKNESS/CURSOR_SIZE)
        
        turtle.stamp()
        
        turtle.shapesize(THICKNESS/CURSOR_SIZE, (7*WIDTH/6 + THICKNESS)/CURSOR_SIZE)
        
        turtle.stamp()
        
        screen.mainloop()
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2016-10-02
          • 2023-01-13
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2022-12-30
          • 1970-01-01
          相关资源
          最近更新 更多