here2share

# Tk_automata.py

Mar 28th, 2021 (edited)
814
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.76 KB | None | 0 0
  1. # Tk_automata.py
  2.  
  3. from Tkinter import *
  4. from PIL import Image, ImageTk
  5. import re
  6. import random
  7.  
  8. ww = 600
  9. hh = 600
  10.  
  11. root = Tk()
  12. root.title("Tk_automata")
  13. root.geometry("%dx%d+0+0"%(ww,hh))
  14. canvas = Canvas(root, width=ww, height=hh)
  15. canvas.grid()
  16.  
  17. PIXELS = [(0,0,0)]*ww
  18.  
  19. def draw():
  20.     img.putdata(data)
  21.     imgTk = ImageTk.PhotoImage(img)
  22.     canvas.create_image(0, 0, anchor=NW, image=imgTk)
  23.     root.update()
  24.  
  25. def io(n):
  26.     return "{0:08b}".format(n)
  27.  
  28. '''
  29. IO = []
  30. t = ''
  31. c = 0
  32. while t != '11111':
  33.     t = io(c)[-5:]
  34.     if t[2] == '1':
  35.         IO += [t]
  36.     print t
  37.     c += 1
  38. print IO
  39. '''
  40.  
  41. SIDES__='1100 1110 1111 1010 1011'.split()
  42. def step(color=''):
  43.     zzz = list(current)
  44.     for ttt in list(re.finditer('1', current[1:-1])):
  45.         if color:
  46.             try:
  47.                 p[ttt.start()+ww/2-L/2] = color
  48.             except:
  49.                 return
  50.         else:
  51.             t = random.randint(0,ww*2)
  52.             if t < iii[0]:
  53.                 iii[0] = 0
  54.                 zzz[t+1:t+4] = random.choice(IO)
  55.             t = ttt.start()-1
  56.             b = current[t:t+5]
  57.             if len(b) < 5:
  58.                 continue
  59.             i = list(IO[IO.index(b)+1])
  60.             zzz[t+1:t+4] = i
  61.         '''
  62.         '''
  63.     zzz = zzz[1:]+zzz[:1]
  64.     sides = random.choice(SIDES__)
  65.     return sides+(''.join(zzz))[1:ww]+sides[::-1]
  66.  
  67. IO__ = ['11111', '10111', '00100', '01110', '11100', '10110', '10100', '11101', '01111', '00110', '00101', '11110', '10101', '01101', '00111', '01100']
  68.  
  69. def again(e):
  70.     global go
  71.     go=0
  72. root.bind('<space>', again)
  73. go=1
  74. iii = [0]
  75. while 1:
  76.     IO = IO__[:]
  77.     random.shuffle(IO)
  78.     IO.append(IO[0])
  79.     print IO
  80.     print
  81.  
  82.     blk = [(0,0,0)]*(ww*hh)
  83.     pix = []
  84.     current = '1'
  85.     prev = ''
  86.     polar = 1
  87.     img = Image.new("RGB",(ww, hh))
  88.     while go:
  89.         L = len(current)
  90.         if L > ww:
  91.             p = PIXELS[:]
  92.             step((0,255,0))
  93.             pix.extend(p)
  94.             data = (pix)[-ww*hh:]
  95.             draw()         
  96.         current = step()
  97.         iii[0] += 1
  98.     go = 1
  99.  
Add Comment
Please, Sign In to add comment