通过将变量与变量分开来复制变量

时间:2018-11-12 23:29:45

标签: python python-3.7

昨天,我问了一个有关我遇到的问题的问题:Create Entities Reduce FPS And Overlapping。通过以下逻辑,我设法解决了FPS下降的问题:

  1. 我为每种类型的实体创建一个模型。
  2. 创建新实体时,只需复制模型数据。

结果,实体创建功能已简化为:

def create_entity(self, pos_x, pos_y, obj):
    if obj not in self.main.model.model_enemy:
        entity = self.main.model.model_enemy["0000"]
    else:
        entity = self.main.model.model_enemy[obj]
    entity["battle"]["position"] = (pos_x, pos_y)
    entity["battle"]["sprite"][entity["battle"]["state"]][entity["battle"]["direction"]].x = pos_x
    entity["battle"]["sprite"][entity["battle"]["state"]][entity["battle"]["direction"]].y = pos_y
    return entity

该函数被调用,并为其提供参数:pos_xpos_y(绘制实体的位置)和obj(为实体选择的模型)。 if语句检查请求的模板是否存在于self.main.model.model_enemy中(它包含所有模型)。如果不存在,则为其分配基本模型。下一部分是位置的简单设置。完成后,将返回创建的实体。

我遇到的问题是,如果我编辑以这种方式创建的任何实体的属性(创建时将每个实体放置在列表中),那么我将为所有实体进行编辑。

我认为这是发生的,因为当我将模型类型分配给实体(entity = self.main.model.model_enemy)时,实际上并没有创建该模型的副本。因此,要更改实体的属性,实际上是要更改模型的属性。结果,将修改使用该模型创建的所有实体。相反,我需要复制模型,以使其与模型保持独立。这样,我可以修改实体的属性,而无需修改模型本身。

我已经尝试使用:copy.deepcopy,但是它不起作用,因为它给我错误。你能给我什么建议?我遇到的问题是Python,这就是为什么没有Pyglet标签的原因。

我为您提供了一个基本模型,以便您可以了解它的组成方式(该模型仍然缺少将来必须添加的部分):

OrderedDict([('battle', OrderedDict([('tile', 24), ('image', OrderedDict([('idle', OrderedDict([(0, <ImageDataRegion 24x24>), (1, <ImageDataRegion 24x24>), (2, <ImageDataRegion 24x24>), (3, <ImageDataRegion 24x24>), (4, <ImageDataRegion 24x24>)])), ('movement', OrderedDict([(0, <pyglet.image.Animation object at 0x000002BF947C2160>), (1, <pyglet.image.Animation object at 0x000002BF947C26A0>), (2, <pyglet.image.Animation object at 0x000002BF947C2C18>), (3, <pyglet.image.Animation object at 0x000002BF9467CF60>), (4, <pyglet.image.Animation object at 0x000002BF947C57F0>)])), ('attack', OrderedDict([(0, <pyglet.image.Animation object at 0x000002BF947C5D68>), (1, <pyglet.image.Animation object at 0x000002BF947CA320>), (2, <pyglet.image.Animation object at 0x000002BF947CA898>), (3, <pyglet.image.Animation object at 0x000002BF947CAE10>), (4, <pyglet.image.Animation object at 0x000002BF952503C8>)])), ('sp_attack_1', OrderedDict([(0, <pyglet.image.Animation object at 0x000002BF952506A0>), (1, <pyglet.image.Animation object at 0x000002BF95250AC8>), (2, <pyglet.image.Animation object at 0x000002BF952570B8>), (3, <pyglet.image.Animation object at 0x000002BF952574A8>), (4, <pyglet.image.Animation object at 0x000002BF952578D0>)])), ('sp_attack_2', OrderedDict([(0, <pyglet.image.Animation object at 0x000002BF95257DD8>), (1, <pyglet.image.Animation object at 0x000002BF9525A208>), (2, <pyglet.image.Animation object at 0x000002BF9525A588>), (3, <pyglet.image.Animation object at 0x000002BF9525A9B0>), (4, <pyglet.image.Animation object at 0x000002BF9525AF60>)])), ('hurt', OrderedDict([(0, <ImageDataRegion 24x24>), (1, <ImageDataRegion 24x24>), (2, <ImageDataRegion 24x24>), (3, <ImageDataRegion 24x24>), (4, <ImageDataRegion 24x24>)])), ('asleep', OrderedDict([(0, <pyglet.image.Animation object at 0x000002BF95261B38>), (1, <pyglet.image.Animation object at 0x000002BF95267080>), (2, <pyglet.image.Animation object at 0x000002BF952674A8>), (3, <pyglet.image.Animation object at 0x000002BF95267A20>), (4, <pyglet.image.Animation object at 0x000002BF95267F28>)]))])), ('texture', OrderedDict([('idle', OrderedDict([(0, <TextureRegion 48x48>), (1, <TextureRegion 48x48>), (2, <TextureRegion 48x48>), (3, <TextureRegion 48x48>), (4, <TextureRegion 48x48>)])), ('movement', OrderedDict([(0, <pyglet.image.Animation object at 0x000002BF947C2128>), (1, <pyglet.image.Animation object at 0x000002BF947C24E0>), (2, <pyglet.image.Animation object at 0x000002BF947C2A58>), (3, <pyglet.image.Animation object at 0x000002BF947C5128>), (4, <pyglet.image.Animation object at 0x000002BF947C5630>)])), ('attack', OrderedDict([(0, <pyglet.image.Animation object at 0x000002BF947C5BA8>), (1, <pyglet.image.Animation object at 0x000002BF947CA160>), (2, <pyglet.image.Animation object at 0x000002BF947CA6D8>), (3, <pyglet.image.Animation object at 0x000002BF947CAC50>), (4, <pyglet.image.Animation object at 0x000002BF95250208>)])), ('sp_attack_1', OrderedDict([(0, <pyglet.image.Animation object at 0x000002BF95250940>), (1, <pyglet.image.Animation object at 0x000002BF95250D68>), (2, <pyglet.image.Animation object at 0x000002BF95257320>), (3, <pyglet.image.Animation object at 0x000002BF95257748>), (4, <pyglet.image.Animation object at 0x000002BF95257B70>)])), ('sp_attack_2', OrderedDict([(0, <pyglet.image.Animation object at 0x000002BF947C5470>), (1, <pyglet.image.Animation object at 0x000002BF9525A400>), (2, <pyglet.image.Animation object at 0x000002BF9525A828>), (3, <pyglet.image.Animation object at 0x000002BF9525AC50>), (4, <pyglet.image.Animation object at 0x000002BF95261160>)])), ('hurt', OrderedDict([(0, <TextureRegion 48x48>), (1, <TextureRegion 48x48>), (2, <TextureRegion 48x48>), (3, <TextureRegion 48x48>), (4, <TextureRegion 48x48>)])), ('asleep', OrderedDict([(0, <pyglet.image.Animation object at 0x000002BF95261EB8>), (1, <pyglet.image.Animation object at 0x000002BF95267320>), (2, <pyglet.image.Animation object at 0x000002BF95267748>), (3, <pyglet.image.Animation object at 0x000002BF95267C88>), (4, <pyglet.image.Animation object at 0x000002BF9526D160>)]))])), ('sprite', OrderedDict([('idle', OrderedDict([(0, <pyglet.sprite.Sprite object at 0x000002BF8884FD30>), (1, <pyglet.sprite.Sprite object at 0x000002BF947B76D8>), (2, <pyglet.sprite.Sprite object at 0x000002BF947B7780>), (3, <pyglet.sprite.Sprite object at 0x000002BF947B7828>), (4, <pyglet.sprite.Sprite object at 0x000002BF947B7A20>)])), ('movement', OrderedDict([(0, <pyglet.sprite.Sprite object at 0x000002BF947C20B8>), (1, <pyglet.sprite.Sprite object at 0x000002BF947C2550>), (2, <pyglet.sprite.Sprite object at 0x000002BF947C2AC8>), (3, <pyglet.sprite.Sprite object at 0x000002BF947C50B8>), (4, <pyglet.sprite.Sprite object at 0x000002BF947C56A0>)])), ('attack', OrderedDict([(0, <pyglet.sprite.Sprite object at 0x000002BF947C5C18>), (1, <pyglet.sprite.Sprite object at 0x000002BF947CA1D0>), (2, <pyglet.sprite.Sprite object at 0x000002BF947CA748>), (3, <pyglet.sprite.Sprite object at 0x000002BF947CACC0>), (4, <pyglet.sprite.Sprite object at 0x000002BF95250278>)])), ('sp_attack_1', OrderedDict([(0, <pyglet.sprite.Sprite object at 0x000002BF952507F0>), (1, <pyglet.sprite.Sprite object at 0x000002BF95250C18>), (2, <pyglet.sprite.Sprite object at 0x000002BF952571D0>), (3, <pyglet.sprite.Sprite object at 0x000002BF952575F8>), (4, <pyglet.sprite.Sprite object at 0x000002BF95257A20>)])), ('sp_attack_2', OrderedDict([(0, <pyglet.sprite.Sprite object at 0x000002BF95257E80>), (1, <pyglet.sprite.Sprite object at 0x000002BF9525A2B0>), (2, <pyglet.sprite.Sprite object at 0x000002BF9525A6D8>), (3, <pyglet.sprite.Sprite object at 0x000002BF9525AB00>), (4, <pyglet.sprite.Sprite object at 0x000002BF952610B8>)])), ('hurt', OrderedDict([(0, <pyglet.sprite.Sprite object at 0x000002BF952612E8>), (1, <pyglet.sprite.Sprite object at 0x000002BF95261390>), (2, <pyglet.sprite.Sprite object at 0x000002BF95261588>), (3, <pyglet.sprite.Sprite object at 0x000002BF95261780>), (4, <pyglet.sprite.Sprite object at 0x000002BF95261978>)])), ('asleep', OrderedDict([(0, <pyglet.sprite.Sprite object at 0x000002BF95261D68>), (1, <pyglet.sprite.Sprite object at 0x000002BF952671D0>), (2, <pyglet.sprite.Sprite object at 0x000002BF952675F8>), (3, <pyglet.sprite.Sprite object at 0x000002BF95267B38>), (4, <pyglet.sprite.Sprite object at 0x000002BF9526D0B8>)]))])), ('state', 'idle'), ('direction', 0), ('position', (0, 0))]))])

编辑: 这是我在copy.deepcopy()中遇到的错误:

Traceback (most recent call last):
  File "F:/Python/Project/PE/Project.py", line 479, in <module>
    pyglet.app.run()
  File "F:\Python\Python37\lib\site-packages\pyglet\app\__init__.py", line 138, in run
    event_loop.run()
  File "F:\Python\Python37\lib\site-packages\pyglet\app\base.py", line 142, in run
    self._run()
  File "F:\Python\Python37\lib\site-packages\pyglet\app\base.py", line 154, in _run
    timeout = self.idle()
  File "F:\Python\Python37\lib\site-packages\pyglet\app\base.py", line 275, in idle
    redraw_all = self.clock.call_scheduled_functions(dt)
  File "F:\Python\Python37\lib\site-packages\pyglet\clock.py", line 346, in call_scheduled_functions
    item.func(now - item.last_ts, *item.args, **item.kwargs)
  File "F:/Python/Project/PE/Project.py", line 142, in update
    up()
  File "F:/Python/Project/PE/Project.py", line 256, in update
    entity = self.create_entity(x, y, "0001")
  File "F:/Python/Project/PE/Project.py", line 238, in create_entity
    entity = copy.deepcopy(self.main.model.model_enemy["0000"])
  File "F:\Python\Python37\lib\copy.py", line 180, in deepcopy
    y = _reconstruct(x, memo, *rv)
  File "F:\Python\Python37\lib\copy.py", line 306, in _reconstruct
    value = deepcopy(value, memo)
  File "F:\Python\Python37\lib\copy.py", line 180, in deepcopy
    y = _reconstruct(x, memo, *rv)
  File "F:\Python\Python37\lib\copy.py", line 306, in _reconstruct
    value = deepcopy(value, memo)
  File "F:\Python\Python37\lib\copy.py", line 180, in deepcopy
    y = _reconstruct(x, memo, *rv)
  File "F:\Python\Python37\lib\copy.py", line 306, in _reconstruct
    value = deepcopy(value, memo)
  File "F:\Python\Python37\lib\copy.py", line 180, in deepcopy
    y = _reconstruct(x, memo, *rv)
  File "F:\Python\Python37\lib\copy.py", line 306, in _reconstruct
    value = deepcopy(value, memo)
  File "F:\Python\Python37\lib\copy.py", line 180, in deepcopy
    y = _reconstruct(x, memo, *rv)
  File "F:\Python\Python37\lib\copy.py", line 280, in _reconstruct
    state = deepcopy(state, memo)
  File "F:\Python\Python37\lib\copy.py", line 150, in deepcopy
    y = copier(x, memo)
  File "F:\Python\Python37\lib\copy.py", line 240, in _deepcopy_dict
    y[deepcopy(key, memo)] = deepcopy(value, memo)
  File "F:\Python\Python37\lib\copy.py", line 180, in deepcopy
    y = _reconstruct(x, memo, *rv)
  File "F:\Python\Python37\lib\copy.py", line 280, in _reconstruct
    state = deepcopy(state, memo)
  File "F:\Python\Python37\lib\copy.py", line 150, in deepcopy
    y = copier(x, memo)
  File "F:\Python\Python37\lib\copy.py", line 240, in _deepcopy_dict
    y[deepcopy(key, memo)] = deepcopy(value, memo)
  File "F:\Python\Python37\lib\copy.py", line 180, in deepcopy
    y = _reconstruct(x, memo, *rv)
  File "F:\Python\Python37\lib\copy.py", line 280, in _reconstruct
    state = deepcopy(state, memo)
  File "F:\Python\Python37\lib\copy.py", line 150, in deepcopy
    y = copier(x, memo)
  File "F:\Python\Python37\lib\copy.py", line 240, in _deepcopy_dict
    y[deepcopy(key, memo)] = deepcopy(value, memo)
  File "F:\Python\Python37\lib\copy.py", line 180, in deepcopy
    y = _reconstruct(x, memo, *rv)
  File "F:\Python\Python37\lib\copy.py", line 280, in _reconstruct
    state = deepcopy(state, memo)
  File "F:\Python\Python37\lib\copy.py", line 150, in deepcopy
    y = copier(x, memo)
  File "F:\Python\Python37\lib\copy.py", line 240, in _deepcopy_dict
    y[deepcopy(key, memo)] = deepcopy(value, memo)
  File "F:\Python\Python37\lib\copy.py", line 169, in deepcopy
    rv = reductor(4)
ValueError: ctypes objects containing pointers cannot be pickled

0 个答案:

没有答案