Skip to content

Arcade.SpriteSolidColor

Code

class SpriteSolidColor(Sprite):
    """
    This sprite is just a rectangular sprite of one solid color. No need to
    use an image file.

    :param int width: Width of the sprite
    :param int height: Height of the sprite
    :param Color color: Color of the sprite
    """

    def __init__(self, width: int, height: int, color: Color):
        """
        Create a solid-color rectangular sprite.
        """
        super().__init__()

        cache_name = _build_cache_name("Solid", width, height, color[0], color[1], color[2])

        # use existing texture if it exists
        if cache_name in load_texture.texture_cache:  # type: ignore
            texture = load_texture.texture_cache[cache_name]  # type: ignore

        # otherwise, generate a filler sprite and add it to the cache
        else:
            image = PIL.Image.new("RGBA", (width, height), color)
            texture = Texture(cache_name, image)
            load_texture.texture_cache[cache_name] = texture  # type: ignore

        # apply chosen texture to the current sprite
        self.texture = texture
        self._points = texture.hit_box_points

See also