自分でタイルベースの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 else ステートメントにあります。これらを使用して、ディクショナリ内の 1 つのタイプのタイルにcollide_list
1 つまたは複数の四角形が既に割り当てられているかどうかを確認します。その場合、既存の rect を 2 番目の値としてキーに追加する必要があります。この種の作品は、シェルでこれを出力します:
(((((((((((((((((((((((((((<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)>)
これは 1 つのキーのみです (スパムで申し訳ありません)。これは読むのが非常に複雑で、後で作業するのが非常に困難です。値を辞書内の既存の値に連結するより良い可能性はありませんか? どんな助けでも大歓迎です。