from manim import *
import numpy as np
def head(cn, en):
    t = Text(cn, font="STLiti", font_size=27).to_edge(UP)
    s = Text(en, font="Consolas", font_size=20, color=YELLOW).next_to(t, DOWN, buff=0.10)
    return VGroup(t, s)
def foot(cn):
    return Text(cn, font="STLiti", font_size=20, color=YELLOW).to_edge(DOWN)
class BubbleEffect(Animation):
    """自定义动画：彩色气泡上升、变大并逐渐透明。"""
    def __init__(self, bubble_count=24, colors=None, seed=7, **kwargs):
        self.colors = colors or [RED, BLUE, GREEN, YELLOW, PURPLE, ORANGE]
        rng = np.random.default_rng(seed)
        bubbles = VGroup()
        for i in range(bubble_count):
            size = rng.uniform(0.11, 0.34)
            c = self.colors[i % len(self.colors)]
            b = Circle(radius=size, color=c).set_fill(c, opacity=0.45)
            b.set_stroke(c, width=2)
            b.move_to(np.array([rng.uniform(-6.0, 6.0), rng.uniform(-3.3, -2.1), 0.0]))
            b.base_pos = b.get_center().copy()
            b.base_w = b.width
            b.rise = rng.uniform(1.8, 4.4)
            b.grow = rng.uniform(0.4, 1.3)
            bubbles.add(b)
        super().__init__(bubbles, **kwargs)
    def interpolate_mobject(self, alpha):
        for b in self.mobject:
            b.move_to(b.base_pos + UP * b.rise * alpha)
            b.set_width(b.base_w * (1 + b.grow * alpha))
            b.set_fill(opacity=0.45 * (1 - 0.85 * alpha))
            b.set_stroke(opacity=1 - 0.8 * alpha)
class BubbleColorful(Scene):
    def construct(self):
        self.add(head("彩色气泡：多色与随机尺寸", "BubbleEffect / colors"))
        eff = BubbleEffect(bubble_count=30,
                           colors=[RED, BLUE, GREEN, YELLOW, PURPLE, ORANGE],
                           seed=23, run_time=4.6)
        self.add(eff.mobject)
        self.play(eff, run_time=4.6)
        eff.mobject.set_fill(opacity=0.5).set_stroke(opacity=1)
        self.play(FadeIn(foot("随机种子固定后，每次渲染都能复现同一组气泡")), run_time=0.4)
        self.wait(1.0)
