Python 3.12 海龟在使用相同代码填充 3.11 的多边形内部为空

问题描述 投票:0回答:1

我正在使用 python 中的海龟库为我的大学做一些工作,并注意到代码在版本 3.12 和 3.11 上的行为有所不同。在 3.11(及以下)上,星星看起来是实心的,但在 3.12 上,里面有一个空的五边形。有什么办法可以使所有版本的行为都相同吗?代码如下

from turtle import *
from random import *
from math import *

speed(10)
colormode(255.0)
hideturtle()


def make_star(step, angle):
    for x in range(5):
        fd(step)
        rt(angle)


for i in range(ceil(random()*50)):
    up()
    setpos(randint(-500, 500), randint(-300, 300))
    color(randint(0, 255), randint(0, 255), randint(0,255))
    fillcolor(randint(0, 255), randint(0, 255), randint(0,255))
    down()

    seth(randint(0, 359))

    begin_fill()
    make_star(ceil(random()*75)+5, 144)
    end_fill()


exitonclick()

我想要完全涂上星星。在 3.11 及以下版本上是这种情况,在 3.12 版本上不是这种情况

附注这是程序在 3.12(1 张图片)和 3.11(2 张图片)上的示例

Python 3.12 output has five-pointed stars with white space in the center -- which is to say, in any area which is not one of the triangles comprising the points

Python 3.11 output has five-pointed stars with the center filled

python python-3.x turtle-graphics python-turtle
1个回答
0
投票

end_fill()

状态的文档:

是否填充自相交多边形或多个形状的重叠区域取决于操作系统图形、重叠类型和重叠数量。例如,上面的海龟星可能是全黄色或有一些白色区域。

您的星星是由具有自相交区域的多边形组成的。解决方案是绘制星形轮廓,而不是依赖于操作系统相关的行为。 这个线程中有多种解决方案,但是这个答案可能是最好的(除了它使用的通配符导入和缺乏解释)。

避免通配符导入。

from turtle import *
将 120 多个函数转储到全局命名空间中,从而带来了名称冲突的巨大潜力。弄错实例和函数接口是另一个常见错误;如果您只使用
from turtle import Turtle
并创建一个实例,您的代码将更容易理解并且不易出错。

© www.soinside.com 2019 - 2024. All rights reserved.