# BallJump.py
#
# Program draws a shape that falls from a random starting point
# and bounces a random number of times off the bottom of the
# graphics window
#
# J Cardell, Feb 17, 2011, Lab 4 CSC 111

from graphics import *
from random import *
from time import *

def main():
    # Create and label graphics window
    win = GraphWin("Shape Bounce", 750, 750)
    win.setBackground('LightYellow')
    win.setCoords(0, 0, 10, 10)
    title = Text(Point(5, 9.75), 'The Bouncing Shape')
    title.setSize(24)
    title.setFill('DarkBlue')
    title.draw(win)

    sleep(2)

    # Generate random starting position for the shape
    shapeX = randrange(1, 5)
    shapeY = randrange(7, 11)
    print "shape coords are: ", shapeX, shapeY

    # Draw shape in starting position
    sz = 0.3   # determines the size of the shape
    #box = Rectangle(Point(1,1), Point(2,2))
    box = Rectangle(Point(shapeX-sz, shapeY-sz), Point(shapeX+sz, shapeY+sz))
    box.setFill('purple')
    box.setOutline('green')
    box.setWidth(4)
    box.draw(win)

    # Use 'for' loop, with randomly generated number of iterations
    # Have between 2 and 4 bounces
    # A bounce is defined as TWO moves - one to the bottom of the window, and
    # the second up to a new height
    nBounce = randrange(2, 5)
    dx = 1   # use a constant dx value to move box to the right slowly
    
    for bnc in range(nBounce):
        # Get the center point y coordinate of 'box'
        boxY = box.getCenter().getY()

        # move box to the bottom, where 'y' = 0
        dy = 0 - (boxY-sz)
        sleep(1)
        box.move(dx, dy)

        # get random distance back up for box to bounce, constrain new
        # position based on previous position and # of bounces completed
        # Note, with no if-else structure yet, need to use shapeY-bnc to ensure
        # no negative number as parameter
        newY = randrange(int(boxY/2), shapeY-bnc)
        sleep(1)
        box.move(0, (newY+sz))


    win.getMouse()
    win.close()


main()
