以下代码仅适用于饰品,并且在其他IDE(例如pyCharm,Jupytor和IDLE)上运行时,会出现很多错误。
我也尝试过在在线编译器中执行此代码,但是有很多错误。
当我在饰品中执行此代码时,错误为零。
from turtle import *
from random import *
def randomcolour():
red = randint(0, 255)
green = randint(0, 255)
blue = randint(0, 255)
color(red, green, blue )
def randomplace():
penup()
x = randint(-100,100)
y = randint(-100,100)
goto(x,y)
pendown()
def randomheading():
heading = randint(0, 360)
setheading(heading)
shape("turtle")
speed(0)
for i in range(4):
randomcolour()
randomplace()
randomheading()
stamp()
def drawrectangle():
randomcolour()
randomplace()
hideturtle()
length = randint(10, 100)
height = randint(10, 100)
begin_fill()
forward(length)
right(90)
forward(height)
right(90)
forward(length)
right(90)
forward(height)
right(90)
end_fill()
clear()
setheading(0)
for i in range(20):
drawrectangle()
from turtle import *
from random import *
def randomcolour():
red = randint(0, 255)
green = randint(0, 255)
blue = randint(0, 255)
color(red, green, blue )
def randomplace():
penup()
x = randint(-100,100)
y = randint(-100,100)
goto(x,y)
pendown()
def randomheading():
heading = randint(0, 360)
setheading(heading)
shape("turtle")
speed(0)
for i in range(4):
randomcolour()
randomplace()
randomheading()
stamp()
def drawrectangle():
randomcolour()
randomplace()
hideturtle()
length = randint(10, 100)
height = randint(10, 100)
begin_fill()
forward(length)
right(90)
forward(height)
right(90)
forward(length)
right(90)
forward(height)
right(90)
end_fill()
def drawcircle():
radius = randint(5, 100)
randomcolour()
randomplace()
dot(radius)
def drawstar():
randomcolour()
randomplace()
randomheading()
begin_fill()
size = randint(20, 100)
#draw the star shape
for side in range(5):
left(144)
forward(size)
end_fill()
clear()
setheading(0)
for i in range(20):
drawrectangle()
clear()
for i in range(20):
drawcircle()
clear()
for i in range(20):
drawstar()
答案 0 :(得分:1)
您传入的RGB值为0到255。默认情况下,乌龟中的颜色模式为0到1.0。因此,您需要先将颜色模式设置为255,然后再尝试设置rgb值。或者您需要将rgb值设置在0到1.0之间。
turtle.color说
"""Return color string corresponding to args.
Argument may be a string or a tuple of three
numbers corresponding to actual colormode,
i.e. in the range 0<=n<=colormode.
If the argument doesn't represent a color,
an error is raised.
"""
颜色模式可以是1.0或255。因此默认情况下,颜色模式为1.0,因此颜色具有此代码。
if self._colormode == 1.0:
r, g, b = [round(255.0*x) for x in (r, g, b)]
if not ((0 <= r <= 255) and (0 <= g <= 255) and (0 <= b <= 255)):
raise TurtleGraphicsError("bad color sequence: %s" % str(color))
所以可以说您将100、100、100分别作为RGB传递,但是色彩模式当前设置为1.0。然后颜色会将所有RGB乘以255.0。因此,您的rgb将变为25500、25500、25500。然后,颜色将检查每个RGB是否在0和255之间(包括0和255),但是由于颜色将所有rgb乘以255,因为颜色模式为1.0,因此所有rgb现在都超过255,因此颜色差颜色序列异常。
因此,要么将颜色模式设置为255,要么在0和1之间传递rgb值
255 RGB
from turtle import *
from random import *
colormode(255)
def randomcolour():
red = randint(0, 255)
green = randint(0, 255)
blue = randint(0, 255)
color(red, green, blue )
浮点数RGB
from turtle import *
from random import *
def randomcolour():
red = uniform(0, 1)
green = uniform(0, 1)
blue = uniform(0, 1)
color(red, green, blue )
关于它在小饰品中工作,但在其他饰品中却不行,也许小饰品正在将您的颜色模式设置为255