版本比较
三体运动
正文
增加 44 行删除 44 行
11 行未改动12131415161718192045 行未改动666768697071727374757677787980818283848586878889909192939495969798991001011021039 行未改动1131141151161171181191201218 行未改动13013113213313413513613713813922 行未改动16216316416516616716816917016 行未改动1871881891901911921931941952 行未改动19819920020120220320420520613 行未改动22022122222322422522622722822923023113 行未改动2452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742753 行未改动27928028128228328428528628742 行未改动33033133233333433533633733812 行未改动3513523533543553563573583592 行未改动36236336436536636736836937013 行未改动3843853863873883893903913923933943955 行未改动4014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304313 行未改动43543643743843944044144244343 行未改动48748848949049149249349449512 行未改动5085095105115125135145155162 行未改动51952052152252352452552652713 行未改动5415425435445455465475485495505515525 行未改动5585595605615625635645655665675685695705715725735745755765775785795805813 行未改动58558658758858959059159259359459559659759859960060160260360460560660718 行未改动62662762862963063163263311 行未改动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/ddef 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].mbodys[i].vx += ax * sfbodys[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-20x, y = bodys[i].xcor(), bodys[i].ycor()if x < -w:bodys[i].setx(-w)bodys[i].vx = -bodys[i].vx * 0.9elif x > w:bodys[i].setx(w)bodys[i].vx = -bodys[i].vx * 0.9if y < -h:bodys[i].sety(-h)bodys[i].vy = -bodys[i].vy * 0.9elif y > h:9 行未改动info.write("ESC:退出 R:重置", align="center", font=("SimHei", 12, "normal"))# 重置功能 - 重新生成随机初始速度def reset():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,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 / 100000000t.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_masst.vx = random.uniform(min_v_star, max_v_star)t.vy = random.uniform(min_v_star, max_v_star)t.p = is_pt.showturtle()# 初始位置设置13 行未改动def grav(x, y):dx = y.xcor() - x.xcor()dy = y.ycor() - x.ycor()dist_sq = dx*dx + dy*dymin_dist = 120 if (x.p or y.p) else 100d = math.sqrt(dist_sq) if dist_sq > (min_dist**2) else min_distf = 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 - 30h_half = sh//2 - 30for i in range(4):body = bodys[i]fx, fy = forces[i]mass = body.max = fx / massay = fy / massbody.vx += ax * sfbody.vy += ay * sfnew_x = body.xcor() + body.vxnew_y = body.ycor() + body.vyif new_x < -w_half or new_x > w_half:new_x = max(-w_half, min(new_x, w_half))body.vx = -body.vx * 0.9if new_y < -h_half or new_y > h_half:new_y = max(-h_half, min(new_y, h_half))body.vy = -body.vy * 0.9body.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:重置 ",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,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 / 250000t.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_masst.vx = random.uniform(min_v_star, max_v_star)t.vy = random.uniform(min_v_star, max_v_star)t.p = is_pt.showturtle()# 初始位置设置13 行未改动def grav(x, y):dx = y.xcor() - x.xcor()dy = y.ycor() - x.ycor()dist_sq = dx * dx + dy * dymin_dist = 50d = math.sqrt(dist_sq) if dist_sq > (min_dist**2) else min_distf = 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 - 30h_half = sh//2 - 30for i in range(4):body = bodys[i]fx, fy = forces[i]mass = body.max = fx / massay = fy / massbody.vx += ax * sfbody.vy += ay * sfnew_x = body.xcor() + body.vxnew_y = body.ycor() + body.vyif new_x < -w_half or new_x > w_half:new_x = max(-w_half, min(new_x, w_half))body.vx = -body.vx * 0.9if new_y < -h_half or new_y > h_half:new_y = max(-h_half, min(new_y, h_half))body.vy = -body.vy * 0.9body.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:重置 ",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,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 / 25000t.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_pt.showturtle()# 初始位置设置13 行未改动def grav(x, y):dx = y.xcor() - x.xcor()dy = y.ycor() - x.ycor()dist_sq = dx * dx + dy * dymin_dist = 50d = math.sqrt(dist_sq) if dist_sq > (min_dist**2) else min_distf = 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 - 30h_half = sh//2 - 30for i in range(4):body = bodys[i]fx, fy = forces[i]mass = body.max = fx / massay = fy / massbody.vx += ax * sfbody.vy += ay * sfnew_x = body.xcor() + body.vxnew_y = body.ycor() + body.vyif new_x < -w_half or new_x > w_half:new_x = max(-w_half, min(new_x, w_half))body.vx *= -0.1if 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清除尾迹",align="center", font=("SimHei", 12, "normal"))# 重置功能def reset():18 行未改动```有什么好的意见可以和我说,考虑加求赞AWA2025/9/13 感谢@[AzusaShirasu](luogu://user/188950)提出的建议,正在学习 BH 算法。11 行未改动12131415161718192045 行未改动666768697071727374757677787980818283848586878889909192939495969798991001011021039 行未改动1131141151161171181191201218 行未改动13013113213313413513613713813922 行未改动16216316416516616716816917016 行未改动1871881891901911921931941952 行未改动19819920020120220320420520613 行未改动22022122222322422522622722822923023113 行未改动2452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742753 行未改动27928028128228328428528628742 行未改动33033133233333433533633733812 行未改动3513523533543553563573583592 行未改动36236336436536636736836937013 行未改动3843853863873883893903913923933943955 行未改动4014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304313 行未改动43543643743843944044144244343 行未改动48748848949049149249349449512 行未改动5085095105115125135145155162 行未改动51952052152252352452552652713 行未改动5415425435445455465475485495505515525 行未改动5585595605615625635645655665675685695705715725735745755765775785795805813 行未改动58558658758858959059159259359459559659759859960060160260360460560660718 行未改动62662762862963063163263311 行未改动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/ddef 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].mbodys[i].vx += ax * sfbodys[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-20x, y = bodys[i].xcor(), bodys[i].ycor()if x < -w:bodys[i].setx(-w)bodys[i].vx = -bodys[i].vx * 0.9elif x > w:bodys[i].setx(w)bodys[i].vx = -bodys[i].vx * 0.9if y < -h:bodys[i].sety(-h)bodys[i].vy = -bodys[i].vy * 0.9elif y > h:9 行未改动info.write("ESC:退出 R:重置", align="center", font=("SimHei", 12, "normal"))# 重置功能 - 重新生成随机初始速度def reset():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,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 / 100000000t.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_masst.vx = random.uniform(min_v_star, max_v_star)t.vy = random.uniform(min_v_star, max_v_star)t.p = is_pt.showturtle()# 初始位置设置13 行未改动def grav(x, y):dx = y.xcor() - x.xcor()dy = y.ycor() - x.ycor()dist_sq = dx*dx + dy*dymin_dist = 120 if (x.p or y.p) else 100d = math.sqrt(dist_sq) if dist_sq > (min_dist**2) else min_distf = 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 - 30h_half = sh//2 - 30for i in range(4):body = bodys[i]fx, fy = forces[i]mass = body.max = fx / massay = fy / massbody.vx += ax * sfbody.vy += ay * sfnew_x = body.xcor() + body.vxnew_y = body.ycor() + body.vyif new_x < -w_half or new_x > w_half:new_x = max(-w_half, min(new_x, w_half))body.vx = -body.vx * 0.9if new_y < -h_half or new_y > h_half:new_y = max(-h_half, min(new_y, h_half))body.vy = -body.vy * 0.9body.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:重置 ",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,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 / 250000t.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_masst.vx = random.uniform(min_v_star, max_v_star)t.vy = random.uniform(min_v_star, max_v_star)t.p = is_pt.showturtle()# 初始位置设置13 行未改动def grav(x, y):dx = y.xcor() - x.xcor()dy = y.ycor() - x.ycor()dist_sq = dx * dx + dy * dymin_dist = 50d = math.sqrt(dist_sq) if dist_sq > (min_dist**2) else min_distf = 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 - 30h_half = sh//2 - 30for i in range(4):body = bodys[i]fx, fy = forces[i]mass = body.max = fx / massay = fy / massbody.vx += ax * sfbody.vy += ay * sfnew_x = body.xcor() + body.vxnew_y = body.ycor() + body.vyif new_x < -w_half or new_x > w_half:new_x = max(-w_half, min(new_x, w_half))body.vx = -body.vx * 0.9if new_y < -h_half or new_y > h_half:new_y = max(-h_half, min(new_y, h_half))body.vy = -body.vy * 0.9body.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:重置 ",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,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 / 25000t.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_pt.showturtle()# 初始位置设置13 行未改动def grav(x, y):dx = y.xcor() - x.xcor()dy = y.ycor() - x.ycor()dist_sq = dx * dx + dy * dymin_dist = 50d = math.sqrt(dist_sq) if dist_sq > (min_dist**2) else min_distf = 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 - 30h_half = sh//2 - 30for i in range(4):body = bodys[i]fx, fy = forces[i]mass = body.max = fx / massay = fy / massbody.vx += ax * sfbody.vy += ay * sfnew_x = body.xcor() + body.vxnew_y = body.ycor() + body.vyif new_x < -w_half or new_x > w_half:new_x = max(-w_half, min(new_x, w_half))body.vx *= -0.1if 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清除尾迹",align="center", font=("SimHei", 12, "normal"))# 重置功能def reset():18 行未改动```有什么好的意见可以和我说,考虑加求赞AWA2025/9/13 感谢@[AzusaShirasu](luogu://user/188950)提出的建议,正在学习 BH 算法。