conways_game_of_life.py

Created by antarctus

Created on December 12, 2021

4.49 KB

Voici le jeu de la vie de conway !
(Page wikipédia du jeu), (Voici une vidéo sur le sujet).
Pour le menu d’édition c’est EXE pour le quitter, Paste and Clear pour changer le plan, et Xnt et Var pour retourner/ faire la symétrie du plan.
Vous pouvez changez la résolution et la vitesse du jeu à la ligne 7 : puissance=3. N’hésitez pas à tester puissance=2 voir même puissance=1.


from math import *
from kandinsky import *
from ion import *
from time import *
from random import *

puissance=2
CELL=40//(2**puissance)
WIDTH=8*(2**puissance)
HEIGHT=5*(2**puissance)
error=3

def to_number(f):
  r=[]
  for i in f:
    r+=[1,]
    for ii in i:
      r[-1]*=2
      r[-1]+=ii
  return r

def from_number(f):
  r=[]
  max_len=0
  for i in f:
    r+=[[],]
    while i>1:
      r[-1]=[i%2,]+r[-1]
      i//=2
    if len(r[-1])>max_len:
      max_len=len(r[-1])
  for i in range(len(r)):
    while len(r[i])<max_len:
      r[i]=[0,]+r[i]
  return r

forms_n=[
[3],
[2],
[12, 11, 14],
[34, 33, 49, 47],
[68719478784, 68719486976, 68732108803, 68737351683, 120293212160, 120295532544, 68753557504, 68737302528, 68732059648],
[47, 49, 33, 50, 32, 32, 40, 36, 36, 44, 48, 32, 32, 32, 47, 49, 33, 50],
[18, 26, 21, 20],
[28, 28, 19, 19],
[30, 23],
[1019, 1019, 515, 899, 899, 899, 896, 959, 959],
[2047],
[4144, 4144, 4096, 4336, 7432, 7464, 4395, 4427, 4336, 4096, 4288, 4288],
[10, 11, 14],
[266, 420, 324, 321],
[65542, 90657, 98718, 95920, 66944],
[2064, 2092, 2048, 2177, 2613, 3106, 3138, 3089, 3201, 3849, 2055],
[15, 13, 13, 13, 15],
]

forms=[
##Example :
#[1,0,0],
#[0,1,1],
#[1,1,0]
]

for i in forms:
    forms_n+=[to_number(i),]

t=[]
pause=True
run=True
selected=0
last_move=monotonic()

for y in range(HEIGHT):
  t+=[[0,]*WIDTH,]


def draw_matrix():
  for y in range(HEIGHT):
    for x in range(WIDTH):
      fill_rect(x*CELL,y*CELL,CELL,CELL,(t[y][x]*255,)*3)

def draw_footbar():
  fill_rect(0,200,320,22,(200,)*3)
  txt=["edit","play","step","quit =>"]
  color_=[(0,0,255),(0,255,0),(255,100,100),(255,0,0)]
  if not pause:
    txt[1]="pause"
    color_[1]=(255,0,0)
  txt[selected]=">"+txt[selected]

  for i in range(len(txt)):
    draw_string(txt[i],70*i+10,203,(255,)*3,color_[i])

def step():
  global t,total
  new_t=[]
  total=0
  for y in range(HEIGHT):
    new_t+=[[0,]*WIDTH,]
    for x in range(WIDTH):
      new_t[y][x]=[0,0,t[y][x],1,0,0,0,0,0][t[y-1][x-1]+t[y-1][x]+t[y-1][(x+1)%WIDTH]+t[y][x-1]+t[y][(x+1)%WIDTH]+t[(y+1)%HEIGHT][x-1]+t[(y+1)%HEIGHT][x]+t[(y+1)%HEIGHT][(x+1)%WIDTH]]
    total+=sum(new_t[y])
  t=new_t[:]

def edit():
  global t
  x,y=0,0
  n=0
  pen=[[1],]
  pen_id=0
  last_text_time=monotonic()-4
  last_text_id=0
  texts=["Clic on EXE to exit the edit","Use past and clear to change","Use OK to print the plan","Use cut/copy to rotate/flip"]

  def clear():
    for yy in range(len(pen)):
      for xx in range(len(pen[yy])):
        fill_rect((x+xx)%WIDTH*CELL,(y+yy)%HEIGHT*CELL,CELL,CELL,(t[(y+yy)%HEIGHT][(x+xx)%WIDTH]*255,)*3)

  while not keydown(KEY_EXE):
    for i in range(4):
      if keydown(i):
        clear()
        y+=keydown(KEY_DOWN)-keydown(KEY_UP)
        x+=keydown(KEY_RIGHT)-keydown(KEY_LEFT)
        x%=WIDTH
        y%=HEIGHT
        break
      
    if last_text_time+4<monotonic():
      last_text_id+=1
      draw_string("{:^32}".format(texts[last_text_id%len(texts)]),0,203,(0,)*3,(200,)*3)
      last_text_time+=4

    if keydown(KEY_OK):
      for yy in range(len(pen)):
        for xx in range(len(pen[yy])):
          t[(y+yy)%HEIGHT][(x+xx)%WIDTH]=pen[yy][xx]

    if keydown(KEY_TOOLBOX) or keydown(KEY_BACKSPACE):
      clear()
      pen_id+=keydown(KEY_BACKSPACE)-keydown(KEY_TOOLBOX)
      pen_id%=len(forms_n)
      pen=from_number(forms_n[pen_id])

    if keydown(KEY_XNT):
      pen.reverse()
    if keydown(KEY_VAR):
      clear()
      pen2=[]
      for i in range(len(pen)):
        for ii in range(len(pen[i])):
          if i==0:
            pen2+=[[0,]*len(pen),]
          pen2[ii][i]=pen[i][ii]
      pen=pen2[:]

    n+=1
    color_plus=50+int(25*(cos(n/2)+1)/2)
    for yy in range(len(pen)):
      for xx in range(len(pen[yy])):
        fill_rect((x+xx)%WIDTH*CELL,(y+yy)%HEIGHT*CELL,CELL,CELL,((125*pen[yy][xx])+color_plus,)*3)
    sleep(0.1)
  draw_matrix()

def press():
  global pause,run
  if selected==0:
    edit()
  elif selected==1:
    pause=1-pause
  elif selected==2:
    step()
    draw_matrix()
  elif selected==3:
    run=False

draw_footbar()
draw_matrix()
while run:
  if keydown(KEY_OK):
    if monotonic()-last_move>0.4:
      last_move=monotonic()
      press()
      draw_footbar()
  if keydown(KEY_LEFT) or keydown(KEY_RIGHT):
    if monotonic()-last_move>0.2:
      last_move=monotonic()
      selected+=keydown(KEY_RIGHT)-keydown(KEY_LEFT)
      if selected==4:
        selected-=1
      elif selected==-1:
        selected=0
      draw_footbar()
  if not pause:
    step()
    draw_matrix()

for i in forms:
    print(to_number(i))