from visual import *
from random import *

zufallszahlX = random()*10+3
zufallszahlZ = random()*10+3
zufallszahlY = random()*10+3



kugel = sphere(radius=20, material=materials.earth)

#for i in range(1):
 #   rate(50)
  #  kugel.rotate(angle=radians(260), axis=(0,1,0), origin=(0,0,0))



kugel2 = sphere (radius=19.99
, color=color.yellow, opacity=0.3)

ZielrohrundMensch=frame()

Mensch = frame(frame=ZielrohrundMensch)

Kopf = sphere(frame=Mensch, pos=(0,3,0),radius = 0.5, color=(.9,.8,.2))
Hals = ellipsoid(frame=Mensch, pos=(0,2.5,0), height=1.3, length=0.5, width=0.5)
Oberkoerper = ellipsoid(frame=Mensch, pos=(0,0.8,0), height=3, length=1.5, width=1, color=(.3,.2,.1))
rechtesBein = cylinder(frame=Mensch, axis=(0,-2,0), pos=(0.2,0,0), radius=0.25, color=color.green)
linkesBein = cylinder(frame=Mensch, axis=(0,-2,0), pos=(-0.2,0,0), radius=0.25, color=color.green)
rechterFuss = ellipsoid(frame=Mensch, pos=(-0.25,-2.1,-0.15), height=0.5, length=0.5, width=1, color=color.blue)
linkerFuss = ellipsoid(frame=Mensch, pos=(0.25,-2.1,-0.15), height=0.5, length=0.5, width=1, color=color.blue)
linkerArm = ellipsoid(frame=Mensch, pos=(-0.75,1.3,-0.1), height=2, length=0.5, width=0.5, axis=(-1,1,-0.3), color=(.3,.2,.1))
linkerArm = ellipsoid(frame=Mensch, pos=(0.75,1.3,-0.1), height=2, length=0.5, width=0.5, axis=(1,1,-0.3), color=(.3,.2,.1))

Untergrund=box(material=materials.rough, pos=(0,-2.4,0), length=100, height=0.0005, width=100)



Zielrohr = frame(frame=ZielrohrundMensch)

ausseresZielrohr = cylinder(frame=Zielrohr, pos=(0,2.95,-0.7), axis=(scene.forward), radius=0.07, length=2, material=materials.wood)
inneresZielrohr = cylinder(frame=Zielrohr, pos=(0,2.95,-0.7), axis=(scene.forward), radius=0.06, length=2, color=color.black)

Zielscheibe = frame()


ball = sphere(frame=Zielrohr, pos=(0,2.95,-2.7), radius=0.05)




Ring1 = cylinder(frame = Zielscheibe, pos=(zufallszahlX-0.04, 0, zufallszahlZ-0.04), length=0.17, radius=0.5, color=color.red)
Ring2 = cylinder(frame = Zielscheibe, pos=(zufallszahlX-0.03, 0, zufallszahlZ-0.03), length=0.15, radius=1)
Ring3 = cylinder(frame = Zielscheibe, pos=(zufallszahlX-0.02, 0, zufallszahlZ-0.02), length=0.13, radius=1.5, color=color.red)
Ring4 = cylinder(frame = Zielscheibe, pos=(zufallszahlX-0.01, 0, zufallszahlZ-0.01), length=0.11, radius=2)

ende=false

scene.center=(0,3.5,0)
scene.range=(3.5,3.5,3.5)
scene.forward=(0,-0.5,-1)
#scene.width=700
#scene.hight=320
#scene.userzoom=False
#scene.userspin=False

drehungpfeil = 0
drehungMensch = 0


dt = 0.00001
vGesamt = 10
vY = sin(radians(drehungpfeil))*vGesamt

vEbene = cos(radians(drehungpfeil))*vGesamt

vX = sin(radians(drehungMensch))*vEbene
vZ = cos(radians(drehungMensch))*vEbene



while ende==false:
    rate(50)


    Zielscheibe.rotate(angle=radians(1), axis=(0,1,0), origin=(zufallszahlX, 0, zufallszahlZ))
    #Ring1.rotate(angle=radians(1), axis=(0,1,0))
    #Ring2.rotate(angle=radians(1), axis=(0,1,0))
    #Ring3.rotate(angle=radians(1), axis=(0,1,0))
    #Ring4.rotate(angle=radians(1), axis=(0,1,0))
    
    if scene.kb.keys:
        s = scene.kb.getkey()
        print s
        
        if   s == 'left':     # links
            ZielrohrundMensch.rotate(angle=radians(3), axis=(0,1,0))
            newforward = rotate(scene.forward, axis=(0,1,0), angle=radians(3))
            scene.forward=newforward
            drehungMensch = drehungMensch -3
        elif s == 'right':
            ZielrohrundMensch.rotate(angle=radians(-3), axis=(0,1,0))
            newforward = rotate(scene.forward, axis=(0,1,0), angle=radians(-3))
            scene.forward=newforward
            drehungMensch = drehungMensch + 3
        elif s == 'up':
            if drehungpfeil<=44:
                Zielrohr.rotate(angle=radians(2), axis=(Hals.axis), origin=(ausseresZielrohr.pos))
                drehungpfeil = drehungpfeil + 2
                newforward = rotate(scene.forward, axis=(ZielrohrundMensch.axis), angle=radians(2))
                scene.forward=newforward
        elif s == 'down':
            if drehungpfeil>=0:
                Zielrohr.rotate(angle=radians(-2), axis=(Hals.axis), origin=(ausseresZielrohr.pos))
                drehungpfeil = drehungpfeil - 2
                newforward = rotate(scene.forward, axis=(ZielrohrundMensch.axis), angle=radians(-2))
                scene.forward=newforward
        elif s == ' ':
            #ball.pos=(1,1,1)
            #i =0
            while True: #Ring4.pos[0] > (ball.pos[0] + 0.05) and Ring4.pos[0] < (ball.pos[0] - 0.05): #and Ring4.pos[1] > (ball.pos[1] + 0.05)and Ring4.pos[1] < (ball.pos[1] - 0.05) and Ring4.pos[2] > (ball.pos[2] + 0.05) and Ring4.pos[2] < (ball.pos[2] - 0.05):
            
                vY = vY - 9.81*dt
                
            
                ball.pos=(ball.pos[0] + vX*dt,  ball.pos[1]+vY*dt,   ball.pos[2] - vZ*dt)
           # b#all.y= ball.y - 9.8 * dt
            #ball.pos = ZielrohrundMensch.frame_to_world(Zielrohr.frame_to_world(inneresZielrohr.axis))+
             #ball.pos
            
                
            
            
            