开发者

Class Inheritance, redefining __init__ on with taking a new argument

I'm new to Python, I still have issues with the semantics of class inheritance.

The following is the relevant class from the module games.py module that I am importing:

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)

and the following is from where I'm trying to call it in my own program:

self.scorebox = games.Text (value = self.s开发者_StackOverflow社区corevar,
                            pygame.font.Font(ardarlingopentype, 50),
                            color = color.white,
                            x = 550,
                            y = 50)

As you can see the syntax is wrong, but how do I go about fixing this such that I can inherit the class Text from my own program and make FONT an accessible argument that I can change?

Thanks.


Your problem is, that you you are ordering the arguments incorrectly: there are positional and keyword arguments. All keywords arguments must succeed the positional arguments.

This would work:

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


Not sure(note that you can't used not named arguments after named and/or mix them - you have used not named argument after 'value') but seems that you need to modify code the following way:

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)

And then:

import pygame
import games

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

OR:

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)


So guys I wrote to the developers of the Livewires package; and I was fortunate enough to receive a reply from one of them.

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.

class Text(Sprite):

                  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,
>                               fontfile=None):
>        self._size = size
>        self._color = color
>        self._value = value
>        self._font = pygame.font.Font(fontfile, self._size)
>        Sprite.__init__(self, self._create_surface(), angle,
>                        x, y,
>                        top, bottom, left, right,
>                        dx, dy,
>                        interval, is_collideable)

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜