class Engine(object):
- def __init__(self, screen, scene):
+ def __init__(self, screen, gamestate):
self._screen = screen
+ self._gamestate = gamestate
+ self._scene = None
+
+ def set_scene(self, scene):
+ if self._scene is not None:
+ self._scene.exit(self._gamestate)
self._scene = scene
+ self._scene.enter(self._gamestate)
def run(self):
clock = pygame.time.Clock()
if ev.type == pgl.QUIT:
return
else:
- pass # TODO: send to scene
+ self._scene.event(ev, self._gamestate)
- # TODO: render scene
+ self._scene.render(self._screen, self._gamestate)
pygame.display.flip()
clock.tick(FPS)
+""" The game state. """
+
+class GameState(object):
+ pass
from .constants import SCREEN_SIZE, TITLE
from .engine import Engine
+from .gamestate import GameState
from .scenes.menu import MenuScene
# TODO: set an icon
screen = pygame.display.get_surface()
- scene = MenuScene()
- engine = Engine(screen, scene)
+ gamestate = GameState()
+ engine = Engine(screen, gamestate)
+ engine.set_scene(MenuScene())
engine.run()
--- /dev/null
+""" Base scene class. """
+
+class BaseScene(object):
+ def enter(self, gamestate):
+ """ Enter the scene. """
+
+ def exit(self, gamestate):
+ """ Leave the scene. """
+
+ def event(self, ev, gamestate):
+ """ Handle an event. """
+
+ def render(self, screen, gamestate):
+ """ Render the scene. """
""" Menu scene. """
-class MenuScene(object):
- pass
+import pygame.event
+import pygame.locals as pgl
+
+from .base import BaseScene
+
+
+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))