我试图创建一个蒙德里安艺术程序…我有随机生成方块的代码…但我有麻烦随机填充方块的原色?有人知道怎么解决吗?这是我的代码:
import turtle
import random
turtle.screensize(1000,1000)
turtle.setworldcoordinates(-500,-500,500,500)
piet = turtle.Turtle()
piet.speed(300)
#primary colors, red, blue, yellow
#piet.color(red,blue,yellow)
rectangles = int(input('How many rectangles should be drawn? '))
rectangle_w = int(input('What should be the max width of the rectangles? '))
rectangle_h = int(input('What should be the max height of the rectangles? '))
def mondrian(t,random_w,random_h):
piet.begin_fill()
for number_r in range(1):
for box in range(2):
t.left(90)
t.forward(random_w)
t.left(90)
t.forward(random_h)
piet.end_fill()
mondrian(piet,random.randint(10,rectangle_w),random.randint(10,rectangle_h))
def repeat_mondrian():
for i in range(rectangles - 1):
mondrian(piet, random.randint(10, rectangle_w), random.randint(10, rectangle_h))
repeat_mondrian()
谢谢!:)
最佳答案
这里是您的程序稍微清理,并与输入暂时固定,以便于开发。请注意,所有矩形的右下角都是原点。你也应该随机化。
import turtle
import random
turtle.screensize(1000,1000)
turtle.setworldcoordinates(-500,-500,500,500)
piet = turtle.Turtle()
piet.speed(300)
rectangles = 8 #int(input('How many rectangles '))
rectangle_w = 500 #int(input('Max width of the rectangles? '))
rectangle_h = 500 #int(input('Max height of the rectangles? '))
def mondrian(t,random_w, random_h):
piet.fillcolor(random.choice(('red','blue','yellow')))
piet.begin_fill()
for box in range(2):
t.left(90)
t.forward(random_w)
t.left(90)
t.forward(random_h)
piet.end_fill()
def repeat_mondrian():
for i in range(rectangles):
mondrian(piet,
random.randint(10, rectangle_w),
random.randint(10, rectangle_h))
repeat_mondrian()