From e899f94fa2121f777f620edec954f97618b3d621 Mon Sep 17 00:00:00 2001 From: Jeremy Thurgood Date: Fri, 16 May 2014 20:24:26 +0200 Subject: [PATCH] New game with difficulty selection. --- naja/__main__.py | 2 +- naja/gamestate.py | 15 ++++++++---- naja/scenes/load_save.py | 2 +- naja/scenes/menu.py | 8 ++++++- naja/scenes/new_game.py | 52 ++++++++++++++++++++++++++++++++++++++++ 5 files changed, 71 insertions(+), 8 deletions(-) create mode 100644 naja/scenes/new_game.py diff --git a/naja/__main__.py b/naja/__main__.py index 70330cb..bc60052 100644 --- a/naja/__main__.py +++ b/naja/__main__.py @@ -22,7 +22,7 @@ def main(): sound.init() screen = pygame.display.get_surface() - state = GameState() + state = GameState.new() scene = MenuScene(state) engine = Engine(screen, scene, state) engine.run() diff --git a/naja/gamestate.py b/naja/gamestate.py index d5c624c..134fba3 100644 --- a/naja/gamestate.py +++ b/naja/gamestate.py @@ -18,12 +18,17 @@ class GameState(object): Naja game state. """ - def __init__(self, data=None): + def __init__(self, gameboard): + self.gameboard = gameboard + + @classmethod + def new(cls, **kw): locations_deck = load_location_deck('standard') - if data is None: - self.gameboard = GameBoard.new_game(locations_deck['cards']) - else: - self.gameboard = GameBoard.import_game(data) + return cls(GameBoard.new_game(locations_deck['cards'], **kw)) + + @classmethod + def load(cls, data): + return cls(GameBoard.import_game(data)) @property def player(self): diff --git a/naja/scenes/load_save.py b/naja/scenes/load_save.py index 8926e56..809adbd 100644 --- a/naja/scenes/load_save.py +++ b/naja/scenes/load_save.py @@ -100,7 +100,7 @@ class LoadGameScene(LoadSaveGameBase): def perform_action(self, slot_num): game_data = self.get_game_data(slot_num) if game_data is not None: - state = GameState(game_data['data']) + state = GameState.load(game_data['data']) LoadGameEvent.post(state) self.succeed() else: diff --git a/naja/scenes/menu.py b/naja/scenes/menu.py index a06e061..560e104 100644 --- a/naja/scenes/menu.py +++ b/naja/scenes/menu.py @@ -10,6 +10,7 @@ from naja.scenes.scene import Scene from naja.scenes.credits import CreditsScene from naja.scenes.game import GameScene from naja.scenes.load_save import LoadGameScene, SaveGameScene +from naja.scenes.new_game import NewGameScene from naja.widgets.selector import SelectorWidget from naja.widgets.text import TextWidget @@ -20,11 +21,16 @@ class MenuScene(Scene): selector = SelectorWidget() self.add(selector) - run_game = TextWidget((100, 100), 'Game', fontsize=32, colour='white') + run_game = TextWidget((100, 50), 'Game', fontsize=32, colour='white') run_game.add_callback( 'click', lambda event: SceneChangeEvent.post(GameScene)) selector.add(run_game) + new_game = TextWidget((100, 100), 'New', fontsize=32, colour='white') + new_game.add_callback( + 'click', lambda event: SceneChangeEvent.post(NewGameScene)) + selector.add(new_game) + credits = TextWidget( (100, 150), 'Credits', fontsize=32, colour='white') credits.add_callback( diff --git a/naja/scenes/new_game.py b/naja/scenes/new_game.py new file mode 100644 index 0000000..f3b8989 --- /dev/null +++ b/naja/scenes/new_game.py @@ -0,0 +1,52 @@ +""" +Load and save scenes. +""" + +import pygame.locals as pgl + +from naja.constants import KEYS +from naja.events import SceneChangeEvent, LoadGameEvent +from naja.gamestate import GameState +from naja.scenes.scene import Scene +from naja.widgets.selector import SelectorWidget +from naja.widgets.text import TextWidget + + +class NewGameScene(Scene): + def __init__(self, state): + super(NewGameScene, self).__init__(state) + selector = SelectorWidget() + self.add(selector) + + easy = TextWidget((100, 100), 'Easy', fontsize=32, colour='white') + easy.add_callback('click', self.easy_game) + selector.add(easy) + + standard = TextWidget( + (100, 150), 'Standard', fontsize=32, colour='white') + standard.add_callback('click', self.standard_game) + selector.add(standard) + + hard = TextWidget((100, 200), 'Hard', fontsize=32, colour='white') + hard.add_callback('click', self.hard_game) + selector.add(hard) + + def easy_game(self, event): + self.start_game(GameState.new(max_health=5, wins_required=3)) + + def standard_game(self, event): + self.start_game(GameState.new(max_health=4, wins_required=4)) + + def hard_game(self, event): + self.start_game(GameState.new(max_health=3, wins_required=5)) + + def start_game(self, state): + from naja.scenes.game import GameScene + LoadGameEvent.post(state=state) + SceneChangeEvent.post(GameScene) + + 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 -- 2.34.1