conway.py

Created by fedyna-kevin

Created on July 08, 2020

982 Bytes

Version en 10x10. Version en 5x5 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(32)
    h = range(22)
    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*10,y*10+1,10,10,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]<=31 and 0<=y+couple[1]<=21:
                        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*10,y*10+1,10,10,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 <a href="https://www.numworks.com/legal/cookies-policy/">cookies policy</a>.