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

Re: [pygame] different behaviour of sprites (in group) with python 2.6 and 3.1 -- silly question?



Hi,

Perhaps the dictionary algorithm changed in Python 3.1. Anyway, regular dictionaries are unordered.

Lenard Lindstrom

On 22/07/10 05:47 AM, Gregor Lingl wrote:
After having written my previous posting I had the idea
that this strange behaviour could have something to do
with the (random) access of items in a dictionary ...

So I examined the sprite module - and indeed, the
Group class stores its sprites in a dictionary.

I  changed line 269 as follows:

       self.spritedict = collections.OrderedDict()  # {}

and in fact now the colored_balls as well as the mailpilot
work correctly. Of course I didn't test the consequences
of this changes extensively, but the docs say that it is safe
to replace an ordinary dict by an ordered one (except
that possibly a program is slowed down if it uses lots of
sprites.)

Additionally I found a remark in Andy Harris' fine book
concerning the ordering of sprite updates (p.301)
where he recommends (for instance) to use
the OrderedUpdates group. (This maintains ordering
via a list of keys.)

Apparently I was fooled by the, I suppose accidentally,
consistent behaviour of these programs with Python 2.6.

Regards,
Gregor