【问题标题】:Deepcopying a 2d arrary (board) with sprites on it深度复制带有精灵的二维数组(板)
【发布时间】:2020-05-09 14:22:08
【问题描述】:

我目前正在完成我在学校的期末项目,所以我正在使用 pygame 制作游戏。 游戏包括一个带有基地、殖民地和防御的棋盘,所有这些都是精灵对象。 在该项目中,我们被要求添加 2 个机器学习算法,其中一个是极小极大。 您可能知道,要使用 minimax,您需要多次 copy.deepcopy 板并计算每个板的 huristic 值,并在最大时找到最好的板,在最小时找到最差的板。

我面临的问题是copy.deepcopy 可能无法对具有精灵属性的对象进行操作。 这是显示的错误消息:

TypeError: can't pickle pygame.Surface objects

它在我的代码中向我介绍了这一行:

b = copy.deepcopy(Board)

我真的不知道在这种情况下该怎么办,我不能回去换板,因为我有大约 1700 行代码 + 完成的图形,并且将所有对象更改为非精灵对象也只是很多。 所以我非常需要你的帮助,必须弄清楚这一点才能继续

这是游戏开始时棋盘中单行的示例;只是为了帮助您理解我在说什么:

[None, "", None, None, None, None, None, None, None, None, None, None, None, None, None, None, None,无,“

殖民地类的示例,最后一段与精灵相关的代码:

class Colony(pg.sprite.Sprite):
    def __init__(self, x, y, clan, type):
        self.clan = clan
        if self.clan == 1:
            name = "joogadars"
        else:
            name = "klagars"
        self.type = type
        if type == 1:
            type_name = "doorks"
        else:
            type_name = "gorgs"
        self.lvl = 1
        self.attackable = True

        self.rates = array_from_txt_colonies("Colonies Data.txt", self.type, 'Rates')
        self.prices = array_from_txt_colonies("Colonies Data.txt", self.type, 'Prices')
        self.hps = array_from_txt_colonies("Colonies Data.txt", self.type, 'Hps')
        self.hp = self.hps[self.lvl - 1]
        self.rate = self.rates[self.lvl - 1]

        self.position = (x, y)
        full_name = type_name + '/' + name + '/' + name + " " + type_name + " colony lvl " + str(self.lvl) + " use.png"
        pg.sprite.Sprite.__init__(self)
        img = 'C:/Users/ariel/Desktop/Ariel/12th/Python/final proj/graphics/islands/colonies/' + full_name
        self.image = pg.image.load(img)
        self.rect = self.image.get_rect(center=self.position)

【问题讨论】:

    标签: python pygame sprite deep-copy minimax


    【解决方案1】:

    如果您不想或无法将所需的游戏状态与绘图代码(也称为精灵)分开,一个简单的解决方案是将图像路径存储在您的精灵中:

    ...
    self.position = (x, y)
    full_name = type_name + '/' + name + '/' + name + " " + type_name + " colony lvl " + str(self.lvl) + " use.png"
    pg.sprite.Sprite.__init__(self)
    self.img = 'C:/Users/ariel/Desktop/Ariel/12th/Python/final proj/graphics/islands/colonies/' + full_name
    self.image = pg.image.load(self.img)
    self.rect = self.image.get_rect(center=self.position)
    ...
    

    然后在尝试复制精灵之前,删除图像,并在复制后重新加载;像这样:

    for sprite in Board.sprites:
        sprite.image = None
    
    b = copy.deepcopy(Board)
    
    for sprite in Board.sprites:
        sprite.image = pygame.load(sprite.img)
    
    for sprite in b.sprites:
        sprite.image = pygame.load(sprite.img)
    

    你会明白的。此外,您可能还应该缓存图像,这样您就不必每次都从磁盘加载它们。

    另一种方法是实现__deepcopy__,以防止deepcopy函数试图复制image属性;你读过它,例如herehere(但您必须在复制后加载图像)。

    使用适合你的任何东西。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-01-11
      • 1970-01-01
      • 2010-09-30
      • 1970-01-01
      • 2011-07-30
      • 1970-01-01
      • 1970-01-01
      • 2012-03-05
      相关资源
      最近更新 更多