import pygame.locals as pgl
from .constants import FPS
+from .events import QuitEvent, SceneChangeEvent
class Engine(object):
if self._scene is not None:
self._scene.exit(self._gamestate)
self._scene = scene
- self._scene.enter(self._gamestate)
+ if scene is not None:
+ self._scene.enter(self._gamestate)
def run(self):
clock = pygame.time.Clock()
while True:
events = pygame.event.get()
for ev in events:
- if ev.type == pgl.QUIT:
+ if QuitEvent.matches(ev) or ev.type == pgl.QUIT:
+ self.set_scene(None)
return
+ elif SceneChangeEvent.matches(ev):
+ self.set_scene(ev.scene)
else:
self._scene.event(ev, self._gamestate)
self._scene.render(self._screen, self._gamestate)
pygame.display.flip()
-
clock.tick(FPS)
--- /dev/null
+""" Custom events. """
+
+import pygame.event
+import pygame.locals as pgl
+
+
+class TabakEvent(object):
+ TYPE = "UNKNOWN"
+ ATTRIBUTES = set()
+
+ @classmethod
+ def post(cls, **attributes):
+ assert set(attributes.keys()) == cls.ATTRIBUTES
+ ev = pygame.event.Event(
+ pgl.USEREVENT, tabak_type=cls.TYPE, **attributes)
+ pygame.event.post(ev)
+
+ @classmethod
+ def matches(cls, ev):
+ return (ev.type == pgl.USEREVENT and ev.tabak_type == cls.TYPE)
+
+
+class QuitEvent(TabakEvent):
+ TYPE = "QUIT"
+
+
+class SceneChangeEvent(TabakEvent):
+ TYPE = "SCENE_CHANGE"
+ ATTRIBUTES = {'scene'}
import pygame.locals as pgl
from .base import BaseScene
+from ..events import QuitEvent
class MenuScene(BaseScene):
def event(self, ev, gamestate):
if ev.type == pgl.KEYDOWN:
if ev.key in (pgl.K_q, pgl.K_ESCAPE):
- pygame.event.post(pygame.event.Event(pgl.QUIT))
+ QuitEvent.post()