【问题标题】:Fill in specific part of triangles with colors with turtle module使用海龟模块用颜色填充三角形的特定部分
【发布时间】:2020-02-25 22:56:31
【问题描述】:

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

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()

我想要的结果是:

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

【问题讨论】:

    标签: python turtle-graphics fractals


    【解决方案1】:

    要用绿色填充三角形,需要在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)
    

    结果:

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

    【讨论】:

      【解决方案2】:

      如果我也可以让三角形在它之后改变颜色,那就太好了 被绘制。 -- @艾略特

      我不知道如何改变已经存在的事物的颜色 除非完全重绘它们。 -- @martineau

      我知道一种方法,在一定范围内:

      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()
      

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-09-04
        • 2016-03-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多