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

Python超人-宇宙模拟器

上级 5aa17fbc
...@@ -23,7 +23,7 @@ class Earth(Body): ...@@ -23,7 +23,7 @@ class Earth(Body):
 平均密度: 5507.85 kg/m³  平均密度: 5507.85 kg/m³
""" """
def __init__(self, name="地球", mass=5.97237e24,rotate_angle=0, 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], init_velocity=[-29.79, 0, 0],
texture="earth1.jpg", size_scale=1.0, distance_scale=1.0, texture="earth1.jpg", size_scale=1.0, distance_scale=1.0,
rotation_speed=15, ignore_mass=False, trail_color=None, show_name=False): rotation_speed=15, ignore_mass=False, trail_color=None, show_name=False):
......
...@@ -23,7 +23,7 @@ class Jupiter(Body): ...@@ -23,7 +23,7 @@ class Jupiter(Body):
 平均密度: 1.326 g/cm³ -> -> 1.326✕10³ kg/m³  平均密度: 1.326 g/cm³ -> -> 1.326✕10³ kg/m³
""" """
def __init__(self, name="木星", mass=1.8982e27, 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], init_velocity=[-13.06, 0, 0],
texture="jupiter1.jpg", size_scale=1.0, distance_scale=1.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): rotation_speed=36.2537, ignore_mass=False, trail_color=None, show_name=False):
......
...@@ -23,7 +23,7 @@ class Mars(Body): ...@@ -23,7 +23,7 @@ class Mars(Body):
 平均密度: 3.9335 g/cm³ -> 3.9335✕10³ kg/m³  平均密度: 3.9335 g/cm³ -> 3.9335✕10³ kg/m³
""" """
def __init__(self, name="火星", mass=6.4171e23, 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], init_velocity=[-24.13, 0, 0],
texture="mars.jpg", size_scale=1.0, distance_scale=1.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): rotation_speed=14.6223, ignore_mass=False, trail_color=None, show_name=False):
......
...@@ -24,7 +24,7 @@ class Mercury(Body): ...@@ -24,7 +24,7 @@ class Mercury(Body):
""" """
def __init__(self, name="水星", mass=3.3011e23, 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], init_velocity=[-47.87, 0, 0],
texture="mercury.jpg", size_scale=1.0, distance_scale=1.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): rotation_speed=0.2558, ignore_mass=False, trail_color=None, show_name=False):
......
...@@ -24,7 +24,7 @@ class Neptune(Body): ...@@ -24,7 +24,7 @@ class Neptune(Body):
""" """
def __init__(self, name="海王星", mass=1.0241e26, 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], init_velocity=[-5.43, 0, 0],
texture="neptune.png", size_scale=1.0, distance_scale=1.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): rotation_speed=22.3463, ignore_mass=False, trail_color=None, show_name=False):
......
...@@ -24,7 +24,7 @@ class Pluto(Body): ...@@ -24,7 +24,7 @@ class Pluto(Body):
""" """
def __init__(self, name="冥王星", mass=1.303e22, 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], init_velocity=[-4.7, 0, 0],
texture="pluto.png", size_scale=1.0, distance_scale=1.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): rotation_speed=-2.3474, ignore_mass=False, trail_color=None, show_name=False):
......
...@@ -24,7 +24,7 @@ class Saturn(Body): ...@@ -24,7 +24,7 @@ class Saturn(Body):
""" """
def __init__(self, name="土星", mass=5.6834e26, 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], init_velocity=[-9.64, 0, 0],
texture="saturn.jpg", size_scale=1.0, distance_scale=1.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): rotation_speed=33.7711, ignore_mass=False, trail_color=None, show_name=False):
......
...@@ -24,7 +24,7 @@ class Uranus(Body): ...@@ -24,7 +24,7 @@ class Uranus(Body):
""" """
def __init__(self, name="天王星", mass=8.681e25, 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], init_velocity=[-6.81, 0, 0],
texture="uranus.png", size_scale=1.0, distance_scale=1.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): rotation_speed=-20.8816, ignore_mass=False, trail_color=None, show_name=False):
......
...@@ -24,7 +24,7 @@ class Venus(Body): ...@@ -24,7 +24,7 @@ class Venus(Body):
""" """
def __init__(self, name="金星", mass=4.8675e24, 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], init_velocity=[-35, 0, 0],
texture="venus.jpg", size_scale=1.0, distance_scale=1.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): rotation_speed=-0.0617, ignore_mass=False, trail_color=None, show_name=False):
......
# -*- 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
...@@ -109,7 +109,6 @@ def ursina_run(bodies, ...@@ -109,7 +109,6 @@ def ursina_run(bodies,
simulator.check_and_evolve() simulator.check_and_evolve()
if ursina_view.appeared: if ursina_view.appeared:
ursina_view.update() ursina_view.update()
# print('....')
import sys import sys
sys.modules["__main__"].update = callback_update sys.modules["__main__"].update = callback_update
......
...@@ -20,8 +20,8 @@ arrived_info = "" ...@@ -20,8 +20,8 @@ arrived_info = ""
CAMERA_FOLLOW_LIGHT = None # 不跟随光 CAMERA_FOLLOW_LIGHT = None # 不跟随光
# CAMERA_FOLLOW_LIGHT = 'ForwardView' # 向前看 CAMERA_FOLLOW_LIGHT = 'ForwardView' # 向前看
# CAMERA_FOLLOW_LIGHT = 'SideView' # 侧面看 CAMERA_FOLLOW_LIGHT = 'SideView' # 侧面看
def on_reset(): def on_reset():
......
# -*- 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()
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()
...@@ -14,7 +14,7 @@ from simulators.ursina.ursina_event import UrsinaEvent ...@@ -14,7 +14,7 @@ from simulators.ursina.ursina_event import UrsinaEvent
from simulators.ursina.ui.control_ui import ControlUI from simulators.ursina.ui.control_ui import ControlUI
from simulators.ursina.ui.control_handler import ControlHandler from simulators.ursina.ui.control_handler import ControlHandler
from simulators.ursina.ursina_mesh import create_arrow_line 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.views.ursina_view import UrsinaView
from simulators.ursina.ursina_config import UrsinaConfig from simulators.ursina.ursina_config import UrsinaConfig
from simulators.simulator import Simulator from simulators.simulator import Simulator
...@@ -177,12 +177,16 @@ class UrsinaSimulator(Simulator): ...@@ -177,12 +177,16 @@ class UrsinaSimulator(Simulator):
evolve_dt = evolve_dt * self.interval_fator evolve_dt = evolve_dt * self.interval_fator
super().evolve(evolve_dt) super().evolve(evolve_dt)
if self.show_timer:
timer = BodyTimer()
timer.calc_time(evolve_dt)
def create_timer(self): # def create_timer(self):
from simulators.ursina.entities.timer import Timer # from simulators.ursina.entities.timer import Timer
# 创建一个文本对象来显示计时器的时间 # # 创建一个文本对象来显示计时器的时间
self.timer = Timer() # self.timer = Timer()
return self.timer # return self.timer
def cosmic_background(self, texture='../textures/cosmic2.jpg'): def cosmic_background(self, texture='../textures/cosmic2.jpg'):
""" """
...@@ -222,9 +226,9 @@ class UrsinaSimulator(Simulator): ...@@ -222,9 +226,9 @@ class UrsinaSimulator(Simulator):
if "view_closely" in kwargs: if "view_closely" in kwargs:
view_closely = kwargs["view_closely"] view_closely = kwargs["view_closely"]
show_timer = False self.show_timer = False
if "show_timer" in kwargs: if "show_timer" in kwargs:
show_timer = kwargs["show_timer"] self.show_timer = kwargs["show_timer"]
if view_closely: if view_closely:
# 近距离查看 # 近距离查看
...@@ -278,8 +282,8 @@ class UrsinaSimulator(Simulator): ...@@ -278,8 +282,8 @@ class UrsinaSimulator(Simulator):
# ui = UrsinaUI() # ui = UrsinaUI()
ctl = ControlUI(ControlHandler(), position=(0.6, 0.5)) ctl = ControlUI(ControlHandler(), position=(0.6, 0.5))
if show_timer: # if show_timer:
self.create_timer() # self.create_timer()
EditorCamera(ignore_paused=True) EditorCamera(ignore_paused=True)
# 防止打开中文输入法 # 防止打开中文输入法
...@@ -308,7 +312,7 @@ class UrsinaSimulator(Simulator): ...@@ -308,7 +312,7 @@ class UrsinaSimulator(Simulator):
audio = Audio(bg_music, pitch=1, loop=True, autoplay=True) audio = Audio(bg_music, pitch=1, loop=True, autoplay=True)
audio.volume = 0.3 audio.volume = 0.3
if show_timer: if self.show_timer:
UrsinaEvent.on_reset() UrsinaEvent.on_reset()
UrsinaEvent.on_ready() UrsinaEvent.on_ready()
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册