Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
VPython:在动画开始之前,是否需要显示画布?_Python_Python 3.x_Ipython_Vpython - Fatal编程技术网

VPython:在动画开始之前,是否需要显示画布?

VPython:在动画开始之前,是否需要显示画布?,python,python-3.x,ipython,vpython,Python,Python 3.x,Ipython,Vpython,我一直在做一个类似于《愤怒的小鸟》的游戏,游戏中一枚炮弹射向一个目标,目的是把它打倒。然而,在输入初始启动参数和动画开始之前,我正在努力找到一种显示画布的方法。代码运行时发生的第一件事是它请求输入,但我想在这之前显示可视化。想知道是否有一种简单的方法可以做到这一点,下面是代码 scene = canvas(width=640, height=480, center=vector(8,5,0),range=8, background=color.blue) ground = box(pos=vec

我一直在做一个类似于《愤怒的小鸟》的游戏,游戏中一枚炮弹射向一个目标,目的是把它打倒。然而,在输入初始启动参数和动画开始之前,我正在努力找到一种显示画布的方法。代码运行时发生的第一件事是它请求输入,但我想在这之前显示可视化。想知道是否有一种简单的方法可以做到这一点,下面是代码

scene = canvas(width=640, height=480, center=vector(8,5,0),range=8, background=color.blue)
ground = box(pos=vector(8,-5,0), length=16, height=10, width=0.5, color=color.green)

dist = 5 + random()*10 
target = box(pos=vector(dist,1,0), length=0.5, height=2, width=0.5, color=color.red)

height = random() #determines random height of platform between 0 and 1 meters
platform = box(pos=vector(-2,height-0.25,0), length=4, height=0.5, width=0.5, color=color.white)

bird = sphere(pos=vector(0,height+0.3,0), radius=0.3, color=color.yellow)

hit = 0

while hit == 0: #loops until player hits target

    #Take initial parameters from player
    v0 = float(input('Input your launch velocity:'))
    dtheta = float(input('Input your launch angle in degrees:'))
    theta = np.radians(dtheta) #convert launch angle to radians

    #Draw arrow for initial momentum
    px = 0.1*v0*np.cos(theta) #calculate initial x momentum
    py = 0.1*v0*np.sin(theta) #calculate initial y momentum
    momentum = arrow(pos=bird.pos, axis=vector(px,py,0), shaftwidth=0.1, color=color.orange) #draw arrow representing projectiles initial momentum

    # Calculate position of the projectile at a specified interval of time
    # Code adapted from my submission to PHAS0007: Computing Session 8

    #Define intial parameters
    g = 9.81
    x = x0 = 0
    y = y0 = height 
    dt = 0.0001 #timestep
    t = 0

    #conditions that alllow looping when projectile is not in contact with target
    while (y > 0 and (x < dist-0.25 or x > dist+0.25)) or (y > 2 and x >= dist-0.25 and x<= dist+0.25):

            rate(5000) #set rate of animation

            x = x0 + v0*t*np.cos(theta) # calculate new x position
            y = y0 + v0*t*np.sin(theta)-0.5*g*t**2 # calculate new y position
            bird.pos = vector(x,y,0) # redraw projectile at new position

            px = 0.1*v0*np.cos(theta) #calculate x momentum
            py = 0.1*v0*np.sin(theta)-0.1*g*t #calculate y momentum
            momentum.pos = bird.pos #redraw momentum arrow in new position
            momentum.axis = vector(px,py,0) #redraw momentum arrow direction

            t += dt # increase timestep increment by dt

    momentum.visible = False #removes momentum arrow after projectile motion is finished

    #use inverse condition of the loop to determine and print whether the target was hit or not
    if y >= 0 and y <= 2 and x >= dist-0.25 and x <= dist+0.25:

        #calculate whether the target topples or not
        trest = 0.5*100*g*0.5 #calculate the restoring torque
        contact_t = 0.01 #define the time projectile is in contact with target
        fapp = vector(px, py, 0) / contact_t #calculate applied force of projectile on target
        tapp = cross(fapp, vector((dist+0.25)-x, -y, 0)) #calculate applied torque vector of projectile on target
        tappm = mag(tapp) #calculate magnitude of applied torque vector

        if tappm > trest: #target toppled

            #redraw target to be toppled
            target.pos = vector(dist+1.25,0.25,0)
            target.length = 2
            target.height =0.5
            print('Target toppled, success!')
            hit = 1

        else: #target hit but not toppled
            print('Target hit but not toppled, try again.')

        #animate bird dropping to ground
        t=0 #reset time counter
        y0=y #set y0 equal to current y position
        while y > 0.3:
            rate(5000)
            y = y0 - 0.5*g*t**2            
            bird.pos = vector(x,y,0)
            t += dt

    else:
        print('Target Missed, try again.')
scene=canvas(宽度=640,高度=480,中心=vector(8,5,0),范围=8,背景=color.blue)
地面=长方体(位置=矢量(8,-5,0),长度=16,高度=10,宽度=0.5,颜色=颜色。绿色)
dist=5+random()*10
目标=框(位置=向量(距离,1,0),长度=0.5,高度=2,宽度=0.5,颜色=颜色。红色)
高度=随机()
平台=框(位置=向量(-2,高度-0.25,0),长度=4,高度=0.5,宽度=0.5,颜色=color.white)
鸟=球体(位置=矢量(0,高度+0.3,0),半径=0.3,颜色=颜色。黄色)
命中率=0
当命中率=0时:#循环直到玩家命中目标
#从播放器获取初始参数
v0=浮动(输入('输入您的发射速度:'))
dtheta=float(输入('以度为单位输入发射角度:'))
θ=np.弧度(dtheta)#将发射角度转换为弧度
#绘制初始动量的箭头
px=0.1*v0*np.cos(θ)#计算初始x动量
py=0.1*v0*np.sin(θ)#计算初始y动量
动量=箭头(pos=bird.pos,axis=vector(px,py,0),shaftwidth=0.1,color=color.orange)#绘制表示弹丸初始动量的箭头
#在指定的时间间隔计算投射物的位置
#代码改编自我提交给PHAS0007:Computing Session 8的文件
#定义初始参数
g=9.81
x=x0=0
y=y0=高度
dt=0.0001#时间步长
t=0
#抛射体未与目标接触时alllow循环的条件
而(y>0和(xdist+0.25))或(y>2和x>=dist-0.25和x=0和y=dist-0.25和x-trest:#目标倒下
#重划目标被推翻
target.pos=向量(距离+1.25,0.25,0)
target.length=2
目标高度=0.5
打印('目标被打倒,成功!')
命中率=1
否则:#目标命中但未被打倒
打印('目标命中但未打倒,请重试')
#制作鸟落地的动画
t=0#重置时间计数器
y0=y#将y0设置为等于当前y位置
当y>0.3时:
费率(5000)
y=y0-0.5*g*t**2
bird.pos=向量(x,y,0)
t+=dt
其他:
打印('未命中目标,请重试')

您可以放置一个回调函数,该函数暂停代码,当用户点击某个键或单击时,该函数将继续运行。(即,如果不想编写自定义函数,请选中waitfor函数)我不理解这个问题,因为在遇到输入语句之前我确实看到了设置。显然,在您的环境中,输入语句不会触发查看已构建的显示。您的环境的详细信息是什么?特别是,您的Vpyton版本是什么,可以通过打印访问(版本)?独立于版本,请注意,短sleep(),不是time.sleep(),而是VPython自己的函数,应确保在执行输入语句之前显示。