0
爲了製作我自己的基於磁貼的RPG,我有一個問題。 我有一本字典,我希望在創建我的瓷磚地圖的過程中存儲瓷磚類型/矩形值對,例如現在將對象添加到字典中的現有值
{1: rect(0, 0, 32, 32), 2: rect(0, 32, 32, 32)}
我的問題是,我必須給每個鍵多個值,因爲只有有限數量的 瓷磚類型,但每種類型的多個瓦片。 所以,我想這一點:
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
可能是一個有點複雜,因爲我不是在寫簡單的代碼非常好。 我的問題是在if語句中。我使用它們來檢查字典collide_list
中的某種類型的磁貼是否已經分配了一個或多個矩形。 如果是這樣,則應該將現有的矩形作爲第二個值添加到鍵中。這類作品,但打印出這個在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)>)
這僅僅是一個關鍵(對不起,垃圾郵件)。這是非常複雜的閱讀,很難與以後工作。 將字典中的值與現有值連接起來的可能性不大嗎? 任何幫助表示讚賞。