logo
Published on Models of Motion (http://www2.evergreen.edu/modelsofmotion)

Target Practice

from visual import *
from random import randrange
scene.width=800
scene.autoscale=0

inity = randrange(0,4)
targetx = randrange(4,10)
ball = sphere(pos=(-9,inity,0), color=color.blue,radius=.3)
target = box(pos=(targetx,-4.5,0),color=color.red,height=.3,length=.5)
hit=0

while hit==0:
    ang = input("enter angle")
    ang = math.pi/180.0*ang
    mag = input("input the force")
    ball.velocity = vector(cos(ang)*mag, sin(ang)*mag,0)


    d=.01
    while ball.pos.y>=-4.5:
        rate(50)
      
        ball.velocity.y = ball.velocity.y-9.8*d
      
        ball.pos = ball.pos + ball.velocity*d

    if abs(ball.pos.x-targetx)<.2:
        print "you have a hit"
        hit=1
    else:
        print "you missed by",ball.pos.x-targetx,"units"
        ball.pos=(-9,inity,0)


Source URL:
http://www2.evergreen.edu/modelsofmotion/modelsofmotion/target-practice