Working with Surface Objects

Loading images into Pygame is done with a simple one-liner; pygame.image.load takes the file name of the image you want to load and returns a surface object, which is a container for an image. Surfaces can represent many types of image, but Pygame hides most of these details from us so we can treat them in much the same way. Once you have a surface in memory, you can draw on it, transform it, or copy it to another surface to build up an image. Even the screen is represented as a surface object. The initial call to pygame.display.set_mode returns a surface object that represents the display.

Creating Surfaces

A call to pygame.image.load is one way to create a surface. It will create a surface that matches the colors and dimensions of the image file, but you can also create blank surfaces of any size that you need (assuming there is enough memory to store it). To create a blank surface, call the pygame.Surface constructor with a tuple containing the required dimensions. The following line creates a surface that is 256 by 256 pixels:

blank_surface = pygame.Surface((256, 256))

Without any other parameters, this will create a surface with the same number of colors as the display. This is usually what you want, because it is faster to copy images when they have the same number of colors.

There are also a few optional parameters that affect how the images are created. You can set the flags parameter to one or both of the following parameters:

• HWSURFACE—Creates a hardware surface, which may be faster than nonhardware surfaces. It is usually better not to set this flag and leave the choice of whether to use hardware surfaces up to Pygame.

• SRCALPHA—Creates a surface with alpha information. Set this if you want parts of the surface to be transparent, for images with irregular edges such as sprites and fonts. This flag also requires that you to set the depth parameter to 32.

There is also a depth parameter for pygame.Surface that defines the color depth for the surface. This is similar to the depth parameter in pygame.display. set_mode and defines the maximum number of colors in the surface. Generally it is best not to set this parameter (or set it to 0), because Pygame will select a depth that matches the display—although if you want alpha information in the surface, you should set depth to 32. The following line creates a surface with alpha information:

blank_alpha_surface = pygame.Surface((256, 256), flags=SRCALPHA, depth=32)

Converting Surfaces

When you use surface objects, you don't have to worry about how the image information is stored in memory because Pygame will hide this detail from you. So most of the time the image format is something you don't need to worry about, since your code will work regardless of what type of images you use. The only downside of this automatic conversion is that Pygame will have to do more work if you are using images with different formats, and that can potentially decrease game performance. The solution is to convert all your images to be the same format. Surface objects have a convert method for this purpose.

If you call convert without any parameters, the surface will be converted to the format of the display surface. This is useful because it is usually fastest to copy surfaces when the source and destination are the same type—and most images will be copied to the display eventually.

It is a good idea to tack on .convert () to any calls to pygame.image.load to ensure your images will be in the fastest format for the display. The exception is when your image has an alpha channel, because convert can discard it. Fortunately Pygame provides a convert_alpha method, which will convert the surface to a fast format but preserve any alpha information in the image. We have used both methods in the previous chapter; the following two lines are taken from Listing 3-1:

background = pygame.image.load(background_image_filename).convert() mouse_cursor = pygame.image.load(mouse_image_filename).convert_alpha()

The background is just a solid rectangle so we use convert. However, the mouse cursor has irregular edges and needs alpha information, so we call convert_alpha.

Both convert and convert_alpha can take another surface as a parameter. If a surface is supplied, the surface will be converted to match the other surface.

0 0

Post a comment

  • Receive news updates via email from this site