1 """ Engine for moving controll scenes and tranistions between them.
8 import pygame.locals as pgl
10 from .constants import FPS, DRAW_FPS, FONTS, FPS_FRAMES
11 from .loader import loader
12 from .events import QuitEvent, SceneChangeEvent
16 def __init__(self, screen, gamestate):
18 self._gamestate = gamestate
21 def set_scene(self, scene):
22 if self._scene is not None:
23 self._scene.exit(self._gamestate)
26 self._scene.enter(self._gamestate)
29 clock = pygame.time.Clock()
30 fpses = [FPS] * FPS_FRAMES
32 font = loader.load_font(FONTS['sans'], size=16)
35 events = pygame.event.get()
37 if QuitEvent.matches(ev) or ev.type == pgl.QUIT:
40 elif SceneChangeEvent.matches(ev):
41 self.set_scene(ev.scene)
43 self._scene.event(ev, self._gamestate)
45 # Advance time on the world
46 # Time is assumed to flow perfectly, so no dt parameter for now
47 self._scene.tick(self._gamestate)
48 self._scene.render(self._screen, self._gamestate)
50 fps = sum(fpses) / FPS_FRAMES
51 text = font.render("FPS: %.2f" % fps, True, (255, 255, 255))
52 self._screen.blit(text, (10, 10), None)
54 fpses[frame] = 1000.0 / clock.tick(FPS)
56 if frame >= FPS_FRAMES: