From: Neil Date: Sat, 17 May 2014 17:38:30 +0000 (+0200) Subject: Add a puzzle selection screen X-Git-Tag: 0.1~62 X-Git-Url: https://git.ctpug.org.za/?a=commitdiff_plain;h=5cf4ebcfa0ac4f6a947992dc7871a124913b4671;p=naja.git Add a puzzle selection screen --- diff --git a/naja/constants.py b/naja/constants.py index 2f8d379..5cde145 100644 --- a/naja/constants.py +++ b/naja/constants.py @@ -155,3 +155,9 @@ TUNES = ( 'scape.ogg', 'another_beek.ogg', ) + +# Puzzles, because resources can't give us file lists +PUZZLES = ( + 'chess', + 'test' +) diff --git a/naja/scenes/menu.py b/naja/scenes/menu.py index 4dde093..3c7ffb5 100644 --- a/naja/scenes/menu.py +++ b/naja/scenes/menu.py @@ -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) diff --git a/naja/scenes/new_game.py b/naja/scenes/new_game.py index 1b74d61..9db6395 100644 --- a/naja/scenes/new_game.py +++ b/naja/scenes/new_game.py @@ -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 index 0000000..f9c51c1 --- /dev/null +++ b/naja/scenes/puzzlelist.py @@ -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