我try 创建一个菜单来更改模拟中局部灯光的 colored颜色 .该模拟是一个中心球体,是一个行星,围绕透明度小于1的大气和局部光源.到目前为止,我的代码如下:

""" Import libraries """

import vpython as vp

import numpy as np
import sys

""" Create Canvas """

canvas = vp.canvas(width=1080, height=720)
scene = vp.scene

""" Create objects """

# Define the planet
planet_radius = 5
planet_texture = 'planet.jpg'  # You can replace this with your own custom picture
planet = vp.sphere(radius=planet_radius, texture=vp.textures.earth, shininess=0)

# Define the atmosphere
atm_thickness = 3.0  # You can modify this using a slide cursor
atm_density = 0.5  # You can modify this using a slide cursor
atm_radius = planet.radius+atm_thickness
atm = vp.sphere(radius=atm_radius, opacity=0.25)

# Define the light source
light_type = 'sun'  # You can modify this using a drop-down menu
light_wavelength = 550  # You can modify this using a slide cursor
light_size = 2  # You can modify this using a slide cursor
light_intensity = 1.0  # You can modify this using a slide cursor
pos_drift = 50
light_pos = vp.vector(atm_radius+pos_drift, atm_radius+pos_drift, atm_radius+pos_drift)
light = vp.local_light(pos=light_pos, color=vp.color.red, radius=light_size)

# Define the camera
scene.autoscale = False
scene.range = 10
scene.forward = vp.vector(0, 0, -1)
scene.up = vp.vector(0, 1, 0)
scene.caption = 'Click and drag on the light source to move it. Use the drop-down menu and slide cursors to adjust its properties.'

""" Define the event handlers """

def on_light_down(evt):
   global light_dragging, light_drag_pos
   light_dragging = True
   light_drag_pos = evt.pos

def on_light_move(evt):
   global light_dragging, light_drag_pos
   if light_dragging:
       light.pos += evt.pos - light_drag_pos
       light_drag_pos = evt.pos

def on_light_up(evt):
   global light_dragging
   light_dragging = False

# Bind the event handlers
vp.scene.bind('mousedown', on_light_down)
vp.scene.bind('mousemove', on_light_move)
vp.scene.bind('mouseup', on_light_up)

def on_mouse_down(event):
   global dragging, last_mouse_pos
   obj = scene.mouse.pick
   if obj == light:
       dragging = True
       last_mouse_pos = scene.mouse.pos

canvas.bind('mousedown', on_mouse_down)

# Create a slider that controls the x position of the sphere
def set_x_position(slider):
   light.pos.x = slider.value
   
slider = vp.slider(min=-25, max=25, length=250, bind=set_x_position)

# Create a menu to change the light colour
def M(m):
   global light
   val = m.selected
   if val == "sun": 
       light.color = vp.color.white
   elif val == "red": 
       light.color = vp.color.red
   elif val == "yellow": 
       light.color = vp.color.yellow
   elif val == "blue": 
       light.color = vp.color.blue

menu = vp.menu(choices=['sun', 'red', 'yellow', 'blue'], index=0, bind=M)

# Define the exit function
def exit_simulation():
   canvas.delete()
   # sys.exit()

# Create the exit button
exit_button = vp.button(bind=exit_simulation, text="Exit Simulation")

""" Run the simulation """

while True:
   vp.rate(30)
   
   # Update the atmosphere
   atm.radius = planet.radius+atm_thickness
   atm.opacity = 0.25*atm_density
   
   # Update the light source
   light.radius = light_size
   light.intensity = light_intensity
   if light_type == 'sun':
       light.color = vp.color.white
   elif light_type == 'red_dwarf':
       light.color = vp.color.red
   elif light_type == 'white_dwarf':
       light.color = vp.color.white
   
   # Update the camera
   vp.scene.center = planet.pos
   
   # Update the planet texture
   planet.texture = planet_texture

然而,光要么是doesn't change colorblip for a split second,要么是back to the initial color的.这可能与menuM功能中如何分配光源的新 colored颜色 有关.有什么办法可以解决这个问题吗?

推荐答案

我相信这是因为whilelight_type的价值.try 删除这三个条件,并在末尾添加更新功能light_type = val,以确保光源类型改变.这应该可以解决您的问题.

Python相关问答推荐

键盘.任务组

如何在vercel中指定Python运行时版本?

Twilio:CallInstance对象没有来自_的属性'

opencv Python稳定的图标识别

Python中是否有方法从公共域检索搜索结果

从webhook中的短代码(而不是电话号码)接收Twilio消息

需要计算60,000个坐标之间的距离

无法通过python-jira访问jira工作日志(log)中的 comments

切片包括面具的第一个实例在内的眼镜的最佳方法是什么?

如何在python xsModel库中定义一个可选[December]字段,以产生受约束的SON模式

如何在solve()之后获得症状上的等式的值

转换为浮点,pandas字符串列,混合千和十进制分隔符

有没有一种ONE—LINER的方法给一个框架的每一行一个由整数和字符串组成的唯一id?

* 动态地 * 修饰Python中的递归函数

Python—为什么我的代码返回一个TypeError

当条件满足时停止ODE集成?

Flask运行时无法在Python中打印到控制台

使用__json__的 pyramid 在客户端返回意外格式

使用tqdm的进度条

在极点中读取、扫描和接收有什么不同?