From 63ff984f49d76cd989f7f4762505d214f54037eb Mon Sep 17 00:00:00 2001 From: march3 Date: Fri, 7 Apr 2023 20:50:38 +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/earth.py | 2 +- bodies/jupiter.py | 2 +- bodies/mars.py | 2 +- bodies/mercury.py | 2 +- bodies/neptune.py | 2 +- bodies/pluto.py | 2 +- bodies/saturn.py | 2 +- bodies/uranus.py | 2 +- bodies/venus.py | 2 +- common/singleton.py | 19 ++++++ sim_scenes/func.py | 1 - sim_scenes/solar_system/speed_of_light.py | 4 +- simulators/ursina/entities/body_timer.py | 79 +++++++++++++++++++++++ simulators/ursina/entities/timer.py | 75 --------------------- simulators/ursina_simulator.py | 26 ++++---- 15 files changed, 124 insertions(+), 98 deletions(-) create mode 100644 common/singleton.py create mode 100644 simulators/ursina/entities/body_timer.py delete mode 100644 simulators/ursina/entities/timer.py diff --git a/bodies/earth.py b/bodies/earth.py index eaf0b43..a261555 100644 --- a/bodies/earth.py +++ b/bodies/earth.py @@ -23,7 +23,7 @@ class Earth(Body):  平均密度: 5507.85 kg/m³ """ def __init__(self, name="地球", mass=5.97237e24,rotate_angle=0, - init_position=[0, 0, 1.12 * AU], + init_position=[0, 0, 1 * AU], init_velocity=[-29.79, 0, 0], texture="earth1.jpg", size_scale=1.0, distance_scale=1.0, rotation_speed=15, ignore_mass=False, trail_color=None, show_name=False): diff --git a/bodies/jupiter.py b/bodies/jupiter.py index e2ff6e8..5e881f1 100644 --- a/bodies/jupiter.py +++ b/bodies/jupiter.py @@ -23,7 +23,7 @@ class Jupiter(Body):  平均密度: 1.326 g/cm³ -> -> 1.326✕10³ kg/m³ """ def __init__(self, name="木星", mass=1.8982e27, - init_position=[0, 0, 5.2 * AU], + init_position=[0, 0, 5.19 * AU], init_velocity=[-13.06, 0, 0], texture="jupiter1.jpg", size_scale=1.0, distance_scale=1.0, rotation_speed=36.2537, ignore_mass=False, trail_color=None, show_name=False): diff --git a/bodies/mars.py b/bodies/mars.py index abd1970..d96837d 100644 --- a/bodies/mars.py +++ b/bodies/mars.py @@ -23,7 +23,7 @@ class Mars(Body):  平均密度: 3.9335 g/cm³ -> 3.9335✕10³ kg/m³ """ def __init__(self, name="火星", mass=6.4171e23, - init_position=[0, 0, 1.5 * AU], + init_position=[0, 0, 1.52 * AU], init_velocity=[-24.13, 0, 0], texture="mars.jpg", size_scale=1.0, distance_scale=1.0, rotation_speed=14.6223, ignore_mass=False, trail_color=None, show_name=False): diff --git a/bodies/mercury.py b/bodies/mercury.py index a463473..e6cc8cf 100644 --- a/bodies/mercury.py +++ b/bodies/mercury.py @@ -24,7 +24,7 @@ class Mercury(Body): """ def __init__(self, name="水星", mass=3.3011e23, - init_position=[0, 0, 0.4 * AU], + init_position=[0, 0, 0.384 * AU], init_velocity=[-47.87, 0, 0], texture="mercury.jpg", size_scale=1.0, distance_scale=1.0, rotation_speed=0.2558, ignore_mass=False, trail_color=None, show_name=False): diff --git a/bodies/neptune.py b/bodies/neptune.py index 13879e7..23647d4 100644 --- a/bodies/neptune.py +++ b/bodies/neptune.py @@ -24,7 +24,7 @@ class Neptune(Body): """ def __init__(self, name="海王星", mass=1.0241e26, - init_position=[0, 0, 30 * AU], + init_position=[0, 0, 30.7 * AU], init_velocity=[-5.43, 0, 0], texture="neptune.png", size_scale=1.0, distance_scale=1.0, rotation_speed=22.3463, ignore_mass=False, trail_color=None, show_name=False): diff --git a/bodies/pluto.py b/bodies/pluto.py index 734171a..c9c5451 100644 --- a/bodies/pluto.py +++ b/bodies/pluto.py @@ -24,7 +24,7 @@ class Pluto(Body): """ def __init__(self, name="冥王星", mass=1.303e22, - init_position=[0, 0, 40 * AU], + init_position=[0, 0, 39.55 * AU], init_velocity=[-4.7, 0, 0], texture="pluto.png", size_scale=1.0, distance_scale=1.0, rotation_speed=-2.3474, ignore_mass=False, trail_color=None, show_name=False): diff --git a/bodies/saturn.py b/bodies/saturn.py index 457836e..4f9a8bb 100644 --- a/bodies/saturn.py +++ b/bodies/saturn.py @@ -24,7 +24,7 @@ class Saturn(Body): """ def __init__(self, name="土星", mass=5.6834e26, - init_position=[0, 0, 10 * AU], + init_position=[0, 0, 9.5 * AU], init_velocity=[-9.64, 0, 0], texture="saturn.jpg", size_scale=1.0, distance_scale=1.0, rotation_speed=33.7711, ignore_mass=False, trail_color=None, show_name=False): diff --git a/bodies/uranus.py b/bodies/uranus.py index f443d3e..e592b3d 100644 --- a/bodies/uranus.py +++ b/bodies/uranus.py @@ -24,7 +24,7 @@ class Uranus(Body): """ def __init__(self, name="天王星", mass=8.681e25, - init_position=[0, 0, 19 * AU], + init_position=[0, 0, 19.2 * AU], init_velocity=[-6.81, 0, 0], texture="uranus.png", size_scale=1.0, distance_scale=1.0, rotation_speed=-20.8816, ignore_mass=False, trail_color=None, show_name=False): diff --git a/bodies/venus.py b/bodies/venus.py index 234feb4..4ca8454 100644 --- a/bodies/venus.py +++ b/bodies/venus.py @@ -24,7 +24,7 @@ class Venus(Body): """ def __init__(self, name="金星", mass=4.8675e24, - init_position=[0, 0, 0.72 * AU], + init_position=[0, 0, 0.721 * AU], init_velocity=[-35, 0, 0], texture="venus.jpg", size_scale=1.0, distance_scale=1.0, rotation_speed=-0.0617, ignore_mass=False, trail_color=None, show_name=False): diff --git a/common/singleton.py b/common/singleton.py new file mode 100644 index 0000000..2773ac5 --- /dev/null +++ b/common/singleton.py @@ -0,0 +1,19 @@ +# -*- coding:utf-8 -*- +# title :单例模式类 +# description :单例模式类 +# author :Python超人 +# date :2023-02-11 +# link :https://gitcode.net/pythoncr/ +# python_version :3.8 +# ============================================================================== + + +class Singleton: + """ + 单例模式类 + """ + + def __new__(cls, *args, **kwargs): + if not hasattr(cls, "_instance"): + cls._instance = super(Singleton, cls).__new__(cls) + return cls._instance diff --git a/sim_scenes/func.py b/sim_scenes/func.py index 60dac39..7adb3b0 100644 --- a/sim_scenes/func.py +++ b/sim_scenes/func.py @@ -109,7 +109,6 @@ def ursina_run(bodies, simulator.check_and_evolve() if ursina_view.appeared: ursina_view.update() - # print('....') import sys sys.modules["__main__"].update = callback_update diff --git a/sim_scenes/solar_system/speed_of_light.py b/sim_scenes/solar_system/speed_of_light.py index b5e4257..c0b122b 100644 --- a/sim_scenes/solar_system/speed_of_light.py +++ b/sim_scenes/solar_system/speed_of_light.py @@ -20,8 +20,8 @@ arrived_info = "" CAMERA_FOLLOW_LIGHT = None # 不跟随光 -# CAMERA_FOLLOW_LIGHT = 'ForwardView' # 向前看 -# CAMERA_FOLLOW_LIGHT = 'SideView' # 侧面看 +CAMERA_FOLLOW_LIGHT = 'ForwardView' # 向前看 +CAMERA_FOLLOW_LIGHT = 'SideView' # 侧面看 def on_reset(): diff --git a/simulators/ursina/entities/body_timer.py b/simulators/ursina/entities/body_timer.py new file mode 100644 index 0000000..af57633 --- /dev/null +++ b/simulators/ursina/entities/body_timer.py @@ -0,0 +1,79 @@ +# -*- coding:utf-8 -*- +# title :计时器天体 +# description :计时器天体 +# author :Python超人 +# date :2023-02-11 +# link :https://gitcode.net/pythoncr/ +# python_version :3.8 +# ============================================================================== +from bodies.body import Body +from common.consts import MO +from common.singleton import Singleton +import os +import random +import math + +from simulators.ursina.ursina_event import UrsinaEvent + + +class BodyTimer(Singleton): + """ + 天体计时器 + """ + + def __init__(self): + if not hasattr(self, "inited"): + self.velocity_inc = 0.00001 + UrsinaEvent.on_reset_subscription(self.reset) + UrsinaEvent.on_pause_subscription(self.pause) + UrsinaEvent.on_start_subscription(self.start) + self.inited = True + + def pause(self): + pass + + def start(self): + pass + + def reset(self): + self.position_sum = 0.0 + + def calc_time(self, dt): + if not hasattr(self, "position_sum"): + self.position_sum = 0.0 + self.position_sum += self.velocity_inc * dt + # 距离(km) / 速度(km/s) = 时间(s) + seconds = round(self.position_sum / self.velocity_inc) + hours, remainder = divmod(seconds, 3600) + minutes, seconds = divmod(remainder, 60) + days, hours = divmod(hours, 24) + years = days // 365 + days = days % 365 + if days > 1: + s_days = str(days).rjust(3, " ") + if days >= 20 or years >= 1: + self.text = f'{int(years)}年{s_days}天' + else: + self.text = f'{int(days)}天 {int(hours):02d}:{int(minutes):02d}:{int(seconds):02d}' + else: + self.text = f'{int(hours):02d}:{int(minutes):02d}:{int(seconds):02d}' + + # print(self.text) + UrsinaEvent.on_timer_changed(self.text, (years, days, hours, minutes, seconds)) + + def ignore_gravity(self, body): + return True + + +if __name__ == '__main__': + import time + + t = BodyTimer() + print(id(t)) + print(id(BodyTimer())) + + for i in range(1000): + time.sleep(0.01) + # 距离(km) = 时间(s) * 速度(km/s) + t.position[0] += 300000 * t.velocity_inc + t.calc_time() diff --git a/simulators/ursina/entities/timer.py b/simulators/ursina/entities/timer.py deleted file mode 100644 index f4408db..0000000 --- a/simulators/ursina/entities/timer.py +++ /dev/null @@ -1,75 +0,0 @@ -from ursina import Text, Ursina, application -import datetime - -from simulators.ursina.ursina_config import UrsinaConfig -from simulators.ursina.ursina_event import UrsinaEvent - - -class Timer(Text): - - def __init__(self): - # 创建一个文本对象来显示计时器的时间 - super().__init__(text='00:00', position=(0.65, -0.45), font=UrsinaConfig.CN_FONT) - # 用来计时的变量 - # self.start_time = time.time() - self.reset() - UrsinaEvent.on_evolving_subscription(self.update) - UrsinaEvent.on_reset_subscription(self.reset) - UrsinaEvent.on_pause_subscription(self.pause) - UrsinaEvent.on_start_subscription(self.start) - self.elapsed_time_offset = datetime.timedelta(microseconds=1) - - def pause(self): - pass - - def start(self): - self.last_time = datetime.datetime.now() - - def reset(self): - self.last_time = datetime.datetime.now() - self.elapsed_time = datetime.timedelta(0) - - def update(self, evolve_dt=1): - # # 计算当前的时间 - # elapsed_time = time.time() - self.start_time - # - # # 将时间转换成“分钟:秒”的形式 - # minutes = int(elapsed_time // 60) - # seconds = int(elapsed_time % 60) - # self.text = f'{minutes:02d}:{seconds:02d}' - time_scale = UrsinaConfig.get_app_time_scale() - current_time = datetime.datetime.now() - # 0.6 是对测试太阳系时间的纠正 - self.elapsed_time += (current_time - self.last_time) * evolve_dt * time_scale * 0.6 - # datetime.timedelta(microseconds=1) 0:00:00.000001 - # datetime.timedelta(milliseconds=1) 0:00:00.001000 - # self.elapsed_time += self.elapsed_time_offset # 按区域取值 - self.last_time = current_time - hours, remainder = divmod(self.elapsed_time.seconds, 3600) - minutes, seconds = divmod(remainder, 60) - days = self.elapsed_time.days - years = days // 365 - days = days % 365 - if days > 1: - s_days = str(days).rjust(3, " ") - if days >= 20 or years >= 1: - self.text = f'{years}年{s_days}天' - else: - self.text = f'{days}天 {hours:02d}:{minutes:02d}:{seconds:02d}' - 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() - - t = Timer() - - - def update(): - t.update() - - - app.run() diff --git a/simulators/ursina_simulator.py b/simulators/ursina_simulator.py index ed1d579..5ba3eed 100644 --- a/simulators/ursina_simulator.py +++ b/simulators/ursina_simulator.py @@ -14,7 +14,7 @@ from simulators.ursina.ursina_event import UrsinaEvent from simulators.ursina.ui.control_ui import ControlUI from simulators.ursina.ui.control_handler import ControlHandler from simulators.ursina.ursina_mesh import create_arrow_line - +from simulators.ursina.entities.body_timer import BodyTimer from simulators.views.ursina_view import UrsinaView from simulators.ursina.ursina_config import UrsinaConfig from simulators.simulator import Simulator @@ -177,12 +177,16 @@ class UrsinaSimulator(Simulator): evolve_dt = evolve_dt * self.interval_fator super().evolve(evolve_dt) + if self.show_timer: + timer = BodyTimer() + timer.calc_time(evolve_dt) + - def create_timer(self): - from simulators.ursina.entities.timer import Timer - # 创建一个文本对象来显示计时器的时间 - self.timer = Timer() - return self.timer + # def create_timer(self): + # from simulators.ursina.entities.timer import Timer + # # 创建一个文本对象来显示计时器的时间 + # self.timer = Timer() + # return self.timer def cosmic_background(self, texture='../textures/cosmic2.jpg'): """ @@ -222,9 +226,9 @@ class UrsinaSimulator(Simulator): if "view_closely" in kwargs: view_closely = kwargs["view_closely"] - show_timer = False + self.show_timer = False if "show_timer" in kwargs: - show_timer = kwargs["show_timer"] + self.show_timer = kwargs["show_timer"] if view_closely: # 近距离查看 @@ -278,8 +282,8 @@ class UrsinaSimulator(Simulator): # ui = UrsinaUI() ctl = ControlUI(ControlHandler(), position=(0.6, 0.5)) - if show_timer: - self.create_timer() + # if show_timer: + # self.create_timer() EditorCamera(ignore_paused=True) # 防止打开中文输入法 @@ -308,7 +312,7 @@ class UrsinaSimulator(Simulator): audio = Audio(bg_music, pitch=1, loop=True, autoplay=True) audio.volume = 0.3 - if show_timer: + if self.show_timer: UrsinaEvent.on_reset() UrsinaEvent.on_ready() -- GitLab