为什么乌龟不填充封闭形状的一部分
Why is turtle not filling in a section of a closed shape
我正在尝试用乌龟填充一个形状,但是当我这样做时,一小部分最终没有被填充。我该如何解决这个问题?我试着做到这一点,使上升的线越过空白区域开始的点,但这没有用。正如您可能从我的代码中看到的那样,我是 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()
首先,我认为你的 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)
你应该将乌龟移动到 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()
您已经获得了两个可行的解决方案(每个 +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()
我正在尝试用乌龟填充一个形状,但是当我这样做时,一小部分最终没有被填充。我该如何解决这个问题?我试着做到这一点,使上升的线越过空白区域开始的点,但这没有用。正如您可能从我的代码中看到的那样,我是 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()
首先,我认为你的 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)
你应该将乌龟移动到 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()
您已经获得了两个可行的解决方案(每个 +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()