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

[pygame] different behaviour of sprites (in group) with python 2.6 and 3.1



Hi,

I've observed that sprites in a group behave
strange with Python 3.1:
the order in which they are drawn is
(1) different from the order used by Python 2.6
(2) strangely I get also different orders when I start
     the script from the commandline in two different ways:

I append a script that I prepared for the purpose
of comparison:  colored_balls.py
(A similar script using gif-images shows the same strange
behaviour)

On my machine:

with python 2.6:  blue covers red covers green    (as expected)

with c:\mycode>colored_balls.py:      (starts the script with python 3.1.2)
    blue covers red covers green

but with c:\python31\python.exe colored-balls.py red covers green covers blue

moreover logged in as a different user on the same machine I got
when starting the script from the commandline:

  green covers bolue covers red

of course this is an inadmissible situation - so one cannot use the sprite module
with Python 3.1?

Any remarks?

Thanks, Gregor

P.S.: While Andy Harris' mail pilot from his "Game Programming"
rund (essentially) with python 3.1 it shows an erratic behaviour
which may be due to the bug demonstrated above.

SCREEN_SIZE = (800, 600)

import pygame
import random
import sys

print(sys.version)
pygame.init()
            
class Ball(pygame.sprite.Sprite):
    
    def __init__(self, color):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.Surface((300,300)).convert()
        self.image.set_colorkey((0, 0, 0))
        pygame.draw.circle(self.image, color, (150, 150), 150)
        self.rect = self.image.get_rect()
        self.reset()
        
    def update(self):
        self.rect.centerx += self.dx
        self.rect.centery += self.dy
        if self.rect.top > SCREEN_SIZE[1]:
            self.reset()

    def reset(self):
        self.rect.bottom = 0
        self.rect.centerx = random.randint(150, SCREEN_SIZE[0]-150)
        self.dx = random.randint(-2, 2)
        self.dy = random.randint(5, 10)


def main():    
    screen = pygame.display.set_mode(SCREEN_SIZE)
    pygame.display.set_caption("BallSprites")
    bg = pygame.Surface(screen.get_size()).convert()

    bg.fill((0, 0, 64))
    screen.blit(bg, (0,0))

    redball = Ball((255, 0, 0))
    greenball = Ball((0, 255, 0))
    blueball = Ball((0, 0, 255))

    allSprites = pygame.sprite.Group(greenball, redball, blueball)

    keepGoing = True
    clock = pygame.time.Clock()

    while keepGoing:
        clock.tick(30)
        pygame.mouse.set_visible(False)
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                keepGoing = False
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_ESCAPE:
                    keepGoing = False

        allSprites.clear(screen, bg)
        allSprites.update()
        allSprites.draw(screen)

        pygame.display.flip()

    pygame.mouse.set_visible(True)

if __name__ == "__main__":
    main()