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

[pygame] Introductions and question about transparency




Hello all!

First of all, a big thank you to Pete Shinners (and whoever worked on it) for 
Pygame. Python is the only programming language I feel somewhat comfortable 
with and I would never have dared to start a game project without Pygame.

My question is about image transparency. I am trying to write a strategy game 
in Python to see how far I will get. As placeholder graphics for the moment, 
I am using the very beautiful Freeciv Trident tileset. And I cannot seem to 
keep their transparence when blitting them in Pygame.

You can see images from my game so far at the bottom of this (French) page:

http://www.loisel.org/jerome/fse.html

The second image illustrates my problem. I get this result when blitting 
mostly-transparent tiles on top of the first image. The transparency is lost.

For more details... Freeciv tiles are not saved as separate graphics. 
Instead, all tiles are stored in big XPM files. Freeciv loads those files and 
cuts them up according to information stored in a .spec file. I have managed 
to successfully parse that spec file, then load and cut up the XPM file. Here 
is the relevant tile-creating code.

# To help you:
# tag is the name of the tile.
# coords[tag] = (column, row)
#
bigtile_surface = pygame.image.load(tile_filename).convert()
tiles = {}
dx, dy = metadata["dx"], metadata["dy"] # Information from the spec file
# Iterate over all tile names
for tag in coords.keys():
    x, y = coords[tag][0] * dx, coords[tag][1] * dy
    tile = bigtile_surface.subsurface((x, y, dx, dy))
    tiles[tag] = tile


Once I have parsed my map file, I create a Surface object. I then blit my 
tiles onto that surface one by one. Here is the function which creates my map 
surface:

def fsem_surface(fsem_names, tiles, metadata):
    # Extrapolate required data
    number_of_rows, number_of_cols = len(fsem_names), len(fsem_names[0])
    dx, dy = metadata["dx"], metadata["dy"]
    # Create the surface
    surface = pygame.Surface((number_of_cols * dx, number_of_rows * dy), 
SRCALPHA)
    # Iterate over the map
    for row_index in range(number_of_rows):
        row = fsem_names[row_index]
        for col_index in range(number_of_cols):
            for tile_name in row[col_index]:
                tile = tiles[tile_name]
                surface.blit(tile, (col_index * dx, row_index * dy))
    return surface

Once I blit this surface onto the screen, you get the result seen on my page. 
The transparency is lost. Can you help me out? Can you tell me what I am 
doing wrong?

Thank you very much for your time and help.

Cheers,

Jérôme Loisel

-- 
Jérôme Loisel, étudiant et webmestre
Lévinux: GNU/Linux pour la communauté
http://www.levinux.org/
____________________________________
pygame mailing list
pygame-users@seul.org
http://pygame.seul.org