Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mondrian Art Program Python

I'm trying to create a Mondrian Art program....I have the code that generates the squares randomly..but I'm having trouble randomly filling the squares with primary colors? Does anyone know how to fix that? This is my code:

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()

Thanks! :)

like image 578
kneesarethebees Avatar asked Oct 27 '25 03:10

kneesarethebees


1 Answers

Here is your program slightly cleaned up, and with inputs temporarily fixed for ease of development. Notice that lower right corner of all rectangles is the origin. You should randomize that also.

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()
like image 86
Terry Jan Reedy Avatar answered Oct 29 '25 18:10

Terry Jan Reedy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!