Advertisement
here2share

# Brain_Cellular_Automaton.py

Jul 12th, 2022 (edited)
752
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.68 KB | None | 0 0
  1. # Brain_Cellular_Automaton.py
  2.  
  3. from tkinter import *
  4. from PIL import Image, ImageTk
  5. import time
  6. import random
  7. import math
  8.  
  9. grid_size = (140, 69)    # Sets the size of the grid
  10. cell_size = 9      # Sets the drawn size of each cell
  11. frame_rate = 30
  12. max_cells = grid_size[0]*grid_size[1]-50
  13.  
  14. root = Tk()
  15. root.title("Brain Cellular Automaton")
  16. canvas = Canvas(root,width=grid_size[0]*cell_size,height=grid_size[1]*cell_size)
  17. text = Text(root)
  18. canvas.pack()
  19.  
  20. default_font = ("Arial 20")
  21. color_239_62_62 = f"#%02x%02x%02x" % (239,62,62)
  22.  
  23. def randomise(cells):
  24.  for i in range(grid_size[0]):
  25.   for j in range(grid_size[1]):
  26.    cells[i][j] = random.randrange(0,3)
  27.  return cells
  28.  
  29. def initialise():
  30.  canvas.configure(bg='black')
  31.  cells = [[0 for y in range(grid_size[1])] for x in range(grid_size[0])]
  32.  cells = randomise(cells)
  33.  return cells
  34.  
  35. def next_gen(crowd, cells):
  36.  crowd = 0
  37.  feed = [1]*50+[0]*max_cells
  38.  random.shuffle(feed)
  39.  new = [[0 for y in range(grid_size[1])] for x in range(grid_size[0])]
  40.  for i in range(grid_size[0]):
  41.   for j in range(grid_size[1]):
  42.    if cells[i][j] == 0:
  43.     alive = 0
  44.     for k in range(-1,2):
  45.      for l in range(-1,2):
  46.       if cells[(i+k) % grid_size[0]][(j+l)% grid_size[1]] == 1:
  47.        alive += 1
  48.     if alive == 2:
  49.      new[i][j] = 1
  50.    elif cells[i][j] == 1:
  51.     new[i][j] = 2
  52.    elif cells[i][j] == 2:
  53.     crowd += 1
  54.     if feed.pop():
  55.      new[i][j] = 1  
  56.     else:
  57.      new[i][j] = 0
  58.  return crowd, new
  59.  
  60. def update_cells(new, cells):
  61.  for i in range(grid_size[0]):
  62.   for j in range(grid_size[1]):
  63.    t = new[i][j]
  64.    cells[i][j] = new[i][j]
  65.  return cells
  66.  
  67. def draw(generation, start_time, cells, crowd):
  68.  canvas.delete('all')
  69.  for i in range(grid_size[0]):
  70.   for j in range(grid_size[1]):
  71.    if cells[i][j] == 1:
  72.     canvas.create_rectangle((i*cell_size,j*cell_size,i*cell_size+cell_size,j*cell_size+cell_size), fill=('white'))
  73.    elif cells[i][j] == 2:
  74.     canvas.create_rectangle((i*cell_size,j*cell_size,i*cell_size+cell_size,j*cell_size+cell_size), fill=(color_239_62_62))
  75.     mGensPerSec = round(generation/round(time.time() - start_time,2),2)
  76.  canvas.create_text((500,60), text=f"Generation: {generation}  Cells: {crowd}  mGensPerSec: {mGensPerSec}", font=default_font, fill=(f"#ffffff"))
  77.  canvas.update()
  78.  
  79. def game_loop(cells):
  80.  crowd = 0
  81.  start_time = time.time()
  82.  time.sleep(0.01)
  83.  generation = 0
  84.  while True:
  85.   generation += 1
  86.   crowd, new = next_gen(crowd, cells)  
  87.   cells = update_cells(new, cells)
  88.   draw(generation, start_time, cells, crowd)
  89.  
  90. def main():
  91.  cells = initialise()
  92.  game_loop(cells)
  93.  
  94. if __name__ == "__main__":
  95.  main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement