clock.py

Created by schraf

Created on September 24, 2022

1.47 KB

Oscillateur Horloge dans le jeu de la vie


from kandinsky import fill_rect, get_pixel

d = 12

BL,CL,GR,NR,FO = (248,252,248), (248,)*3, (200,)*3, (0,)*3, (8,)*3
COLS, LIGN = 320 // d, 220 // d

def grille():
  for i in range(COLS):
    fill_rect(d * i, 0, 1,d * LIGN, GR)
    if d * i <= 220: fill_rect(0, d * i, 320, 1, GR)

def lancer(fig):
  x, y = 14, 7
  for i,ligne in enumerate(fig):
    for j,v in enumerate(ligne):
      coul = NR if v == "1" else BL
      rect(d * (j + y), d * (x - i), coul )

def noir(x,y):
  return get_pixel(d * (x % COLS) + 1, d * (y % LIGN) + 1) == NR

def voisins(c,l):
  nb = 0
  for i in [-1,0,1]:
    for j in [-1,0,1]:
      if (i != 0 or j != 0) and noir(c + i, l + j): nb += 1
  return nb

def suivant():
  for c in range(COLS):
    for l in range(LIGN):
      a = noir(c,l)
      n = voisins(c,l)
      if n == 3 and not(a) : fill_rect(d * c + 2, d * l + 2, 1, 1, FO)
      elif a and (n<2 or n>3): fill_rect(d * c + 2, d * l + 2, 1, 1, CL)
  for c in range(COLS):
    for l in range(LIGN):
      u = get_pixel(d * c + 2, d * l + 2)
      if u == FO: rect(d * c, d * l, NR)
      elif u == CL: rect(d * c, d * l, BL)

def rect(x,y,c): fill_rect(x + 1, y + 1, d - 1, d - 1, c)

def jeu():
  grille()
  lancer(["00000011",
          "00000011",
          "0",
          "00001111",
          "110100001",
          "110100101",
          "000100011011",
          "000101001011",
          "00001111",
          "0",
          "000011",
          "000011"])
  while True: suivant()
   
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 <a href="https://www.numworks.com/legal/cookies-policy/">cookies policy</a>.