301. Sprite attribute definition

Define the sprite attribute data model.

Lesson 301 of 356 · tests/chapter_09_sprite_rendering/test_301_sprite_attribute_definition.py

File to update

emulator/rendering/sprite_renderer.py

Why this step exists

Each SpriteEntry contains one raw attributes byte. Before rendering sprites, we need names for the bits that matter and a small dataclass for the decoded meaning.

NES sprite attribute byte

bits 0-1: sprite palette ID
bits 2-4: unused / ignored for now
bit 5: priority behind background
bit 6: horizontal flip
bit 7: vertical flip

Suggested implementation example

SPRITE_PALETTE_ID_MASK = 0b0000_0011
SPRITE_IS_BEHIND_BACKGROUND = 1 << 5
SPRITE_FLIP_HORIZONTAL = 1 << 6
SPRITE_FLIP_VERTICAL = 1 << 7


@dataclass(frozen=True)
class SpriteAttributes:
    palette_id: int
    is_behind_background: bool
    flip_horizontal: bool
    flip_vertical: bool

Common misconception

"Sprite attributes are the same as background attribute table bytes."

No. Background attribute table bytes select background palettes for tile regions. Sprite attributes are per-sprite and live in OAM byte 2.

Out of scope

  • decode_sprite_attributes()
  • sprite palette RAM helper
  • rendering pixels
  • sprite 0 hit
  • sprite overflow
  • pygame

Run this lesson

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