# Program stored in animateShape3.py

# Author: Ernest Ackermann

# Purpose: animate a shape

from time import *

from graphics import *

# function to determine the distance between two points

from math import *

def distance(p,q):

    dx = p.getX() - q.getX()

    dy = p.getY() - q.getY()

    dist = sqrt( dx**2 + dy**2)

    return dist

def animateShape():

    #set height and width of window

    windowWidth = 500

    windowHeight = 500

   

    win = GraphWin('Animate shape', windowWidth, windowHeight)

    # Set and then daw instructions  to the user

    # get center point from the user

    message = Text(Point(windowWidth/2, windowHeight*2/3), "click anywhere in the window ")

    message.draw(win)

   

    center = win.getMouse()

    radius = windowWidth/10 # set radius to 1/10 of the window width

    # create shape as  a Circle

    shape = Circle(center,radius)

    # fill cirlce with green

    shape.setFill('green')

    # draw shape

    shape.draw(win)

    #set the shape in motion

    message.setText("Click to set the shape in motion ")

    message.setFill('green')

    junk = win.getMouse()

    #dx is horizontal motion, dy is vertical motion

    dx = 1

    dy = 2

    # in the loop, pause then move the shape dx pixels right and dy pixels down

    message.setFill("black")

    message.setText("Click again to stop the ball")

    while win.checkMouse() == None :

        sleep(0.04)       # pause

        shape.move(dx,dy) # move shape

        #get the Center of the shape

        center= shape.getCenter()

        # if shape touches/crosses the right edge

        # chage horizontal direction

       

        edgePoint = Point(windowWidth, center.getY() )

        if distance(center,edgePoint )< radius :

                    dx = -dx

        # if shape touches/crosses the bottom edge

        # change vertical direction

        edgePoint = Point(center.getX(), windowHeight)

        if distance(center,edgePoint )< radius :

                    dy = -dy

    #clean up

    message.setFill("red")

    message.setText("Click again to close the window")

    win.getMouse()

    win.close()

animateShape()