scene  *
 numpy  np
 random
 MyScene(Scene):
    
     setup():
        .background_color='#00483a'
        .width,.height=.size
        balls_name=['emj:Black_Circle',
                                'emj:Blue_Circle',
                                'emj:Red_Circle',
                                'emj:White_Circle',
                                'emj:Grinning',
                                'emj:Smiling_1',
                                'emj:Smiling_2',
                                'emj:Smiling_3',
                                'emj:Smiling_4',
                                'emj:Smiling_5',
                                'emj:Smiling_6']
        .balls=[]
         ball_name in balls_name:
            r=28
            px=random.randrange(r,.width-r)
            py=random.randrange(r,.height-r)
            vx=random.uniform(-10,10)
            vy=random.uniform(-10,10)
            ball=ExSpriteNode(ball_name,r,px,py,vx,vy)
            .balls.append(ball)
            .add_child(ball)
    
     update():
         ball in .balls:
            ball.move(1.0)
        n=(.balls)
         i1 in (n-1):
            b1=.balls[i1]
             i2 in (i1+1,n):
                b2=.balls[i2]
                r=b1.radius+b2.radius
                d=.get_distance(b1,b2)                    
                 (d-r,2)<=0:
                    t=.get_contact_time(b1,b2)
                    b1.position+=b1.velocity*t
                    b2.position+=b2.velocity*t
                    v1=.get_new_velocity(b1,b2)
                    v2=.get_new_velocity(b2,b1)
                    b1.velocity+=(-v1)+v2
                    b2.velocity+=v1+(-v2)
                    b1.position+=b1.velocity*(-t)
                    b2.position+=b2.velocity*(-t)
                                        
     get_new_velocity(,ball1,ball2):
        a,b=ball1.velocity
        X,Y=ball2.position-ball1.position
        k=(a*X+b*Y)/(X**2+Y**2)
         np.array([k*X,k*Y])
     get_distance(,ball1,ball2):
        v=ball2.position-ball1.position
        r=ball1.radius+ball2.radius
         np.linalg.norm(v)
        
     get_contact_time(,ball1,ball2):
        A,B=ball2.position-ball1.position
        C,D=ball2.velocity-ball1.velocity
        R=ball1.radius+ball2.radius
        a=C**2+D**2
        b=2*(A*C+B*D)
        c=A**2+B**2-R**2
        t=(-b-(b**2-4*a*c)**(1/2))/(2*a)
         t
                            
 ExSpriteNode(SpriteNode):
        
     __init__(,texture,radius,px,py,vx,vy):
        (ExSpriteNode,).__init__(texture)
        .radius=radius
        .position=np.array([px,py])
        .velocity=np.array([vx,vy])
        
     move(,decay):
        .position+=.velocity
        px,py=.position
        vx,vy=.velocity
        r=.radius
        w=.parent.width-r
        h=.parent.height-r
         px<r:
            px=r
            vx=-vx*decay            
         px>w:
            px=w
            vx=-vx*decay
         py<r:
            py=r
            vy=-vy*decay
         py>h:
            py=h
            vy=-vy*decay
        .velocity=np.array([vx,vy])
        .position=np.array([px,py])        
            
run(MyScene())