grid module¶
Generate random hexagonal cell grids.
Grids are composed of hexagonal Cells.
Groups of adjacent Cells are assigned to adjacent
Areas of random size and shape.
All Cell and Area references are indices into the
Grid.cells and Grid.areas tuples, respectively.
Grid instances are immutable and intended for
Game generation, but may be useful for other
hex-map based games as well. They provide coordinates for convenient
frontend map rendering.
-
class
dicewars.grid.Grid(grid_width=28, grid_height=32, max_num_areas=30, min_area_size=5)¶ Generate a grid and assign
Cells toAreas.- Parameters
grid_width (int) – number of cell columns
grid_height (int) – number of cell rows
max_num_areas (int) – maximal number of areas to create
min_area_size (int) – minimal number of cells per area
- Raises
TypeError – if a parameter is not int
ValueError – if a parameter is out of range
Note
The number of created areas is less than
max_num_areasif there are not enough cells left to assign.-
DEFAULT_GRID_WIDTH= 28¶ Default for the
grid_widthparameter. (int)
-
DEFAULT_GRID_HEIGHT= 32¶ Default for the
grid_heightparameter. (int)
-
DEFAULT_MAX_NUM_AREAS= 30¶ Default for the
max_num_areasparameter. (int)
-
DEFAULT_MIN_AREA_SIZE= 5¶ Default for the
min_area_sizeparameter. (int)
-
property
grid_size¶ The number of cell columns (
grid_width) and rows (grid_height). (tuple(int, int))
-
property
map_size¶ The size of the grid in pixels. (tuple(int, int))
For frontend map rendering.
map_sizeis the bounding box size of allCell.bboxes and may be used for proper map scaling.
-
dump()¶ Dump the grid (area indices) to the console.
-
class
dicewars.grid.Cell(idx, grid_x, grid_y, area, border, bbox)¶ A hexagonal cell - the basic building block of each
Grid. (namedtuple)Cell instances are created by
Gridand used forAreaassignment. They are not used by the game logic, but may be useful for frontends.-
idx: int¶ The cell’s index in
Grid.cells.
-
grid_x: int¶ The cell’s grid column.
-
grid_y: int¶ The cell’s grid row.
-
border: tuple(tuple(int, int))¶ For frontend map rendering. A 6-tuple of (x, y) pixel coordinates of the hexagonal cell polygon on the map (in counter-clockwise order, starting at the top center point).
-
bbox: tuple(tuple(int, int), tuple(int, int))¶ The upper left (
bbox[0]) and lower right (bbox[1]) (x, y) pixel coordinates of the cell’s bounding box on the map.
-
-
class
dicewars.grid.Area(idx, cells, neighbors, center, border, bbox)¶ A group of adjacent
Cells in aGrid. (namedtuple)Area instances are created by
Gridand used by the game logic. They may be useful for frontend map rendering as well.-
idx: int¶ The area’s index in
Grid.areas.
-
neighbors: tuple(int)¶ The indices of all areas adjacent to the area.
-
border: tuple(tuple(int, int))¶ For frontend map rendering. A tuple of (x, y) pixel coordinates of the area polygon on the map (outer
Celledges, in counter-clockwise order).
-
bbox: tuple(tuple(int, int), tuple(int, int))¶ The upper left (
bbox[0]) and lower right (bbox[1]) (x, y) pixel coordinates of the area’s bounding box on the map.
-