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

[pygame] How to subclass from Surface?



Hello,

I am trying to subclass from surface but no matter what I do, I cant get it working. Here a example code snippet:

from pygame import Surface
from pygame.surfarray import *
from Numeric import *

class sprite(Surface):
def __init__(self, array, palette):
Surface.__init__(self, shape(array), 0, 8)
self.array = array
self.palette = palette


a = sprite(zeros((100,100)), ((255,255,0), ))

I get the following error:
a = sprite(zeros((100,100)), ((255,255,0), ))
TypeError: argument 1 must be sequence of length 2, not 100

It just looked like my constructor is ignored at all.

So maybe I have to override __new__, but I don't understand it very well so all my attempts with overriding __new__ actually failed because I have no Idea how to return a sprite instance to be used with the __init__ to initialize the instance. Returning a Surface instance was no problem there :-)

What is even more confusing to me is that http://mu.arete.cc/pcr/save/zoomscreen/1/zoomscreen.py is subclassing Surface but it actually doesn't work on my machine. I just put a print statement into __init__ of ZSurface but when I create an instance of it there is nothing printed.

So my simple quenstion is, how can I subclass Surface? :-)

I tried it on a Linux (Debian sarge) box with python 2.3.4 and pygame 1.6.2 and on a Windows XP box.

Bye
Alex