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

Re: [pygame] why is sometimes so much time between 2 frames ? (400 milliseconds instead of 30)



Yes, your collision checking is wasteful and garbage is piling up. :)

You can see easiest this if you simply "import gc" and then "gc.collect()" at the end of your main loop. As you make more penguins you will notice your green meter grows a lot. It was killing my dual-core at around 20 penguins.

In a slapdash attempt to improve, I did the following in your main loop:

    collisions = pygame.sprite.groupcollide(birdgroup, birdgroup, False, False)
    for bird in collisions.keys():
        for crashbird in collisions[bird]:
            if bird is not crashbird:
                bird.crashing = True
                bird.dx -= crashbird.pos[0] - bird.pos[0]
                bird.dy -= crashbird.pos[1] - bird.pos[1]

This seems to do what you intended. You just need to handle the special case where bird is crashbird, since a collision test always reports that a sprite collides with itself.

With this minor rewrite I can have 170 penguins alive while maintaining 40+ FPS.

Gumm

On Sun, Jan 24, 2010 at 2:00 PM, Horst JENS <horst.jens@xxxxxxxxx> wrote:
I wrote a pygame program with sprites bouncing around. fps and time
between 2 frames is displayed (as number and as green bars).

If many sprites are present, sometimes (like each half minute) there is
a extreme huge value of passed time between 2 frames. (>400 instead of
the usual 30).

Is this some kind of garbage collection ? Is it because i copy a sprite
group (for collision check) ?

http://www.spielend-programmieren.at/wiki/doku.php?id=en:code:pygame:zeitproblem

greetings,
       -Horst

--
Horst JENS
horst.jens@xxxxxxxxxxxxxxxxxxxxxxxxx
http://www.spielend-programmieren.at