Advertisement
here2share

# tk_particle_life.py

Jun 24th, 2024
562
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.40 KB | None | 0 0
  1. # tk_particle_life.py
  2.  
  3. import tkinter as tk
  4. from PIL import Image, ImageTk, ImageDraw
  5. import random
  6. import math
  7. import time
  8.  
  9. atoms = []
  10. window_size = 600
  11. g = 0.1
  12. cell_size = 20
  13. cells = {}
  14.  
  15. def atom(x, y, c):
  16.     return {"x": x, "y": y, "vx": 0, "vy": 0, "color": c}
  17.  
  18. def randomxy():
  19.     return round(random.random() * window_size + 1)
  20.  
  21. def cxy(a):
  22.     return int(a["x"] // cell_size), int(a["y"] // cell_size)
  23.  
  24. def create(number, color):
  25.     group = []
  26.     for i in range(number):
  27.         a = atom(randomxy(), randomxy(), color)
  28.         cx, cy = cxy(a)
  29.         key = (cx, cy)
  30.         group.append(a)
  31.         atoms.append(a)
  32.         add_to_cell(a, cx, cy, key)
  33.     return group
  34.  
  35. def add_to_cell(a, cx, cy, key):
  36.     if key not in cells:
  37.         cells[key] = []
  38.     cells[key].append(a)
  39.  
  40. def remove_from_cell(a, cx, cy, key):
  41.     if key in cells and a in cells[key]:
  42.         cells[key].remove(a)
  43.  
  44. def update_cells():
  45.     for a in atoms:
  46.         cx, cy = cxy(a)
  47.         key = (cx, cy)
  48.         remove_from_cell(a, cx, cy, key)
  49.         add_to_cell(a, cx, cy, key)
  50.  
  51. def rule(atoms1, atoms2, g):
  52.     for i in range(len(atoms1)):
  53.         fx = 0
  54.         fy = 0
  55.         a = atoms1[i]
  56.         cx, cy = cxy(a)
  57.         for dx in range(-1, 2):
  58.             for dy in range(-1, 2):
  59.                 nx, ny = cx + dx, cy + dy
  60.                 key = (nx % (window_size // cell_size), ny % (window_size // cell_size))
  61.                 if key in cells:
  62.                     for b in cells[key]:
  63.                         dx = a["x"] - b["x"]
  64.                         dy = a["y"] - b["y"]
  65.                         dx = dx - round(dx / window_size) * window_size
  66.                         dy = dy - round(dy / window_size) * window_size
  67.                         d = (dx * dx + dy * dy) ** 0.5
  68.                         if d > 0 and d < 100:
  69.                             F = (g + g) / d
  70.                             fx += F * dx
  71.                             fy += F * dy
  72.         a["vx"] = (a["vx"] + fx) * 0.5
  73.         a["vy"] = (a["vy"] + fy) * 0.5
  74.         a["x"] = (a["x"] + a["vx"] * 0.07) % window_size
  75.         a["y"] = (a["y"] + a["vy"] * 0.07) % window_size
  76.  
  77. def draw_atom(x, y, color):
  78.     draw.line([x, y, x - 1, y + 1], fill=color, width=3)
  79.  
  80. def set_gravity():
  81.     for i in range(6):
  82.         gravity[i] = gravity[i, 'f'] * random.uniform(1, 2.999)
  83.         print(round(gravity[i], 4), end=', ')
  84.     print('\n')
  85.  
  86. gravity = {}
  87. t = 0.6, -1.5, 0.9, -1.4, 1.0, -3.2
  88. for i in range(6):
  89.     gravity[i, 'f'] = t[i]
  90. set_gravity()
  91. green = create(123, "green")
  92. yellow = create(99, "yellow")
  93. red = create(79, "red")
  94.  
  95. root = tk.Tk()
  96. root.title("tk_particle_life.py")
  97. root.geometry(f"{window_size}x{window_size}+10+10")
  98. label = tk.Label(root)
  99. label.pack()
  100.  
  101. delay = 10
  102. t = time.time() + delay
  103. while True:
  104.     rule(red, red, gravity[0])
  105.     rule(red, yellow, gravity[1])
  106.     rule(yellow, yellow, gravity[2])
  107.     rule(green, green, gravity[3])
  108.     rule(green, red, gravity[4])
  109.     rule(green, yellow, gravity[5])
  110.        
  111.     update_cells()
  112.     img = Image.new("RGB", (window_size + 10, window_size + 10), "black")
  113.     draw = ImageDraw.Draw(img)
  114.     for i in range(len(atoms)):
  115.         draw_atom(atoms[i]["x"], atoms[i]["y"], atoms[i]["color"])
  116.     img_tk = ImageTk.PhotoImage(img)
  117.     label.config(image=img_tk)
  118.     if time.time() > t:
  119.         t = time.time() + delay
  120.         set_gravity()
  121.     root.update()
  122.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement