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

During your visit to our site, NumWorks needs to install "cookies" or use other technologies to collect data about you in order to:

With the exception of Cookies essential to the operation of the site, NumWorks leaves you the choice: you can accept Cookies for audience measurement by clicking on the "Accept and continue" button, or refuse these Cookies by clicking on the "Continue without accepting" button or by continuing your browsing. You can update your choice at any time by clicking on the link "Manage my cookies" at the bottom of the page. For more information, please consult our cookies policy.