[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: [pygame] Sprite Collision



> Actually the correct syntax to address an element would be:
>
> passible_at[x][y]
>
> Not thinking in Python this morning ;^)

The original syntax - passable_at[x,y] - is something that I've used
before; use a dictionary keyed on location instead of nested arrays. This
can be more space efficient if you're only storing things that sparsely
populate your world. The dictionary lookup might even be faster, but you'd
have to profile that for your specific case to know for sure.

Of course, abstracting your game code from the underlying representation
gives you some flexibility to optimize later on:


g_passable_dict={}

def is_passable(x,y):
  return g_passable_dict.get((x,y),False)

def make_passable(x, y, isPassable):
  if isPassable:
    g_passable_dict[(x,y)]=True
  else:
    try:
      g_passable_dict.pop((x,y))
    except KeyError:
      # was already unpassable - up to you if this is OK
      pass


-----

However, if I was trying to handle more than a hundred or so objects, I'd
quickly switch over to a system that can do localized queries more
efficiently.

Keywords you might look into:
quadtrees
octtrees
k-d trees
spatial hashing


Hope this helps,
Dave LeCompte