如何 "hold" 随机生成颜色?
How do I "hold" the randomly generated color?
random_turtle_color 借自 this page.
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
使用 ''.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
您缺少的一个关键部分是告诉乌龟使用您通过 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()
输出
有关某些解释,请参阅下面代码中的注释:
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
给出:
你的问题让我想到了 faker, a nice package to generate random data, including 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()
- 定义画笔的大小
random_turtle_color 借自 this page.
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
使用 ''.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
您缺少的一个关键部分是告诉乌龟使用您通过 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()
输出
有关某些解释,请参阅下面代码中的注释:
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
给出:
你的问题让我想到了 faker, a nice package to generate random data, including 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()
- 定义画笔的大小