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

Python超人-宇宙模拟器

上级 84831096
# -*- coding:utf-8 -*-
# title :与木星跳舞
# description :与木星跳舞
# author :Python超人
# date :2023-02-11
# link :https://gitcode.net/pythoncr/
# python_version :3.8
# ==============================================================================
from bodies import Sun, Earth, Jupiter, Mars, Venus
from common.consts import SECONDS_PER_YEAR, SECONDS_PER_MONTH, AU
from sim_scenes.func import mayavi_run, ursina_run, camera_look_at
from simulators.ursina.entities.body_timer import TimeData
from simulators.ursina.ursina_config import UrsinaConfig
from simulators.ursina.ursina_event import UrsinaEvent
from simulators.ursina.ursina_mesh import create_line
from ursina import color
if __name__ == '__main__':
"""
与木星跳舞
"""
# 选择舞者
Dancer = Earth # 舞者为地球
Dancer = Venus # 舞者为金星
Dancer = Mars # 舞者为火星
bodies = [
Sun(size_scale=0.8e2), # 太阳放大 80 倍
Dancer(size_scale=2e3), # 舞者放大 2000 倍
Jupiter(size_scale=5e2), # 木星放大 500 倍
]
sun, dancer, jupiter = bodies[0], bodies[1], bodies[2]
def on_ready():
camera_look_at(sun)
UrsinaConfig.trail_length = 235
UrsinaConfig.trail_type = "line"
pass
def on_timer_changed(time_data: TimeData):
if int(time_data.total_days) % 10 == 0:
create_line(from_pos=jupiter.planet.position, to_pos=dancer.planet.position, color=color.white)
UrsinaEvent.on_ready_subscription(on_ready)
UrsinaEvent.on_timer_changed_subscription(on_timer_changed)
# 使用 ursina 查看的运行效果
# 常用快捷键: P:运行和暂停 O:重新开始 I:显示天体轨迹
# position = 左-右+、上+下-、前+后-
ursina_run(bodies, SECONDS_PER_YEAR,
position=(0, 20 * AU, 0),
show_timer=True,
show_trail=True)
...@@ -8,22 +8,43 @@ ...@@ -8,22 +8,43 @@
# ============================================================================== # ==============================================================================
from bodies import Sun, Earth, Jupiter from bodies import Sun, Earth, Jupiter
from common.consts import SECONDS_PER_YEAR, SECONDS_PER_MONTH, AU from common.consts import SECONDS_PER_YEAR, SECONDS_PER_MONTH, AU
from sim_scenes.func import mayavi_run, ursina_run from sim_scenes.func import mayavi_run, ursina_run, camera_look_at
from simulators.ursina.entities.body_timer import TimeData
from simulators.ursina.ursina_config import UrsinaConfig
from simulators.ursina.ursina_event import UrsinaEvent
from simulators.ursina.ursina_mesh import create_line
from ursina import color
if __name__ == '__main__': if __name__ == '__main__':
""" """
太阳、地球、木星 太阳、地球、木星
""" """
bodies = [ bodies = [
Sun(size_scale=5e1), # 太阳放大 50 倍 Sun(size_scale=5e1), # 太阳放大 50 倍
Earth(size_scale=2e3, distance_scale=1), # 地球放大 2000 倍,距离保持不变 Earth(size_scale=2e3, distance_scale=1), # 地球放大 2000 倍,距离保持不变
Jupiter(size_scale=5e2, distance_scale=1), # 木星放大 500 倍,距离保持不变 Jupiter(size_scale=5e2, distance_scale=1), # 木星放大 500 倍,距离保持不变
] ]
sun, earth, jupiter = bodies[0], bodies[1], bodies[2]
def on_ready():
camera_look_at(sun, rotation_x=None, rotation_y=None, rotation_z=None)
UrsinaConfig.trail_length = 235
UrsinaConfig.trail_type = "line"
pass
def on_timer_changed(time_data: TimeData):
if int(time_data.total_days) % 10 == 0:
# print(time_data.total_hours)
create_line(from_pos=jupiter.planet.position, to_pos=earth.planet.position, color=color.white)
# 使用 mayavi 查看的运行效果 UrsinaEvent.on_ready_subscription(on_ready)
# mayavi_run(bodies, SECONDS_PER_WEEK, view_azimuth=-45) UrsinaEvent.on_timer_changed_subscription(on_timer_changed)
# 使用 ursina 查看的运行效果 # 使用 ursina 查看的运行效果
# 常用快捷键: P:运行和暂停 O:重新开始 I:显示天体轨迹 # 常用快捷键: P:运行和暂停 O:重新开始 I:显示天体轨迹
# position = 左-右+、上+下-、前+后- # position = 左-右+、上+下-、前+后-
ursina_run(bodies, SECONDS_PER_MONTH, position=(0, AU, -4 * AU), show_trail=True) ursina_run(bodies, SECONDS_PER_YEAR, position=(0, 20 * AU, 0), show_timer=True, show_trail=True)
...@@ -50,6 +50,10 @@ class TimeData: ...@@ -50,6 +50,10 @@ class TimeData:
return self.total_seconds / 3600 return self.total_seconds / 3600
@property
def total_days(self):
return self.total_hours / 24
class BodyTimer(Singleton): class BodyTimer(Singleton):
""" """
......
...@@ -105,7 +105,9 @@ def create_trails(parent): ...@@ -105,7 +105,9 @@ def create_trails(parent):
if last_pos_distance < parent.trail_scale * trail_int_scale: # 间隔距离不小于1.2倍的拖尾球体 if last_pos_distance < parent.trail_scale * trail_int_scale: # 间隔距离不小于1.2倍的拖尾球体
return return
# trail = create_trail_line(parent, pos) # 拖尾为线条 if UrsinaConfig.trail_type == "line":
trail = create_trail_line(parent, pos) # 拖尾为线条
else:
trail = create_trail_sphere(parent, pos) # 拖尾为球体 trail = create_trail_sphere(parent, pos) # 拖尾为球体
if trail is not None: if trail is not None:
......
...@@ -25,6 +25,8 @@ class ControlUI(UiPanel): ...@@ -25,6 +25,8 @@ class ControlUI(UiPanel):
控制面板界面 控制面板界面
""" """
def component_init(self): def component_init(self):
UrsinaEvent.after_ready_subscription(self.after_ready)
self.start_button_text = "●" # 》●▲○◎ self.start_button_text = "●" # 》●▲○◎
self.pause_button_text = "〓" # 〓 || ‖ self.pause_button_text = "〓" # 〓 || ‖
self.no_trail_button_text = "○ " self.no_trail_button_text = "○ "
...@@ -75,6 +77,9 @@ class ControlUI(UiPanel): ...@@ -75,6 +77,9 @@ class ControlUI(UiPanel):
return content return content
def after_ready(self):
self.slider_trail_length.value = UrsinaConfig.trail_length
def after_component_init(self): def after_component_init(self):
self.sec_per_time_switch.x = -0.4 self.sec_per_time_switch.x = -0.4
self.on_off_switch.x = 0.2 self.on_off_switch.x = 0.2
......
...@@ -37,6 +37,8 @@ class UrsinaConfig: ...@@ -37,6 +37,8 @@ class UrsinaConfig:
show_trail = False show_trail = False
# 拖尾球体的数量 # 拖尾球体的数量
trail_length = 100 trail_length = 100
# 拖尾的类型(球体、线条)
trail_type = "sphere" # trail_type="line" or trail_type="sphere"
# 默认秒数(0表示默认) # 默认秒数(0表示默认)
seconds_per = 0 seconds_per = 0
# # 控制摄像机动作速度(天体越大,速度越快,天体越小,速度越慢) # # 控制摄像机动作速度(天体越大,速度越快,天体越小,速度越慢)
......
...@@ -26,7 +26,8 @@ class UrsinaEvent: ...@@ -26,7 +26,8 @@ class UrsinaEvent:
UrsinaEvent.on_start_funcs = [] UrsinaEvent.on_start_funcs = []
# 运行准备的订阅事件 # 运行准备的订阅事件
UrsinaEvent.on_ready_funcs = [] UrsinaEvent.on_ready_funcs = []
# 准备后的订阅事件
UrsinaEvent.after_ready_funcs = []
# 搜索天体的订阅事件 # 搜索天体的订阅事件
UrsinaEvent.on_searching_bodies_funcs = [] UrsinaEvent.on_searching_bodies_funcs = []
# 应用运行的订阅事件 # 应用运行的订阅事件
...@@ -115,6 +116,19 @@ class UrsinaEvent: ...@@ -115,6 +116,19 @@ class UrsinaEvent:
for f in UrsinaEvent.on_ready_funcs: for f in UrsinaEvent.on_ready_funcs:
f() f()
@staticmethod
def after_ready_subscription(fun):
UrsinaEvent.after_ready_funcs.append(fun)
@staticmethod
def after_ready_unsubscription(fun):
UrsinaEvent.after_ready_funcs.remove(fun)
@staticmethod
def after_ready():
for f in UrsinaEvent.after_ready_funcs:
f()
@staticmethod @staticmethod
def on_start_subscription(fun): def on_start_subscription(fun):
UrsinaEvent.on_start_funcs.append(fun) UrsinaEvent.on_start_funcs.append(fun)
......
...@@ -98,6 +98,17 @@ def create_label(parent, label, pos, color, scale=50, alpha=1.0): ...@@ -98,6 +98,17 @@ def create_label(parent, label, pos, color, scale=50, alpha=1.0):
return text return text
def create_line(from_pos, to_pos, parent=None, alpha=1.0, len_scale=1,set_light_off=True,
color=color.white, thickness=1):
line = Entity(parent=parent,
model=Mesh(vertices=(from_pos * len_scale, to_pos * len_scale),
mode='line', thickness=thickness),
color=color, alpha=alpha)
if set_light_off:
line.set_light_off()
return line
def create_arrow_line(from_pos, to_pos, parent=None, label=None, def create_arrow_line(from_pos, to_pos, parent=None, label=None,
set_light_off=True, alpha=1.0, len_scale=0.5, set_light_off=True, alpha=1.0, len_scale=0.5,
color=color.white, thickness=2, color=color.white, thickness=2,
...@@ -123,6 +134,7 @@ def create_arrow_line(from_pos, to_pos, parent=None, label=None, ...@@ -123,6 +134,7 @@ def create_arrow_line(from_pos, to_pos, parent=None, label=None,
line = Entity(parent=parent, line = Entity(parent=parent,
model=Mesh(vertices=(from_pos * len_scale, to_pos * len_scale), mode='line', thickness=thickness), model=Mesh(vertices=(from_pos * len_scale, to_pos * len_scale), mode='line', thickness=thickness),
color=color, alpha=alpha) color=color, alpha=alpha)
arrow = Entity(parent=line, model=arrow_mesh, position=to_pos * len_scale, arrow = Entity(parent=line, model=arrow_mesh, position=to_pos * len_scale,
scale=thickness * arrow_scale, color=color, alpha=alpha) scale=thickness * arrow_scale, color=color, alpha=alpha)
arrow.look_at(to_pos * 100) arrow.look_at(to_pos * 100)
......
...@@ -281,6 +281,8 @@ class UrsinaSimulator(Simulator): ...@@ -281,6 +281,8 @@ class UrsinaSimulator(Simulator):
UrsinaEvent.on_ready() UrsinaEvent.on_ready()
UrsinaEvent.after_ready()
self.app.run() self.app.run()
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册