Package pajammin :: Package util :: Module grid :: Class ProximityGrid
[show private | hide private]
[frames | no frames]

Class ProximityGrid


A container class that organizes objects by their location, based on an attribute whose value is a pygame rect. Each ProximityGrid divides the world into a grid of fixed-sized tiles, and records which tiles each contained object's rect attribute intersects with.

In order to keep the proximity grid up-to-date, it's update method must be called on an object whenever that object's rect-valued attribute is modified.

ProximityGrids are useful because they let us quickly look up all objects that are close to a given point or rect. For more details, see the documentation for the near_point() and near_rect() methods.
Method Summary
  __init__(self, tile_width, tile_height, rect_attr)
Create a new, empty, ProximityGrid.
  __iter__(self)
Return a list of all objects in the ProximityGrid
  add(self, object)
Add a new object to the ProximityGrid.
  near_point(self, x, y)
  near_rect(self, rect)
  remove(self, object)
Remove an object from the ProximityGrid.
  update(self, object)

Method Details

__init__(self, tile_width, tile_height, rect_attr='rect')
(Constructor)

Create a new, empty, ProximityGrid.
Parameters:
tile_width, tile_height - The width and height of the tiles used to organize objects.
rect_attr - The name of the rect-valued attribute that is used to organize the objects.

__iter__(self)

Return a list of all objects in the ProximityGrid

add(self, object)

Add a new object to the ProximityGrid. object must define the rect-valued attribute specified in the ProximityGrid constructor to be a pygame rect.

near_point(self, x, y)

near_rect(self, rect)

remove(self, object)

Remove an object from the ProximityGrid.

update(self, object)