这是一个粒子类。当我初始化不同粒子的“byPosition”方法时,粒子共享“self.position”变量,有人知道为什么共享“self.position”变量吗
import random
from PyQt5.QtCore import QPointF
from PyQt5.QtGui import QPen, QColor
class Particle:
seed = False
def __init__(self, velocity, position, h, seed):
self.hue = h
self.acceleration = QPointF(0, 0)
self.velocity = velocity
self.position = position
self.seed = seed
@classmethod
def byXY(cls, x, y, h):
lowVel = -11 * (y / 480)
highVel = -5 * (y / 480)
velocity = QPointF(0, random.uniform(lowVel, highVel))
position = QPointF(x, y)
return cls(velocity, position, h, True)
@classmethod
def byPosition(cls, position, h):
mul = random.uniform(6, 9)
randomVel = [random.uniform(-1, 1) * mul for i in range(2)]
velocity = QPointF(randomVel[0], randomVel[1])
position = position
return cls(velocity, position, h, False)
“byPosition”中的“position”从“QPointF(0,0)”传递 我创建了两个粒子对象“byPosition”,给它们随机速度,并计算下一帧中每个对象的位置
我希望看到两个不同的点的路径。 但是,这两个对象共享“self.position”数据。 虽然它们的速度不同
在我按照下面的方式编辑了“byPosition”方法之后,一切都很好。 我一点也不懂
@classmethod
def byPosition(cls, position, h):
mul = random.uniform(6, 9)
randomVel = [random.uniform(-1, 1) * mul for i in range(2)]
velocity = QPointF(randomVel[0], randomVel[1])
position = QPointF(position.x(), position.y())
return cls(velocity, position, h, False)
目前没有回答
相关问题 更多 >
编程相关推荐