Advertisement
Chl_Snt

Pastel Colors

Apr 12th, 2025
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.43 KB | None | 0 0
  1. import tkinter as tk
  2. import math
  3. import random
  4. import colorsys
  5.  
  6. BALL_COUNT = 500
  7. SPEED_RANGE = (1, 4)
  8. RADIUS_RANGE = (5, 15)
  9.  
  10. root = tk.Tk()
  11. root.geometry("800x600")
  12. root.title("Пастельные мячики")
  13. canvas = tk.Canvas(root, bg="white")
  14. canvas.pack(fill=tk.BOTH, expand=True)
  15.  
  16. target_x, target_y = 300, 200
  17.  
  18. canvas.bind("<Motion>", lambda e: update_target(e))
  19. canvas.bind("<Button-1>", lambda e: repel_balls(e.x, e.y))
  20.  
  21. balls = []
  22.  
  23. def pastel_color():
  24.     h = random.random()
  25.     s = 0.4  # низкая насыщенность
  26.     v = 0.95  # высокая яркость
  27.     r, g, b = colorsys.hsv_to_rgb(h, s, v)
  28.     return f"#{int(r*255):02x}{int(g*255):02x}{int(b*255):02x}"
  29.  
  30. def update_target(event):
  31.     global target_x, target_y
  32.     target_x = event.x
  33.     target_y = event.y
  34.  
  35. for _ in range(BALL_COUNT):
  36.     r = random.randint(*RADIUS_RANGE)
  37.     x = random.randint(r, 800 - r)
  38.     y = random.randint(r, 600 - r)
  39.     color = pastel_color()
  40.     speed = random.uniform(*SPEED_RANGE)
  41.     ball_id = canvas.create_oval(x - r, y - r, x + r, y + r, fill=color, outline="")
  42.     balls.append({
  43.         "x": x,
  44.         "y": y,
  45.         "vx": 0,
  46.         "vy": 0,
  47.         "r": r,
  48.         "speed": speed,
  49.         "id": ball_id,
  50.         "color": color,
  51.         "repel_timer": 0,
  52.     })
  53.  
  54. def repel_balls(cx, cy):
  55.     for ball in balls:
  56.         dx = ball["x"] - cx
  57.         dy = ball["y"] - cy
  58.         dist = math.hypot(dx, dy) + 0.01
  59.         if dist < 200:
  60.             force = 12 / dist
  61.             ball["vx"] += force * dx
  62.             ball["vy"] += force * dy
  63.             ball["repel_timer"] = 20
  64.  
  65. def move_balls():
  66.     for ball in balls:
  67.         if ball["repel_timer"] > 0:
  68.             ball["x"] += ball["vx"]
  69.             ball["y"] += ball["vy"]
  70.             ball["vx"] *= 0.9
  71.             ball["vy"] *= 0.9
  72.             ball["repel_timer"] -= 1
  73.         else:
  74.             dx = target_x - ball["x"]
  75.             dy = target_y - ball["y"]
  76.             dist = math.hypot(dx, dy)
  77.             if dist > 1:
  78.                 step_x = ball["speed"] * dx / dist
  79.                 step_y = ball["speed"] * dy / dist
  80.                 ball["x"] += step_x
  81.                 ball["y"] += step_y
  82.  
  83.         r = ball["r"]
  84.         canvas.coords(ball["id"],
  85.                       ball["x"] - r, ball["y"] - r,
  86.                       ball["x"] + r, ball["y"] + r)
  87.     root.after(16, move_balls)
  88.  
  89. move_balls()
  90. root.mainloop()
  91.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement