3 from pgzero.constants import keys
4 from pygame import Surface
5 from ..loaders.levelloader import levels
6 from .base import Scene, ChangeSceneEvent
7 from ..constants import TILE_SIZE
10 class LevelScene(Scene):
13 def enter(self, world):
14 self._level_data = levels.load(world.level.name)
15 self._tiles = self._level_data['tiles']
20 # We cache the rendered surface to avoid doing a large number
21 # of blits each frame, as that introduces a large performance
23 self._surface = Surface((len(self._tiles[0]) * TILE_SIZE,
24 len(self._tiles) * TILE_SIZE))
25 for y, row in enumerate(self._tiles):
26 for x, tile in enumerate(row):
27 pos = (x * TILE_SIZE, y * TILE_SIZE)
28 if 'image' not in tile:
29 # Skip broken tiles for now
31 self._surface.blit(tile['image'], pos)
33 def draw(self, screen, viewport):
35 # Viewport is the position of the screen relative to the
36 # surface. We need the position of the surface relative to
37 # the screen for the blit, so this conversion
38 screen_pos = -viewport[0], -viewport[1]
39 screen.blit(self._surface, screen_pos)
41 def on_key_down(self, key, mod, unicode):
42 if key == keys.ESCAPE:
43 from .menu import MenuScene
44 return [ChangeSceneEvent(MenuScene())]