from sprites import *
from random import random

X=7  # 19
Y=21  # 38

step = 1
dessin_cellule = square
# alternatives: square, circle, heart, dot

taille_cellule = 10

grille = [[random() < .3 for _ in range(X)] for _ in range(Y)]
#grille = [[0,1,0,0],[0,1,0,0],[0,1,0,0],[0,0,0,0]]

generation = 0
while True:
  old = [line.copy()for line in grille]
  generation+=1
  for x in range(X):
    for y in range(Y):
      voisins = sum(map(lambda dx, dy: old[(y+dy)%Y][(x+dx)%X],
                        (-1,-1,-1,0,0,1,1,1), (-1,0,1,-1,1,-1,0,1)))
      grille[y][x]=(voisins==3) or (voisins==2 and old[y][x])
      if grille[y][x] != old[y][x] and not generation%step:
        col = ((256,256,256),(0,0,0))[grille[y][x]]
        draw(dessin_cellule,taille_cellule*x,taille_cellule*y, col)  # fonction draw du module sprite
#        set_pixel(y,x,col)
  show_screen()
#  print(generation)
