我该如何复制pygame sprite组? [英] How can I deepcopy a pygame sprite group?

查看:138
本文介绍了我该如何复制pygame sprite组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用蒙特卡洛树搜索来实现国际象棋AI.这需要从每个位置玩800个随机游戏来评估每个动作的价值.但是,我使用的是pygame sprite组来保存片段的实例,然后循环浏览以查找该位置中每个片段的可能移动.当我拿一块时,我使用.kill()函数,否则我将以我的legal_moves()函数结束,从已拿出的块中返回动作.但是,当在搜索中使用它时,它也会杀死游戏中的棋子,我不希望这样做.

I am trying to implement a chess AI using a monte carlo tree search. This requires playing through 800 random games from each position to evaluate the value of each move. However, I am using a pygame sprite group to hold instances of the pieces, which I then loop through to find the possible moves for each piece in the position. When I take a piece I use the .kill() function as otherwise I will end up with my legal_moves() function returning moves from pieces that have been taken. However, when this is used in the search, it also kills the piece in the game, which I don't want it to do.

我尝试使用内置复制功能,但这只是一个浅表复制.我尝试了copy.deepcopy(piece_list),但这给出了一条错误消息:

I have tried using the inbuilt copy function, but this is just a shallow copy. I tried copy.deepcopy(piece_list), but this gives an error message:

File "C:\Users\Sean\PycharmProjects\Chess\Monte_Carlo_Tree_Search.py", line 339, in run_mcts
exploration_game = copy.deepcopy(game)

File "C:\Users\Sean\AppData\Local\Programs\Python\Python37-32\lib\copy.py",  line 180, in deepcopy
y = _reconstruct(x, memo, *rv)

File "C:\Users\Sean\AppData\Local\Programs\Python\Python37-32\lib\copy.py",  line 280, in _reconstruct
state = deepcopy(state, memo)

File "C:\Users\Sean\AppData\Local\Programs\Python\Python37-32\lib\copy.py",  line 150, in deepcopy
y = copier(x, memo)

File "C:\Users\Sean\AppData\Local\Programs\Python\Python37-32\lib\copy.py",  line 240, in _deepcopy_dict
y[deepcopy(key, memo)] = deepcopy(value, memo)

File "C:\Users\Sean\AppData\Local\Programs\Python\Python37-32\lib\copy.py",  line 180, in deepcopy
y = _reconstruct(x, memo, *rv)

File "C:\Users\Sean\AppData\Local\Programs\Python\Python37-32\lib\copy.py", line 280, in _reconstruct
state = deepcopy(state, memo)

File "C:\Users\Sean\AppData\Local\Programs\Python\Python37-32\lib\copy.py", line 150, in deepcopy
y = copier(x, memo)

File "C:\Users\Sean\AppData\Local\Programs\Python\Python37-32\lib\copy.py", line 240, in _deepcopy_dict
y[deepcopy(key, memo)] = deepcopy(value, memo)

File "C:\Users\Sean\AppData\Local\Programs\Python\Python37-32\lib\copy.py", line 150, in deepcopy
y = copier(x, memo)

File "C:\Users\Sean\AppData\Local\Programs\Python\Python37-32\lib\copy.py", line 240, in _deepcopy_dict
y[deepcopy(key, memo)] = deepcopy(value, memo)

File "C:\Users\Sean\AppData\Local\Programs\Python\Python37-32\lib\copy.py", line 180, in deepcopy
y = _reconstruct(x, memo, *rv)

File "C:\Users\Sean\AppData\Local\Programs\Python\Python37-32\lib\copy.py", line 280, in _reconstruct
state = deepcopy(state, memo)

File "C:\Users\Sean\AppData\Local\Programs\Python\Python37-32\lib\copy.py", line 150, in deepcopy
y = copier(x, memo)

File "C:\Users\Sean\AppData\Local\Programs\Python\Python37-32\lib\copy.py", line 240, in _deepcopy_dict
y[deepcopy(key, memo)] = deepcopy(value, memo)

File "C:\Users\Sean\AppData\Local\Programs\Python\Python37-32\lib\copy.py", line 169, in deepcopy
rv = reductor(4)

TypeError: can't pickle pygame.Surface objects

我已经在网上浏览了一下,但是没有一个回应似乎提供了一种方法,而是一种替代方法.

I've had a look around online, but none of the responses seem to give a way to do it, rather an alternate method.

import pygame

knight = pygame.sprite.Sprite()
piece_list = pygame.sprite.Group()
piece_list.add(knight)

piece_list_copy = piece_list.copy()

for piece in piece_list_copy:
    piece.kill()

print(piece_list)

我想要一些更改此代码的方法,以使其返回"Group(1 sprites)",而不是"Group(0 sprites)"

I would want some method of changing this code such that it returns "Group(1 sprites)", as opposed to "Group(0 sprites)"

推荐答案

.copy() 创建一个新的组,该组包含与该组相同的精灵,但这些精灵不会被(深层)复制.

.copy() creates a new group, which contains the same sprites as the group, but the sprites are not (deep) copied.

您可以使用 .remove() 从单个

You can use .remove() to remove a Sprite from a single pygame.sprite.Group:

for piece in piece_list_copy:
    piece_list.remove(piece)

这篇关于我该如何复制pygame sprite组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆