用乌龟模块用颜色填充三角形的特定部分

Fill in specific part of triangles with colors with turtle module

我想用绿色填充三角形,倒置的三角形除外,但它总是给我错误:

import turtle
import time
import random
t = turtle.Turtle()
width = 500
height = 500
Color = {"Purple":"#A46BFF","Aqua":"#56BCE8","Tree":"#5EFF7A","Summer":"#E8A83A"}
def triangle(length,draw):


    if draw==0:
    #draw the triangle
        t.color(Color.get("Aqua", "Tree"))
        for i in range(3):
            t.forward(length)
            t.left(120)
    else:
    #draws 3 triangles
        triangle(length/2,draw-1)
        t.fillcolor('Tree')
        t.begin_fill()
        t.color(Color["Tree"])
        t.fd(length/2)
        triangle(length/2,draw-1)
        t.color(Color["Summer"])
        t.bk(length/2)
        t.left(60)
        t.forward(length/2)
        t.right(60)
        triangle(length/2,draw-1)
        t.color(Color["Purple"])
        t.left(60)
        t.backward(length/2)
        t.right(60)



triangle(150,3)
window = turtle.Screen()
window.exitonclick()

我想要的结果是:

如果我也可以让三角形在绘制后改变颜色就好了。比如画完之后会继续给线条涂上彩虹色。现在,它变成了一种特定的颜色,一旦绘制出来,它就保持不变。我试图导入随机模块来随机化颜色字典。

要用绿色填充三角形,需要在draw == 0时在triangle()函数中进行填充。请注意,颠倒的那些只是没有绘制任何东西的区域,而不是明确渲染的东西。

def triangle(length, draw):
    if draw == 0:
        # draw the triangle
        t.color(Color["Aqua"])
        t.fillcolor(Color["Tree"])
        t.begin_fill()
        for i in range(3):
            t.fd(length)
            t.left(120)
        t.end_fill()
    else:
        # draws 3 triangles
        triangle(length/2, draw-1)
        t.color(Color["Tree"])
        t.fd(length/2)

        triangle(length/2, draw-1)
        t.color(Color["Summer"])
        t.bk(length/2)
        t.left(60)
        t.forward(length/2)
        t.right(60)

        triangle(length/2, draw-1)
        t.color(Color["Purple"])
        t.left(60)
        t.backward(length/2)
        t.right(60)

结果:

我不知道有什么方法可以改变已经绘制的东西的颜色,除非完全重绘它们。

It would be nice if I can also make the triangle change color after it is drawned. -- @Elliot

我不知道有什么方法可以改变已经存在的东西的颜色 除非通过完全重绘它们来绘制。 -- @马蒂诺

我知道一个方法,在有限的范围内:

from turtle import Screen, Turtle
from random import choice

CURSOR_SIZE = 20

COLORS = {"Purple": "#A46BFF", "Aqua": "#56BCE8", "Tree": "#5EFF7A", "Summer": "#E8A83A"}

def triangle(length, draw):
    if draw == 0:  # draw the triangle
        turtle.shapesize(length / CURSOR_SIZE)
        turtle.clone().showturtle()
    else:  # draws 3 triangles
        triangle(length/2, draw-1)

        turtle.left(60)
        turtle.forward(length/2)
        turtle.right(60)

        triangle(length/2, draw-1)

        turtle.right(60)
        turtle.forward(length/2)
        turtle.left(60)

        triangle(length/2, draw-1)

        turtle.backward(length/2)

def recolor():
    color = choice(list(COLORS.values()))

    for turtle in screen.turtles():
        turtle.color(color)

    screen.ontimer(recolor, 1000)

screen = Screen()

turtle = Turtle()
turtle.hideturtle()
turtle.shape('triangle')
turtle.settiltangle(90)
turtle.penup()

turtle.goto(-130, -120)  # roughly center on the screen

triangle(300, 3)

recolor()

screen.exitonclick()

这里的技巧是我们不绘制三角形,我们让每个三角形都有自己的海龟。然后我们可以要求海龟做一些事情,比如改变颜色,或者其他技巧...