SurfaceLayers

In a game, you’ll typically want to have multiple layers of drawable objects - for example, one for the background, one for collidable objects, one for the character, and one for your overlay, or HUD. The SurfaceLayers class provides a way to keep these layers in order, and render them correctly.

class sappho.layers.SurfaceLayers(target_surface, number_of_layers)[source]

Ordered series of pygame surfaces, each the size of the target surface given at creation time.

Parameters:
  • target_surface (pygame.Surface) – Surface that will have have the layers blitted to when render() is called. The size of this surface is used as the size of the generated layers.
  • number_of_layers (int) – Number of layers to generate.
static create_surface_layers(target_surface, number_of_layers)[source]

Create a list of pygame surfaces the size of the target surface.

Parameters:
  • target_surface (pygame.Surface) – The surface whose dimensions will be used for each layer.
  • number_of_layers (int) – The number of surfaces to create/return.
Returns:

List of surfaces

Return type:

list[pygame.Surface]

render()[source]

Draw each layer onto the target surface in the correct order.

Typical usage

Typically, you will want two SurfaceLayers objects, one for the game’s map (which would ideally be rendering to a Camera), and one for the game as a whole, containing the aforementioned Camera, and your game elements, such as a heads up display. This setup would look something like this:

# Screen resolution.
RESOLUTION = (800, 600)

# Map camera and layers
map_surfaces = [...]
map_camera = sappho.camera.Camera(map_surfaces[0].get_size(),
                                  RESOLUTION, # Screen resolution
                                  (100, 100)) # Size of the view
map_layers = sappho.layers.SurfaceLayers(map_camera.source_surface,
                                         len(map_surfaces))

# Heads up display (created with RESOLUTION as to fill the whole screen)
hud = pygame.Surface(RESOLUTION)

# Overall game layers.
# 2 layers - one for the map camera, one for the HUD
game_layers = sappho.layers.SurfaceLayers(screen, 2)

while game_is_running:
    # Any code that needs to be run before screen updates can go here,
    # for example, updating the HUD
    ...

    # Render the map surfaces to the map SurfaceLayers object
    for i, surface in enumerate(map_surfaces):
        map_layers[i].blit(surface, (0, 0))

    # Render the map_layers to the camera then update the camera
    map_layers.render()
    map_camera.update()

    # Render the map camera and the HUD to game_layers
    game_layers[0].blit(map_camera, (0, 0))
    game_layers[1].blit(hud, (0, 0))

    # Render everything to the screen and update it with our changes
    game_layers.render()
    pygame.display.flip()