#Jennifer Pillion, Recitation 2, Mini Golf
from visual import *

#Elements of the mini golf hole
grass1=box(pos=(3.5,9.5,0), length=7, height=19, width=0.01, color=color.green)
grass2=box(pos=(13,15.5,0),length=12, height=7, width=0.01,color=color.green)
hole=cylinder(axis=(0,0,0.1),pos=(17,15.5,0),radius=0.5, color=color.black)
tee=box(pos=(3.5,1,0), length=3, height=2,width=0.02)
ball=sphere(pos=(3.5,1,0.25),radius=0.25,color=color.blue)
#Vertical wall farthest to the left
wall1=box(pos=(0,9.5,0.5),length=0.2, height=19, width=1)
#Horizontal wall farthest down
wall2=box(pos=(3.5,0.1,0.5),length=7,height=0.2, width=1)
#Middle vertical wall
wall3=box(pos=(6.99,6,0.5),length=0.2, height=12, width=1)
#Middle horizontal wall
wall4=box(pos=(13,12.01,0.5),length=12,height=0.2,width=1)
#Vertical wall farthest to the right
wall5=box(pos=(18.9,15.5,0.5),length=0.2,height=7,width=1)
#Horizontal wall farthest up
wall6=box(pos=(9.5,18.9,0.5),length=19,height=0.2,width=1)

#Centers the scene to maximize golf hole size
scene.center=(9.5,9.5,0)
#Disallows the user to move the scene
scene.userspin = 0

#Mass of the golf ball
ball.m=0.3
t=0
dt=.0005
print "Par = 2"
print "First Stroke"
print "Please enter up(+)/down(-) velocity(m/s)"
vy=input()
print "Please enter right(+)/left(-) velocity(m/s)"
vx=input()
#Coefficient of friction from the grass
mu=0.4
#Value of gravity to calculate the normal force used in the frictional force
g=9.8
end=0

#Defines the ball's momentum, non-relativistic can be used since it is going much less than the speed of light
ball.p=ball.m*vector(vx,vy,0)
#Trail used so people can see the path of their ball and how close it was
ball.trail=curve(color=color.blue)


while (mag(ball.p)> 0):
    ball.trail.append(pos=ball.pos)
    t=t+dt
    #Updates the ball's momentum
    ball.pos=ball.pos+ball.p/ball.m*dt
    #Frictional force, assuming that the ball is sliding to create a slowing down effect in the opposite direction of the velocity
    Fr=-mu*g*ball.m*(ball.p/mag(ball.p))
    #Updates the momentum
    ball.p=dt*Fr+ball.p
    #All of these allow for deflection of the ball from the wall; the momentum that is transferred to the wall is nearly zero, allowing for all of the momentum to remain in the ball
    if (ball.pos.x<=wall1.pos.x):
        ball.p.x=-ball.p.x
    if (ball.pos.y<=wall2.pos.y):
        ball.p.y=-ball.p.y
    if (ball.pos.x>=wall5.pos.x):
        ball.p.x=-ball.p.x
    if (ball.pos.y>=wall6.pos.y):
        ball.p.y=-ball.p.y
    if (ball.pos.y<12):
        if (ball.pos.x>=wall3.pos.x):
            ball.p.x=-ball.p.x
    if(ball.pos.x>7):
        if (ball.pos.y<=wall4.pos.y):
            ball.p.y=-ball.p.y
    #Prints out the win
    if(abs((hole.pos)-(ball.pos))<=0.5):
        print "Hole in one!  You're a pro or you've played this too many times and now know the right velocity to but in."
        #Terminates the while loop so that only one message is sent through the terminal
        ball.p=vector(0,0,0)
        end = 1
    #Allows the ball to escape the while loop so the next shot can be initiated
    if(mag(ball.p)<0.1):
        ball.p=vector(0,0,0)
if end ==0:
    print "Second Stroke"
    print "Please enter up(+)/down(-) velocity(m/s)"
    vy=input()
    print "Please enter right(+)/left(-) velocity(m/s)"
    vx=input()

        
    
    ball.p=ball.m*vector(vx,vy,0)
    
    while (mag(ball.p)> 0):
        ball.trail.append(pos=ball.pos)
        t=t+dt
        ball.pos=ball.pos+ball.p/ball.m*dt
        Fr=-mu*g*ball.m*(ball.p/mag(ball.p))
        ball.p=dt*Fr+ball.p
        if (ball.pos.x<=wall1.pos.x):
            ball.p.x=-ball.p.x
        if (ball.pos.y<=wall2.pos.y):
            ball.p.y=-ball.p.y
        if (ball.pos.x>=wall5.pos.x):
            ball.p.x=-ball.p.x
        if (ball.pos.y>=wall6.pos.y):
            ball.p.y=-ball.p.y
        if (ball.pos.y<12):
            if (ball.pos.x>=wall3.pos.x):
                ball.p.x=-ball.p.x
        if(ball.pos.x>7):
            if (ball.pos.y<=wall4.pos.y):
                ball.p.y=-ball.p.y
        if(abs((hole.pos)-(ball.pos))<=0.5):
            print "Par"
            ball.p=vector(0,0,0)
            end=1
        if(mag(ball.p)<0.1):
            ball.p=vector(0,0,0)
if end ==0:            
    print "Third Stroke"
    print "Please enter up(+)/down(-) velocity(m/s)"
    vy=input()
    print "Please enter right(+)/left(-) velocity(m/s)"
    vx=input()
    
    ball.p=ball.m*vector(vx,vy,0)
    
    while (mag(ball.p)> 0):
        ball.trail.append(pos=ball.pos)
        t=t+dt
        ball.pos=ball.pos+ball.p/ball.m*dt
        Fr=-mu*g*ball.m*(ball.p/mag(ball.p))
        ball.p=dt*Fr+ball.p
        if (ball.pos.x<=wall1.pos.x):
            ball.p.x=-ball.p.x
        if (ball.pos.y<=wall2.pos.y):
            ball.p.y=-ball.p.y
        if (ball.pos.x>=wall5.pos.x):
            ball.p.x=-ball.p.x
        if (ball.pos.y>=wall6.pos.y):
            ball.p.y=-ball.p.y
        if (ball.pos.y<12):
            if (ball.pos.x>=wall3.pos.x):
                ball.p.x=-ball.p.x
        if(ball.pos.x>7):
            if (ball.pos.y<=wall4.pos.y):
                ball.p.y=-ball.p.y
        if(abs((hole.pos)-(ball.pos))<=0.5):
            print "Bogey"
            ball.p=vector(0,0,0)
            end=1
        if(mag(ball.p)<0.1):
            ball.p=vector(0,0,0)
if end ==0:            
    print "Fourth Stroke"
    print "Please enter up(+)/down(-) velocity(m/s)"
    vy=input()
    print "Please enter right(+)/left(-) velocity(m/s)"
    vx=input()
    
    ball.p=ball.m*vector(vx,vy,0)
    
    while (mag(ball.p)> 0):
        ball.trail.append(pos=ball.pos)
        t=t+dt
        ball.pos=ball.pos+ball.p/ball.m*dt
        Fr=-mu*g*ball.m*(ball.p/mag(ball.p))
        ball.p=dt*Fr+ball.p
        if (ball.pos.x<=wall1.pos.x):
            ball.p.x=-ball.p.x
        if (ball.pos.y<=wall2.pos.y):
            ball.p.y=-ball.p.y
        if (ball.pos.x>=wall5.pos.x):
            ball.p.x=-ball.p.x
        if (ball.pos.y>=wall6.pos.y):
            ball.p.y=-ball.p.y
        if (ball.pos.y<12):
            if (ball.pos.x>=wall3.pos.x):
                ball.p.x=-ball.p.x
        if(ball.pos.x>7):
            if (ball.pos.y<=wall4.pos.y):
                ball.p.y=-ball.p.y
        if(abs((hole.pos)-(ball.pos))<=0.5):
            print "Double Bogey"
            ball.p=vector(0,0,0)
            end=1
        if(mag(ball.p)<0.1):
            ball.p=vector(0,0,0)
if end ==0:          
    print "Fifth Stroke"
    print "Please enter up(+)/down(-) velocity(m/s)"
    vy=input()
    print "Please enter right(+)/left(-) velocity(m/s)"
    vx=input()
    
    ball.p=ball.m*vector(vx,vy,0)
    
    while (mag(ball.p)> 0):
        ball.trail.append(pos=ball.pos)
        t=t+dt
        ball.pos=ball.pos+ball.p/ball.m*dt
        Fr=-mu*g*ball.m*(ball.p/mag(ball.p))
        ball.p=dt*Fr+ball.p
        if (ball.pos.x<=wall1.pos.x):
            ball.p.x=-ball.p.x
        if (ball.pos.y<=wall2.pos.y):
            ball.p.y=-ball.p.y
        if (ball.pos.x>=wall5.pos.x):
            ball.p.x=-ball.p.x
        if (ball.pos.y>=wall6.pos.y):
            ball.p.y=-ball.p.y
        if (ball.pos.y<12):
            if (ball.pos.x>=wall3.pos.x):
                ball.p.x=-ball.p.x
        if(ball.pos.x>7):
            if (ball.pos.y<=wall4.pos.y):
                ball.p.y=-ball.p.y
        if(abs((hole.pos)-(ball.pos))<=0.5):
            print "3 Over"
            ball.p=vector(0,0,0)
            end=1
        if(mag(ball.p)<0.1):
            ball.p=vector(0,0,0)
if end ==0:    
    print "Sixth Stroke"
    print "Please enter up(+)/down(-) velocity(m/s)"
    vy=input()
    print "Please enter right(+)/left(-) velocity(m/s)"
    vx=input()
    
    ball.p=ball.m*vector(vx,vy,0)
    
    while (mag(ball.p)> 0):
        ball.trail.append(pos=ball.pos)
        t=t+dt
        ball.pos=ball.pos+ball.p/ball.m*dt
        Fr=-mu*g*ball.m*(ball.p/mag(ball.p))
        ball.p=dt*Fr+ball.p
        if (ball.pos.x<=wall1.pos.x):
            ball.p.x=-ball.p.x
        if (ball.pos.y<=wall2.pos.y):
            ball.p.y=-ball.p.y
        if (ball.pos.x>=wall5.pos.x):
            ball.p.x=-ball.p.x
        if (ball.pos.y>=wall6.pos.y):
            ball.p.y=-ball.p.y
        if (ball.pos.y<12):
            if (ball.pos.x>=wall3.pos.x):
                ball.p.x=-ball.p.x
        if(ball.pos.x>7):
            if (ball.pos.y<=wall4.pos.y):
                ball.p.y=-ball.p.y
        if(abs((hole.pos)-(ball.pos))<=0.5):
            print "4 Over"
            ball.p=vector(0,0,0)
            end=1
        if(mag(ball.p)<0.1):
            ball.p=vector(0,0,0)
if end ==0:    
    print "Seventh Stroke"
    print "Please enter up(+)/down(-) velocity(m/s)"
    vy=input()
    print "Please enter right(+)/left(-) velocity(m/s)"
    vx=input()
    
    ball.p=ball.m*vector(vx,vy,0)
    
    while (mag(ball.p)> 0):
        ball.trail.append(pos=ball.pos)
        t=t+dt
        ball.pos=ball.pos+ball.p/ball.m*dt
        Fr=-mu*g*ball.m*(ball.p/mag(ball.p))
        ball.p=dt*Fr+ball.p
        if (ball.pos.x<=wall1.pos.x):
            ball.p.x=-ball.p.x
        if (ball.pos.y<=wall2.pos.y):
            ball.p.y=-ball.p.y
        if (ball.pos.x>=wall5.pos.x):
            ball.p.x=-ball.p.x
        if (ball.pos.y>=wall6.pos.y):
            ball.p.y=-ball.p.y
        if (ball.pos.y<12):
            if (ball.pos.x>=wall3.pos.x):
                ball.p.x=-ball.p.x
        if(ball.pos.x>7):
            if (ball.pos.y<=wall4.pos.y):
                ball.p.y=-ball.p.y
        if(abs((hole.pos)-(ball.pos))<=0.5):
            print "4 Over"
            ball.p=vector(0,0,0)
        if(mag(ball.p)<0.1):
            print "You lose.  Just so you know, I think you should keep your day job."
            ball.p=vector(0,0,0)
