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

Re: [pygame] y-Sorted RenderGroup



steve wrote:
>   I've developed a RenderUpdate group that sorts it's sprites on their y 
> position, useful for things like Japaneese style RPGs where sprites 
> closer to the bottom of the screen are drawn first, and sprites near the 
> top of the screen are drawn last.
> 
>   As you can see, I sort when I paint the sprites, because Python's 
> dictionaries return their items in random order, sorting the dictionary 
> would be pointless (you can't anyways).  After I sort, I paint the 
> sorted list of sprites instead of spritedict.items(), #3 below.  I use 
> my own sorting routine called y_sort(a, b).
> 
>   My question is, is creating an iternal list inside draw(), "y" in #1 
> below, the right way of doing this?  Since draw is called so frequently, 
> should I worry about garbage, and make this sorted list a class variable 
> (self.y)?  Is there a better way of doing this within the existing 
> Sprite classes, or am I on track here?

i believe your method of handling it pretty close to best. you'll 
probably have to do some kind of sorting every frame, since the 
sprites are always moving around.

one thing that might add a little speed is to change the sorting 
slightly. instead of adding all the sprites to a list then sorting 
them i'd add the items to the list in a sorted order. something like 
a priority queue is perfect for this. i believe there are some 
python modules for this lying around.
	
if anything change the y_sort function to this
	def y_sort(a,b):
		return cmp(a.rect.top, b.rect.top)

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