conway_giant2.py

Created by mathieu-croslacoste

Created on September 15, 2025

2.71 KB

Giant version of Conway’s Game of Life (320×222).

Extremely slow.

Uses the screen to store the info while displaying it.


from kandinsky import draw_string as txt,fill_rect as fill,set_pixel as sp,get_pixel as gp,color as c
from ion import keydown as k
from time import sleep
black=c(0,0,0)
def readLine(y):
  line,sum=[],0
  if y!=222:
    for x in range(320):
      line.append(gp(x,y)==black)
      sum+=gp(x,y)==black
  else:
    for x in range(320):line.append(0)
  return line,(sum>0)
def move(oldC,pos,key):
  sp(pos[0],pos[1],(255*(not oldC),)*3)
  newPos=pos
  h=(key==1 or key==2)
  if key<2:mod=-1
  else:mod=1
  newPos[h]+=(1-(321*(not h)+223*h)*(pos[h]==0+(321*(not h)+223*h)*(key==2 or key==3)))*mod
  newOldC=(gp(newPos[0],newPos[1])==black)
  sp(newPos[0],newPos[1],(0,0,150))
  sleep(0.2)
  return newOldC,newPos
def setup():
  fill(0,0,320,222,(255,)*3);pos,oldC=[0,0],0;sp(0,0,(0,0,150))
  while not k(12):
    for i in range(4):
      if k(i):oldC,pos=move(oldC,pos,i)
    if k(4):oldC=1-oldC;sleep(.2)
  sp(pos[0],pos[1],(255*(oldC==0),)*3);del pos,oldC
fill(0,0,320,222,(50,150,255))
fill(25,55,270,112,(220,)*3)
txt("CONWAY'S GAME OF LIFE",55,5,(200,50,20),(50,150,255))
txt("(giant version)",85,25,(200,50,20),(50,150,255))
txt("[OK] to place/remove cells",30,60,(0,)*3,(220,)*3)
txt("Move with arrow keys",60,80,(0,)*3,(220,)*3)
txt("[shift] to start",80,100,(0,)*3,(220,)*3)
col,mod,frame=0,1,0
while k(4)|k(52):0
while(k(4)|k(52))^1:
  if frame%4==0:
    if col<220 and mod:col+=1
    else:
      col-=1
      if mod:mod=0
      elif col==0:mod=1
    txt("[OK] to start",95,147,(col,)*3,(220,)*3)
  frame+=1
del col,mod,frame
while k(4)|k(52):0
setup()
mod,surround,line0=0,[-1,0,1,-1,1,-1,0,1],[]
for i in range(320):line0.append(0)
while 1:
  sum=[0,1,1];line1,sum[1]=readLine(0);lines=[line0,line1,[]];del line1
  for y in range(222):
    lines[2],sum[2]=readLine(y+1)
    if sum[0]+sum[1]+sum[2]>0:
      for x,cell in enumerate(lines[1]):
        count,surround=0,[[-1,-1],[-1,0],[-1,1],[0,-1],[0,1],[1,-1],[1,0],[1,1]]
        if sum[0]==0:surround=[[0,-1],[0,1],[1,-1],[1,0],[1,1]]
        elif sum[1]==0:
          new=[]
          for i in surround:
            if i[0]!=0:new.append(i)
          surround=new
        elif sum[2]==0:
          new=[]
          for i in surround:
            if i[0]!=1:new.append(i)
          surround=new
        if x==0:
          new=[]
          for i in surround:
            if i[1]!=-1:new.append(i)
          surround=new
        elif x==319:
          new=[]
          for i in surround:
            if i[1]!=1:new.append(i)
          surround=new
        for i in surround:
          if lines[1+i[0]][x+i[1]]:count+=1
        if cell:
          if count<2 or count>3:sp(x,y,(255,)*3)
        elif count==3:sp(x,y,black)
    lines[0],sum[0]=lines[1],sum[1]
    lines[1],sum[1]=lines[2],sum[2]

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.