#Matt Salerno
#Recitation 002, Assignment 8

from visual import *
from random import *

ground          = box(length=50,width=30,height=0.5,color=color.green) #The ground
ball            = sphere(pos=vector(-25,1,-5)) #The ball that's being moved
ball.m          = 0.5
ball.radius     = .8

nemesis         = sphere(pos=vector(-25,1,5)) #Computer's ball
nemesis.m       = 0.5
nemesis.radius  = .8
nemesis.color   = color.red

scene.autoscale = 0 


print 'The object of the game is to shoot your ball (the white one) and have it hit the opposite wall, come back and get closer to the original wall than your evil nemisis (Dan McGovern, red ball). How fast do you want to hit your ball? (m/s)' 
vx = input()



ball.p          = vector(ball.m * vx, 0 ,0)
#nemesis.p       = vector(nemesis.m *(( random() + 4)*5),0,0)
nemesis.p       = vector(nemesis.m * 16,0,0)
### Constants

kf  = 0.2 #Coefficient of kinetic friction
cr  = 0.9 #Sort of like the coefficient of restitution, when it hits the wall and bounces back it's not perfectly elastic
g   = 9.8 #gravity

t   = 0
dt  = .01


wingame = False

## Updating Your ball

while (ball.pos.x >= -25 and ball.pos.x <=25 and ball.p.x > 0) :
    rate(100)
    Ff            = vector(-ball.m * g * kf, 0, 0)
    Fnet          = Ff
    ball.p       += Fnet * dt
    ball.pos     += (ball.p/ball.m) * dt
   # print ball.pos, ball.p,t
    t            += dt
    if ball.p.x < 0:
        print 'Oh shoot, it did not even reach the wall'
    if (abs(25-ball.pos.x)<0.2):
        break

ball.pa           = -1 * ball.p * cr

while (ball.pos.x > -25 and ball.pos.x <= 27 and ball.pa.x <=0) :
    rate(50)
    FF            = vector(ball.m * g * kf,0,0) #positive direction because the ball will be moving in the negative direction now
    Net           = FF
    ball.pa      += Net * dt
    ball.pos     += (ball.pa/ball.m) * dt
  #  print ball.pos, ball.pa, t
    t            += dt
    if (ball.pos.x <-25):
        print 'Too bad, you hit the wall.'
    elif (ball.pos.x >-25 and ball.pos.x < 25 and abs(ball.pa.x) < 0.02):
        break

### Now updating the nemesis' ball.

while (nemesis.pos.x >= -25 and nemesis.pos.x <=25 and nemesis.p.x > 0) :
    rate(100)
    Ffn            = vector(-nemesis.m * g * kf, 0, 0)
    Fnetn          = Ffn
    nemesis.p     += Fnet * dt
    nemesis.pos   += (nemesis.p/nemesis.m) * dt
  #  print nemesis.pos, nemesis.p,t, Ffn
    t            += dt
    if (abs(25-nemesis.pos.x)<0.2):
        break

nemesis.pa           = -1 * nemesis.p * cr

while (nemesis.pos.x > -25 and nemesis.pos.x <= 27 and nemesis.pa.x <=0) :
    rate(50)
    FFn              = vector(nemesis.m * g * kf,0,0) #positive direction because the ball will be moving in the negative direction now
    Netn             = FFn
    nemesis.pa      += Netn * dt
    nemesis.pos     += (nemesis.pa/nemesis.m) * dt
   # print nemesis.pos, nemesis.pa, t, FFn
    t            += dt
    if (nemesis.pos.x <-25):
        wingame = True
        print 'You are lucky Dan McGovern stinks at life (as seen in emacs prog6.py). He hit the wall. '
    elif (nemesis.pos.x >-25 and nemesis.pos.x < 25 and abs(nemesis.pa.x) < 0.02):
        break


if (ball.pos.x < nemesis.pos.x or wingame == True):
    print 'Congratulations! You are better than Dan McGovern (obviously...)'

if (nemesis.pos.x < ball.pos.x and wingame == False):
    print 'How the hell could you let Dan McGovern beat you?'

#print t, ball.p, ball.pos
