From c2e6cf6b0c175eee7606e7b1aaaf1f88d0f648c4 Mon Sep 17 00:00:00 2001 From: Simon Cross Date: Sun, 4 Sep 2016 15:53:54 +0200 Subject: [PATCH] Rework scenes and allow the user to quit. --- tabakrolletjie/engine.py | 13 ++++++++++--- tabakrolletjie/gamestate.py | 4 ++++ tabakrolletjie/main.py | 6 ++++-- tabakrolletjie/scenes/base.py | 14 ++++++++++++++ tabakrolletjie/scenes/menu.py | 13 +++++++++++-- 5 files changed, 43 insertions(+), 7 deletions(-) create mode 100644 tabakrolletjie/scenes/base.py diff --git a/tabakrolletjie/engine.py b/tabakrolletjie/engine.py index eee88b7..8ee5fc9 100644 --- a/tabakrolletjie/engine.py +++ b/tabakrolletjie/engine.py @@ -11,9 +11,16 @@ from .constants import FPS 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() @@ -24,9 +31,9 @@ class Engine(object): 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) diff --git a/tabakrolletjie/gamestate.py b/tabakrolletjie/gamestate.py index e69de29..6f7f5c4 100644 --- a/tabakrolletjie/gamestate.py +++ b/tabakrolletjie/gamestate.py @@ -0,0 +1,4 @@ +""" The game state. """ + +class GameState(object): + pass diff --git a/tabakrolletjie/main.py b/tabakrolletjie/main.py index 77af827..f09d681 100644 --- a/tabakrolletjie/main.py +++ b/tabakrolletjie/main.py @@ -6,6 +6,7 @@ import pygame.locals as pgl from .constants import SCREEN_SIZE, TITLE from .engine import Engine +from .gamestate import GameState from .scenes.menu import MenuScene @@ -18,7 +19,8 @@ def main(): # 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() diff --git a/tabakrolletjie/scenes/base.py b/tabakrolletjie/scenes/base.py new file mode 100644 index 0000000..3b0f60a --- /dev/null +++ b/tabakrolletjie/scenes/base.py @@ -0,0 +1,14 @@ +""" 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. """ diff --git a/tabakrolletjie/scenes/menu.py b/tabakrolletjie/scenes/menu.py index e7092d5..ca9e9b5 100644 --- a/tabakrolletjie/scenes/menu.py +++ b/tabakrolletjie/scenes/menu.py @@ -1,4 +1,13 @@ """ 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)) -- 2.34.1