From 0fe675f0a5532025e431905ec0db9eac7a638976 Mon Sep 17 00:00:00 2001 From: march3 Date: Sat, 8 Apr 2023 12:08:23 +0800 Subject: [PATCH] =?UTF-8?q?Python=E8=B6=85=E4=BA=BA-=E5=AE=87=E5=AE=99?= =?UTF-8?q?=E6=A8=A1=E6=8B=9F=E5=99=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sim_scenes/solar_system/speed_of_light.py | 7 ++-- .../solar_system/speed_of_light_init.py | 36 +++++++++++++++---- simulators/ursina/entities/timer.py | 7 +++- 3 files changed, 39 insertions(+), 11 deletions(-) diff --git a/sim_scenes/solar_system/speed_of_light.py b/sim_scenes/solar_system/speed_of_light.py index 5427959..a93e3c7 100644 --- a/sim_scenes/solar_system/speed_of_light.py +++ b/sim_scenes/solar_system/speed_of_light.py @@ -12,7 +12,7 @@ from sim_scenes.solar_system.speed_of_light_init import SpeedOfLightInit camera_follow_light = None # 摄像机固定,不会跟随光 camera_follow_light = 'ForwardView' # 摄像机跟随光,方向是向前看 -camera_follow_light = 'SideView' # 摄像机跟随光,方向是侧面看 +# camera_follow_light = 'SideView' # 摄像机跟随光,方向是侧面看 # 实例化一个初始化对象(订阅事件,记录到达每个行星所需要的时间) init = SpeedOfLightInit(camera_follow_light) @@ -34,7 +34,8 @@ init.event_subscription() # 常用快捷键: P:运行和暂停 O:重新开始 I:显示天体轨迹 # position = 左-右+、上+下-、前+后- ursina_run(bodies, 60, - position=init.position, - show_trail=init.show_trail, show_timer=True, + position=init.camera_position, + show_trail=init.show_trail, + show_timer=True, # view_closely=True, bg_music="sounds/interstellar.mp3") diff --git a/sim_scenes/solar_system/speed_of_light_init.py b/sim_scenes/solar_system/speed_of_light_init.py index 04aa1c7..ac8f71b 100644 --- a/sim_scenes/solar_system/speed_of_light_init.py +++ b/sim_scenes/solar_system/speed_of_light_init.py @@ -34,31 +34,44 @@ class SpeedOfLightInit: if self.__camera_follow_light == "SideView": # 摄像机位置 = 前-后+、上+下-、左-右+、 - self.position = (AU, 0, 0) + self.camera_position = (AU, 0, 0) self.show_trail = True self.light_size_scale = 1e3 self.light_init_position = [AU / 3, 0, 0] elif self.__camera_follow_light == "ForwardView": # 摄像机位置 = 左-右+、上+下-、前+后- - self.position = (0, AU / 10, -AU) - self.show_trail = False + self.camera_position = (0, AU / 10, -AU) + self.show_trail = True self.light_size_scale = 1e2 self.light_init_position = [AU / 12, 0, 0] else: # 摄像机位置 = 左-右+、上+下-、前+后- - self.position = (0, AU, -6 * AU) + self.camera_position = (0, AU, -6 * AU) self.show_trail = True self.light_size_scale = 2e3 self.light_init_position = [AU / 3, 0, 0] - # 点击了重置按钮 def on_reset(self): + """ + 点击了重置按钮触发 + @return: + """ self.arrived_bodies.clear() # 重置存放记录光体已到达天体列表 self.arrived_info = "[00:00:00]\t从 [太阳] 出发\n\n" if self.text_panel is not None: self.text_panel.text = self.arrived_info def event_subscription(self): + """ + 订阅事件 + @return: + """ + if self.light_body is None: + raise Exception("请指定 SpeedOfLightInit.light_body") + + if self.bodies is None: + raise Exception("请指定 SpeedOfLightInit.bodies") + # 订阅重新开始事件 UrsinaEvent.on_reset_subscription(self.on_reset) UrsinaEvent.on_ready_subscription(self.on_ready) @@ -66,6 +79,10 @@ class SpeedOfLightInit: UrsinaEvent.on_timer_changed_subscription(self.on_timer_changed) def on_ready(self): + """ + 模拟器开始运行前触发 + @return: + """ self.text_panel = create_text_panel() self.text_panel.text = self.arrived_info @@ -73,12 +90,17 @@ class SpeedOfLightInit: camera.parent = self.light_body.planet camera.rotation_y = -85 elif self.__camera_follow_light == "ForwardView": - self.light_body.planet.enabled = False + # self.light_body.planet.enabled = False camera.parent = self.light_body.planet camera.rotation_y = -15 def on_timer_changed(self, time_text, time_data): - global arrived_info + """ + 计时器触发 + @param time_text: 计时器时间文本 + @param time_data: 计时器时间数据 + @return: + """ years, days, hours, minutes, seconds = time_data for body in self.bodies: if body is self.light_body or isinstance(body, Sun) \ diff --git a/simulators/ursina/entities/timer.py b/simulators/ursina/entities/timer.py index e68be25..65b279e 100644 --- a/simulators/ursina/entities/timer.py +++ b/simulators/ursina/entities/timer.py @@ -17,12 +17,17 @@ class Timer(Text): def __init__(self): # 创建一个文本对象来显示计时器的时间 - super().__init__(text='00:00', position=(0.65, -0.45), font=UrsinaConfig.CN_FONT) + super().__init__(text=' ', position=(0.70, -0.465), + origin=(-0.5, 0.5), + font=UrsinaConfig.CN_FONT, background=True) UrsinaEvent.on_timer_changed_subscription(self.on_timer_changed) def on_timer_changed(self, time_text, time_data): self.text = time_text + def update(self): + self.text = "00:00:00" + if __name__ == '__main__': app = Ursina() -- GitLab