我应该如何为精灵 sheet 中的精灵分配名称?

How should I be assigning names to sprites from a sprite sheet?

我知道如何加载 sprite sheet、剪辑图像和 return 这些图像的列表,但我喜欢按名称而不是索引来引用我的 sprite,以保持代码可读和可管理,但有时写出精灵名称列表又长又乏味。

我通常创建一个字典,并通过名称引用我需要的精灵,但我想知道是否应该对名称进行硬编码?

这是我通常做的:

sprites = load_spritesheet('spr_buttons.png', (64, 64))
BUTTON_NAMES = ['start', 'start_hover', 'start_click', 'back', 'back_hover',
                'back_click', 'skip', 'skip_hover', 'skip_click', 'exit',
                'exit_hover', 'exit_click', 'check', 'check_hover',
                'check_click', 'blank']
spr_buttons = {}
for index, sprite in enumerate(sprites):
    spr_buttons[BUTTON_NAMES[index]] = sprite

以这些按钮精灵为例,我也有一个按钮class并为每个按钮创建一个新实例,传入精灵的名称。

这很好用,但我想找到更好的方法。我是否创建一个 .txt 文件来伴随我的 sprite sheets 并从中获取名称?或者我是否使用分配给每个精灵的关键字 + 索引号(例如 button_1button_2 等)?

这是我的 NewButton class:

import pygame


class NewButton:
    """Create and maintain a new button."""`

    def __init__(self, name, sprites, x_pos, y_pos, size):
        """Initialise a new button instance.

        :param name:    The name of the button from the dictionary.
        :param sprites: A dictionary of button sprite images.
        :param x_pos:   The x position of the button image.
        :param y_pos:   The y position of the button image.
        :param size:    The squared size of the button image.
        """
        self.img_normal = sprites[name]
        self.img_hover = sprites[name + '_hover']
        self.img_click = sprites[name + '_click']
        self.rect = pygame.Rect(x_pos, y_pos, size, size)
        self.image = self.img_normal
        self.hover = False
        self.clicked = False
        self.execute = False
        self.max_timer = 20
        self.click_timer = 0

    def update(self, delta):
        """Update the button instance.

        :param delta: A multiplier based on real time between updates.
        """
        if self.clicked:
            self.click_timer += delta
            if self.click_timer < self.max_timer / 2:
                self.image = self.img_click
            elif self.click_timer >= self.max_timer:
                self.clicked = False
                self.click_timer = 0
                self.image = self.img_normal
                self.execute = True
            else:
                self.image = self.img_normal
        else:
            mx, my = pygame.mouse.get_pos()
            if self.rect.collidepoint(mx, my):
                self.image = self.img_hover
                self.hover = True
            else:
                self.image = self.img_normal
                self.hover = False

    def click(self):
        """Set the button as being clicked."""
        self.clicked = True

    def reset(self):
        """Reset the execute status of the button."""
        self.execute = False

您可以使用精灵的序列解包将它们分配给您希望使用的名称:

sprites = load_spritesheet('spr_buttons.png', (64, 64))

start, start_hover, start_click, back, back_hover, back_click, \
skip, skip_hover, skip_click, exit, exit_hover, exit_click, \
check, check_hover, check_click, blank = sprites

首先你要考虑的是,pygame.sprite.Sprite in pygame.sprite.Group 没有排序。不能保证精灵在迭代分组时以相同的顺序被枚举,因为它们被追加到组中。
请参阅 pygame.sprite.Group 的文档:

The Sprites in the Group are not ordered, so drawing and iterating the Sprites is in no particular order.


我建议定义 enums 来命名按钮:

from enum import Enum
class Button(Enum):
    start = 0
    start_hover = 1
    # [...]

创建一个字典,其中每个按钮都关联到一个枚举器:

spr_buttons = dict(zip(Button, sprites))

因此可以通过以下方式访问按钮:

sprite = spr_buttons[Button.start] 

甚至通过使用 getattr 和名称:

sprite = spr_buttons[getattr(Button, "start")]

或者,您可以创建按钮列表并通过枚举器的 .value 属性 进行访问,它是列表的索引:

spr_buttons = list(sprites)
sprite = spr_buttons[Button.start_hover.value]

您可以使用 zip() 函数以更简单的方式创建字典:

sprites = load_spritesheet('spr_buttons.png', (64, 64))

BUTTON_NAMES = ['start', 'start_hover', 'start_click',
                'back', 'back_hover', 'back_click',
                'skip', 'skip_hover', 'skip_click',
                'exit', 'exit_hover', 'exit_click',
                'check', 'check_hover', 'check_click',
                'blank']

spr_buttons = dict(zip(BUTTON_NAMES, sprites))