Python Forum
[PyGame] finding local area in dictionary
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[PyGame] finding local area in dictionary
#4
You could use dict.get(key, value). This returns a special value (default is None) when the dictionary lookup fails. Here I make a list of all possible near tiles, then remove all missing tiles.
def find_local(self):
    x = self.rect.centerx // self.game.level.tilesize
    y = self.rect.centery // self.game.level.tilesize
    tiles = self.game.level.tile_dict
    return [t for t in [tiles.get((i, j)) for i in range(x-3, x+3) for j in range(y-3, y+3)] if t]
Reply


Messages In This Thread
RE: finding local area in dictionary - by Windspar - Sep-02-2022, 03:10 AM
RE: finding local area in dictionary - by deanhystad - Sep-02-2022, 03:15 AM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020