from pgzero.constants import keys
from pygame import Surface
+import pygame.locals as pgl
from ..loaders.levelloader import levels
from .base import Scene, ChangeSceneEvent
from ..constants import TILE_SIZE
def enter(self, world):
self._level_data = levels.load(world.level.name)
self._tiles = self._level_data['tiles']
- self._surface = None
- self._render()
+ self._layer = 'floor'
+ self._surfaces = {}
+ self._overlay = {}
+ for layer in ['floor', 'tunnels']:
+ self._surfaces[layer] = self._render(layer)
+ self._overlay = self._surfaces['floor'].copy()
- def _render(self):
+ def _render(self, layer):
# We cache the rendered surface to avoid doing a large number
# of blits each frame, as that introduces a large performance
# overhead.
- self._surface = Surface((len(self._tiles[0]) * TILE_SIZE,
- len(self._tiles) * TILE_SIZE))
+ surface = Surface((len(self._tiles[0]) * TILE_SIZE,
+ len(self._tiles) * TILE_SIZE))
+ layer_key = '%s image' % layer
for y, row in enumerate(self._tiles):
for x, tile in enumerate(row):
pos = (x * TILE_SIZE, y * TILE_SIZE)
- if 'image' not in tile:
+ if layer_key not in tile:
# Skip broken tiles for now
continue
- self._surface.blit(tile['image'], pos)
+ surface.blit(tile[layer_key], pos)
+ return surface.convert_alpha()
def draw(self, screen, viewport):
screen.clear()
# surface. We need the position of the surface relative to
# the screen for the blit, so this conversion
screen_pos = -viewport[0], -viewport[1]
- screen.blit(self._surface, screen_pos)
+ if self._layer == 'floor':
+ screen.blit(self._surfaces[self._layer], screen_pos)
+ else:
+ # blit tunnels, with translucent overlay
+ # We need to call pygame.Surface.blit ourselves,
+ # since pgzero's screen blit hides the blend flags
+ # from us
+ tunnels = self._surfaces[self._layer].copy()
+ tunnels.blit(self._overlay, (0, 0),
+ special_flags=pgl.BLEND_MULT)
+ screen.blit(tunnels, screen_pos)
def on_key_down(self, key, mod, unicode):
if key == keys.ESCAPE: