0

我正在开发一款游戏,当你输掉它时,它基本上意味着游戏结束,但我想更改字体,但我不知道如何,这是我关注的代码部分:

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

4

1 回答 1

1

简答

你不能。

稍微长一点的回答

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

然而,在某些时候,这样一个复杂的系统会做一些你可能不希望它做的事情。在我们的例子中,LiveWires 让所有文本都使用“默认”字体,而且它似乎没有提供任何改变它的方法。

更长的答案

我可以通过查看 LiveWires 框架本身的源代码来确定这一点(它也是用 Python 编写的)。Message您使用的 LiveWires 类继承自 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 代码。您可以将上面显示的 Text 类中的行替换为...

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

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

如果您计划更改 LiveWires 源代码,请确保将原始副本放在手边,以防万一您犯了错误并导致 LiveWires 源代码本身损坏。

基于此解决方案

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

于 2013-09-28T03:29:38.567 回答