From 8e147141f64f4208a430165b06f6675976b5f6e9 Mon Sep 17 00:00:00 2001 From: march3 Date: Sat, 18 Mar 2023 14:38:19 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A4=AA=E9=98=B3=E7=B3=BB=E4=B8=89=E4=BD=93?= =?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 --- scenes/gravity_slingshot.py | 35 +++++++++++++++++++++++++++++ scenes/solar_system_1.py | 1 + scenes/solar_system_2.py | 1 + scenes/sun_earth.py | 1 + scenes/sun_earth_jupiter.py | 1 + scenes/sun_earth_moon.py | 1 + scenes/three_body_01.py | 1 + scenes/three_body_03.py | 1 + scenes/two_body_01.py | 1 + simulators/ursina/ursina_ui.py | 40 +++++++++++++++++++++++++++++++--- 10 files changed, 80 insertions(+), 3 deletions(-) create mode 100644 scenes/gravity_slingshot.py diff --git a/scenes/gravity_slingshot.py b/scenes/gravity_slingshot.py new file mode 100644 index 0000000..621774b --- /dev/null +++ b/scenes/gravity_slingshot.py @@ -0,0 +1,35 @@ +# -*- coding:utf-8 -*- +# title :引力弹弓模拟演示 +# description :引力弹弓模拟演示 +# author :Python超人 +# date :2023-02-11 +# link :https://gitcode.net/pythoncr/ +# python_version :3.8 +# ============================================================================== +from bodies import Sun, Earth, Moon +from common.consts import SECONDS_PER_HOUR, SECONDS_PER_HALF_DAY, SECONDS_PER_DAY, SECONDS_PER_WEEK, SECONDS_PER_MONTH +from scenes.func import mayavi_run, ursina_run +from bodies.body import AU + +if __name__ == '__main__': + """ + 太阳、地球 + """ + bodies = [ + Sun(size_scale=2e1), # 太阳放大 20 倍 + Earth(size_scale=1e3, # 地球放大 1000 倍 + init_position=[0, -3 * AU, 0], # 地球距离太阳 3 个天文单位 + # TODO: 尝试调整朝向太阳的速度,取值 33、38、50 或者其他 + # init_velocity=[0, 33, -1], + init_velocity=[0, 38, -1], # 朝向太阳的速度为 38km/s,-1 km/s 是为了防止地球正面对着太阳冲去 + # init_velocity=[0, 50, -1], + ), + ] + + # 使用 mayavi 查看的运行效果 + # mayavi_run(bodies, SECONDS_PER_WEEK, view_azimuth=-45) + + # 使用 ursina 查看的运行效果 + # position = 左-右+、上+下-、前+后- + # 常用快捷键: P:运行和暂停 O:重新开始 I:显示天体轨迹 + ursina_run(bodies, SECONDS_PER_MONTH, position=(0, AU, -3 * AU)) diff --git a/scenes/solar_system_1.py b/scenes/solar_system_1.py index 981bc20..1456cfd 100644 --- a/scenes/solar_system_1.py +++ b/scenes/solar_system_1.py @@ -40,4 +40,5 @@ if __name__ == '__main__': # 使用 ursina 查看的运行效果 # position = 左-右+、上+下-、前+后- + # 常用快捷键: P:运行和暂停 O:重新开始 I:显示天体轨迹 ursina_run(bodies, SECONDS_PER_YEAR, position=(0, 2 * AU, -11 * AU)) diff --git a/scenes/solar_system_2.py b/scenes/solar_system_2.py index c6923a5..a5d8c91 100644 --- a/scenes/solar_system_2.py +++ b/scenes/solar_system_2.py @@ -43,4 +43,5 @@ if __name__ == '__main__': # 使用 ursina 查看的运行效果 # position = 左-右+、上+下-、前+后- + # 常用快捷键: P:运行和暂停 O:重新开始 I:显示天体轨迹 ursina_run(bodies, SECONDS_PER_YEAR, position=(0, 2 * AU, -11 * AU)) diff --git a/scenes/sun_earth.py b/scenes/sun_earth.py index 62266f3..eea9672 100644 --- a/scenes/sun_earth.py +++ b/scenes/sun_earth.py @@ -24,4 +24,5 @@ if __name__ == '__main__': # 使用 ursina 查看的运行效果 # position = 左-右+、上+下-、前+后- + # 常用快捷键: P:运行和暂停 O:重新开始 I:显示天体轨迹 ursina_run(bodies, SECONDS_PER_WEEK, position=(0, AU, -3 * AU)) diff --git a/scenes/sun_earth_jupiter.py b/scenes/sun_earth_jupiter.py index 5a8f480..71be10b 100644 --- a/scenes/sun_earth_jupiter.py +++ b/scenes/sun_earth_jupiter.py @@ -25,4 +25,5 @@ if __name__ == '__main__': # 使用 ursina 查看的运行效果 # position = 左-右+、上+下-、前+后- + # 常用快捷键: P:运行和暂停 O:重新开始 I:显示天体轨迹 ursina_run(bodies, SECONDS_PER_WEEK, position=(0, AU, -3 * AU)) diff --git a/scenes/sun_earth_moon.py b/scenes/sun_earth_moon.py index 1571532..6a379df 100644 --- a/scenes/sun_earth_moon.py +++ b/scenes/sun_earth_moon.py @@ -42,6 +42,7 @@ if __name__ == '__main__': # 使用 ursina 查看的运行效果 # position = 左-右+、上+下-、前+后- # position=(0, 0, 0) 的位置是站在地球视角,可以观看月相变化的过程 + # 常用快捷键: P:运行和暂停 O:重新开始 I:显示天体轨迹 ursina_run(bodies, SECONDS_PER_DAY, position=(0, 0, 0)) # 使用 mayavi 查看的运行效果 diff --git a/scenes/three_body_01.py b/scenes/three_body_01.py index 9c19a41..bc30401 100644 --- a/scenes/three_body_01.py +++ b/scenes/three_body_01.py @@ -33,4 +33,5 @@ if __name__ == '__main__': # 使用 ursina 查看的运行效果 # position = 左-右+、上+下-、前+后- + # 常用快捷键: P:运行和暂停 O:重新开始 I:显示天体轨迹 ursina_run(bodies, SECONDS_PER_DAY, position=(3 * AU, AU, -4 * AU)) \ No newline at end of file diff --git a/scenes/three_body_03.py b/scenes/three_body_03.py index 72d4644..92d9e07 100644 --- a/scenes/three_body_03.py +++ b/scenes/three_body_03.py @@ -58,4 +58,5 @@ if __name__ == '__main__': # 使用 ursina 查看的运行效果 # position = 左-右+、上+下-、前+后- + # 常用快捷键: P:运行和暂停 O:重新开始 I:显示天体轨迹 ursina_run(bodies, SECONDS_PER_YEAR, position=(3 * AU, 3 * AU, -20 * AU)) diff --git a/scenes/two_body_01.py b/scenes/two_body_01.py index 03f1f78..1a05490 100644 --- a/scenes/two_body_01.py +++ b/scenes/two_body_01.py @@ -32,4 +32,5 @@ if __name__ == '__main__': # 使用 ursina 查看的运行效果 # position = 左-右+、上+下-、前+后- + # 常用快捷键: P:运行和暂停 O:重新开始 I:显示天体轨迹 ursina_run(bodies, SECONDS_PER_YEAR, position=(0, 2 * AU, -5 * AU)) \ No newline at end of file diff --git a/simulators/ursina/ursina_ui.py b/simulators/ursina/ursina_ui.py index 3914119..7f1062c 100644 --- a/simulators/ursina/ursina_ui.py +++ b/simulators/ursina/ursina_ui.py @@ -43,7 +43,7 @@ class UrsinaUI: self.on_off_switch = SwithButton((self.pause_button_text, self.start_button_text), default=self.start_button_text, - tooltips=('暂停', '运行')) + tooltips=('暂停(P)', '运行(P)')) self.on_off_switch.selected_color = color.red self.sec_per_time_switch = SwithButton(("默认", "天", "周", "月", "年", "十年", "百年"), @@ -58,8 +58,8 @@ class UrsinaUI: tooltips=('天体运行无轨迹', '天体运行有拖尾轨迹')) self.on_off_trail.on_value_changed = self.on_off_trail_changed - self.point_button = UiButton(text='寻找', on_click=self.on_searching_bodies_click) - self.reset_button = UiButton(text='重置', on_click=self.on_reset_button_click) + self.point_button = UiButton(text='寻找天体(Y)', on_click=self.on_searching_bodies_click) + self.reset_button = UiButton(text='重新开始(O)', on_click=self.on_reset_button_click) # button1 = Button(text='Button 1', scale=(0.1, 0.1), position=(-0.1, 0)) # button2 = Button(text='Button 2', scale=(0.1, 0.1), position=(0.1, 0)) @@ -327,6 +327,40 @@ class UrsinaUI: self.wp.enabled = not self.wp.enabled elif key == 'left mouse down': print(key) + elif key == 'y': # 寻找天体 + if hasattr(self, "bodies_button_list"): + if self.bodies_button_list.enabled: + self.bodies_button_list.enabled = False + destroy(self.bodies_button_list) + return + self.on_searching_bodies_click() + elif key == 'o': # 重新开始 + self.on_reset_button_click() + elif key == 'i': # 拖尾开关 + if self.on_off_trail.value == self.trail_button_text: + self.on_off_trail.value = self.no_trail_button_text + else: + self.on_off_trail.value = self.trail_button_text + self.on_off_trail_changed() + elif key == 'p': # 开始、暂停 + if self.on_off_switch.value == self.pause_button_text: + self.on_off_switch.value = self.start_button_text + else: + self.on_off_switch.value = self.pause_button_text + self.on_off_switch_changed() + elif key == '+' or key == "= up": + run_speed_factor = self.slider_run_speed_factor.value + self.slider_run_speed_factor.step * 50 + if run_speed_factor > self.slider_run_speed_factor.max: + run_speed_factor = self.slider_run_speed_factor.max + self.slider_run_speed_factor.value = run_speed_factor + self.slider_run_speed_factor.knob.drop() + elif key == '-' or key == "- up": + run_speed_factor = self.slider_run_speed_factor.value - self.slider_run_speed_factor.step * 50 + if run_speed_factor < self.slider_run_speed_factor.min: + run_speed_factor = self.slider_run_speed_factor.min + self.slider_run_speed_factor.value = run_speed_factor + self.slider_run_speed_factor.knob.drop() + # UrsinaConfig.run_speed_factor *= 2 # application.paused = not application.paused # Pause/unpause the game. # elif key == 'tab': # # application.time_scale 属性控制游戏时间流逝的速度。 -- GitLab