conway_small.py

Created by fedyna-kevin

Created on July 08, 2020

980 Bytes

Version en 5x5. Version en 10x10 dispo ici


from kandinsky import fill_rect
from ion import keydown
from random import randint

def conway():
    couples = [[x,y] for x in [-1,0,1] for y in [-1,0,1] if (x,y) != (0,0)]
    w = range(64)
    h = range(44)
    M1 = [[0 for j in w] for i in h]
    M2 = [[0 for j in w] for i in h]
    for y in h:
        for x in w:
            M1[y][x] = randint(0,1)
            fill_rect(x*5,y*5+1,5,5,M1[y][x] and (0,)*3 or (255,)*3)
    while not keydown(1):
        for y in h:
            for x in w:
                somme = 0
                for couple in couples:
                    if 0<=x+couple[0]<=63 and 0<=y+couple[1]<=43:
                        somme += M1[y+couple[1]][x+couple[0]]
                M2[y][x] = somme == 3 or M1[y][x] and somme == 2
                if M2[y][x] != M1[y][x]:
                    fill_rect(x*5,y*5+1,5,5,M2[y][x] and (0,)*3 or (255,)*3)
        for y in h:
            for x in w:
                M1[y][x] = M2[y][x]

conway()

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.