如何在vPython中调用不同函数内的函数

时间:2018-05-18 00:28:28

标签: function while-loop call vpython

更新:忽略最热门的问题。我想通了(感谢gilch)。我现在唯一需要知道的是下面的第二个问题......如何在另一个函数中调用一个单独的函数。

当while语句结束并且我重置时,我再也无法移动球,因为while循环不会重新启动,所以我无法重新将球再次降低。我必须完全重启代码。我是vpython的初学者(为物理课做这个),我找不到重新启动while循环的方法(或者不管怎样修复它)。

我很确定这是因为while循环结束,然后当球的位置和速度重置时,while循环不会重新启动,所以我无法移动球,但我不知道该怎么做。感谢任何可以提供帮助的人。

代码:

GlowScript 2.7 VPython
#Start without running
running = False
#when "Run" is initiated
def Run(b):
    #Bring in running variable from outside
    global running
    #When Run/Pause Button is pressed, switch between running and pausing the program
    running = not running
    #When run button is pressed, change text to "Pause"
    if running: bbutton.text = "Pause"
    #When pause button is pressed, change text to "Run"
    else: bbutton.text = "Run"

#Create Run Button above program
bbutton = button(text="Run", pos=scene.title_anchor, bind=Run)

#When "Reset" is initiated
def Reset(c):
    #Bring in t and ball variables from outside
    global t, ball
    #Set back to time = 0 (when reset)
    t = 0
    #Reset ball's position to initial position (when reset)
    ball.pos = vec(0,2,0)
    #resets ball's velocity to initial velocity (when reset)
    ball.vel = vec(0,0,0)

#Create reset button above program
cbutton = button(text="Reset", pos=scene.title_anchor, bind=Reset)

#Create ground
ground = box(pos=vec(0,0,0), size=vec(10,0.2,10))
#Create ball
ball = sphere(pos=vec(0,2,0), radius=0.4, color=color.yellow)
#Initial Velocity
ball.vel = vec(0,0,0)
#Acceleration (free-falling)
g = -9.8
#Initial Time
t = 0
#Time increase per frame
deltat = 0.01

#Turns "drag" condition to false to begin
drag = False
#Initial mouse position
R = vec(0,0,0)
#When you click
scene.bind("mousedown", def():
    #Bring in drag variable from outside
    global drag
    #Set drag to true on mouseclick
    drag = True

    #When you release click
    scene.bind("mouseup", def():
        #Bring in drag variable from outside
        global drag
        #Set drag to false when you release click
        drag = False
    )
)

#While True (the program is running) and the ball is higher than slightly under the ground
#If the second condition is not there, the ball will continually go down through the ground forever
#The second condition stops the ball once it begins going underneath the platform, so it looks normal

while True and ball.pos.y > ground.pos.y + 0.48:
    #Slow down program
    rate(100)
    #When the drag condition is true (When mouse is clicked on ball)...
    if drag:
        #Set R to position of mouse
        R = scene.mouse.pos
        #Set ball position to R (position of mouse) -- allows the ball to be     dragged (changes ball position)
        ball.pos = R
    #When the program is running (run button is pressed)...
    if running:
        #Update ball velocity
        #   v      =     v0     + a (change in time)
        ball.vel.y = ball.vel.y + g*deltat
        #Update position velocity
        #   x    =    x0    +    v    *(change in time)
        ball.pos = ball.pos + ball.vel*deltat
        #When the ball hits the surface of the ground...
        if (ball.pos.y <= ground.pos.y + 0.5):
            #Change the sign of the velocity (to go back up)
            ball.vel.y = -ball.vel.y
        if (ball.pos.y < ground.pos.y +0.48):
            break
        #Update time
        t = t + deltat

更新:我将while循环置于函数Initiate中,但不知道如何将Initiate函数添加到Reset函数中...

新密码(仅包含相关代码):     #OTHER CODE

def Reset(c):
    #Bring in t and ball variables from outside
    global t, ball
    #Set back to time = 0 (when reset)
    t = 0
    #Reset ball's position to initial position (when reset)
    ball.pos = vec(0,2,0)
    #resets ball's velocity to initial velocity (when reset)
    ball.vel = vec(0,0,0)

    #Initiate() -- HOW DO I RUN INITIATE INSIDE THE RESET FUNCTION**

#OTHER CODE

Initiate()    

def Initiate():
    global ground, ball, g, t, deltat, drag, R

    while True and ball.pos.y > ground.pos.y + 0.48:
        #Slow down program
        rate(100)
        #When the drag condition is true (When mouse is clicked on ball)...
        if drag:
            #Set R to position of mouse
            R = scene.mouse.pos
            #Set ball position to R (position of mouse) -- allows the ball to be dragged (changes ball position)
            ball.pos = R
        #When the program is running (run button is pressed)...
        if running:
            #Update ball velocity
            #   v      =     v0     + a (change in time)
            ball.vel.y = ball.vel.y + g*deltat
            #Update position velocity
            #   x    =    x0    +    v    *(change in time)
            ball.pos = ball.pos + ball.vel*deltat
            #When the ball hits the surface of the ground...
            if (ball.pos.y <= ground.pos.y + 0.5):
                #Change the sign of the velocity (to go back up)
                ball.vel.y = -ball.vel.y
            if (ball.pos.y < ground.pos.y +0.48):
                break
            #Update time
            t = t + deltat

2 个答案:

答案 0 :(得分:0)

尝试将while循环放在函数def中,并通过调用函数启动它。然后你可以再次调用该函数重新启动循环。

答案 1 :(得分:0)

转到vpython.org并点击vpython论坛或glowscript论坛的链接,然后在那里提问。