302. 解码精灵属性

解码一个原始精灵属性字节。

302 / 356 · tests/chapter_09_sprite_rendering/test_302_decode_sprite_attributes.py

需要更新的文件

emulator/rendering/sprite_renderer.py

为什么需要这一步

上一步定义了 SpriteAttributes。现在我们要把原始的 OAM 属性字节转换成这个解码后的结构体。

建议的实现示例

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,
    )

示例

attributes = 0b1110_0011

含义

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

第 2-4 位

目前忽略。即使它们被置位,也不要抛出异常。

本步骤不涉及的内容

  • 精灵调色板 RAM 辅助函数
  • 渲染像素
  • sprite 0 hit
  • sprite overflow
  • pygame

运行本课

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