我正在尝试制作5个屏幕来显示关于一个团队的信息,并且需要在每个屏幕上打印一些文本。我怎样才能避免所有这些线路
fonte = pygame.font.SysFont('Times New Roman', 20)
nomeEquipe = fonte.render("Nome da Equipe", True, preto)
nomeDavid = fonte.render("David Waters Teixeira Rodrigues", True, preto)
nomeRafael = fonte.render("Rafael Pereira de Souza", True, preto)
nomeVicente = fonte.render("Vicente de Paulo Vidal Alencar", True, preto)
nomeVictor = fonte.render("Victor Jerrysson Gama Bastos", True, preto)
nomeWillian = fonte.render("Willian Alves Batista", True, preto)
funcaoDavid = fonte.render("Função: menu inicial", True, preto)
funcaoRafael = fonte.render("Função: tela da equipe", True, preto)
funcaoVicente = fonte.render("Função: tela sobre", True, preto)
funcaoVictor = fonte.render("Função: jogo", True, preto)
funcaoWillian = fonte.render("Função: jogo", True, preto)
我试图实现这一点:
class Fonte:
def __init__(self, fonte, tamanho):
pygame.font.SysFont.__init__(self)
self.fonte = fonte
self.tamanho = tamanho
class Tela:
def __init__(self, texto, antialias, cor):
Fonte.render.__init__(self)
self.texto = texto
self.antialias = antialias
self.cor = cor
fonte = Fonte('Times New Roman', 20)
nomeEquipe = Tela("Nome da Equipe", True, preto)
但我收到了这个错误:
Traceback (most recent call last):
File "/home/rafael/teste.py", line 55, in <module>
nomeEquipe = Tela("Nome da Equipe", True, preto)
File "/home/rafael/teste.py", line 45, in __init__
Fonte.render.__init__(self)
AttributeError: type object 'Fonte' has no attribute 'render'
首先是眼前的错误
render
似乎是pygame.font.SysFont
类的一个方法,但您试图在没有定义它的Fonte
类(类型本身,而不是实例!)上调用它,因此Python不知道该做什么,并且会抱怨。要使类执行编写时可能意味着的操作,您应该继承SysFont
(以及它的render
定义),或者自己定义一个合适的render
方法,然后创建一个实例并调用该方法除此之外,您的
Tela
类尝试在方法上调用__init__
,这毫无意义,同时还向该方法传递Tela
的实例。那条线乱七八糟下面是一个固定的版本,它应该与您的原始代码相同,但是使用类,这在某种程度上是出于给您带来错误的精神。我不认为这是一个好主意,事实上,鉴于现有的情况,我认为这是不合适的
免责声明:我没有用PyGame测试这一点,我只是用玩具类旋转解释器来检查
super().__init__(...)
调用。这应该与您的“before”代码具有相同的行为,我假设您对此感到满意,并且做了一些有用的事情现在,从更实际的角度来看,请注意新类并没有减少我们必须编写的内容!我们本来可以在第一时间使用
SysFont
,就像在原始版本中一样,并节省了代码和精力了解您可能想要做的事情,即获取“before”代码的行为,而不需要上千行
您的案例看起来很像您想要一系列包含相当同质调用结果的变量。如果坚持使用单独的变量,无论你怎么说,这都是冗长的,但是如果你接受只有一个变量是某种集合的事实,比如
list
或dict
,这就很容易缩小:最后一个
for
循环是一个列表理解的教科书案例,但我发现初学者更容易找到好的旧循环我建议使用^{} 表达式:
但是,我建议将文本Surfaces保存在列表中。定义字符串列表并使用list comprehensions创建曲面列表:
可以通过订阅(
surf_list[0]
,surf_list[1], ...
)访问文本Surfces如果要为字体使用类,请执行以下操作:
可以将
SysFont
的单个实例作为类变量:但是,对于不同的文本,仍然需要所有这些行
但是,如果可以默认最后两个参数:
相关问题 更多 >
编程相关推荐