fixed_star.py 3.5 KB
Newer Older
三月三net's avatar
三月三net 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
# -*- 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.image_utils import gen_fixed_star_texture, find_texture_root_path
import os


class FixedStar(Body):
    """
    恒星基类
    ------------------------
    == 太阳参数 ==
    自转周期: 24.47 地球日,自转角速度约为 0.6130 度/小时 = 360/(24.47*24)
    天体质量: 1.9891×10³⁰ kg
    平均密度: 1.408×10³ kg/m³
    """

    def __init__(self, name="恒星", mass=1 * MO,
                 init_position=[0, 0, 0],
                 init_velocity=[0, 0, 0],
                 color=(0xFF, 0xFF, 0xFF),
                 texture=None, size_scale=1.0, distance_scale=1.0,
三月三net's avatar
三月三net 已提交
30 31
                 rotation_speed=0.1, ignore_mass=False, density=1.408e3, trail_color=None,
                 texture_bright=None, texture_contrast=None):
三月三net's avatar
三月三net 已提交
32 33
        if texture is None or texture == "fixed_star.png":
            self.color = color
三月三net's avatar
三月三net 已提交
34 35
            # bright=1.1, contrast=3.2
            texture = self.gen_texture(texture, texture_bright, texture_contrast)
三月三net's avatar
三月三net 已提交
36 37 38 39 40 41 42 43 44 45 46
        params = {
            "name": name,
            "mass": mass,
            "init_position": init_position,
            "init_velocity": init_velocity,
            "density": density,
            "color": color,
            "texture": texture,
            "size_scale": size_scale,
            "distance_scale": distance_scale,
            "rotation_speed": rotation_speed,
三月三net's avatar
三月三net 已提交
47 48
            "ignore_mass": ignore_mass,
            "trail_color": trail_color
三月三net's avatar
三月三net 已提交
49 50
        }
        super().__init__(**params)
三月三net's avatar
三月三net 已提交
51
        self.light_on = True
三月三net's avatar
三月三net 已提交
52
        self.glows = (12, 1.005, 0.08)
三月三net's avatar
三月三net 已提交
53

三月三net's avatar
三月三net 已提交
54
    def gen_texture(self, texture, texture_bright, texture_contrast):
三月三net's avatar
三月三net 已提交
55 56
        if texture is None:
            return None
三月三net's avatar
三月三net 已提交
57 58 59 60
        texture_path = find_texture_root_path()
        if texture_path is None:
            err_msg = "未找到纹理图片目录"
            raise Exception(err_msg)
三月三net's avatar
三月三net 已提交
61
        save_file = os.path.join(texture_path, "fixed_star_%s.png" % str(self.__class__.__name__).lower())
三月三net's avatar
三月三net 已提交
62 63
        if os.path.exists(save_file):
            return save_file
三月三net's avatar
三月三net 已提交
64
        fixed_star_img = os.path.join(texture_path, texture)
三月三net's avatar
三月三net 已提交
65 66 67 68 69
        gen_fixed_star_texture(self.color,
                               bright=texture_bright,
                               contrast=texture_contrast,
                               save_file=save_file,
                               fixed_star_img=fixed_star_img)
三月三net's avatar
三月三net 已提交
70 71 72 73 74 75 76 77 78 79
        return save_file

    @property
    def is_fixed_star(self):
        """
        恒星
        :return:
        """
        return True

三月三net's avatar
三月三net 已提交
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
    def compare_with_sun(self):
        from bodies import Sun
        sun = Sun()
        print("质量倍数", self.mass / sun.mass)
        print("半径倍数", self.raduis / sun.raduis)

    def density_by_radius(self, raduis=None, num_sun_raduis=None):
        """
        密度換算
        @param raduis: 半径的长度(km)
        @param num_sun_raduis: 多少个太阳半径
        @return:
        """
        from bodies import Sun
        import math

        sun = Sun()
        if num_sun_raduis is not None:
            raduis = num_sun_raduis * sun.raduis
        print("密度換算", self.mass / 1e9 / (4 / 3 * math.pi * pow(raduis, 3)))

三月三net's avatar
三月三net 已提交
101 102 103

if __name__ == '__main__':
    print(FixedStar())