GBTilemap

The GBTilemap class represents a GameBoy tilemap. It can map up to 32x32 tiles that are stored in a GBTileset (if not given, a new empty tileset is created.

Creating a new tilemap:

from img2gb import GBTilemap, GBTileset, GBTile

tileset = GBTileset()
tilemap = GBTilemap(
        width=32,
        height=32,
        gbtileset=tilset
        )

tile = GBTile()  # blank tile

tilemap.put_tile(0, 0, tile)  # The tile will be added in the tileset

Creating a tilemap from an image:

from img2gb import GBTilemap
from PIL import Image

image = Image.open("./my_tilemap.png")
tilemap = tilemap.from_image(image)
class img2gb.gbtilemap.GBTilemap(width=32, height=32, gbtileset=None)[source]

Stores and manipulates GameBoy tilemaps.

Parameters:
  • width (int) – With of the tilemap in tile (0-32, default = 32).
  • height (int) – Height of the tilemap in tile (0-32, default = 32).
  • gbtileset (GBTileset) – The tileset to use (a new empty one will be created if set to None, default = None).
data

Raw data of the tilemap.

Type:list of int
classmethod from_image(pil_image, gbtileset=None, missing='append', replace=0, dedup=True)[source]

Generates the tilemap from the given image. The tileset can also be generated at the same time.

Parameters:
  • pil_image (PIL.Image.Image) – The image that represents the tilemap.
  • gbtileset (GBTileset) – The tileset that contains the tiles used in the tilemap (a new empty one is created if not provided).
  • missing (str) –

    What to do if a tile is missing from the tileset:

    • "append" (default): append the tile to the tileset,
    • "error": raise an error,
    • "replace": relpace by an other tile (see the replace argument).
  • replace (int) – The id of the replacement tile when missing="replace".
  • dedup (bool) – Deduplicate tiles when missing="append" (default = True).
height

The height of the tilemap.

Type:int
put_tile(x, y, gbtile, missing='append', replace=0, dedup=True)[source]

Put a tile at the given position in the tilemap.

Parameters:
  • x (int) – The x coordinate where to put the tile in the tilemap.
  • y (int) – The y coordinate where to put the tile in the tilemap.
  • gbtile (GBTile) – The tile to put in the tilemap.
  • missing (str) –

    What to do if a tile is missing from the tileset:

    • "append" (default): append the tile to the tileset,
    • "error": raise an error,
    • "replace": relpace by an other tile (see the replace argument).
  • replace (int) – The id of the replacement tile when missing="replace".
  • dedup (bool) – Deduplicate tiles when missing="append" (default = True).
size

The with and height of the tilemap.

Type:tuple of two int (width, height)
tileset

The tileset that contains the tiles used by the tilemap.

Type:GBTileset
to_c_header_string(name='TILEMAP')[source]

Returns the C header (.h) code for the tilemap.

Parameters:name (str) – The name of the variable in the generated code (always converted to uppercase in the generated code, default = "TILEMAP")
Return type:str
to_c_string(name='TILEMAP')[source]

Returns C code that represents the tilemap.

Parameters:name (str) – The name of the variable in the generated code (always converted to uppercase in the generated code, default = "TILEMAP")
Return type:str
to_hex_string()[source]

Returns the tilemap as an hexadecimal-encoded string.

Return type:str

e.g. (4x4 tiles):

00 00 00 00
00 01 02 00
00 03 04 00
00 00 00 00
width

The width of the tilemap.

Type:int