提交 8e61db6a 编写于 作者: 三月三net's avatar 三月三net

Python超人-宇宙模拟器

上级 e7f55fee
...@@ -115,6 +115,7 @@ def ursina_run(bodies, ...@@ -115,6 +115,7 @@ def ursina_run(bodies,
show_trail=False, show_trail=False,
show_name=False, show_name=False,
show_timer=False, show_timer=False,
timer_enabled=False,
save_as_json=None, save_as_json=None,
view_closely=False): view_closely=False):
""" """
...@@ -128,6 +129,7 @@ def ursina_run(bodies, ...@@ -128,6 +129,7 @@ def ursina_run(bodies,
@param show_trail: 是否显示拖尾 @param show_trail: 是否显示拖尾
@param show_name: 是否显示天体名称 @param show_name: 是否显示天体名称
@param show_timer: 是否显示计时器 @param show_timer: 是否显示计时器
@param timer_enabled: 计时器是否有效
@param save_as_json: 将所有天体的信息保存为 json 文件 @param save_as_json: 将所有天体的信息保存为 json 文件
@param view_closely: 是否近距离查看天体 @param view_closely: 是否近距离查看天体
@return: @return:
...@@ -170,6 +172,7 @@ def ursina_run(bodies, ...@@ -170,6 +172,7 @@ def ursina_run(bodies,
cosmic_bg=cosmic_bg, cosmic_bg=cosmic_bg,
show_grid=show_grid, show_grid=show_grid,
show_timer=show_timer, show_timer=show_timer,
timer_enabled=timer_enabled,
bg_music=bg_music, bg_music=bg_music,
view_closely=view_closely) view_closely=view_closely)
......
...@@ -56,5 +56,5 @@ if __name__ == '__main__': ...@@ -56,5 +56,5 @@ if __name__ == '__main__':
# 常用快捷键: P:运行和暂停 O:重新开始 I:显示天体轨迹 # 常用快捷键: P:运行和暂停 O:重新开始 I:显示天体轨迹
# position = 左-右+、上+下-、前+后- # position = 左-右+、上+下-、前+后-
ursina_run(bodies, SECONDS_PER_YEAR, position=(0, 10 * AU, -10 * AU), ursina_run(bodies, SECONDS_PER_YEAR, position=(0, 10 * AU, -10 * AU),
show_timer=True, timer_enabled=True,
bg_music="sounds/interstellar.mp3") bg_music="sounds/interstellar.mp3")
...@@ -16,12 +16,14 @@ from simulators.ursina.ursina_event import UrsinaEvent ...@@ -16,12 +16,14 @@ from simulators.ursina.ursina_event import UrsinaEvent
class Timer(Text): class Timer(Text):
def __init__(self): def __init__(self, show=True):
# 创建一个文本对象来显示计时器的时间 # 创建一个文本对象来显示计时器的时间
super().__init__(text=' ', position=(0.70, -0.465), super().__init__(text=' ', position=(0.70, -0.465),
origin=(-0.5, 0.5), origin=(-0.5, 0.5),
font=UrsinaConfig.CN_FONT, background=True) font=UrsinaConfig.CN_FONT, background=True)
UrsinaEvent.on_timer_changed_subscription(self.on_timer_changed) UrsinaEvent.on_timer_changed_subscription(self.on_timer_changed)
if not show:
self.enabled = False
def on_timer_changed(self, time_data: TimeData): def on_timer_changed(self, time_data: TimeData):
self.text = time_data.time_text self.text = time_data.time_text
......
...@@ -209,14 +209,14 @@ class UrsinaSimulator(Simulator): ...@@ -209,14 +209,14 @@ class UrsinaSimulator(Simulator):
evolve_dt = evolve_dt * self.interval_fator evolve_dt = evolve_dt * self.interval_fator
super().evolve(evolve_dt) super().evolve(evolve_dt)
if self.show_timer: if self.show_timer or self.timer_enabled:
timer = BodyTimer() timer = BodyTimer()
timer.calc_time(evolve_dt) timer.calc_time(evolve_dt)
def create_timer(self): def create_timer(self, show=True):
from simulators.ursina.entities.timer import Timer from simulators.ursina.entities.timer import Timer
# 创建一个文本对象来显示计时器的时间 # 创建一个文本对象来显示计时器的时间
self.timer = Timer() self.timer = Timer(show)
return self.timer return self.timer
def cosmic_background(self, texture='../textures/cosmic2.jpg'): def cosmic_background(self, texture='../textures/cosmic2.jpg'):
...@@ -262,6 +262,10 @@ class UrsinaSimulator(Simulator): ...@@ -262,6 +262,10 @@ class UrsinaSimulator(Simulator):
if "show_timer" in kwargs: if "show_timer" in kwargs:
self.show_timer = kwargs["show_timer"] self.show_timer = kwargs["show_timer"]
self.timer_enabled = False
if "timer_enabled" in kwargs:
self.timer_enabled = kwargs["timer_enabled"]
if view_closely: if view_closely:
# 近距离查看 # 近距离查看
if isinstance(view_closely, float): if isinstance(view_closely, float):
...@@ -314,9 +318,13 @@ class UrsinaSimulator(Simulator): ...@@ -314,9 +318,13 @@ class UrsinaSimulator(Simulator):
if cosmic_bg is not None and os.path.exists(cosmic_bg): if cosmic_bg is not None and os.path.exists(cosmic_bg):
self.cosmic_background(cosmic_bg) self.cosmic_background(cosmic_bg)
if self.show_timer: if self.show_timer:
self.create_timer() self.timer_enabled = True
# 创建和显示计时器
self.create_timer(True)
elif self.timer_enabled:
# 创建计时器,但是不显示
self.create_timer(False)
# 防止打开中文输入法 # 防止打开中文输入法
# self.switch_to_english_input_method() # self.switch_to_english_input_method()
...@@ -346,7 +354,7 @@ class UrsinaSimulator(Simulator): ...@@ -346,7 +354,7 @@ class UrsinaSimulator(Simulator):
EditorCamera(ignore_paused=True) EditorCamera(ignore_paused=True)
if self.show_timer: if self.show_timer or self.timer_enabled:
UrsinaEvent.on_reset() UrsinaEvent.on_reset()
UrsinaEvent.on_ready() UrsinaEvent.on_ready()
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册