Memory 在pygame中共享两个对象的内存

Memory 在pygame中共享两个对象的内存,memory,pygame,share,Memory,Pygame,Share,我正在写一个游戏,我有一辆坦克,每辆坦克都能射出子弹。坦克级和子弹级都有坐标和方向。当我们射击一颗子弹时,它会得到坦克的坐标,但是当我用move方法改变子弹的坐标时,坦克的坐标也会改变。如何避免这种内存共享,而不产生大量变量 class Tank: def __init__(self, coords, direction): self.coords = coords self.direction = direction self.bull

我正在写一个游戏,我有一辆坦克,每辆坦克都能射出子弹。坦克级和子弹级都有坐标和方向。当我们射击一颗子弹时,它会得到坦克的坐标,但是当我用
move
方法改变子弹的坐标时,坦克的坐标也会改变。如何避免这种内存共享,而不产生大量变量

class Tank:

    def __init__(self, coords, direction):
        self.coords = coords
        self.direction = direction
        self.bullet = None

    def shoot_bullet(self):
        self.bullet = Bullet(coords, direction)

class Bullet:

    def __init__(self, coords, direction):
        self.coords = coords
        self.direction = direction

    def _move(self):
         self.coords[0] += 4

我猜
coords
是一个
列表
。因此,当您将列表从
坦克
传递到
子弹
时,您可以对单个
列表
进行操作,因此,在
坦克
子弹
内的更改将“可见”

复制该
列表

def shoot_bullet(self):
    self.bullet = Bullet(self.coords[:], self.direction)
或者使用另一种数据结构,比如
元组
,或者,因为您使用的是pygame,所以使用的是
Rect