views:

24

answers:

1

I have a function that loads a sprite sheet, finds a block of sprites, and then puts each individual sprite into a list. Before it appends a sprite into the list, it will blit it onto the screen. Once it's done loading sprites, it will then iterate through the list, blitting each sprite as it goes. The two sets of blits should be identical, but instead the first sprite is dropped from the list, and the last sprite is duplicated. The two sets of blits look like this:

alt text

Each sprite is blitted in the order it was appended to the list, going from left to right, top to bottom, so the first sprite is the top left one, and the last is the bottom right.

Here's the function that loads the sprites:

def assembleSprites(name, screen):
    """Given a character name, this function will return a list of all that
    character's sprites.  This is used to populate the global variable spriteSets"""
    spriteSize = (35, 35)
    spritesheet = pygame.image.load("./images/patchconsprites.png")
    sprites = []
    start = charCoords[name]
    char = list(start)
    image = pygame.Surface((35,35))

    # load each sprite and blit them as they're added to the list
    for y in range(5):
        char[0] = start[0]
        for x in range(9):
            rect = (char[0], char[1], char[0]+spriteSize[0], char[1]+spriteSize[1])
            image.blit(spritesheet, (0,0), rect)
            image = image.convert()
            colorkey = image.get_at((0,0))
            image.set_colorkey(colorkey, RLEACCEL)
            screen.blit(image, (x*40, y*40))
            pygame.display.update()
            sprites.append(image)
            char[0] += spriteSize[0]+2
        char[1] += spriteSize[1]+2

    # check that the list was constructed correctly
    count = 0
    for y in range(6,11):
        for x in range(9):
            screen.blit(sprites[count], (x*40,y*40))
            count += 1
    pygame.display.update()
    return sprites

Anyone see how I'm screwing the list up?

+6  A: 
image.blit(spritesheet, (0,0), rect)

You haven't re-initialised image each time around the loop, it's still the same surface you used in the previous iteration, a surface that is already in the list. Each time round the loop you overwrite the sprite you appended to the list in the previous step.

I suggest grabbing a new image= pygame.Surface((35,35)) immediately before the line instead of before the start of the loop.

bobince
That was it. Thanks for your help.
Max