Загрузка данных


import math
import random
import pygame

pygame.init()
W, H = 800, 600
screen = pygame.display.set_mode((W, H))
clock = pygame.time.Clock()

circles = []
for _ in range(random.randint(5, 15)):
    r = random.randint(15, 50)
    circles.append({
        "x": random.randint(r, W - r),
        "y": random.randint(r, H - r),
        "vx": random.choice([-1, 1]) * random.uniform(1, 4),
        "vy": random.choice([-1, 1]) * random.uniform(1, 4),
        "r": r,
        "color": (random.randint(50, 255), random.randint(50, 255), random.randint(50, 255)),
    })

running = True
while running:
    for e in pygame.event.get():
        if e.type == pygame.QUIT:
            running = False

    mx, my = pygame.mouse.get_pos()
    screen.fill((20, 20, 20))

    for c in circles:
        c["x"] += c["vx"]
        c["y"] += c["vy"]

        if c["x"] < c["r"] or c["x"] > W - c["r"]:
            c["vx"] = -c["vx"]
        if c["y"] < c["r"] or c["y"] > H - c["r"]:
            c["vy"] = -c["vy"]

        dx = c["x"] - mx
        dy = c["y"] - my
        d = math.hypot(dx, dy)
        if 0 < d < c["r"]:
            nx, ny = dx / d, dy / d
            dot = c["vx"] * nx + c["vy"] * ny
            if dot < 0:
                c["vx"] -= 2 * dot * nx
                c["vy"] -= 2 * dot * ny
            c["x"] = mx + nx * c["r"]
            c["y"] = my + ny * c["r"]

        pygame.draw.circle(screen, c["color"], (int(c["x"]), int(c["y"])), c["r"])

    pygame.display.flip()
    clock.tick(60)

pygame.quit()