Glowscript和VPython中的菜单 [英] Menu in Glowscript and VPython

查看:313
本文介绍了Glowscript和VPython中的菜单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于一个学校项目,我和我的一个朋友目前正在尝试对我们的太阳系进行模拟编程.我们使用的平台是Glowscript,我们正在使用VPython.我们已经用它的所有行星,太阳以及它们之间的力对整个太阳系进行了编程.

For a school project a friend of mine and I are currently trying to program a simulation of our solar system. The platform we use is Glowscript and we're working with VPython. We have programmed our whole solar system already with all its planets, the sun and the forces between them.

现在,我们正在尝试添加一些附加功能,其中一个是菜单,用户可以在其中选择一个行星.在我们的代码中,所选对象(代码中定义的行星或太阳)将被标记为当前对象".之后,用户可以使用滑块更改当前对象的质量.

Now we're trying to add some extras and one of them is a menu in which the user can select a planet. In our code the selected object (a planet or the sun which are defined in the code) will be labeled as the 'currentobject'. After that it will be possible for the user to change the currentobject's mass using a slider.

在编程过程中发生了两个问题.第一个是我们无法将对象定义为当前对象,因此以后可以更改其质量.遗憾的是,我们无法在代码中找到问题.

Two problems occurred during the process of programming. The first one is that we were not able to define an object as the currentobject so that its mass can be changed later on. Sadly, we couldn't find the problem in our code.

第二个问题是,我们无法在Glowscript中为模拟添加多于一个的附加功能.我们已经编程了一个滑块功能来更改模拟的速率"(速度),此后,菜单和用于更改质量的滑块就没有显示出来.

The second problem is that we weren't able to add more than one extra to our simulation in Glowscript. We have already programmed a slider feature to change the simulations 'rate' (velocity) and after that the menu and the slider for changing the mass simply didn't show up.

def M(m):
    global col, currentobject
    currentobject.visible = True
    obj = m.selected
    if obj == 'Sun':
        currentobject = sun
    elif obj == "Mercury":
        currentobject = mercury
    elif obj == "Venus": 
        currentobject = venus
    elif obj == "Earth": 
        currentobject = earth
    elif obj == "Mars": 
        currentobject = mars
    elif obj == "Jupiter": 
        currentobject = jupiter
    elif obj == "Saturn": 
        currentobject = saturn
    elif obj == "Uranus": 
        currentobject = uranus
    elif obj == "Neptune": 
        currentobject = neptune
    currentobject=m.selected
    print(m.selected,m.index)


menu(choices=['Choose an object', 'Sun', 'Mercury', 'Venus', 'Earth', 'Mars', 'Jupiter', 'Saturn', 'Uranus', 'Neptune'], bind=M)
scene.append_to_caption('\n\n')


def setmass(b):
    wt.text = '{:1.2f}'.format(b.value)
sl = slider(min=0.1, max=10, value=0.1, length = 1000, bind=setmass)  
wt = wtext(text='{:1.2f}'.format(sl.value))


#mainloop 

while (True):  


    rate(10)

    currentobject.mass=currentobject.mass*sl.value

最后,我们的模拟用户应该能够在下拉菜单中选择行星或太阳.之后,用户应该能够(使用滑块)更改所选对象的质量,并在上面的模拟中看到效果(其他行星轨道发生变化,因为它们对当前对象的吸引力更大).

In the end, the user of our simulation should be able to select a planet or the sun in a drop down menu. After that the user should be able to change the selected object's mass (using a slider) and see the effects in the simulation above (the other planets orbits change because they are more attracted to the currentobject).

错误消息:

TypeError:无法读取未定义的属性质量"

TypeError: Cannot read property 'mass' of undefined

推荐答案

您的代码片段不足以完全了解问题所在,但是我注意到对M的第一次调用具有currentobject.visible = True,但是currentobject尚不存在,因此会出现错误.同样,在循环中,您引用currentobject.mass,但currentobject尚不存在.

You code fragment isn't sufficiently complete to know exactly what the problem is, but I note that the first call to M has currentobject.visible = True, but currentobject doesn't yet exist, so you get an error. Similarly, in your loop you reference currentobject.mass, but currentobject doesn't yet exist.

这篇关于Glowscript和VPython中的菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆