Python 如何从精灵工作表为精灵指定名称?

Python 如何从精灵工作表为精灵指定名称?,python,python-3.x,pygame,sprite-sheet,Python,Python 3.x,Pygame,Sprite Sheet,我知道如何加载精灵工作表、剪辑图像并返回这些图像的列表,但我喜欢按名称而不是索引引用我的精灵,以保持代码可读性和可管理性,但有时写出精灵名称列表既冗长又乏味 我通常会创建一个字典,并通过名字引用我需要的精灵,但我想知道我是否应该硬编码这些名字 这是我通常做的: sprites=load_spritesheet('spr_buttons.png',(64,64)) 按钮名称=[“开始”、“开始悬停”、“开始单击”、“后退”、“后退悬停”, “后退单击”、“跳过”、“跳过悬停”、“跳过单击”、“退出

我知道如何加载精灵工作表、剪辑图像并返回这些图像的列表,但我喜欢按名称而不是索引引用我的精灵,以保持代码可读性和可管理性,但有时写出精灵名称列表既冗长又乏味

我通常会创建一个字典,并通过名字引用我需要的精灵,但我想知道我是否应该硬编码这些名字

这是我通常做的:

sprites=load_spritesheet('spr_buttons.png',(64,64))
按钮名称=[“开始”、“开始悬停”、“开始单击”、“后退”、“后退悬停”,
“后退单击”、“跳过”、“跳过悬停”、“跳过单击”、“退出”,
“退出悬停”、“退出单击”、“检查”、“检查悬停”,
'选中\单击','空白']
spr_按钮={}
对于索引,枚举中的精灵(精灵):
spr_按钮[按钮名称[索引]]=精灵
以这些按钮精灵为例,我还有一个按钮类,并为每个按钮创建一个新实例,传入精灵的名称

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

这是我的
NewButton
课程:

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
导入pygame
类纽扣:
“”“创建并维护新按钮。”“”`
定义初始值(自身、名称、精灵、x位置、y位置、大小):
“”“初始化一个新按钮实例。
:param name:字典中按钮的名称。
:param精灵:按钮精灵图像的字典。
:param x_pos:按钮图像的x位置。
:param y_pos:按钮图像的y位置。
:param size:按钮图像的平方大小。
"""
self.img_normal=精灵[名称]
self.img_hover=精灵[name+'_hover']
self.img_click=精灵[名称+'_click']
self.rect=pygame.rect(x位置,y位置,大小,大小)
self.image=self.img\u正常
self.hover=False
self.clicked=False
self.execute=False
self.max_定时器=20
self.click_timer=0
def更新(自我,增量):
“”“更新按钮实例。”。
:param delta:基于更新之间的实时性的乘数。
"""
如果单击self.click:
self.click_timer+=delta
如果self.click\u timer=self.max_timer:
self.clicked=False
self.click_timer=0
self.image=self.img\u正常
self.execute=True
其他:
self.image=self.img\u正常
其他:
mx,my=pygame.mouse.get_pos()
如果self.rect.collidepoint(mx,my):
self.image=self.img\u悬停
self.hover=True
其他:
self.image=self.img\u正常
self.hover=False
def单击(自我):
“”“将按钮设置为正在单击。”“”
self.clicked=True
def重置(自):
“”“重置按钮的执行状态。”“”
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

首先你要考虑的是,In没有被订购。无法保证在对精灵进行分组迭代时,精灵会按照相同的顺序进行枚举,因为它们是附加到组中的。
见以下文件:

组中的精灵没有顺序,因此绘制和迭代精灵没有特定的顺序


我建议定义命名按钮的:

从枚举导入枚举
类按钮(枚举):
开始=0
开始悬停=1
# [...]
创建一个字典,其中每个按钮都与枚举器关联:

spr_buttons=dict(zip(按钮,精灵))
因此,可以通过以下方式访问按钮:

sprite=spr_按钮[按钮.开始]
或者甚至通过使用和名称:

sprite=spr_按钮[getattr(按钮,“开始”)]

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

spr\u按钮=列表(精灵)
sprite=spr\u按钮[按钮.开始\u悬停.值]

您可以使用创建字典的函数以更简单的方式完成:

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

我认为把所有这些精灵的名字硬编码到自己的代码中不是一个好主意。在我看来,将按钮名称与精灵关联起来的字典会更好。我同意@martineau,这可能不是一个好主意;但是,可能会有一些用例,很容易实现,这就是op所要求的。我认为不用硬编码就可以做到非常简单,请参见我的。感谢@martineau,
zip()
函数确实缩短了将图像分配给名称的过程,但是,对于创建一个名单,然后分配给他们的整个过程,你认为这是一个方法,还是你知道一个更具蟒蛇风格的方法来完成我想做的事情?杰斯:我很难根据你的问题推荐任何东西。您提到过一些关于也有一个button类并使用sprite名称的内容,但是没有这样的例子。我也不能确定您是否应该硬编码名称,因为我不知道应用程序是什么,以及在其上下文中什么是有意义的。应用程序的GUI通常有固定数量的按钮,所以我不确定您想要完成什么。这是我正在制作的一个游戏。我的GUI中确实有固定数量的按钮,这些按钮列在我的
按钮名称
列表中。它们是我的标题屏幕、游戏设置和游戏屏幕的按钮。我感谢你的帮助