提交 313215da 编写于 作者: 三月三net's avatar 三月三net

Python超人-宇宙模拟器

上级 cf78298c
# -*- coding:utf-8 -*-
# title :组成上帝之眼的星球群(自定义星球)
# description :组成上帝之眼的星球群(自定义星球)
# author :Python超人
# date :2023-03-26
# link :https://gitcode.net/pythoncr/
# python_version :3.8
# ==============================================================================
from bodies import FixedStar, Body, Earth
from common.consts import SECONDS_PER_HOUR, SECONDS_PER_DAY, SECONDS_PER_WEEK
from sim_scenes.func import ursina_run, create_sphere_sky
from sim_scenes.funny.utils.body_utils import gen_bodies_from_image
from simulators.ursina.ursina_event import UrsinaEvent
def my_universe():
"""
上帝之眼
"""
D = 6000 # 基本距离单位:km(随意赋值)
# 观看摄像机的位置
# camera_pos = (左-右+, 上+下-, 前+后-)
camera_pos = (D, D, -2000 * D)
# 根据 pixel_image 指定图片生成有色星球(注意:图片的像素不要太多)
bodies = gen_bodies_from_image(pixel_image="./images/china.png",
texture="color_body.jpg",
params={"camera_pos": camera_pos})
# 放一个恒星作为背景
# TODO: ignore_mass=True
# 注意:这里的算法是基于牛顿的万有引力(质量为0不受引力的影响在天体物理学中是不严谨)
bg = FixedStar(name="背景恒星", texture="fixed_star.png",
mass=50e32, color=(111, 140, 255),
init_position=[-400 * D, 100 * D, 6000 * D], # [ 左-右+, 上+下-, 远+近- ]
ignore_mass=True)
my_planet = Earth(
texture="earth_cartoon.jpg",
rotate_angle=0,
rotation_speed=0.1,
# init_position=[-200 * D, -100 * D, 0],
init_position=[50 * D, -50 * D, 0],
init_velocity=[0, 0, 0],
size_scale=99).set_resolution(50)
bodies.append(bg)
bodies.append(my_planet)
def on_ready():
from ursina import camera
# camera.fov = 30
# 运行前触发
create_sphere_sky(scale=10000)
# 订阅事件后,上面的函数功能才会起作用
# 运行前会触发 on_ready
UrsinaEvent.on_ready_subscription(on_ready)
# 使用 ursina 查看的运行效果
# 常用快捷键: P:运行和暂停 O:重新开始 I:显示天体轨迹
# position = camera_pos = (左-右+, 上+下-, 前+后-)
ursina_run(bodies,
# SECONDS_PER_HOUR,
SECONDS_PER_WEEK * 2,
cosmic_bg='',
position=camera_pos,
show_grid=False,
show_camera_info=False,
view_closely=True)
if __name__ == '__main__':
my_universe()
# -*- coding:utf-8 -*- # -*- coding:utf-8 -*-
# title :太阳系场景模拟1 # title :天狼星进入太阳系场景模拟
# description :太阳系场景模拟(展示的效果为太阳系真实的距离) # description :天狼星进入太阳系场景模拟
# author :Python超人 # author :Python超人
# date :2023-02-11 # date :2023-02-11
# link :https://gitcode.net/pythoncr/ # link :https://gitcode.net/pythoncr/
......
...@@ -83,7 +83,7 @@ def gen_bodies_from_image(pixel_image, params, texture="color_body.png"): ...@@ -83,7 +83,7 @@ def gen_bodies_from_image(pixel_image, params, texture="color_body.png"):
# 获取像素的颜色 # 获取像素的颜色
pixel = img.getpixel((w, h)) pixel = img.getpixel((w, h))
# 对于纯白色的颜色,就忽略,不生成星球(这样图片中,纯白色越多,对电脑的压力就越小) # 对于纯白色的颜色,就忽略,不生成星球(这样图片中,纯白色越多,对电脑的压力就越小)
if pixel[0] >= 220 and pixel[1] >= 220 and pixel[1] >= 220: if pixel[0] >= 220 and pixel[1] >= 220 and pixel[2] >= 220:
continue continue
body_str = body_template % (f"星球{w}:{h}", pixel[0], pixel[1], pixel[2], scale * 10, body_str = body_template % (f"星球{w}:{h}", pixel[0], pixel[1], pixel[2], scale * 10,
(width - w) * interval_factor, (height - h) * interval_factor, scale) (width - w) * interval_factor, (height - h) * interval_factor, scale)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册