302. Decode sprite attributes

Decode a raw sprite attributes byte.

Lesson 302 of 356 · tests/chapter_09_sprite_rendering/test_302_decode_sprite_attributes.py

File to update

emulator/rendering/sprite_renderer.py

Why this step exists

The previous step defined SpriteAttributes. Now we convert the raw OAM attribute byte into that decoded structure.

Suggested implementation example

def decode_sprite_attributes(attributes: int) -> SpriteAttributes:
    attributes &= 0xFF

    return SpriteAttributes(
        palette_id=attributes & SPRITE_PALETTE_ID_MASK,
        is_behind_background=(attributes & SPRITE_IS_BEHIND_BACKGROUND) != 0,
        flip_horizontal=(attributes & SPRITE_FLIP_HORIZONTAL) != 0,
        flip_vertical=(attributes & SPRITE_FLIP_VERTICAL) != 0,
    )

Example

attributes = 0b1110_0011

Means

palette_id = 3
is_behind_background = True
flip_horizontal = True
flip_vertical = True

Bits 2-4

Ignored for now. Do not raise if they are set.

Out of scope

  • sprite palette RAM helper
  • rendering pixels
  • sprite 0 hit
  • sprite overflow
  • pygame

Run this lesson

uv run pytest tests/chapter_09_sprite_rendering/test_302_decode_sprite_attributes.py -v