【问题标题】:How do I "hold" the randomly generated color?如何“保持”随机生成的颜色?
【发布时间】:2017-09-30 12:50:01
【问题描述】:

this page.借来的random_turtle_color

import turtle, random
r = lambda: random.randint(0,255)
print('#%02X%02X%02X' % (r(),r(),r()))

turtle.width(10) #What does this line do?
length = 5
for count in range(100):
    colors = (z) #I want the randomly generated hexadecimal color to be #the pen in the drawing
    turtle.speed(0)
    turtle.forward(length)
    turtle.right(135)
    length = length + 5

【问题讨论】:

    标签: python-2.7 random lambda colors turtle-graphics


    【解决方案1】:

    使用''.format() 而不是print()。如果您想每次都获得不同的颜色,请将 z 赋值行移到 for 循环中:

    import turtle, random
    r = lambda: random.randint(0,255)
    z = '#{:02X}{:02X}{:02X}'.format(r(),r(),r())
    
    turtle.width(10) #What does this line do?
    length = 5
    for count in range(100):
        colors = (z) #I want the randomly generated hexadecimal color to be #the pen in the drawing
        turtle.speed(0)
        turtle.forward(length)
        turtle.right(135)
        length = length + 5
    

    【讨论】:

    • 我觉得你的能力是我的壁橱,但它不起作用。我所有的颜色都是黑色的。 '###random_turtle_color### ###从此页面借用 trinket.io/python/f0a2314bf3 ###import turtle, random import turtle, random r = lambda: random.randint(0,255) z = '#{:02X}{:02X }{:02X}'.format(r(),r(),r()) turtle.width(10) length = 5 for count in range(100): colors = (z) turtle.speed(0) turtle .forward(length) turtle.right(135) length = length + 5'
    • 抱歉,我不熟悉海龟包,也没有意识到您必须按照所有其他答案的建议设置笔颜色,请选择其中一个,就像 @Claudio 使用的那样turtle.pen(fillcolor=z,pencolor=z,pensize=1)
    • 我觉得你的能力是我的壁橱,但它不起作用。我所有的颜色都是黑色的。 '###random_turtle_color### ###borrowed from trinket.io/python/f0a2314bf3 import turtle, random r = lambda: random.randint(0,255) z = '#{:02X} {:02X}{:02X}'.format(r(),r(),r()) turtle.width(10) length = 5 for count in range(100): colors = (z) turtle.speed( 0) turtle.forward(length) turtle.right(135) length = length + 5'
    • 谢谢。你可能会说我对 Stack Overflow 还不熟悉
    • 我认为你问了一个很好的问题,引起了很多很好的回应(带有漂亮的图形)!
    【解决方案2】:

    您缺少的关键部分是告诉海龟使用您通过turtle.pencolor() 生成的颜色。这是一个重新设计的示例:

    import turtle
    import random
    
    color_values = [random.randrange(0, 256) for _ in 'rgb']
    hex_string = '#{:02X}{:02X}{:02X}'.format(*color_values)
    
    turtle.speed('fastest')
    turtle.pencolor(hex_string)  # Set the pen color
    
    turtle.width(10)  # Width in pixels of the lines drawn (constant)
    
    length = 5  # Length in pixels of the lines drawn (grows)
    
    for _ in range(100):
        turtle.forward(length)
        turtle.right(135)
        length += 5
    
    turtle.hideturtle()
    turtle.done()
    

    您不需要使用十六进制字符串来设置颜色,如果颜色值与颜色模式匹配,您可以直接传递颜色值。这是一个返工,它在循环期间也改变了颜色:

    import turtle
    import random
    
    turtle.colormode(255)
    turtle.speed('fastest')
    
    turtle.width(10)  # Width in pixels of the lines drawn (constant)
    
    length = 5  # Length in pixels of the lines drawn (grows)
    
    for _ in range(100):
        color_values = [random.randrange(0, 256) for _ in 'rgb']
        turtle.pencolor(color_values)  # Set the pen color
    
        turtle.forward(length)
        turtle.right(135)
        length += 5
    
    turtle.hideturtle()
    turtle.done()
    

    输出

    【讨论】:

    • for _ in range(100): 称为 while 循环?而且,我有点想知道 '#{:02X}{:02X}{:02X}'.format(*color_values) 做了什么。非常感谢你的帮助。我想我正在考虑“保存一个已定义的变量”并稍后在函数中使用它是倒退的想法。我可以在函数中定义变量,函数自动知道使用它?
    • @RickWeinberg,这称为for 循环,while 循环不同。 range(100) 是生成(1, 2, 3, ..., 97, 98, 99) 的方式,而下划线_ 通常是包含当前值的变量,但我们不关心这种情况下的值,只是它循环指定的次数。
    • @RickWeinberg,Python 3 引入了一种格式化字符串的新方法,它取代了旧的 % 运算符——这里是 nice article that contrasts the two
    【解决方案3】:

    一些解释见下面代码中的cmets:

    import turtle, random
    r = lambda: random.randint(0,255)
    z = '#{:02X}{:02X}{:02X}'.format(r(),r(),r())
    print(z)
    length = 5
    turtle.pen(fillcolor=z,pencolor=z,pensize=1) # pensize= width of drawed line (here 1 pixel) 
    turtle.begin_fill() # Called just before drawing a shape which is to be filled.
    for _ in range(100): # without '_' unused numbers are generated
        turtle.speed(0)
        turtle.forward(length)
        turtle.right(135)
        length = length + 5
    turtle.end_fill() # It's question of taste, but I like the filled figure better ...
    turtle.done()     # the turtle window will stay open 
    

    给予:

    【讨论】:

      【解决方案4】:

      你的问题让我想到了faker,这是一个很好的生成随机数据的包,包括random colors,即:

      import random
      from faker import Faker
      length = 1
      for count in range(800):
          turtle.width(random.randint(2, 15))
          turtle.speed(200)
          turtle.forward(length)
          turtle.right(135)
          turtle.left(2)
          turtle.color(Faker().hex_color())
          length = length + 15
      

      注意: turtle.width() - 定义画笔的大小


      【讨论】:

      • 我以前从未听说过“faker”。这可能很有帮助。谢谢。
      • 直到昨天我都没有;) GL
      • 好的。所以我仍在努力,似乎我拥有的 2.7 python 版本不包括faker。我去了 GitHub 并尝试下载它,但似乎我最终“分叉了它”。我不确定“分叉”是什么意思。下载faker有技巧吗? @佩德罗洛比托
      • 在哪里输入 pip install faker?在 Windows 上运行命令?壳牌?
      • 是的,你应该可以做到的。
      猜你喜欢
      • 2010-12-07
      • 2023-01-05
      • 1970-01-01
      • 2012-06-13
      • 1970-01-01
      • 1970-01-01
      • 2022-12-25
      • 2013-01-21
      • 2014-04-14
      相关资源
      最近更新 更多