Python turtle end_poly()does n

2024-03-29 05:34:08 发布

您现在位置:Python中文网/ 问答频道 /正文

海龟医生说当到达end_poly()时:

Stop recording the vertices of a polygon. Current turtle position is last vertex of polygon. This will be connected with the first vertex.

在我的例子中,最后一条线不是从最后一个顶点画回到第一个顶点。Python和7.7.7中的行为相同。在

from turtle import *

print("position 0:",position())
width(5)
pencolor("red")
fillcolor("blue")
begin_fill()
begin_poly()
fd(100)
print("position 1:",position())
left(90)
fd(100)
print("position 2:",position())
end_poly()
end_fill()

p = get_poly()
print("poly p:",p)
register_shape("myShape",p)
shapes_ = getshapes()
print("shapes_:", shapes_)

输出:

^{pr2}$

Image of polygon


Tags: ofthepositionfillendvertex海龟print
1条回答
网友
1楼 · 发布于 2024-03-29 05:34:08

我相信这是文档中的错误,但也是可以理解的。在

首先,您可以通过自己关闭图形来清楚地绘制您想要的:

from turtle import Turtle, Screen

screen = Screen()
turtle = Turtle(visible=False)

turtle.width(5)
turtle.color("red", "blue")

position = turtle.position()

turtle.begin_fill()
turtle.forward(100)
turtle.left(90)
turtle.forward(100)
turtle.goto(position)
turtle.end_fill()

screen.exitonclick()

那么多边形是怎么回事?好吧,除非您自己实现代码,否则默认情况下,turtle无法对多边形执行任何操作,除非将它们用作turtle光标。在这种情况下,它会将它们传递给负责闭合多边形的tkinter:

^{pr2}$

(我猜是在tkinter的canvas.coords()中多边形闭合)注意,width(5)在本例中没有任何意义,*_fill()也没有,因为光标的轮廓宽度是用shapesize()设置的,而光标是自然填充的。它不需要在创建多边形时指定颜色,您可以等到部署新光标。在

我相信end_poly()文档中的这句话:

last vertex of polygon. This will be connected with the first vertex.

应该在turtle的register_shape()文档中的polygon部分。但是您可以理解这个错误,因为turtle只认为*_poly()的作用是创建新的游标。而polygons应该是海龟中第一类灵活的数据类型。在

相关问题 更多 >