game_of_life.py

Created by sautax

Created on March 01, 2022

858 Bytes

The game of life, right part of the screen is used as a buffer. Use start(size) to start it.


from math import *
from kandinsky import *
from random import *

m=color(0,0,0)
v=color(255,255,255)


def init(si):
  for x in range(si):
    for y in range(si):
      c=choice((m,v))
      set_pixel(x,y,c)
      set_pixel(x+si,y,c)
def loop(si):
  for x in range(si):
    for y in range(si):
      n=0
      for i in range(-1,2):
        for j in range(-1,2):
          if (j**2)+(i**2)!=0 and x+i<si and y+j<si and x+i>0 and y+j>0:
            c=get_pixel(x+i,y+j)[0]
            if c>0:
              n+=1
      c=get_pixel(x,y)[0]
      if c>0:
        if n!=3 and n!=2:
          set_pixel(x+si,y,m)
      else:
        if n==3:
          set_pixel(x+si,y,v)
      
          
  for x in range(si):
    for y in range(si):
      set_pixel(x,y,get_pixel(x+si,y))
      
          
def start(si):
  
  init(si)
  while 1:
    loop(si)