【发布时间】: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