New game with difficulty selection.
authorJeremy Thurgood <firxen@gmail.com>
Fri, 16 May 2014 18:24:26 +0000 (20:24 +0200)
committerJeremy Thurgood <firxen@gmail.com>
Fri, 16 May 2014 18:24:26 +0000 (20:24 +0200)
naja/__main__.py
naja/gamestate.py
naja/scenes/load_save.py
naja/scenes/menu.py
naja/scenes/new_game.py [new file with mode: 0644]

index 70330cbbd82173d8184a4095184ae82e468676d0..bc60052528b1925aea465a91067cdb6ed9b478ad 100644 (file)
@@ -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()
index d5c624c4882513ba7cf27201bf8ec29c263429f8..134fba3bad70649c1ed39b6bc08c1c2b535839d0 100644 (file)
@@ -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):
index 8926e569e6b44992f6076a486549052ec5b64e32..809adbdeb743d0dfe4886f63c0aa390af59906d4 100644 (file)
@@ -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:
index a06e061f0d7c03afec99b22122841420f2d1f7d8..560e104449a5808bdc064cb7d823ad4b52359476 100644 (file)
@@ -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 (file)
index 0000000..f3b8989
--- /dev/null
@@ -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