我生成一个1到100范围内的数字。该数字应位于pos = vec(0,0,0)
的第一个坐标中(因此在x轴上)。其他轴应保持为0。如何将生成的数字转移到该点?
from vpython import*
from random import randint
#Sonne
sun = sphere(pos = vec(0,0,0), radius = 9, make_trail = True )
sun.mass = 2e30
sun.velocity = vec(0,0,0)
#Merkur
mercury_pos_x = randint(1, 100)
mercury = sphere(pos = vec(mercury_pos_x,0,0), radius = 5, color=color.red, make_trail = True )
mercury.mass = 3.25e23
mercury.velocity = vec(0,0,-47000)
#Venus
venus_pos_x = randint(1, 100)
venus = sphere(pos = vec(venus_pos_x,0,0), radius = 6, color=color.cyan, make_trail = True )
venus.mass = 4.9e24
venus.velocity = vec(0,0,-35000)
答案 0 :(得分:1)
我怀疑您希望每个行星都有一个不同的随机起点,使太阳保持在(0,0,0)。对于两个随机数,您需要调用两次random
,每次都保留该值以备后用。
merc_pos = randint(1, 100)
mercury = sphere(pos = vec(merc_pos,0,0), radius = s_rad1/2,
color=color.red, make_trail = True )
venus_pos = randint(1, 100)
venus = sphere(pos = vec(venus_pos,0,0), radius = s_rad1/1.8,
color=color.cyan, make_trail = True )
如果您不想在其他地方使用随机值,则可以将其插入到位:
mercury = sphere(pos = vec(randint(1, 100),0,0), radius = s_rad1/2,
color=color.red, make_trail = True )