Arcade.SpriteSolidColor
lastmod 2026-11-10created 2022-03-032 edits
SpriteSolidColor는 Python 게임 라이브러리 Arcade 에서 이미지 파일 없이 단색으로 채워진 직사각형 스프라이트를 만드는 Sprite 의 하위 클래스이다. 너비, 높이, 색상만 넘기면 내부에서 PIL 로 해당 크기의 이미지를 생성해 텍스처로 쓰며, 같은 조건의 텍스처는 캐시에서 재사용한다. 충돌 영역이 사각형 그대로 잡히므로 벽이나 바닥, 자리 표시용 사각형을 빠르게 만드는 데 쓴다.
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