galton.py

Created by schraf

Created on November 04, 2021

906 Bytes


from kandinsky import fill_rect,get_pixel
from random import random
from time import sleep

class bille():
    def __init__(self):
        self.y = 0
        self.x = 160
        self.encore = True
    def tombe(self):
        while self.encore:
            d = 2 if random()<.5 else -2
            if sum(get_pixel(self.x + d,self.y + 2)) != 0:
                fill_rect(self.x,self.y,2,2,(255,255,255))
                self.y += 2
                self.x += d
                fill_rect(self.x,self.y,2,2,(0,0,0))
            else:
                if sum(get_pixel(self.x,self.y + 2)) == 0 or self.y >= 220: self.encore = False  
                else:
                    fill_rect(self.x,self.y,2,2,(255,255,255))
                    self.y += 2
                    fill_rect(self.x,self.y,2,2,(0,0,0))                                 


for i in range(8000):
    b = bille()
    b.tombe()