我目前正试图使两个处于气态的粒子碰撞,但我不太确定如何编写一个代码来实现这一点,我有一个大概的草案,我可以做什么,但我真的不知道如何正确地实现它(第43-54行,即注释#I与另一个粒子j碰撞后的代码)碰撞发生后,我想让它们以不同的速度朝相反的方向运动。我将根据粒子的质量计算动能。该项目的目标是基本显示不同参数(速度、质量、方向)的多个粒子在气体中运动的动能守恒。这是我当前的代码,任何帮助都将不胜感激!!:
from tkinter import *
from random import *
myHeight=400
myWidth=600
mySpeed=10
x1=5
y=5
radius1=20
x2=7
y2=4
radius1=20
x=60
width=40
length=10
global particules
particules = []
def initialiseBall(dx,dy,radius,color):
b = [myWidth/2,myHeight/2, dx, dy, radius]
particules.append(b)
k = myCanvas.create_oval(myWidth/2-radius, myHeight/2,\
myWidth/2+radius,myHeight/2+radius,\
width=2,fill=color)
print(k)
def updateBalls():
N = len(particules)
for i in range(N):
particules[i][0] += particules [i][2]
particules[i][1] += particules [i][3]
# collision of i with the walls
if particules[i][0]<0 or particules[i][0]>=myWidth:
particules[i][2] *= -1
if particules[i][1]<0 or particules[i][1]>=myHeight:
particules[i][3] *= -1
#collision of i with another particle j
# for j in range(N):
# if j != i:
# compare the position of i and j
# dij = ...
# if dij ... :
#if collision, compute the normal vector
#change velocities
# if particules[i][1]<=particules[i][1]:
# particules[i][2] *= -1
# r = particules[i][4]
myCanvas.coords(i+1, particules[i][0]-particules[i][4],
particules[i][1]-particules[i][4],
particules[i][0]+particules[i][4],
particules[i][1]+particules[i][4])
def animation ():
updateBalls()
myCanvas.after(mySpeed, animation)
def kineticenergy(mass, velocity):
Ec = 1/2 * mass * velocity ** 2
return Ec
# def averagetrip(number, radius):
# #
# #
# #
# #
mainWindow=Tk()
mainWindow.title('particles reservoir')
myCanvas = Canvas(mainWindow, bg = 'grey', height = myHeight, width = myWidth)
myCanvas.pack(side=TOP)
# create 2 particules
initialiseBall(-1,0, 50, 'red')
initialiseBall(1,0, 50, 'blue')
print(particules)
'''
N = 100
for n in range(N):
initialiseBalle(-1 ,0, randint(5,10), 'red')
'''
animation()
mainWindow.mainloop()
试试这个:
这段代码有很多注释来描述它是如何工作的。如果你还有什么问题,问我。而且
move
方法也没有得到优化。run
方法不是很好,如果它仍在运行并且用户关闭窗口,则可能会抛出错误。我将设法找到一种更好的方法来使用这种方法。如果你发现任何错误,请告诉我。我会设法解决它们相关问题 更多 >
编程相关推荐