[英]Add object to existing value in dictionary
In order to make my own tile based rpg, I have a question. 为了制作自己的基于图块的rpg,我有一个问题。 I have a dictionary where I want, during the creation of my tile map, to store pairs of tile type/ rect values eg
我有一个字典,在创建图块贴图的过程中,我想在其中存储成对的图块类型/矩形值,例如
{1: rect(0, 0, 32, 32), 2: rect(0, 32, 32, 32)}
My problem now is that I have to give every key multiple values, since there is only a limited number of tile types but multiple tiles of each type. 我现在的问题是,我必须给每个键提供多个值,因为只有有限数量的图块类型,但是每种类型都有多个图块。 So I tried this:
所以我尝试了这个:
def create(self):
x, y = 0, 0
for row in self.matrix:
for tile in row:
self.surface.blit(tiles[int(tile)].img, (x-camerax, y-cameray))
tiles[int(tile)].rect = pygame.Rect(x-camerax, y-cameray, 32, 32)
if numbers[tiles[int(tile)]] not in collision_list:
collision_list[numbers[tiles[int(tile)]]] = tiles[int(tile)].rect
else:
dummytuple = collision_list[numbers[tiles[int(tile)]]]
collision_list[numbers[tiles[int(tile)]]] = dummytuple, tiles[int(tile)].rect
x += 32
if x >= self.surface.get_width():
y += 32
x = 0
Might be a bit complicated since I'm not very good at writing simple code. 可能有点复杂,因为我不太擅长编写简单的代码。 My problem is in the if else statements.
我的问题是在if else语句中。 I use them to check whether one type of tile in the dictionary
collide_list
already has one or multiple rects assigned. 我用它们来检查字典
collide_list
中的一种瓷砖是否已经分配了一个或多个collide_list
。 If so, the existing rect should be added as a second value to the key. 如果是这样,应将现有的rect作为第二个值添加到密钥中。 This kind of works, but prints out this in the shell:
这种工作方式,但在外壳中打印出来:
(((((((((((((((((((((((((((<rect(0, 0, 32, 32)>, <rect(32, 0, 32, 32)>), <rect(64, 0, 32, 32)>), <rect(96, 0, 32, 32)>), <rect(128, 0, 32, 32)>), <rect(160, 0, 32, 32)>), <rect(192, 0, 32, 32)>), <rect(224, 0, 32, 32)>), <rect(256, 0, 32, 32)>), <rect(288, 0, 32, 32)>), <rect(320, 0, 32, 32)>), <rect(352, 0, 32, 32)>), <rect(384, 0, 32, 32)>), <rect(416, 0, 32, 32)>), <rect(448, 0, 32, 32)>), <rect(0, 32, 32, 32)>), <rect(160, 32, 32, 32)>), <rect(192, 32, 32, 32)>), <rect(224, 32, 32, 32)>), <rect(256, 32, 32, 32)>), <rect(288, 32, 32, 32)>), <rect(320, 32, 32, 32)>), <rect(352, 32, 32, 32)>), <rect(384, 32, 32, 32)>), <rect(416, 32, 32, 32)>), <rect(448, 32, 32, 32)>), <rect(0, 64, 32, 32)>), <rect(32, 64, 32, 32)>)
This is only for one key (sorry for spamming). 这仅适用于一个密钥(对不起,此为垃圾邮件)。 This is VERY complicated to read and very hard to work with later on.
这是非常复杂的阅读,以后很难使用。 Isn't there a better possibility of concatenating a value to an existing value in a dictionary?
将值连接到字典中的现有值是否存在更好的可能性? Any help is appreciated.
任何帮助表示赞赏。
You are collecting the existing tuple, then creating a new tuple with that existing tuple nested into it: 您正在收集现有的元组,然后使用嵌套在其中的现有元组创建一个新的元组:
dummytuple = collision_list[numbers[tiles[int(tile)]]]
collision_list[numbers[tiles[int(tile)]]] = dummytuple, tiles[int(tile)].rect
Instead, append a new one-element tuple: 而是添加一个新的单元素元组:
dummytuple = collision_list[numbers[tiles[int(tile)]]]
collision_list[numbers[tiles[int(tile)]]] = dummytuple + (tiles[int(tile)].rect,)
You certainly should leave your one-element values to be just a rectangle, start with a one-element tuple: 当然,您应该将一个元素的值保留为一个矩形,以一个元素元组开始:
if numbers[tiles[int(tile)]] not in collision_list:
collision_list[numbers[tiles[int(tile)]]] = (tiles[int(tile)].rect,)
else:
dummytuple = collision_list[numbers[tiles[int(tile)]]]
collision_list[numbers[tiles[int(tile)]]] = dummytuple + (tiles[int(tile)].rect,)
or, much better still, use a list, you are after all altering it constantly: 或者,最好使用列表,毕竟您在不断地对其进行更改:
if numbers[tiles[int(tile)]] not in collision_list:
collision_list[numbers[tiles[int(tile)]]] = [tiles[int(tile)].rect]
else:
collision_list[numbers[tiles[int(tile)]]].append(tiles[int(tile)].rect)
If you make collision_list
a collections.defaultdict
object you don't even have to test for the key first: 如果将
collision_list
设为collections.defaultdict
对象,则您甚至不必先测试密钥:
from collections import defaultdict
collision_list = defaultdict(list)
# ....
# No `if numbers[tiles[int(tile)]] not in collision_list` needed
collision_list[numbers[tiles[int(tile)]]].append(tiles[int(tile)].rect)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.