【问题标题】:Changing font in a livewires message更改 livewires 消息中的字体
【发布时间】:2013-09-27 21:15:30
【问题描述】:

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

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

【问题讨论】:

    标签: python python-3.x fonts pygame livewires


    【解决方案1】:

    简答

    你不能。

    答案稍长

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

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

    更长的答案

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

    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_text 是从 LiveWires 中的 Message.init_message 函数调用的)。

    【讨论】:

    • 默认字体是什么?
    • 我已经更新了我的答案,包括一个关于获取默认字体的代码 sn-p。
    • 非常感谢您,您是真正的救生员!成功了,谢谢你。我投票赞成这个答案并将其作为我接受的答案! :)
    猜你喜欢
    • 2013-02-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-19
    • 2015-07-06
    • 1970-01-01
    相关资源
    最近更新 更多