弹跳球蟒蛇

时间:2013-04-26 21:11:37

标签: python python-2.7 vpython

所以我是Python新手,我一直在为我的班级做一个弹跳球项目。我几乎完成了教授问我的一切。我遇到的唯一问题就是让球在撞到地板后停下来。我在网上寻找答案,但我不想改变我的代码。

这是代码

from visual import *

scene1 = display(title='Bouncing Ball Project',
                 x = 0, y= 0, width = 600, height = 600,
                 center = (0,0,0), background = (.3,.3,.3))

wallL = box(pos=(-20, 0, 0), size=(0.2,12, 12), color=color.cyan)
wallR = box(pos=(20, 0, 0), size=(0.2,12, 12), color=color.cyan)
wallD = box(pos=(0, -6, 0), size=(40,0.2, 12), color=color.cyan)

ball = sphere(pos=(-20, 6, 0), radius=0.7, color=color.red, make_trail=True,
          retain=50)

ball.trail = curve(color=ball.color)
angle = pi/4
v0 = vector(cos(angle),sin(angle),0)*10

gravity = vector(0, -9.8, 0)
p0 = vector(-20, 6,0)

x0 = -20
y0 = 6

yMax = wallR.height/2
yMin = wallR.y - wallR.height/2
xMin = wallD.length/2


ballV=arrow(pos=ball.pos, axis=v0/25, shaftwidth=.15,color=color.blue)
ballA=arrow(pos=ball.pos, axis=gravity/5, shaftwidth=.15,color=color.yellow)
ballInfo=label(pos=(0,13,0), text='(-5,0,0)', height=20)
Arc = 0

def ballpos():
    temp ='pos = <%5.1f,%5.1f> \n v =(%5.1f) m/s ' %(ball.x,ball.y,ball.velocity.mag)
    ballInfo.text= temp + '\n length = (%5.3f)' %(Arc)


dt = 0.005; t=0
k = .5

done = False
while  t <10:

    rate(100)

    ball.velocity = v0 + gravity*t
    ball.pos = p0 + v0*t + gravity*(t**2)/2
    Arc += ball.velocity.mag*dt

    ballV.axis = 3*ball.velocity/ball.velocity.mag
    ballV.pos = ball.pos
    ballA.pos = ball.pos
    ballA.axis = 3*gravity/gravity.mag

    if ball.pos.x > wallR.pos.x:
        k = .25
        p0.x = wallR.pos.x
        p0.y = ball.pos.y
        ball.pos.x = wallR.pos.x
        ball.velocity.x = -k*ball.velocity.x
        v0.x = ball.velocity.x
        t =0
        print ("Right")

    elif ball.pos.x < wallL.pos.x:
        p0.x = wallL.pos.x
        p0.y = ball.pos.y
        ball.pos.x = wallL.pos.x
        ball.velocity.x = -k*ball.velocity.x 
        v0.x = ball.velocity.x
        t = 0
        print ("Left")


    elif ball.pos.y < yMin:
        p0.y = yMin
        p0.x = ball.pos.x 
        ball.pos.y = yMin
        ball.velocity.y = -k*ball.velocity.y
        v0.y = ball.velocity.y
        t=0
        print  ("Bottom", ball.pos, ball.velocity, p0)


    ballpos()
    ball.velocity
    ball.trail.append(pos=ball.pos)
    t=t+dt

0 个答案:

没有答案