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
#1
I have a dictionary of tile sprites, (x,y) are the keys. I think I have found how to determine the tiles local to the player, but I get a KeyError when those tiles don't exist.

Is it best practice to use an Exception to pass, or is there some better way avoid the error?

def find_local(self):
        local = []
        pos = (self.rect.centerx // self.game.level.tilesize, self.rect.centery // self.game.level.tilesize)
        
        for x in range(pos[0] - 3, pos[0] + 3):
            for y in range(pos[1] - 3, pos[1] + 3):
                local.append(self.game.level.tile_dict[x, y])
        


The hope is that I can use this local list to prevent the player and bullets from checking collisions for tiles that are a million miles away.

Thanks for help.
Reply


Messages In This Thread
finding local area in dictionary - by XavierPlatinum - Sep-01-2022, 11:07 PM
RE: finding local area in dictionary - by Windspar - Sep-02-2022, 03:10 AM

Forum Jump:

User Panel Messages

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