from manim import * import pygments # 确保已安装pygments class CodeDemo(Scene): def construct(self): # 使用Text显示代码(兼容性最佳方案) code_content = '''from manim import * class FadeInSquare(Scene): def construct(self): s = Square() self.play(FadeIn(s)) self.play(s.animate.scale(2)) self.wait() ''' # 创建代码文本对象 code_text = Text( code_content, font="Monospace", line_spacing=0.6, font_size=20, color=WHITE ).scale(0.7) # 添加背景框 frame = SurroundingRectangle(code_text, color=BLUE, buff=0.5) # 动画序列 self.play(Create(frame), run_time=1) self.play(Write(code_text), run_time=3) self.wait(2) self.play(FadeOut(code_text), FadeOut(frame))