Developers' Diary

Paraplu: Ordered Layers!

Whoa, I just completed the ordered layer drawing system. Before, all Guys on a particular layer were drawn in whatever dang order the game pleased. This was fine in Soft Landing, because it had a purely side-on view and it was unlikely to matter which of any given pair of peers on a layer draws in front of the other. But Paraplu has a kind of hybrid top-down and side-on view, like A Link to the Past. So when two Guys overlap, it’s important for the one that’s higher up on the screen to appear behind the one that’s lower down.

Python made this pretty easy with its custom comparison functions for sorting. These ordered layers just use this loop for drawing:

for oneGuy in sorted(layer.sprites(), cmpY):

Instead of this one:

for oneGuy in layer.sprites():

And the cmpY function is just this:

return -cmp(a.rect.bottom, b.rect.bottom)

Super simple! Thanks, Python and Pygame!

2 responses to 'Paraplu: Ordered Layers!'.

Comments and pings are currently closed for 'Paraplu: Ordered Layers!'.
  1. 1 renpytom
    Posted on August 12th, 2008 at 13:03. About 'Paraplu: Ordered Layers!'.

    It would be moderately faster to do:

    for oneGuy in sorted(layer.sprites(), key=lambda a : -a.rect.bottom):

    using your comparison function requires two function calls per comparison, and up to n lg n comparisons, depending on how ordered layer.sprites() is. If you use a key function, it’s called once per item… and since it doesn’t need cmp, you save a function call as well.

    Of course, this is probably unimportant, unless you have a large amount of stuff on a single layer.

  2. 2 fetjuel
    Posted on August 12th, 2008 at 15:40. About 'Paraplu: Ordered Layers!'.

    Wow, thanks RenPyTom! Good to see you, by the way. :D