From d661e2df92b606db873bd114886b97c1b7a292cd Mon Sep 17 00:00:00 2001 From: march3 Date: Sat, 17 Jun 2023 14:55:36 +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 --- bodies/saturn.py | 7 +- .../solar_system/solarsystem_planets_size.py | 68 +++++++++++++++++++ 2 files changed, 74 insertions(+), 1 deletion(-) create mode 100644 sim_scenes/solar_system/solarsystem_planets_size.py diff --git a/bodies/saturn.py b/bodies/saturn.py index a5bed5f..e6a4c9c 100644 --- a/bodies/saturn.py +++ b/bodies/saturn.py @@ -43,15 +43,20 @@ class Saturn(Body): "trail_color": trail_color, "show_name": show_name } + self.__has_rings = True super().__init__(**params) + def show_rings(self, value): + self.__has_rings = value + return self + @property def has_rings(self): """ 土星带光环的天体 @return: """ - return True + return self.__has_rings @property def rings_color(self): diff --git a/sim_scenes/solar_system/solarsystem_planets_size.py b/sim_scenes/solar_system/solarsystem_planets_size.py new file mode 100644 index 0000000..2772b65 --- /dev/null +++ b/sim_scenes/solar_system/solarsystem_planets_size.py @@ -0,0 +1,68 @@ +# -*- coding:utf-8 -*- +# title :太阳系行星大小比较 +# description :太阳系行星大小比较 +# author :Python超人 +# date :2023-06-17 +# link :https://gitcode.net/pythoncr/ +# python_version :3.8 +# ============================================================================== +from bodies import Sun, Mercury, Venus, Earth, Mars, Jupiter, Saturn, Uranus, Neptune, Pluto, Moon +from common.consts import SECONDS_PER_HOUR, SECONDS_PER_DAY, AU +from sim_scenes.func import mayavi_run, ursina_run +from simulators.ursina.entities.body_timer import TimeData +from simulators.ursina.ursina_event import UrsinaEvent + + +# 地球和月球之间的距离常量,距地距离约: 363104 至 405696 km,平均距离 384000 km +E_M_DISTANCE = 405696 +earth = Earth("地球", init_position=[0, 0, 0]) +moon = Moon("月球", init_position=[E_M_DISTANCE, 0, 0]) + +bodies = [ + earth, moon, + Mercury(name="水星"), + Venus(name="金星"), + Mars(name="火星"), + Jupiter(name="木星"), + Saturn(name="土星").show_rings(False), + Uranus(name="天王星"), + Neptune(name="海王星"), + Pluto(name="冥王星") +] + +# 从第三个星球(水星)开始 +index = 2 +last_total_hours = 0 + +if __name__ == '__main__': + last_diameter = earth.diameter / 2 + plant_positions = [] + for i, body in enumerate(bodies): + body.ignore_mass = True + body.init_velocity = [0, 0, 0] + if i >= 2: # 从第三个星球(水星)开始 + plant_positions.append([(body.diameter / 2) + last_diameter, 0, 0]) + last_diameter += body.diameter + + def on_timer_changed(time_data: TimeData): + global index, last_total_hours + + if index >= len(bodies): + return + total_hours = int(time_data.total_hours) + if total_hours % 3 == 0 and last_total_hours != total_hours: + last_total_hours = total_hours + bodies[index].init_position = plant_positions[index - 2] + index += 1 + + + # 运行中,每时每刻都会触发 on_timer_changed + UrsinaEvent.on_timer_changed_subscription(on_timer_changed) + + # 使用 ursina 查看的运行效果 + # 常用快捷键: P:运行和暂停 O:重新开始 I:显示天体轨迹 + # position = 左-右+、上+下-、前+后- + ursina_run(bodies, SECONDS_PER_HOUR, + position=(E_M_DISTANCE / 2, 0, -E_M_DISTANCE * 1), + view_closely=True, + timer_enabled=True) -- GitLab