例4
- 画圆
- 画半径
- 画三角
- 画周长
- 复制圆转变为周长
class Tute4(Scene):
def construct(self):
#这个浮点数变量初始为0.5,这个变量是可以动画变化线性等方式增加或减少的
r = ValueTracker(0.5) #Tracks the value of the radius
#根据每帧不断重绘圆,半径大小是r
circle = always_redraw(lambda :
circle(radius = r.get_value(), stroke_color = YELLOW,
stroke_width =5))
#画一条线,起点圆心,终点圆底部,也是实时重绘
line_radius = always_redraw(lambda :
Line(start = circle.get_center(), end = circle.get_ bottom(), stroke_color = RED_B, stroke_width = 10)
#画线,长度是圆的周长,位置在圆的下部,缓冲0.2
line_circumference = always_redraw(lambda :
Line(stroke_color = YELLOW, stroke_width = 5
).set_length(2*r.get_value()* PI).next_to(circle, DOWN, buff=0.2)
#三角形,实时重绘,圆顶部左部右部
triangle = always_redraw(lambda :
Polygon(circle.get_top(), circle.get_left(), circle.get_right(), fill_color = GREEN_C)
#动画分部进行,画圆,画半径,画三角
self.play(Laggedstart(
Create(circle), DrawBorderThenFill(line_radius), DrawBorderThenFill(triangle),
run_time =4, lag_ratio = 0.75)
#复制一个圆变成周长
self.play(ReplacementTransform(circle.copy(), line_ circumference), run_time = 2)
#动画,半径变大,其他关联图形随着变化
self.play(r.animate.set_value(2), run_time = 5)