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

Python超人-宇宙模拟器

上级 24d43dc6
...@@ -18,12 +18,15 @@ from simulators.ursina.ursina_event import UrsinaEvent ...@@ -18,12 +18,15 @@ from simulators.ursina.ursina_event import UrsinaEvent
class BodyTimer(Singleton): class BodyTimer(Singleton):
""" """
天体计时器 天体计时器,原理就是:
以 BodyTimer 为一个天体在宇宙中运行,以速度 self.velocity_inc 递增。
通过公式: 速度 * 时间 = 距离,获取到累计距离, self.position_sum += self.velocity_inc * dt
最后通过公式: 时间 = 距离 / 速度, 从而得到天体运行了多长时间
""" """
def __init__(self): def __init__(self):
if not hasattr(self, "inited"): if not hasattr(self, "inited"):
self.velocity_inc = 0.00001 self.velocity_inc = 1e-30 # 理论上数值越小越好,可以使用 1e-300 ,但建议使用:1e-30 # 0.00001
UrsinaEvent.on_reset_subscription(self.reset) UrsinaEvent.on_reset_subscription(self.reset)
UrsinaEvent.on_pause_subscription(self.pause) UrsinaEvent.on_pause_subscription(self.pause)
UrsinaEvent.on_start_subscription(self.start) UrsinaEvent.on_start_subscription(self.start)
...@@ -36,14 +39,19 @@ class BodyTimer(Singleton): ...@@ -36,14 +39,19 @@ class BodyTimer(Singleton):
pass pass
def reset(self): def reset(self):
# 累计距离置零
self.position_sum = 0.0 self.position_sum = 0.0
def calc_time(self, dt): def calc_time(self, dt):
if not hasattr(self, "position_sum"): if not hasattr(self, "position_sum"):
# 累计距离置零
self.position_sum = 0.0 self.position_sum = 0.0
# 通过公式: 速度 * 时间 = 距离,获取到累计距离
self.position_sum += self.velocity_inc * dt self.position_sum += self.velocity_inc * dt
# 距离(km) / 速度(km/s) = 时间(s) # 距离(km) / 速度(km/s) = 时间(s)
seconds = round(self.position_sum / self.velocity_inc) seconds = round(self.position_sum / self.velocity_inc)
# 获取到 seconds 后,通过下面的计算得到时分秒、年、天
hours, remainder = divmod(seconds, 3600) hours, remainder = divmod(seconds, 3600)
minutes, seconds = divmod(remainder, 60) minutes, seconds = divmod(remainder, 60)
days, hours = divmod(hours, 24) days, hours = divmod(hours, 24)
...@@ -52,14 +60,14 @@ class BodyTimer(Singleton): ...@@ -52,14 +60,14 @@ class BodyTimer(Singleton):
if days > 1: if days > 1:
s_days = str(days).rjust(3, " ") s_days = str(days).rjust(3, " ")
if days >= 20 or years >= 1: if days >= 20 or years >= 1:
self.text = f'{int(years)}{s_days}天' time_text = f'{int(years)}{s_days}天'
else: else:
self.text = f'{int(days)}{int(hours):02d}:{int(minutes):02d}:{int(seconds):02d}' time_text = f'{int(days)}{int(hours):02d}:{int(minutes):02d}:{int(seconds):02d}'
else: else:
self.text = f'{int(hours):02d}:{int(minutes):02d}:{int(seconds):02d}' time_text = f'{int(hours):02d}:{int(minutes):02d}:{int(seconds):02d}'
# print(self.text) # print(self.text)
UrsinaEvent.on_timer_changed(self.text, (years, days, hours, minutes, seconds)) UrsinaEvent.on_timer_changed(time_text, (years, days, hours, minutes, seconds))
def ignore_gravity(self, body): def ignore_gravity(self, body):
return True return True
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册