grid—abstract grid representations

class Grid(ntiles, tile_size, gap = 0)[source]

Bases: object

A representation of a 2D grid of rectangular integer-sized tiles.

Used for aligning mouse input, graphics, etc. on a grid.

Parameters:
  • ntiles(x, y) number of tiles in the grid, or a single number for a square grid.
  • tile_size(tile_width, tile_height) integers giving the size of every tile, or a single number for square tiles. tile_width and tile_height can also be functions that take the column/row index and return the width/height of that column/row respectively, or lists (or anything supporting indexing) that perform the same task.
  • gap(col_gap, row_gap) integers giving the gap between columns and rows respectively, or a single number for the same gap in both cases. As with tile_size, this can be a tuple of functions (or lists) which take the index of the preceding column/row and return the gap size.

col and row arguments to all methods may be negative to wrap from the end of the row/column, like list indices.

ntiles = None

The (x, y) number of tiles in the grid.

ncols

The number of tiles in a row.

nrows

The number of tiles in a column.

w

The total width of the grid.

h

The total height of the grid.

size

The total (width, height) size of the grid.

tile_x(col)[source]

Get the x position of the tile in the column with the given index.

This is the position of the left side of the tile relative to the left side of the grid.

tile_y(row)[source]

Get the y position of the tile in the row with the given index.

This is the position of the top side of the tile relative to the top side of the grid.

tile_pos(col, row)[source]

Get the (x, y) position of the tile in the given column and row.

This is the top-left corner of the tile relative to the top-left corner of the grid.

tile_size(col, row)[source]

Get the (width, height) size of the given tile.

tile_rect(col, row)[source]

Get a Pygame rect for the tile in the given column and row.

This is relative to the top-left corner of the grid.

tile_rects(pos=False)[source]

Iterator over tile_rect() for all tiles.

Parameters:pos – whether to yield (col, row, tile_rect) instead of just tile_rect.
tile_at(x, y)[source]

Return the (col, row) tile at the point (x, y), or None.

align(self, graphic, col, row, alignment=0, pad=0, offset=0) → aligned_rect[source]

Align a graphic or surface within a tile.

alignment, pad and offset are as taken by align_rect.

Parameters:
  • graphic – a gfx.Graphic instance or a Pygame surface. In the former case, the graphic is moved (but it is not cropped to fit in the tile).
  • col – column of the tile.
  • row – row of the tile.
Returns:

a Pygame rect clipped within the tile giving the area the graphic should be put in.

class InfiniteGrid(tile_size, gap=0)[source]

Bases: object

A representation of an infinite 2D grid of rectangular tiles.

Parameters:
  • tile_size(tile_width, tile_height) numbers giving the size of every tile, or a single number for square tiles.
  • gap(col_gap, row_gap) numbers giving the gap between columns and rows respectively, or a single number for the same gap in both cases.

The grid expands in all directions, so col and row arguments to methods may be negative, and tile/gap sizes may be floats.

tile_size = None

tile_size as taken by the constructor.

gap = None

gap as taken by the constructor.

tile_x(col)[source]

Get the x position of the tile in the column with the given index.

This is the position of the left side of the tile relative to the left side of column 0.

tile_y(row)[source]

Get the y position of the tile in the row with the given index.

This is the position of the top side of the tile relative to the top side of row 0.

tile_pos(col, row)[source]

Get the (x, y) position of the tile in the given column and row.

This is the top-left corner of the tile relative to the top-left corner of the tile (0, 0).

tile_rect(col, row)[source]

Get a Pygame-style rect for the tile in the given column and row.

This is relative to tile (0, 0), and elements can be floats.

tile_rects(rect, pos=False)[source]

Iterator over tile_rect() for tiles that intersect rect.

Parameters:
  • rect(x, y, w, h) with elements possibly floats.
  • pos – whether to yield (col, row, tile_rect) instead of just tile_rect.
tile_at(x, y)[source]

Return the (col, row) tile at the point (x, y), or None.

Returns None within gaps between tiles.

align(self, graphic, col, row, alignment=0, pad=0, offset=0) → aligned_rect[source]

Align a graphic or surface within a tile.

alignment, pad and offset are as taken by align_rect.

Parameters:
  • graphic – a gfx.Graphic instance or a Pygame surface. In the former case, the graphic is moved (but it is not cropped to fit in the tile).
  • col – column of the tile.
  • row – row of the tile.
Returns:

a Pygame rect clipped within the tile giving the area the graphic should be put in.