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)

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.