Python 更改livewires消息中的字体

Python 更改livewires消息中的字体,python,python-3.x,fonts,pygame,livewires,Python,Python 3.x,Fonts,Pygame,Livewires,我正在做一个游戏,当你输了的时候,游戏基本上结束了,但我想改变字体,我不知道怎么做,下面是我关注的代码部分: def end_game(self): """ End the game. """ end_message = games.Message(value = "You didn't eat the pie, game over!", size = 70,

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

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
类。您可以自己查看
文本
类代码;它应该位于名为“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...
具体地说,我正在研究
\uuuuu init\uuuu
方法的这一部分

self._font = pygame.font.Font(None, self._size)
self._a = 0
surface = self._create_surface()
基本上,LiveWires中的每个文本项都是使用一个对象构建的(pygame是LiveWires用来实际完成很多工作的实用工具)。LiveWires使用特定方式创建字体对象(使用
None
作为第一个参数)来查找要使用的“默认”字体。您可以通过执行以下操作来确定它使用的默认字体

import pygame
print pygame.font.get_default_font()
让字体在不同的计算机上正常工作可能会很棘手,所以让所有文本使用“默认”字体对于LiveWires源代码的作者来说是一种合理的方法

您应该注意,
\u font
属性以下划线开头,这是Python中的一个惯例,基本上是说“不要玩这个!”换句话说,创建消息对象然后尝试执行以下操作是个坏主意

# Don't do this!
end_message._font = pygame.font.Font('/path/to/my/font/file', size)
虽然这将用不同的字体替换Message类中的font对象,但如果您再多读一点代码,就会发现文本已经以原始(默认)字体呈现在曲面上;更改字体对象可能太晚了

也许有一个解决办法。。。 您可以尝试做的一件事是修改LiveWires代码。您可以将我上面显示的文本类中的行替换为

self._font = pygame.font.Font("/path/to/my/font/file", self._size)
这将从字体文件加载不同的字体。请记住,这将更改游戏中所有文本的字体,因为所有文本使用相同的文本类

如果您计划更改LiveWires源代码,请确保随身携带原始代码的副本,以防出错并导致LiveWires源代码本身中断

基于此解决方案
如果您能够实现上一个功能,那么也许您可以进一步修改Text类,以允许您指定字体文件名。这意味着您需要更改文本构造函数(
\uuuu init\uuu
方法)以及
init\u Text
方法。另外,调用这些方法的任何其他类(例如,
init_text
是从LiveWires中的
Message.init_Message
函数调用的)。

默认字体是什么?我已经更新了我的答案,在获得默认字体时包含了一个代码片段。多谢了,伙计,你真是个救命恩人!多亏了你,它成功了。我对这个答案投了赞成票,并把它作为我接受的答案!:)