[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[pygame] subclassable Rect and Surface



Tinkering with pygame, i've just checked code in that allows you to 
subclass the pygame Rect and Surface objects as python classes (this of 
course requires python-2.2)

import pygame
class MySurface(pygame.Surface):
     def erase(self):
         self.fill(0)

ok, this example may not be wildly useful, but it shows what's possible.

keep in mind that this is a 99.4% backwards compatable change. the big 
difference is that in python-2.2 "Surface" is really SurfaceType. For 
previous versions of python, "Surface" remains a function. kind of 
magic, and i can't see how the change would break anyone's code.

unfortunately i'm not quite sure how useful this really is. it would be 
nice if you could perhaps add drawing functions or something to a 
Surface class. the problem is, any Surface returned from the Pygame code 
is going to be a real Surface object, and not any super class you've 
subclassed from Surface.

since you can't "override" the type of an existing object, it is 
therefore impossible for something like the "display" surface to be 
changed into some other class with your handy draw functions.

because of this i'm not sure how useful it really is. for now i'm just 
going to leave it at Surface and Rect. there are plenty of pygame 
objects that could be converted too, maybe i'll get to them. in the 
meantime i'll be interested to see if anyone can really put this sort of 
thing to use.

here's a quick list of the remaining Pygame object types, if anyone 
would really like to see some of these converted to subclassable 
objects, let me know:
   CD, VidInfo, Event, Font, Joystick, Sound, Channel, Movie

hmm, actually "Event" could potentially be useful, but i'm afraid i'd 
have to really rework the internal Event passing mechanism to allow the 
subclassed objects to get sent through. (ugh)

anyways, it's kind of fun to play with. if anyone has any inspirations 
for this, do share!

____________________________________
pygame mailing list
pygame-users@seul.org
http://pygame.seul.org