版本比较
三体运动
正文
增加 44 行删除 44 行
11 行未改动1213141516161718192045 行未改动666768697070717273747576777778798081828384848586878888899091929293949596979899991001011021039 行未改动1131141151161171171181191201218 行未改动13013113213313413413513613713813922 行未改动16216316416516616616716816917016 行未改动1871881891901911911921931941952 行未改动19819920020120220220320420520613 行未改动22022122222322422422522622722722822923023113 行未改动2452462472482492492502512522522532542552562562572582592602612622632642642652662672682692702712712722732742753 行未改动27928028128228328328428528628742 行未改动33033133233333433433533633733812 行未改动3513523533543553553563573583592 行未改动36236336436536636636736836937013 行未改动3843853863873883883893903913913923933943955 行未改动4014024034044054054064074084084094104114124124134144154164174184194204204214224234244254264274274284294304313 行未改动43543643743843943944044144244343 行未改动48748848949049149149249349449512 行未改动5085095105115125125135145155162 行未改动51952052152252352352452552652713 行未改动5415425435445455455465475485485495505515525 行未改动5585595605615625625635645655655665675685695695705715725735745755765775775785795805813 行未改动58558658758858958959059159259359459459559659759859960060160260360360460560660718 行未改动62662762862963063063163263311 行未改动sw = r.winfo_screenwidth()sh = r.winfo_screenheight()r.destroy()
# 屏幕设置 # 屏幕设置s = t.Screen()s.setup(sw, sh)s.title("三体")s.bgcolor("black")45 行未改动 return f * dx/d, f * dy/d
def update(): forces = [(0,0)]*3 # 各天体受力
# 计算引力 for i in range(3): for j in range(i+1, 3): fx, fy = grav(bodys[i], bodys[j]) forces[i] = (forces[i][0]+fx, forces[i][1]+fy) forces[j] = (forces[j][0]-fx, forces[j][1]-fy)
# 更新运动状态 for i in range(3): # 加速度 ax, ay = forces[i][0]/bodys[i].m, forces[i][1]/bodys[i].m bodys[i].vx += ax * sf bodys[i].vy += ay * sf
# 更新位置 bodys[i].setx(bodys[i].xcor() + bodys[i].vx) bodys[i].sety(bodys[i].ycor() + bodys[i].vy)
# 边界处理 w, h = sw/2-20, sh/2-20 x, y = bodys[i].xcor(), bodys[i].ycor()
if x < -w: bodys[i].setx(-w) bodys[i].vx = -bodys[i].vx * 0.9 elif x > w: bodys[i].setx(w) bodys[i].vx = -bodys[i].vx * 0.9
if y < -h: bodys[i].sety(-h) bodys[i].vy = -bodys[i].vy * 0.9 elif y > h:9 行未改动info.write("ESC:退出 R:重置", align="center", font=("SimHei", 12, "normal"))
# 重置功能 - 重新生成随机初始速度def reset(): for bd in bodys: for bd in bodys: bd.clear() init_rand_vel()
# 键盘事件8 行未改动 s.update() time.sleep(td)except: pass
```# 2.0(加行星,无尾迹版)
```python22 行未改动colors = ["#FF4500", "#32CD32", "#1E90FF", "yellow"]
# 背景矩形bg_canvas = s.getcanvas()bg_rect = bg_canvas.create_rectangle(-sw//2, -sh//2, sw//2, sh//2, bg_rect = bg_canvas.create_rectangle(-sw//2, -sh//2, sw//2, sh//2, fill="black", outline="black")
# 核心参数sf = 0.06 # 时间步长系数16 行未改动 t.penup() t.goto(x, y) t.pendown() t.pensize(0.8)
if is_p: t.m = sz * 150 / 100000000 t.vx = random.uniform(p_min_v, p_max_v) t.vy = random.uniform(p_min_v_y, p_max_v_y)2 行未改动 random_mass = random.uniform(-20, 20) t.m = base_mass + random_mass t.vx = random.uniform(min_v_star, max_v_star) t.vy = random.uniform(min_v_star, max_v_star)
t.p = is_p t.showturtle()
# 初始位置设置13 行未改动def grav(x, y): dx = y.xcor() - x.xcor() dy = y.ycor() - x.ycor() dist_sq = dx*dx + dy*dy
min_dist = 120 if (x.p or y.p) else 100 d = math.sqrt(dist_sq) if dist_sq > (min_dist**2) else min_dist
f = G * x.m * y.m / (d*d) return f * dx/d, f * dy/d
# 运动更新13 行未改动 else: fx, fy = grav(x, y) forces[i] = (forces[i][0] + fx, forces[i][1] + fy) forces[j] = (forces[j][0] - fx, forces[j][1] - fy)
w_half = sw//2 - 30 h_half = sh//2 - 30
for i in range(4): body = bodys[i] fx, fy = forces[i]
mass = body.m ax = fx / mass ay = fy / mass body.vx += ax * sf body.vy += ay * sf new_x = body.xcor() + body.vx new_y = body.ycor() + body.vy
if new_x < -w_half or new_x > w_half: new_x = max(-w_half, min(new_x, w_half)) body.vx = -body.vx * 0.9 if new_y < -h_half or new_y > h_half: new_y = max(-h_half, min(new_y, h_half)) body.vy = -body.vy * 0.9
body.penup() body.goto(new_x, new_y) body.pendown()
3 行未改动info.color("white")info.penup()info.hideturtle()info.goto(0, sh//2 - 50)info.write("《三体》模拟 | ESC:退出 R:重置 ", info.write("《三体》模拟 | ESC:退出 R:重置 ", align="center", font=("SimHei", 12, "normal"))
# 重置功能def reset():42 行未改动colors = ["white", "white", "white", "cyan"]
# 背景矩形bg_canvas = s.getcanvas()bg_rect = bg_canvas.create_rectangle(-sw//2, -sh//2, sw//2, sh//2, bg_rect = bg_canvas.create_rectangle(-sw//2, -sh//2, sw//2, sh//2, fill="black", outline="black")
# 核心参数sf = 0.01 # 时间步长系数12 行未改动 t.penup() t.goto(x, y) t.pendown() t.pensize(0.8)
if is_p: t.m = sz * 600 / 250000 t.vx = random.uniform(min_v_star, max_v_star) t.vy = random.uniform(min_v_star, max_v_star)2 行未改动 random_mass = random.uniform(-20, 20) t.m = base_mass + random_mass t.vx = random.uniform(min_v_star, max_v_star) t.vy = random.uniform(min_v_star, max_v_star)
t.p = is_p t.showturtle()
# 初始位置设置13 行未改动def grav(x, y): dx = y.xcor() - x.xcor() dy = y.ycor() - x.ycor() dist_sq = dx * dx + dy * dy
min_dist = 50 d = math.sqrt(dist_sq) if dist_sq > (min_dist**2) else min_dist
f = G * x.m * y.m / (d*d) return f * dx/d, f * dy/d
# 运动更新5 行未改动 y = bodys[j] fx, fy = grav(x, y) forces[i] = (forces[i][0] + fx, forces[i][1] + fy) forces[j] = (forces[j][0] - fx, forces[j][1] - fy)
w_half = sw//2 - 30 h_half = sh//2 - 30
for i in range(4): body = bodys[i] fx, fy = forces[i]
mass = body.m ax = fx / mass ay = fy / mass body.vx += ax * sf body.vy += ay * sf new_x = body.xcor() + body.vx new_y = body.ycor() + body.vy
if new_x < -w_half or new_x > w_half: new_x = max(-w_half, min(new_x, w_half)) body.vx = -body.vx * 0.9 if new_y < -h_half or new_y > h_half: new_y = max(-h_half, min(new_y, h_half)) body.vy = -body.vy * 0.9
body.penup() body.goto(new_x, new_y) body.pendown()
3 行未改动info.color("white")info.penup()info.hideturtle()info.goto(0, sh//2 - 50)info.write("《三体》模拟 | ESC:退出 R:重置 ", info.write("《三体》模拟 | ESC:退出 R:重置 ", align="center", font=("SimHei", 12, "normal"))
# 重置功能def reset():43 行未改动colors = ["white", "white", "white", "cyan"]
# 背景矩形bg_canvas = s.getcanvas()bg_rect = bg_canvas.create_rectangle(-sw//2, -sh//2, sw//2, sh//2, bg_rect = bg_canvas.create_rectangle(-sw//2, -sh//2, sw//2, sh//2, fill="black", outline="black")
# 核心参数sf = 0.1 # 时间步长系数12 行未改动 t.penup() t.goto(x, y) t.pendown() t.pensize(0.8)
if is_p: t.m = sz * 150 / 25000 t.vx = random.uniform(min_v_star, max_v_star) t.vy = random.uniform(min_v_star, max_v_star)2 行未改动 random_mass = random.uniform(-10, 1000) t.m = base_mass + (base_mass * (1 / random_mass)) t.vx = random.uniform(min_v_star, max_v_star) t.vy = random.uniform(min_v_star, max_v_star)
t.p = is_p t.showturtle()
# 初始位置设置13 行未改动def grav(x, y): dx = y.xcor() - x.xcor() dy = y.ycor() - x.ycor() dist_sq = dx * dx + dy * dy
min_dist = 50 d = math.sqrt(dist_sq) if dist_sq > (min_dist**2) else min_dist
f = G * x.m * y.m / (d*d) return f * dx/d, f * dy/d
# 运动更新5 行未改动 y = bodys[j] fx, fy = grav(x, y) forces[i] = (forces[i][0] + fx, forces[i][1] + fy) forces[j] = (forces[j][0] - fx, forces[j][1] - fy)
w_half = sw//2 - 30 h_half = sh//2 - 30
for i in range(4): body = bodys[i] fx, fy = forces[i]
mass = body.m ax = fx / mass ay = fy / mass body.vx += ax * sf body.vy += ay * sf new_x = body.xcor() + body.vx new_y = body.ycor() + body.vy
if new_x < -w_half or new_x > w_half: new_x = max(-w_half, min(new_x, w_half)) body.vx *= -0.1 if new_y < -h_half or new_y > h_half:3 行未改动 bodys[0].clear() bodys[1].clear() bodys[2].clear() bodys[3].clear()
s.listen() s.onkey(UP, "p") body.goto(new_x, new_y) body.pendown()
# 信息显示info = t.Turtle()info.speed(0)info.color("white")info.penup()info.hideturtle()info.goto(0, sh//2 - 50)info.write("《三体》模拟 | ESC:退出 | R:重置 | P清除尾迹", info.write("《三体》模拟 | ESC:退出 | R:重置 | P清除尾迹", align="center", font=("SimHei", 12, "normal"))
# 重置功能def reset():18 行未改动
```
有什么好的意见可以和我说,考虑加 有什么好的意见可以和我说,考虑加求赞AWA
2025/9/13 感谢@[AzusaShirasu](luogu://user/188950)提出的建议,正在学习 BH 算法。