[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: [pygame] Surface objects



On 15.07.2011 13:52, sam.hacking@xxxxxxxx wrote:
On Thu, 14 Jul 2011 13:57 -0700, "Michael Carius" <michael.a.carius@xxxxxxxxx> wrote:
# Python 2.x code
class MySurface(object):
    def __init__(self, obj):
        if isinstance(obj, tuple):
            self._surf = pygame.Surface(*tuple)
        elif isinstance(obj, pygame.Surface):
            self._surf = obj
    def __getattr__(self, attr):
        return getattr(self._surf, attr)
 
This looks the most promising, but I couldn't actually get it to work. What I want to do, is add extra attributes to a pygame.Surface object, such as pos and size, including _abs attributes that find the position relative to the screen. I'm using this in my GUI toolkit (which I talked about doing for GSoC earlier in the year). You can see the current surface.py module at http://bazaar.launchpad.net/~dreamsorcerer/simplegc/trunk/view/head:/sgc/surface.py
 
This works perfectly if I create a new surface by passing in a tuple, but I'm not sure how to get the same effect by passing in an existing pygame.Surface object.
 
Thanks,
Sam Bull


Hi

Peeking through your code I would suggest to use composition instead of inheritance. It is more flexible, but might look a bit more complex first. And personally I would think twice before overriding __getattr__.

You know, in the newer pygame version there is a copy method ( see: http://www.pygame.org/docs/ref/surface.html#Surface.copy).

If you still prefere to use inheritance and make an SurfaceObject, then maybe you could use the Surface.copy method and a metaclass to create a fancy SurfaceObject with the desired behavior.


~DR0ID