# HG changeset patch # User Simon Cross # Date 1328960451 -7200 # Node ID ebb2efcb4ea7e56e1d278c7723d5416d60ca8c61 # Parent 15713dfe555d8dc4e5c8b66411c9bd6b4bdb6327 Create a re-usable main function. diff -r 15713dfe555d -r ebb2efcb4ea7 gamelib/main.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/gamelib/main.py Sat Feb 11 13:40:51 2012 +0200 @@ -0,0 +1,7 @@ +from scenes import SCENE_LIST, INITIAL_SCENE + +from pyntnclick.main import main as pyntnclick_main + + +def main(): + return pyntnclick_main(INITIAL_SCENE, SCENE_LIST) diff -r 15713dfe555d -r ebb2efcb4ea7 pyntnclick/gamescreen.py --- a/pyntnclick/gamescreen.py Sat Feb 11 13:30:56 2012 +0200 +++ b/pyntnclick/gamescreen.py Sat Feb 11 13:40:51 2012 +0200 @@ -11,7 +11,7 @@ from pyntnclick.constants import SCREEN, BUTTON_SIZE, SCENE_SIZE, LEAVE from pyntnclick.cursor import CursorWidget -from pyntnclick.state import initial_state, handle_result +from pyntnclick.state import handle_result from pyntnclick.widgets import ( MessageDialog, BoomButton, HandButton, PopupMenu, PopupMenuButton) @@ -201,10 +201,11 @@ class GameScreen(Screen, CursorWidget): - def __init__(self, shell): + def __init__(self, shell, create_initial_state): CursorWidget.__init__(self, self) Screen.__init__(self, shell) self.running = False + self.create_initial_state = create_initial_state def _clear_all(self): for widget in self.subwidgets[:]: @@ -212,7 +213,7 @@ def start_game(self): self._clear_all() - self.state = initial_state() + self.state = self.create_initial_state() self.state_widget = StateWidget(self) self.add(self.state_widget) diff -r 15713dfe555d -r ebb2efcb4ea7 pyntnclick/main.py --- a/pyntnclick/main.py Sat Feb 11 13:30:56 2012 +0200 +++ b/pyntnclick/main.py Sat Feb 11 13:40:51 2012 +0200 @@ -39,16 +39,16 @@ class MainShell(Shell): - def __init__(self, display): + def __init__(self, display, initial_state): Shell.__init__(self, display) self.menu_screen = MenuScreen(self) - self.game_screen = GameScreen(self) + self.game_screen = GameScreen(self, initial_state) self.end_screen = EndScreen(self) self.set_timer(FRAME_RATE) self.show_screen(self.menu_screen) -def main(): +def main(initial_scene, scene_list, debug_rects=False): opts = parse_args(sys.argv) pygame.display.init() pygame.font.init() @@ -63,13 +63,15 @@ if DEBUG: if opts.scene is not None: # debug the specified scene - state.DEBUG_SCENE = opts.scene - state.DEBUG_RECTS = opts.rects + initial_scene = opts.scene + debug_rects = opts.rects + initial_state = state.initial_state_creator(initial_scene, scene_list, + debug_rects) display = pygame.display.set_mode(SCREEN, SWSURFACE) pygame.display.set_icon(pygame.image.load( data.filepath('icons/suspended_sentence24x24.png'))) pygame.display.set_caption("Suspended Sentence") - shell = MainShell(display) + shell = MainShell(display, initial_state) try: shell.run() except KeyboardInterrupt: diff -r 15713dfe555d -r ebb2efcb4ea7 pyntnclick/state.py --- a/pyntnclick/state.py Sat Feb 11 13:30:56 2012 +0200 +++ b/pyntnclick/state.py Sat Feb 11 13:40:51 2012 +0200 @@ -11,14 +11,6 @@ from pyntnclick import constants from pyntnclick.sound import get_sound -from gamelib.scenes import SCENE_LIST, INITIAL_SCENE - -# override the initial scene to for debugging -DEBUG_SCENE = None - -# whether to show debugging rects -DEBUG_RECTS = False - class Result(object): """Result of interacting with a thing""" @@ -62,15 +54,17 @@ res.process(scene_widget) -def initial_state(): - """Load the initial state.""" - state = GameState() - for scene in SCENE_LIST: - state.load_scenes(scene) - initial_scene = INITIAL_SCENE if DEBUG_SCENE is None else DEBUG_SCENE - state.set_current_scene(initial_scene) - state.set_do_enter_leave() - return state +def initial_state_creator(initial_scene, scene_list, debug_rects=False): + def initial_state(): + """Load the initial state.""" + state = GameState() + state.set_debug_rects(debug_rects) + for scene in scene_list: + state.load_scenes(scene) + state.set_current_scene(initial_scene) + state.set_do_enter_leave() + return state + return initial_state class GameState(object): @@ -81,7 +75,6 @@ * items * scenes """ - def __init__(self): # map of scene name -> Scene object self.scenes = {} @@ -105,6 +98,11 @@ # current thing self.current_thing = None self.highlight_override = False + # debug rects + self.debug_rects = False + + def set_debug_rects(self, value=True): + self.debug_rects = value def add_scene(self, scene): self.scenes[scene.name] = scene @@ -479,7 +477,7 @@ self.current_interact.rect = old_rect.move(self.scene.OFFSET) self.current_interact.draw(surface) self.current_interact.rect = old_rect - if DEBUG_RECTS and self._interact_hilight_color: + if self.state.debug_rects and self._interact_hilight_color: if hasattr(self.rect, 'collidepoint'): frame_rect(surface, self._interact_hilight_color, self.rect.inflate(1, 1), 1) diff -r 15713dfe555d -r ebb2efcb4ea7 run_game.py --- a/run_game.py Sat Feb 11 13:30:56 2012 +0200 +++ b/run_game.py Sat Feb 11 13:40:51 2012 +0200 @@ -1,4 +1,4 @@ #! /usr/bin/env python -from pyntnclick import main +from gamelib import main main.main()