From 3f8f23e1e8ae603fc4735a225b83fbf7053832d6 Mon Sep 17 00:00:00 2001 From: march3 Date: Sun, 23 Mar 2025 10:31:11 +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 --- simulators/mpl_2d_simulator.py | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/simulators/mpl_2d_simulator.py b/simulators/mpl_2d_simulator.py index f5b2958..d0899e6 100644 --- a/simulators/mpl_2d_simulator.py +++ b/simulators/mpl_2d_simulator.py @@ -76,10 +76,12 @@ class 星球: # 初始化图形 图形, 坐标轴 = plt.subplots() 星球列表 = [] - +# 时间步长,单位:s +时间步长 = 0.01 def 场景1(): - global 星球列表 + global 星球列表, 时间步长 + 时间步长 = 0.01 # 创建星球列表 星球列表 = [ 星球(质量=1e12, 位置=[0, 0], 速度=[0, 1], 颜色='red', 名称="太阳", 名称偏移=[2, 0]), @@ -91,7 +93,8 @@ def 场景1(): def 场景2(): - global 星球列表 + global 星球列表, 时间步长 + 时间步长 = 0.2 星球列表 = [ 星球(质量=1e12, 位置=[10, 0], 速度=[0, 1], 颜色='blue', 名称="蓝星", 名称偏移=[2, 0]), 星球(质量=1e12, 位置=[-10, 0], 速度=[0, -1], 颜色='green', 名称="绿星", 名称偏移=[2, 0]) @@ -100,8 +103,8 @@ def 场景2(): 坐标轴.set_ylim(-50, 50) -# 场景1() -场景2() +场景1() +# 场景2() # 调整边距 plt.subplots_adjust(left=0.05, bottom=0.04, right=0.97, top=0.97, wspace=0.4, hspace=0.4) @@ -131,8 +134,8 @@ def 更新(帧): :param 帧: 当前动画的帧数,无单位 """ - # 时间步长,单位:s - 时间步长 = 0.01 + global 时间步长 + for 星球 in 星球列表: 星球.更新(星球列表, 时间步长) for i, 星球 in enumerate(星球列表): -- GitLab