[pygame] Extending Surface Class
Is it possible to extend the surface class? Every time I do the surface ends up dieing somehow right after it is created... I may just be doing it in the wrong way, though. The main point is to override the __str__() method, so if you know another way besides extending pygame.Surface, I'd be happy to hear it. class ExtendedSurface(pygame.Surface): def __init__(self, surface, string): pygame.Surface.__init__(surface, surface.get_size()) #The docs say to include a size, etc. but that just gives me too many values to unpack errors. self.string = string def __str__(self): return self.string Thanks!
Re: [pygame] Extending Surface Class
Hi I would write a wrapper instead of inheriting from Surface because Surface is a C object. class MySurface(object): def __init__(self): self._sruf = Surface() I know, you would have to wrap all methods, but maybe this could be done using setattr. ~DR0ID ERName schrieb: Is it possible to extend the surface class? Every time I do the surface ends up dieing somehow right after it is created... I may just be doing it in the wrong way, though. The main point is to override the __str__() method, so if you know another way besides extending pygame.Surface, I'd be happy to hear it. class ExtendedSurface(pygame.Surface): def __init__(self, surface, string): pygame.Surface.__init__(surface, surface.get_size()) #The docs say to include a size, etc. but that just gives me too many values to unpack errors. self.string = string def __str__(self): return self.string Thanks!
[pygame] Re: Extending Surface Class
Well, I appreciate your reply, but I don't really understand what you mean by wrapper... and this mysterious _sruf attribute? The part about being a C object I understand, though, and I was afraid of that. If there's not any easy way of doing this, I can just live with the default __str__(), I guess.
Re: [pygame] Re: Extending Surface Class
By wrapper, he means writing a python class that has an internal member, _sruf, which is the surface in question. Then you can override the __str__ function with your own. Otherwise, you just call _sruf's own methods and attrs -Tyler On Sat, Jul 25, 2009 at 2:48 PM, ERName easily.remembered.n...@gmail.comwrote: Well, I appreciate your reply, but I don't really understand what you mean by wrapper... and this mysterious _sruf attribute? The part about being a C object I understand, though, and I was afraid of that. If there's not any easy way of doing this, I can just live with the default __str__(), I guess. -- Visit my blog at http://oddco.ca/zeroth/zblog
Re: [pygame] Extending Surface Class
You have to call pygame.Surface.__init__ with your surface object first (self), then pass further arguments like size: class ExtendedSurface(pygame.Surface): def __init__(self, string): pygame.Surface.__init__(self, (100, 100)) self.fill((220,22,22)) # ... ERName wrote: Is it possible to extend the surface class? Every time I do the surface ends up dieing somehow right after it is created... I may just be doing it in the wrong way, though. The main point is to override the __str__() method, so if you know another way besides extending pygame.Surface, I'd be happy to hear it. class ExtendedSurface(pygame.Surface): def __init__(self, surface, string): pygame.Surface.__init__(surface, surface.get_size()) #The docs say to include a size, etc. but that just gives me too many values to unpack errors. self.string = string def __str__(self): return self.string Thanks!
[pygame] Re: Extending Surface Class
Oh thanks for the translation :) Well, the whole point was to make the process automatic, and my program not even know that the class was different, so a wrapper attribute kind of defeats the purpose. It's alright, though, I can just live with the standard Surface __str__(). No biggie. Thanks for the help, though, guys!