当用户输入要绘制的正方形数量和这些正方形的大小时,此代码将绘制形状。我希望每个绘制的正方形都改变颜色,并填充它。但是代码卡住了:
import turtle
t = turtle.Turtle()
size = int(input("How long do you want the side lengths to be?"))
number = int(input("How many squares do you want in the image?"))
red = 40.0
blue = 30.0
green = 10.0
def square (size):
count = 0
while count < 4:
t.forward(size)
t.right(90)
count = count + 1
def drawing(number):
times = 0
while times < number:
t.pencolor(red, blue, green)
t.fillcolor(red, blue, green)
t.begin_fill()
square(size)
t.right(360/number)
if t.filling():
t.pensize(5)
else:
t.pensize(3)
t.color()
(red + 1, blue + 1, green + 1)
times = times + 1
drawing(number)
答案 0 :(得分:0)
您的代码有几个问题-主要的问题是您使用了错误的颜色模型。默认情况下,乌龟颜色被指定为0.0到1.0之间的浮点值。但是,您想使用代码中的值,我们可以通过colormode(255)
进行切换,该值允许从0到255。
接下来,begin_fill()
必须具有与之匹配的end_fill()
,而您丢失了它。颜色是按(red, green, blue)
而不是(red, blue, green)
的顺序指定的。另外,如果将pencolor()
和fillcolor()
都设置为相同的颜色,则只需使用一个调用color()
。
最后,这两个语句不执行任何操作:
t.color()
(red + 1, blue + 1, green + 1)
就您的代码而言。您需要重新考虑它们。以下是我对您的代码的重做,该代码解决了上述问题和其他一些细节:
from turtle import Screen, Turtle
def square(size):
count = 0
if turtle.filling():
turtle.pensize(5)
else:
turtle.pensize(3)
while count < 4:
turtle.forward(size)
turtle.right(90)
count += 1
def drawing(number):
red = 30
green = 10
blue = 20
times = 0
while times < number:
turtle.color(red % 255, green % 255, blue % 255)
turtle.begin_fill()
square(size)
turtle.end_fill()
turtle.right(360 / number)
red, green, blue = red + 20, green + 30, blue + 10
times += 1
size = int(input("How long do you want the side lengths to be? "))
number = int(input("How many squares do you want in the image? "))
screen = Screen()
screen.colormode(255)
turtle = Turtle()
turtle.speed('fastest') # because I have no patience
drawing(number)
screen.exitonclick()