From dab9d5719bbac3e65bc03700b5f2237957f19fd9 Mon Sep 17 00:00:00 2001 From: Jeremy Thurgood Date: Fri, 16 May 2014 19:23:24 +0200 Subject: [PATCH] Saved games can now be loaded as well. --- naja/engine.py | 4 +++- naja/events.py | 8 ++++++++ naja/scenes/load_save.py | 34 ++++++++++++++++++++++++---------- naja/scenes/menu.py | 7 ++++++- 4 files changed, 41 insertions(+), 12 deletions(-) diff --git a/naja/engine.py b/naja/engine.py index cbe27ce..d7e0caf 100644 --- a/naja/engine.py +++ b/naja/engine.py @@ -2,7 +2,7 @@ import pygame import pygame.locals as pgl from naja.constants import FPS -from naja.events import SceneChangeEvent, QuitGameEvent +from naja.events import SceneChangeEvent, QuitGameEvent, LoadGameEvent class Engine(object): @@ -21,6 +21,8 @@ class Engine(object): return elif SceneChangeEvent.matches(ev): self.change_scene(ev.scene_cls) + elif LoadGameEvent.matches(ev): + self._state = ev.state else: self._scene.handle_event(ev) self._scene.render(self._surface) diff --git a/naja/events.py b/naja/events.py index 1d7609f..dbf7a03 100644 --- a/naja/events.py +++ b/naja/events.py @@ -46,3 +46,11 @@ class InvalidateTheWorld(NajaEvent): class SelectEvent(NajaEvent): TYPE = "SELECT" + + +class LoadGameEvent(NajaEvent): + TYPE = "LOAD_GAME" + + @classmethod + def post(cls, state): + super(LoadGameEvent, cls).post(state=state) diff --git a/naja/scenes/load_save.py b/naja/scenes/load_save.py index 485b032..2879504 100644 --- a/naja/scenes/load_save.py +++ b/naja/scenes/load_save.py @@ -9,7 +9,8 @@ from datetime import datetime import pygame.locals as pgl from naja.constants import KEYS -from naja.events import SceneChangeEvent, InvalidateTheWorld +from naja.events import SceneChangeEvent, InvalidateTheWorld, LoadGameEvent +from naja.gamestate import GameState from naja.options import options from naja.scenes.scene import Scene from naja.widgets.save_slot import SaveSlotWidget @@ -26,9 +27,9 @@ def ensure_save_path_exists(): os.makedirs(location) -class SaveGameScene(Scene): +class LoadSaveGameBase(Scene): def __init__(self, state): - super(SaveGameScene, self).__init__(state) + super(LoadSaveGameBase, self).__init__(state) selector = SelectorWidget() self.add(selector) self.slots = {} @@ -55,10 +56,21 @@ class SaveGameScene(Scene): game_data = self.get_game_data(slot_num) y_offset = 74 * slot_num slot = SaveSlotWidget((100, y_offset), slot_num, game_data) - slot.add_callback('click', lambda event: self.save_game(slot_num)) + slot.add_callback('click', lambda event: self.perform_action(slot_num)) return slot - def save_game(self, slot_num): + def handle_scene_event(self, ev): + if ev.type == pgl.KEYDOWN and ev.key in KEYS.QUIT: + from naja.scenes.menu import MenuScene + SceneChangeEvent.post(MenuScene) + return + + def perform_action(self, slot_num): + raise NotImplementedError("Nothing to see here.") + + +class SaveGameScene(LoadSaveGameBase): + def perform_action(self, slot_num): save_data = { 'timestamp': datetime.now().ctime(), 'data': self.state.gameboard.export(), @@ -72,8 +84,10 @@ class SaveGameScene(Scene): self.slots[slot_num].game_data = self.get_game_data(slot_num) InvalidateTheWorld.post() - def handle_scene_event(self, ev): - if ev.type == pgl.KEYDOWN and ev.key in KEYS.QUIT: - from naja.scenes.menu import MenuScene - SceneChangeEvent.post(MenuScene) - return + +class LoadGameScene(LoadSaveGameBase): + def perform_action(self, slot_num): + game_data = self.get_game_data(slot_num) + state = GameState(game_data['data']) + if game_data is not None: + LoadGameEvent.post(state) diff --git a/naja/scenes/menu.py b/naja/scenes/menu.py index e0383b5..a06e061 100644 --- a/naja/scenes/menu.py +++ b/naja/scenes/menu.py @@ -9,7 +9,7 @@ from naja.events import SceneChangeEvent, QuitGameEvent from naja.scenes.scene import Scene from naja.scenes.credits import CreditsScene from naja.scenes.game import GameScene -from naja.scenes.load_save import SaveGameScene +from naja.scenes.load_save import LoadGameScene, SaveGameScene from naja.widgets.selector import SelectorWidget from naja.widgets.text import TextWidget @@ -31,6 +31,11 @@ class MenuScene(Scene): 'click', lambda event: SceneChangeEvent.post(CreditsScene)) selector.add(credits) + load = TextWidget((100, 200), 'Load', fontsize=32, colour='white') + load.add_callback( + 'click', lambda event: SceneChangeEvent.post(LoadGameScene)) + selector.add(load) + save = TextWidget((100, 250), 'Save', fontsize=32, colour='white') save.add_callback( 'click', lambda event: SceneChangeEvent.post(SaveGameScene)) -- 2.34.1