import drawSvg as draw
from random import randint, random


# Ball class
class Ball:
    def __init__(self, start_x, start_y, start_v_x, start_v_y, color='blue'):
        # Ball location and velocity
        self.x = start_x
        self.y = start_y
        self.v_x = start_v_x
        self.v_y = start_v_y

        # Ball color, for drawing purposes
        self.color = color

    def update_position(self, timestep):
        self.x = self.x + timestep * self.v_x
        self.y = self.y + timestep * self.v_y 

    def update_velocity(self, timestep):
        self.v_y = self.v_y + timestep * EARTH_GRAVITY_ACCELERATION

    def animate_step(self, timestep):
        self.update_position(timestep)
        self.update_velocity(timestep)

    def draw_ball(self):
        D.append(draw.Circle(self.x, self.y, BALL_RADIUS, fill=self.color))  



# test Ball class
D = draw.Drawing(200, 200, origin='center') # define drawing canvas
EARTH_GRAVITY_ACCELERATION = -9.8   # acceleration due to gravity, m/sec^2
BALL_RADIUS = 10  # radius of the ball in pixels

ball1 = Ball(5.0, 5.0, 3.0, 6.0)
print( "the x-coordinate is ", ball1.x)
print( "the y-coordinate is ", ball1.y)

ball1.update_position(0.1)
print( "the x-coordinate is ", ball1.x)
print( "the y-coordinate is ", ball1.y)

ball1.animate_step(0.1)
print( "the y-velocity is ", ball1.v_y)

ball1.draw_ball()

ball2 = Ball(-5.0, -5.0, -3.0, -6.0, "red")
ball2.draw_ball()
D



# Ball object animation
D = draw.Drawing(200, 200, origin='center') # define drawing canvas
EARTH_GRAVITY_ACCELERATION = -9.8   # acceleration due to gravity, m/sec^2
BALL_RADIUS = 10  # radius of the ball in pixels

ball1 = Ball(5.0, 5.0, 10.0, 20.0, "green")
ball2 = Ball(-5.0, -5.0, -10.0, 20.0, "red")

def draw_frame():
    ball1.draw_ball()
    ball2.draw_ball()
    return D

for i in range(0,100):
    D = draw.Drawing(200, 200, origin='center') # erase canvas
    
    with draw.animate_jupyter(draw_frame, delay=0.05) as anim:
        anim.draw_frame()
        
    ball1.animate_step(0.1)
    ball2.animate_step(0.1)




