Tile

Technical overview. Tiles are the Game Boy’s basic graphics units: each tile is an 8 by 8 pixel pattern stored as 16 bytes in video RAM, with two bits per pixel selecting a palette entry. The original Game Boy provides 384 tile patterns, while Game Boy Color adds a second video-memory bank.

Using the API. Tile identifies a tile and its current video-memory location, then exposes the pixels as a Pillow image or a NumPy array. Tile objects are returned by PyBoy.get_tile, Sprite, and TileMap; their image data is read from emulated memory and can change after each tick.

The Game Boy uses tiles as the building block for all graphics on the screen. This base-class is used both for pyboy.api.sprite.Sprite and pyboy.api.tilemap.TileMap, when refering to graphics.

class pyboy.api.tile.Tile(mb, identifier)[source]

Bases: object

data_address

The tile data is defined in a specific area of the Game Boy. This function returns the address of the tile data corresponding to the tile identifier. It is advised to use pyboy.api.tile.Tile.image or one of the other image-functions if you want to view the tile.

You can read how the data is read in the Pan Docs: VRAM Tile Data.

Returns:

address in VRAM where tile data starts

Return type:

int

tile_identifier

The Game Boy has a slightly complicated indexing system for tiles. This identifier unifies the otherwise complicated indexing system on the Game Boy into a single range of 0-383 (both included) or 0-767 for Game Boy Color.

Returns:

Unique identifier for the tile

Return type:

int

shape

Tiles are always 8x8 pixels.

Returns:

The width and height of the tile.

Return type:

(int, int)

raw_buffer_format

Returns the color format of the raw screen buffer.

Returns:

Color format of the raw screen buffer. E.g. ‘RGBA’.

Return type:

str

image()[source]

Use this function to get a PIL.Image.Image object of the tile. The image is 8x8 pixels. The mode may change between versions, and Pillow must be installed.

Be aware, that the graphics for this tile can change between each call to pyboy.PyBoy.tick.

Example:

>>> tile = pyboy.get_tile(1)
>>> tile.image().save('tile_1.png')
Returns:

Image of tile in 8x8 pixels and RGBA colors.

Return type:

PIL.Image

ndarray()[source]

Use this function to get an numpy.ndarray object of the tile. The array has a shape of (8, 8, 4) and each value is of numpy.uint8. The values corresponds to an image of 8x8 pixels with each sub-color in a separate cell. The format is given by pyboy.api.tile.Tile.raw_buffer_format.

Be aware, that the graphics for this tile can change between each call to pyboy.PyBoy.tick.

Example:

>>> tile1 = pyboy.get_tile(1)
>>> tile1.ndarray()[:,:,0] # Upper part of "P"
array([[255, 255, 255, 255, 255, 255, 255, 255],
       [255, 255, 255, 255, 255, 255, 255, 255],
       [255, 255, 255, 255, 255, 255, 255, 255],
       [255,   0,   0,   0,   0,   0, 255, 255],
       [255,   0,   0,   0,   0,   0,   0, 255],
       [255,   0,   0, 255, 255,   0,   0, 255],
       [255,   0,   0, 255, 255,   0,   0, 255],
       [255,   0,   0, 255, 255,   0,   0, 255]], dtype=uint8)
>>> tile2 = pyboy.get_tile(2)
>>> tile2.ndarray()[:,:,0] # Lower part of "P"
array([[255,   0,   0,   0,   0,   0,   0, 255],
       [255,   0,   0,   0,   0,   0, 255, 255],
       [255,   0,   0, 255, 255, 255, 255, 255],
       [255,   0,   0, 255, 255, 255, 255, 255],
       [255,   0,   0, 255, 255, 255, 255, 255],
       [255,   0,   0, 255, 255, 255, 255, 255],
       [255,   0,   0, 255, 255, 255, 255, 255],
       [255, 255, 255, 255, 255, 255, 255, 255]], dtype=uint8)
Returns:

Array of shape (8, 8, 4) with data type of numpy.uint8.

Return type:

numpy.ndarray