我大体上同意@martineau的评论:If you do the graphical drawing correctly using only commands which
are relative to the current position, you can put all of them in a
function and just before calling it, position the turtle to the
location you want them all to be relative to.
但是如果你的画足够简单的话,还有另一种方法。您可以制作一个海龟光标,它是您的绘图,将turtle切换到该光标,并使您的绘图执行turtle光标本身可以执行的任何移动。在
下面的代码以不可见的方式绘制房屋并将其作为海龟光标隐藏起来。然后,它将乌龟形状设置为房屋图纸,并将房子旋转一圈。从字面上说。将房屋旋转一圈后,将其剪切,同时将其移动到图形的右上角:from turtle import Turtle, Screen, Shape
screen = Screen()
screen.bgcolor('SkyBlue')
shape = Shape('compound')
turtle = Turtle(visible=False)
turtle.speed('fastest')
turtle.penup()
turtle.goto(-100, 0)
turtle.begin_poly()
turtle.goto(-100, 50)
turtle.goto(100, 50)
turtle.goto(100, 0)
turtle.goto(-100, 0)
turtle.end_poly()
shape.addcomponent(turtle.get_poly(), 'red')
turtle.goto(-100, 50)
turtle.begin_poly()
turtle.goto(0, 100)
turtle.goto(100, 50)
turtle.goto(-100, 50)
turtle.end_poly()
shape.addcomponent(turtle.get_poly(), 'brown')
turtle.goto(-40, 0)
turtle.begin_poly()
turtle.goto(-40, 30)
turtle.goto(-20, 30)
turtle.goto(-20, 0)
turtle.goto(-40, 0)
turtle.end_poly()
shape.addcomponent(turtle.get_poly(), 'orange')
turtle.goto(20, 20)
turtle.begin_poly()
turtle.goto(20, 40)
turtle.goto(50, 40)
turtle.goto(50, 20)
turtle.goto(20, 20)
turtle.end_poly()
shape.addcomponent(turtle.get_poly(), 'white')
screen.register_shape('house', shape)
turtle.reset()
turtle.penup()
# Now we can move our house in any manner that we move a turtle:
turtle.shape('house')
for theta in range(0, 360, 10):
turtle.setheading(theta)
turtle.setheading(90)
turtle.shearfactor(0.5)
turtle.goto(300, 300)
turtle.shearfactor(0)
screen.mainloop()
同样,这种方法只适用于由填充多边形组成的简单图形。在