#Leah KaneRisman
#December 9, 2008
#CS110 final project

#Goal: to recreate the video game pong in python

from myro import*
from myro.chuck import*
initChuck()
wait(2)

def pong():
    # create canvas
    winWidth=700
    winHeight=500
    w = GraphWin("pong", winWidth, winHeight)
    w.setBackground(color_rgb(60,180,60))

    # draw ball
    r=12
    ball = Circle(Point(winWidth/2, winHeight/2),r)
    ball.setFill(color_rgb(100,40,150))
    ball.draw(w)

    #draw paddles
    paddleR=Rectangle(Point(winWidth-10,(winHeight/2)-25),Point(winWidth,(winHeight/2)+25))
    paddleR.setFill("black")
    paddleR.draw(w)
    paddleL=Rectangle(Point(0,(winHeight/2)-25),Point(10,(winHeight/2)+25))
    paddleL.setFill("black")
    paddleL.draw(w)
    
    # ball bounce defined
    dx=2
    dy=2

    #create Mandolin
    m=Mandolin()
    m.connect()
    m.setGain(1.0)
    
    #ball and paddles move
    while timeRemaining(30):
        #right paddle moves
        bx=0
        by=2
        b=getGamepadNow("button")[7]
        bb=getGamepadNow("button")[6]
        if (b==1):
            by=by
        elif (bb==1):
            by=-by
        else:
            by=0
        paddleR.move(bx,by)

        #left paddle moves
        ax=0
        ay=2
        a=getGamepadNow("button")[5]
        aa=getGamepadNow("button")[4]
        if(a==1):
            ay=ay
        elif (aa==1):
            ay=-ay
        else:
            ay=0
        paddleL.move(ax,ay)

        #the ball bounces and the mandolin plays
        center=ball.getCenter()
        cx=center.getX()
        cy=center.getY()
        if (cx+r >= winWidth) or (cx-r <= 0):
            dx=-dx
            m.setFrequency(cy+200)
            m.pluck(0.5)
        elif (cy+r >= winHeight) or (cy-r <= 0):
            dy=-dy
            m.setFrequency(440)
            m.pluck(0.5)            
        ball.move(dx,dy)
        
        wait(0.01)
        
pong()

