python里graphics的使用_如何仅使用内置函数移动一个turtlegraphics绘制的对象?

我大体上同意@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()

同样,这种方法只适用于由填充多边形组成的简单图形。在

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值