Advertisement
here2share

# tk_streaking_fireworks.py

Mar 25th, 2024 (edited)
785
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.69 KB | None | 0 0
  1. # tk_streaking_fireworks.py
  2.  
  3. import tkinter as tk
  4. from PIL import Image, ImageDraw, ImageTk, ImageChops
  5. import time
  6. import random
  7. import math
  8.  
  9. root = tk.Tk()
  10. root.title("Fireworks")
  11.  
  12. ww = 400
  13. hh = 400
  14. root.geometry(f"{ww}x{hh}+0+0")
  15.  
  16. canvas = tk.Canvas(root, width=ww, height=hh, bg='black')
  17. canvas.pack()
  18.  
  19. fireworks = []
  20. gravity = 0.03
  21.  
  22. def create_firework():
  23.     r = random.randint(0, 255)
  24.     g = random.randint(0, 255)
  25.     b = min(255, max(0, 500 - r - g))
  26.     x = random.randint(50, ww - 50)
  27.     y = hh
  28.     angle = random.uniform(1.2, 0.7)
  29.     if x > ww // 2:
  30.         angle *= -1
  31.     firework = {
  32.         'x': x,
  33.         'y': y,
  34.         'exploded': False,
  35.         'angle': angle,
  36.         'color': (r, g, b),
  37.         'peak': random.randint(100, 300),
  38.         'particles': []
  39.     }
  40.     fireworks.append(firework)
  41.  
  42. def update():
  43.     for firework in fireworks[::-1]:
  44.         if not firework['exploded']:
  45.             firework['y'] -= 5
  46.             firework['x'] += firework['angle']
  47.             render(firework)
  48.             if firework['y'] <= firework['peak']:
  49.                 firework['exploded'] = True
  50.                 explode(firework)
  51.         else:
  52.             if firework['particles']:
  53.                 for i in range(len(firework['particles']) - 1, -1, -1):
  54.                     particle = firework['particles'][i]
  55.                     particle['color'] = [max(0, i - 3) for i in particle['color']]
  56.                     particle['y'] += particle['y0'] * 3
  57.                     particle['x'] += particle['x0'] * 3
  58.                     particle['y'] += particle['gravity']
  59.                     particle['x'] += random.choice([-0.1, 0.1])
  60.                     particle['gravity'] += gravity
  61.                     render(particle)
  62.                     if sum(particle['color']) < 20:
  63.                         firework['particles'].pop(i)
  64.             else:
  65.                 fireworks.remove(firework)
  66.  
  67. def explode(firework):
  68.     for _ in range(25):
  69.         angle = random.uniform(0, 2 * math.pi)
  70.         distance = random.uniform(0, 1.0)
  71.         x_offset = distance * math.cos(angle)
  72.         y_offset = distance * math.sin(angle)
  73.         particle = {
  74.             'x': firework['x'],
  75.             'y': firework['y'],
  76.             'x0': x_offset,
  77.             'y0': y_offset,
  78.             'gravity': gravity,
  79.             'color': firework['color'],
  80.         }
  81.         firework['particles'].append(particle)
  82.  
  83. def random_rgb_hex(rgb):
  84.     return '#{:02x}{:02x}{:02x}'.format(*rgb)
  85.  
  86. def render(firework):
  87.     color = random_rgb_hex(firework['color'])
  88.     draw.ellipse((firework['x'], firework['y'], firework['x'] + 5, firework['y'] + 5), fill=color)
  89.  
  90. timer = 0
  91. fade = Image.new("RGBA", (ww, hh), (0, 0, 0, 0))
  92. image = Image.new("RGBA", (ww, hh), (0, 0, 0, 0))
  93. while 1:
  94.     canvas.delete('all')
  95.     draw = ImageDraw.Draw(image)
  96.     update()
  97.     if timer < time.time():
  98.         timer = time.time() + random.uniform(0.5, 5)
  99.         if len(fireworks) < 3:
  100.             create_firework()
  101.        
  102.     photo_image = ImageTk.PhotoImage(image)
  103.     canvas.create_image(0, 0, anchor=tk.NW, image=photo_image)
  104.     root.update()
  105.     image = ImageChops.blend(image, fade, 0.009)
  106.  
  107. root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement