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

[pygame] Cross-References



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.:

class SubPart:
    def __init__(self,**options):
        self.foo = 42
        self.parent = options.get("parent")

class BiggerThing:
    def __init__(self,**options):
        self.subpart = SubPart(parent=self)


Is this a good coding practice? If not, why not? Does it make sense to give drawable objects references to the screen, so that I can tell them to go blit themselves instead of having a manager object do so?


Also, when I last worked on my main project I'd organized it in a modular way:
-AI
-AI subsystems
-Character bodies
-Game world ("worldsim","Aquablue")
-Graphics system/tile engine ("Coral")
-Main interface


I ran the interface, which created a game world and Coral engine, then created character "bodies" run by AIs. The characters got placed in the game world, but also had references to a sprite which was placed in the Coral system. That created a complicated system to keep track of everything:

-Main
  -Aquablue
    -Characters
      -Niss (a character)
        -AI
        -Personal stats
        -Sprite
    -Tile-based world
      -Niss (a reference located in a specific tile)
  -Coral
    -Tile stuff
    -Sprites
      -Niss.sprite

Is this overkill, or is it all necessary? I'd like to have the game world independent of the graphics engine, so that if I ever make the leap to 3D the world will still work. (I also want the AI independent of having a tile-based 2D world.) But this system creates a whole lot of cross-references so that I can, eg:
-Look up a character by name/ID
-See the contents of a specific tile without asking every entity in the world "are you here?"
-Move a character within the game world
-Tell the graphics engine to move the character on screen


Re: a bit of code I haven't done, it seems like there has to be coordination between Aquablue and Coral to make sure the graphics system gets notified not just when a character moves but what animation they should start playing, and I'm not sure whether the sprite system should be explicitly told "play animation walk_north" or what.

Kris