Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # tk_particle_life.py
- import tkinter as tk
- from PIL import Image, ImageTk, ImageDraw
- import random
- import math
- import time
- atoms = []
- window_size = 600
- g = 0.1
- cell_size = 20
- cells = {}
- def atom(x, y, c):
- return {"x": x, "y": y, "vx": 0, "vy": 0, "color": c}
- def randomxy():
- return round(random.random() * window_size + 1)
- def cxy(a):
- return int(a["x"] // cell_size), int(a["y"] // cell_size)
- def create(number, color):
- group = []
- for i in range(number):
- a = atom(randomxy(), randomxy(), color)
- cx, cy = cxy(a)
- key = (cx, cy)
- group.append(a)
- atoms.append(a)
- add_to_cell(a, cx, cy, key)
- return group
- def add_to_cell(a, cx, cy, key):
- if key not in cells:
- cells[key] = []
- cells[key].append(a)
- def remove_from_cell(a, cx, cy, key):
- if key in cells and a in cells[key]:
- cells[key].remove(a)
- def update_cells():
- for a in atoms:
- cx, cy = cxy(a)
- key = (cx, cy)
- remove_from_cell(a, cx, cy, key)
- add_to_cell(a, cx, cy, key)
- def rule(atoms1, atoms2, g):
- for i in range(len(atoms1)):
- fx = 0
- fy = 0
- a = atoms1[i]
- cx, cy = cxy(a)
- for dx in range(-1, 2):
- for dy in range(-1, 2):
- nx, ny = cx + dx, cy + dy
- key = (nx % (window_size // cell_size), ny % (window_size // cell_size))
- if key in cells:
- for b in cells[key]:
- dx = a["x"] - b["x"]
- dy = a["y"] - b["y"]
- dx = dx - round(dx / window_size) * window_size
- dy = dy - round(dy / window_size) * window_size
- d = (dx * dx + dy * dy) ** 0.5
- if d > 0 and d < 100:
- F = (g + g) / d
- fx += F * dx
- fy += F * dy
- a["vx"] = (a["vx"] + fx) * 0.5
- a["vy"] = (a["vy"] + fy) * 0.5
- a["x"] = (a["x"] + a["vx"] * 0.07) % window_size
- a["y"] = (a["y"] + a["vy"] * 0.07) % window_size
- def draw_atom(x, y, color):
- draw.line([x, y, x - 1, y + 1], fill=color, width=3)
- def set_gravity():
- for i in range(6):
- gravity[i] = gravity[i, 'f'] * random.uniform(1, 2.999)
- print(round(gravity[i], 4), end=', ')
- print('\n')
- gravity = {}
- t = 0.6, -1.5, 0.9, -1.4, 1.0, -3.2
- for i in range(6):
- gravity[i, 'f'] = t[i]
- set_gravity()
- green = create(123, "green")
- yellow = create(99, "yellow")
- red = create(79, "red")
- root = tk.Tk()
- root.title("tk_particle_life.py")
- root.geometry(f"{window_size}x{window_size}+10+10")
- label = tk.Label(root)
- label.pack()
- delay = 10
- t = time.time() + delay
- while True:
- rule(red, red, gravity[0])
- rule(red, yellow, gravity[1])
- rule(yellow, yellow, gravity[2])
- rule(green, green, gravity[3])
- rule(green, red, gravity[4])
- rule(green, yellow, gravity[5])
- update_cells()
- img = Image.new("RGB", (window_size + 10, window_size + 10), "black")
- draw = ImageDraw.Draw(img)
- for i in range(len(atoms)):
- draw_atom(atoms[i]["x"], atoms[i]["y"], atoms[i]["color"])
- img_tk = ImageTk.PhotoImage(img)
- label.config(image=img_tk)
- if time.time() > t:
- t = time.time() + delay
- set_gravity()
- root.update()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement