Add a puzzle selection screen
authorNeil <neil@dip.sun.ac.za>
Sat, 17 May 2014 17:38:30 +0000 (19:38 +0200)
committerNeil <neil@dip.sun.ac.za>
Sat, 17 May 2014 17:38:40 +0000 (19:38 +0200)
naja/constants.py
naja/scenes/menu.py
naja/scenes/new_game.py
naja/scenes/puzzlelist.py [new file with mode: 0644]

index 2f8d3790245186852e93dcf60771e17b9a6887da..5cde1452eacb714a72d7925fddefd7b0fe1290ae 100644 (file)
@@ -155,3 +155,9 @@ TUNES = (
     'scape.ogg',
     'another_beek.ogg',
 )
+
+# Puzzles, because resources can't give us file lists
+PUZZLES = (
+    'chess',
+    'test'
+)
index 4dde093a9a12ecff4faf807cb10c4e967115a5ca..3c7ffb5d8d464c31691b59adcfcf842e8b2db11a 100644 (file)
@@ -13,6 +13,7 @@ from naja.scenes.game import GameScene
 from naja.scenes.howto import HowtoScene
 from naja.scenes.load_save import LoadGameScene, SaveGameScene
 from naja.scenes.new_game import NewGameScene
+from naja.scenes.puzzlelist import PuzzleListScene
 from naja.widgets.selector import SelectorWidget
 from naja.widgets.text import TextWidget
 from naja.widgets.image_box import ImageBox
@@ -29,7 +30,7 @@ class MenuScene(Scene):
 
         selector = SelectorWidget()
         self.add(selector)
-        y_offset, y_diff = 270, 40
+        y_offset, y_diff = 270, 36
         x_offset = 400
 
         y_offset += y_diff
@@ -42,11 +43,18 @@ class MenuScene(Scene):
 
         y_offset += y_diff
         new = TextWidget(
-            (x_offset, y_offset), 'New Game', colour=PALETTE.WHITE,
+            (x_offset, y_offset), 'New Random Game', colour=PALETTE.WHITE,
             centre=True)
         new.add_callback('click', self.scene_callback(NewGameScene))
         selector.add(new)
 
+        y_offset += y_diff
+        new = TextWidget(
+            (x_offset, y_offset), 'New Puzzle', colour=PALETTE.WHITE,
+            centre=True)
+        new.add_callback('click', self.scene_callback(PuzzleListScene))
+        selector.add(new)
+
         y_offset += y_diff
         load = TextWidget((x_offset, y_offset), 'Load', colour=PALETTE.WHITE,
                           centre=True)
index 1b74d6164613ab467b951f23b27b82ce72bf3b21..9db6395ffe535f39dda0b65954ad578607a8e9a8 100644 (file)
@@ -61,13 +61,6 @@ class NewGameScene(Scene):
         hard.add_callback('click', self.hard_game)
         selector.add(hard)
 
-        y_offset += y_diff
-        puzzle = TextWidget(
-            (x_offset, y_offset), 'Puzzle', fontsize=32, colour='white',
-            centre=True)
-        puzzle.add_callback('click', self.puzzle_game)
-        selector.add(puzzle)
-
     def easy_game(self, event):
         self.start_game(GameState.new(max_health=5, wins_required=3))
 
@@ -77,10 +70,6 @@ class NewGameScene(Scene):
     def hard_game(self, event):
         self.start_game(GameState.new(max_health=3, wins_required=5))
 
-    def puzzle_game(self, event):
-        self.start_game(GameState.new(
-            deck='puzzles/test', max_health=4, wins_required=4))
-
     def start_game(self, state):
         from naja.scenes.game import GameScene
         LoadGameEvent.post(state=state)
diff --git a/naja/scenes/puzzlelist.py b/naja/scenes/puzzlelist.py
new file mode 100644 (file)
index 0000000..f9c51c1
--- /dev/null
@@ -0,0 +1,63 @@
+"""
+Load and save scenes.
+"""
+
+import functools
+import pygame.locals as pgl
+
+from naja.constants import KEYS, PUZZLES
+from naja.events import SceneChangeEvent, LoadGameEvent
+from naja.gamestate import GameState, load_location_deck
+from naja.scenes.scene import Scene
+from naja.widgets.image_box import ImageBox
+from naja.widgets.selector import SelectorWidget
+from naja.widgets.text import TextWidget
+
+
+class PuzzleListScene(Scene):
+    def __init__(self, state):
+        super(PuzzleListScene, self).__init__(state)
+
+        background = ImageBox(
+            (0, 0), "screens/splash.png")
+        self.add(background)
+
+        selector = SelectorWidget()
+        selector.position = 1
+        self.add(selector)
+
+        y_offset, y_diff = 270, 36
+        x_offset = 400
+
+        y_offset += y_diff
+        title = TextWidget(
+            (x_offset, y_offset), 'CHOOSE PUZZLE', colour='white',
+            centre=True)
+        self.add(title)
+
+        for puzzle in PUZZLES:
+            y_offset += y_diff
+            deck = load_location_deck('puzzles/%s' % puzzle)
+            puzzle_but = TextWidget(
+               (x_offset, y_offset), deck['description'], fontsize=32,
+               colour='white', centre=True)
+            callback = functools.partial(self.start_puzzle_game,
+                                         puzzle=puzzle, deck=deck)
+            puzzle_but.add_callback('click', callback)
+            selector.add(puzzle_but)
+
+    def start_puzzle_game(self, event, puzzle=None, deck=None):
+        from naja.scenes.game import GameScene
+        max_health = deck.get('max_health', 4)
+        wins_required = deck.get('wins_required', 4)
+        level = 'puzzles/%s' % puzzle
+        state = GameState.new(deck=level, max_health=max_health,
+                              wins_required=wins_required)
+        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