python - Add object to existing value in dictionary -


in order make own tile based rpg, have question. have dictionary want, during creation of tile map, store pairs of tile type/ rect values e.g.

{1: rect(0, 0, 32, 32), 2: rect(0, 32, 32, 32)} 

my problem have give every key multiple values, since there limited number of tile types multiple tiles of each type. tried this:

def create(self):     x, y = 0, 0     row in self.matrix:         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 bit complicated since i'm not @ writing simple code. problem in if else statements. use them check whether 1 type of tile in dictionary collide_list has 1 or multiple rects assigned. if so, existing rect should added second value key. kind of works, prints out in 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 1 key (sorry spamming). complicated read , hard work later on. isn't there better possibility of concatenating value existing value in dictionary? appreciated.

you collecting existing tuple, creating new tuple existing tuple nested it:

dummytuple = collision_list[numbers[tiles[int(tile)]]] collision_list[numbers[tiles[int(tile)]]] = dummytuple, tiles[int(tile)].rect 

instead, append new one-element tuple:

dummytuple = collision_list[numbers[tiles[int(tile)]]] collision_list[numbers[tiles[int(tile)]]] = dummytuple + (tiles[int(tile)].rect,) 

you should leave one-element values rectangle, start 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, better still, use list, after altering 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 make collision_list collections.defaultdict object don't have test key first:

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) 

Comments

Popular posts from this blog

curl - PHP fsockopen help required -

HTTP/1.0 407 Proxy Authentication Required PHP -

c# - Resource not found error -