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

Python超人-宇宙模拟器

上级 f4dcae33
......@@ -9,6 +9,8 @@
from bodies import Sun, Mercury, Venus, Earth, Mars, Jupiter, Saturn, Uranus, Neptune, Pluto, Moon, Asteroids, Body
from common.consts import SECONDS_PER_WEEK, SECONDS_PER_DAY, SECONDS_PER_YEAR, AU
from sim_scenes.func import mayavi_run, ursina_run
from simulators.ursina.ursina_config import UrsinaConfig
from simulators.ursina.ursina_event import UrsinaEvent
if __name__ == '__main__':
# 八大行星:木星(♃)、土星(♄)、天王星(♅)、海王星(♆)、地球(⊕)、金星(♀)、火星(♂)、水星(☿)
......@@ -46,12 +48,34 @@ if __name__ == '__main__':
init_velocity=[0, 0, 299792.458]).set_light_disable(True) # 1 光速=299792.458 千米/秒(km/秒)
bodies.append(light_body)
arrived_bodies = []
def on_reset():
arrived_bodies.clear()
def on_timer_changed(time_text, time_data):
years, days, hours, minutes, seconds = time_data
# UrsinaEvent.on_timer_changed(self.text, (years, days, hours, minutes, seconds))
# 光到达地球8.3分钟,
# 光到达冥王星平均用时要20000秒,333.3分钟 也就是约5.56小时
for body in bodies:
if body is light_body or isinstance(body, Sun) \
or body in arrived_bodies or isinstance(body, Asteroids):
continue
if light_body.position[2] >= body.position[2]:
arrived_bodies.append(body)
print(f"[{time_text}] 已经到达 [{body.name}]")
# print(light_body.planet)
UrsinaEvent.on_timer_changed_subscription(on_timer_changed)
UrsinaEvent.on_reset_subscription(on_reset)
# 使用 ursina 查看的运行效果
# 常用快捷键: P:运行和暂停 O:重新开始 I:显示天体轨迹
# position = 左-右+、上+下-、前+后-
ursina_run(bodies, 60, position=(0, 2 * AU, -11 * AU),
show_trail=True, show_timer=True,
bg_music="sounds/interstellar.mp3")
# 光到达地球8.3分钟,
# 光到达冥王星平均用时要20000秒,333.3分钟 也就是约5.56小时
......@@ -13,7 +13,7 @@ class Timer(Text):
# 用来计时的变量
# self.start_time = time.time()
self.reset()
UrsinaEvent.on_timer_changed_subscription(self.update)
UrsinaEvent.on_evolving_subscription(self.update)
UrsinaEvent.on_reset_subscription(self.reset)
UrsinaEvent.on_pause_subscription(self.pause)
UrsinaEvent.on_start_subscription(self.start)
......@@ -59,6 +59,8 @@ class Timer(Text):
else:
self.text = f'{hours:02d}:{minutes:02d}:{seconds:02d}'
UrsinaEvent.on_timer_changed(self.text, (years, days, hours, minutes, seconds))
if __name__ == '__main__':
app = Ursina()
......
......@@ -227,7 +227,7 @@ class ControlHandler(EventHandler):
kv = key.split(" ")
self.shift_keys[kv[0]] = False
print(self.shift_keys)
# print(self.shift_keys)
return self.shift_keys['left'] or self.shift_keys['right']
def settings_handler_input(self, key):
......@@ -241,7 +241,7 @@ class ControlHandler(EventHandler):
self.shift_key_handle(key)
self.camera_update()
print(key)
# print(key)
if key == "escape":
if self.shift_keys['left'] or self.shift_keys['right']:
sys.exit()
......
......@@ -31,6 +31,8 @@ class UrsinaEvent:
UrsinaEvent.on_application_run_callback = []
# 天体大小发生变化的订阅事件
UrsinaEvent.on_body_size_changed_callback = []
# 逐步演变触发的订阅事件
UrsinaEvent.on_evolving_callback = []
# 计时器触发的订阅事件
UrsinaEvent.on_timer_changed_callback = []
......@@ -48,8 +50,17 @@ class UrsinaEvent:
UrsinaEvent.on_timer_changed_callback.append(fun)
@staticmethod
def on_timer_changed(evolve_dt):
def on_timer_changed(time_text, time_data):
for f in UrsinaEvent.on_timer_changed_callback:
f(time_text, time_data)
@staticmethod
def on_evolving_subscription(fun):
UrsinaEvent.on_evolving_callback.append(fun)
@staticmethod
def on_evolving(evolve_dt):
for f in UrsinaEvent.on_evolving_callback:
f(evolve_dt)
@staticmethod
......
......@@ -172,7 +172,7 @@ class UrsinaSimulator(Simulator):
# 配置中,每年、月、天等等有多少秒
evolve_dt = UrsinaConfig.seconds_per * run_speed_factor
UrsinaEvent.on_timer_changed(evolve_dt)
UrsinaEvent.on_evolving(evolve_dt)
# interval_fator 能让更新天体运行状态(位置、速度)更精确
evolve_dt = evolve_dt * self.interval_fator
super().evolve(evolve_dt)
......
......@@ -24,7 +24,7 @@ class UrsinaView(BodyView):
self.velocity = body.velocity
self.planet = Planet(self)
body.planet = self.planet
def update(self):
"""
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册