polyrhythm from wangbw

from manim import *
from os import system
import random
import os
config.frame_width  = 16
config.frame_height = 9
config.frame_size = (1920,1080)#(1080,1920)#(3840,2160)#(2560,1440)#(1920,1080)#
config.frame_rate = 30
config.background_color = BLACK


class B_Polyrhythm_234AgainAll(Scene):
    firstProgres = 0
    secondProgres = 0
    currentAgainst = 0
    factor = 0.01
    def construct(self):

        sounds = ['bongo.mp3','open_conga.wav','low_bongo.wav','kick.wav', 'snare.wav', 'hiHat.wav']
        soundPath = 'sounds\drum'
        dirname = os.path.dirname(__file__)
        sounds = [os.path.join(dirname, soundPath, s) for s in sounds]


        ######3 或者 4  again 5 7 11 13 17 19
        author = Text("Wangbw", fill_opacity=0.3).scale(0.6).to_edge(DOWN, buff=0.2)

        firstNumbers = [2,3,4]
        firsts = []
        vgFirst = VGroup()
        for t in firstNumbers:
            r = RegularPolygon(n=t, stroke_color=GREY, stroke_width=8, fill_opacity=0.3, fill_color=GREY).scale(1.5)
            if t==3:
                rotate = 0
            else:
                rotate = PI/2
            r.rotate(rotate)
            vgFirst.add(r)
            firsts.append(r)

        vgFirst.move_to(LEFT*5.6).arrange_in_grid(cols=1, buff=0.2)

        otherNumber = [5, 7, 9, 11, 13, 15]
        others = []
        vg = VGroup()
        for t in otherNumber:
            r = RegularPolygon(n=t, stroke_color=GREY, stroke_width=8, fill_opacity=0.3, fill_color=GREY).scale(1.5)
            vg.add(r)
            others.append(r)

        vg.arrange_in_grid(rows=2, buff=0.6).move_to(RIGHT*2.3)
        self.add(author)
        self.play(Create(vgFirst, run_time=0.5), Create(vg, run_time=1))

        ###here to make voice.
        eachRoundCount = 2
        startIndex = 1.5
        firstPosition = 0
        SecondPosition = 0
        roundTime = 2
        for t1 in range(len(firstNumbers)):
            for t2 in range(len(others)):
                t1_slice = roundTime / firstNumbers[t1]
                t2_slice = roundTime / otherNumber[t2]
                eachRoundCurrent = 0
                while eachRoundCurrent < eachRoundCount:
                    for x in range(firstNumbers[t1]):
                        self.add_sound(sounds[0], firstPosition)
                        firstPosition = firstPosition + t1_slice
                    for x in range(otherNumber[t2]):
                        self.add_sound(sounds[1], SecondPosition)
                        SecondPosition = SecondPosition + t2_slice
                    eachRoundCurrent = eachRoundCurrent + 1


        firstBall = Circle(radius=0.2, stroke_width=1, stroke_color=WHITE, fill_opacity=1,
                                color=ORANGE)
        secondBall = Circle(radius=0.2, stroke_width=1, stroke_color=WHITE, fill_opacity=1,
                                color=YELLOW)
        aniFirstGroup  = [] #= MoveAlongPath(firstBall, p3, run_time=2, rate_func=linear)
        for me in firsts:
            aniFirstGroup.append(MoveAlongPath(firstBall, me, run_time=roundTime, rate_func=linear))

        aniSecondGroup = []
        for against in others:
            aniSecondGroup.append(MoveAlongPath(secondBall, against, run_time=roundTime, rate_func=linear))




        for tttt in range(len(firsts)):

            for t in range(len(firsts)):
                firsts[t].stroke_color = WHITE
            firsts[tttt].stroke_color = ORANGE

            for index in range(len(others)):
                roundCount = 0
                for t in range(len(others)):
                        others[t].stroke_color = WHITE
                others[index].stroke_color = YELLOW

                while roundCount < eachRoundCount:
                    self.play(AnimationGroup(aniFirstGroup[tttt], aniSecondGroup[index]))
                    roundCount = roundCount + 1


        self.wait(1)
if __name__ == '__main__':
    system("manim -qh {} B_Polyrhythm_234AgainAll --disable_caching".format(__file__))

By Admin

Think-Math Website