kesako7.py

Created by schraf

Created on March 05, 2026

985 Bytes

Enoncé d’origine par Marge


from kandinsky import fill_rect
from random import randint
from time import sleep

T = 4                      # taille des cellules
COLS = 320 // T            # nombre de cellules

NOIR = (0,0,0)
BLANC = (255,255,255)

ligne = [0]*COLS

def init_alea():
  global ligne
  ligne = [randint(0,1) for i in range(COLS)]

def afficher(y):
  for x in range(COLS):
    c = NOIR if ligne[x] else BLANC
    fill_rect(x*T, y*T, T, T, c)

def voisins(i):
  return (
    ligne[(i-2)%COLS] + ligne[(i-1)%COLS] + ligne[(i+1)%COLS] + ligne[(i+2)%COLS]
  )

def suivant():
  global ligne
  nouv = [0]*COLS
  for i in range(COLS):
    n = voisins(i)
    if ligne[i] == 0:        # naissance
      if n == 2 or n == 3: nouv[i] = 1
    else:                    # survie
      if n == 2: nouv[i] = 1
  ligne = nouv

def jeu():
  init_alea()
  y = 0
  while True:
    afficher(y)
    sleep(.1)
    suivant()
    y += 1
    if y >= 220//T:
      y = 0
      fill_rect(0,0,320,220,BLANC)

jeu()

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.