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

Re: [pygame] Cross-References



On Sun, 2006-05-21 at 15:22 -0400, Kris Schnee wrote:
> That problem of creating a Pong demo really did get me thinking about 
> the possible ways of organizing data and drawing objects. I've been used 
> to giving objects lots of cross-references, eg.:

My preferred style is to handle all object interactions at higher "game
state" level. I try to keep my game objects from keeping references to
other game objects. 

The main reason is because of object lifetime management. In a game like
pong there really aren't that many objects being created and destroyed.
In most games you will have temporary objects coming and going. Once
objects start weaving a web of references to one another it gets very
hard to unravel.

If it is easier to place code that deals with other objects on my game
object classes, I try to write them so that they take the other objects
as arguments to the methods.

But there are times when you want to store hierarchies of objects. At
this point you need some way for objects to manage other objects.

The pygame sprite system can help a little with this. Each object could
have Group containers to other sprites. The classes system makes sure
when you call Sprite.kill() that it is removed from all groups.