mondrian.py

Created by schraf

Created on October 15, 2021

1.14 KB

Version améliorée :

  • Les blocs ne sont plus remplis plusieurs fois


from random import randint,seed,random
from kandinsky import *
from time import sleep

def pos(nb, d):
  l =[]
  for i in range(nb):
    # espace entre traites >= 10px
    l.append(randint(1, d) * 10)
  # Ajout des bords extremes
  l.append(0)
  l.append(10*d+8)
  l.sort()  
  return l

def br(v) : # ajout de bruit
  return int(v+v*(2*random()/20-1/20))

def coul(n):
  # rouge - bleu - jaune - noir
  c = [[225,45,31],[19,4,109],[242,192,17],[12,12,12]]
  return color(br(c[n][0]),br(c[n][1]),br(c[n][2]))
      
def mondrian(s):
  seed(s)
  # position lignes vert et horiz
  x = pos(randint(3,6), 31)
  y = pos(randint(3,5), 23)
  # nb de blocs en couleurs
  n = randint(4,len(x)*len(y))
  b = []
  for k in range(0,n):
    # choix du bloc
    i = randint(0,len(x)-2)
    j = randint(0,len(y)-2) 
    if [i,j] not in b:
      b.append([i,j])
  for k in b:
    c = randint(0,3)
    fill_rect(x[k[0]],y[k[1]],x[k[0]+1]-x[k[0]],y[k[1]+1]-y[k[1]],coul(c))
  # Traits verticaux
  for i in x:
    fill_rect(i,0,2,240,coul(3))
  # Traits horizontaux
  for i in y:
    fill_rect(0,i,320,2,coul(3))
    
while True:
  mondrian(randint(1,500))
  sleep(.2)