类继承,使用新的argumen重新定义

2024-04-19 20:11:51 发布

您现在位置:Python中文网/ 问答频道 /正文

我是Python的新手,我仍然对类继承的语义有问题。在

以下是模块中的相关类游戏.py我要导入的模块:

class Text(Sprite): 
    """ 
    Alphanumeric values displayed on the screen.
    """      
    def __init__(self, value, size, color, angle=0, 
                 x=0, y=0,
                 top=None, bottom=None, left=None, right=None,
                 dx=0, dy=0,
                 interval=1, is_collideable=True):
        self._size = size 
        self._color = color 
        self._value = value
        self._font = pygame.font.Font(None, self._size)
        Sprite.__init__(self, self._create_surface(), angle,
                        x, y,
                        top, bottom, left, right,
                        dx, dy,
                        interval, is_collideable)

下面是我在自己的程序中尝试调用它的地方:

^{pr2}$

正如您所看到的,语法是错误的,但是如何解决这个问题,以便我可以从我自己的程序继承类文本,并使字体成为一个可以修改的可访问参数?在

谢谢。在


Tags: 模块selfrightnonesizeinitvaluetop
3条回答

不确定(请注意,您不能在命名后使用Not-named参数和/或将它们混合使用-您在'value'之后使用了Not-named参数'),但似乎需要按以下方式修改代码:

class Text(Sprite): 
    """ 
    Alphanumeric values displayed on the screen.
    """      
    def __init__(self, value, size, color, angle=0, 
                 x=0, y=0,
                 top=None, bottom=None, left=None, right=None, font=None,
                 dx=0, dy=0,
                 interval=1, is_collideable=True):
        self._size = size 
        self._color = color 
        self._value = value
        if font:
            self.font_ = font
        else:
            self._font = pygame.font.Font(None, self._size)
        Sprite.__init__(self, self._create_surface(), angle,
                        x, y,
                        top, bottom, left, right,
                        dx, dy,
                        interval, is_collideable)

然后:

^{pr2}$

或者:

import pygame
import games

self.scorebox = games.Text (value = self.scorevar,
                            size = 50, 
                            font = pygame.font.Font(ardarlingopentype, 50),
                            color = color.white,
                            x = 550,
                            y = 50)

所以伙计们,我写信给Livewires包的开发人员;我很幸运地收到了其中一个的回复。在

First, make a backup copy of games.py and put it somewhere safe. That way if you do make a mistake, you can always recover the original code.

Now our games.py is written on top of the PyGame library, which does provide a way of setting the font. As you might have guessed, it's to do with that line reading:

> self._font = pygame.font.Font(None, self._size)

The documentation is available online at http://www.pygame.org/docs/ref/font.html#pygame.font.Font but I'll just quickly summarise here. pygame.font.Font() creates a new PyGame font object, which PyGame uses to tell it how to draw text. The "None" parameter tells it to use the default font, but you can replace that with the full name of a font file instead. The easiest way to do that is to modify the Text classes initialiser to pass it as an optional parameter.

^{pr2}$

You would then create your Text object by calling 'Text(blah blah blah, fontfile="/some/font/file/name.ttf")' or whatever the filename is. Any other Text objects that don't specify a "fontfile" will automatically use "None" instead, which will give them the default font exactly as before.

So what's the fully-qualified pathname of the font file for "TimesNewRoman"? I have no idea what it would be on your computer. Fortunately PyGame provides a way of not having to know: pygame.font.match_font(). You can use that in your own program (rather than modifying games.py any more), but you will have to either "import pygame.font" for yourself or call it "games.pygame.font.match_font()" either should work equally well.

您的问题是,您对参数的排序不正确:有位置参数和关键字参数。所有关键字参数必须成功位置参数。在

这将起作用:

self.scorebox = games.Text (
                        pygame.font.Font(ardarlingopentype, 50),
                        value = self.scorevar,
                        color = color.white,
                        x = 550,
                        y = 50
)

相关问题 更多 >