更改实时线路消息中的字体



我正在开发一个游戏,当你输了它时,基本上说游戏结束了,但我想改变字体,我不知道怎么做,这是我关注的代码部分:

def end_game(self):
        """ End the game. """
        end_message = games.Message(value = "You didn't eat the pie, game over!",
                                    size = 70,
                                    color = color.red,
                                    x = games.screen.width/2,
                                    y = games.screen.height/2,
                                    lifetime = 7 * games.screen.fps,
                                    after_death = games.screen.quit)
        games.screen.add(end_message)

我想要的是基本上将字体更改为Calibri。

简答题

你不能。

稍长的答案

LiveWires旨在让您开始学习编程。在尝试这样做时,它将尝试为您简化事情。首先,像LiveWires这样的库很有帮助:它为你做了大部分工作,让你专注于学习编码(并制作一个有趣的游戏!

但是,在某些时候,这样一个复杂的系统会做你可能不希望它做的事情。在我们的例子中,LiveWires使所有文本都使用"默认"字体,并且它似乎没有任何方法可以更改它。

更长的答案

我能够通过查看LiveWires框架本身的源代码来确定这一点(它也是用Python编写的(。您正在使用的 LiveWires Message类继承自 LiveWires Text 类。您可以自己查看Text类代码;它应该在一个名为"games.py"的文件中的"livewires"目录中。我将在下面展示"Text"类的一些代码:

class Text(Object, ColourMixin):
    """
    A class for representing text on the screen.
    The reference point of a Text object is the centre of its
    bounding box.
    """
    def __init__(self, screen, x, y, text, size, colour, static=0):
        self.init_text (screen, x, y, text, size, colour, static)
    def init_text (self, screen, x, y, text, size, colour, static=0):
        """
        Arguments:
        screen -- the screen the object is on.
        x -- x-coordinate of centre of bounding box.
        y -- y-coordinate of centre of bounding box.
        text -- the text to display.
        size -- nominal height of the text, in pixels.
        colour -- the colour the text should be.
        """
        if not _have_font:
            raise GameError, "We don't have pygame.font, so can't create text objects"
        self._size = size
        self._colour = colour
        self._text = text
        self._font = pygame.font.Font(None, self._size)
        self._a = 0
        surface = self._create_surface()
        Object.__init__(self, screen, x, y, surface, x_offset=self._x_offset,
                        y_offset=self._y_offset, static=static)
        self.move_to(x,y)
    # There are more methods...

具体来说,我正在研究__init__方法的这一部分......

self._font = pygame.font.Font(None, self._size)
self._a = 0
surface = self._create_surface()
基本上,LiveWires

中的每个文本项都是使用pygame.font.Font对象构建的(pygame是LiveWires用来实际执行许多操作的实用程序(。LiveWires使用以特定方式创建Font对象(使用None作为第一个参数(来查找要使用的"默认"字体。您可以通过执行以下命令来确定它用作默认字体的内容...

import pygame
print pygame.font.get_default_font()
让字体在不同的

计算机上正常工作可能会变得棘手,因此让所有文本都使用"默认"字体对于 LiveWires 源代码的作者来说是一种合理的方法。

你应该注意,_font属性以下划线开头,这是 Python 中的一个约定,基本上是"不要玩这个!换句话说,创建 Message 对象然后尝试执行...

# Don't do this!
end_message._font = pygame.font.Font('/path/to/my/font/file', size)

虽然这会将 Message 类中的字体对象替换为不同的字体,但如果您多阅读代码,您会意识到文本已经以原始(默认(字体呈现到表面上;您将更改字体对象为时已晚。

也许有一个解决方案...

您可以尝试做的一件事是修改LiveWires代码。您可以将我上面显示的文本类中的行替换为...

self._font = pygame.font.Font("/path/to/my/font/file", self._size)

这应该从字体文件中加载不同的字体。请记住,这将更改游戏中所有文本的字体,因为所有文本都使用相同的文本类。

如果您打算更改LiveWires源代码

,请确保随身携带原始副本,以防您犯错并导致LiveWires源代码本身损坏。

在此解决方案的基础上构建

如果您能够完成前一个,那么也许您可以更进一步并修改 Text 类以允许您指定字体文件名。这意味着您需要更改 Text 构造函数(__init__ 方法(以及 init_text 方法。此外,调用这些方法的任何其他类(例如,从 LiveWired 中的 Message.init_message 函数调用init_text(。

最新更新