comparison pyntnclick/main.py @ 554:99a1420097df pyntnclick

Create GameDescription object.
author Simon Cross <hodgestar+bzr@gmail.com>
date Sat, 11 Feb 2012 14:04:48 +0200
parents ebb2efcb4ea7
children a4f28da12720
comparison
equal deleted inserted replaced
553:ebb2efcb4ea7 554:99a1420097df
46 self.end_screen = EndScreen(self) 46 self.end_screen = EndScreen(self)
47 self.set_timer(FRAME_RATE) 47 self.set_timer(FRAME_RATE)
48 self.show_screen(self.menu_screen) 48 self.show_screen(self.menu_screen)
49 49
50 50
51 def main(initial_scene, scene_list, debug_rects=False): 51 class GameDescriptionError(Exception):
52 opts = parse_args(sys.argv) 52 """Raised when an GameDescription is invalid."""
53 pygame.display.init() 53
54 pygame.font.init() 54
55 if opts.sound: 55 class GameDescription(object):
56
57 # initial scene for start of game (unless overridden by debug)
58 INITIAL_SCENE = None
59
60 # list of game scenes
61 SCENE_LIST = None
62
63 def __init__(self):
64 if self.INITIAL_SCENE is None:
65 raise GameDescriptionError("A game must have an initial scene.")
66 if not self.SCENE_LIST:
67 raise GameDescriptionError("A game must have a non-empty list"
68 " of scenes.")
69 self._initial_scene = self.INITIAL_SCENE
70 self._scene_list = self.SCENE_LIST
71 self._debug_rects = False
72
73 def initial_state(self):
74 """Create a copy of the initial game state."""
75 initial_state = state.GameState()
76 initial_state.set_debug_rects(self._debug_rects)
77 for scene in self._scene_list:
78 initial_state.load_scenes(scene)
79 initial_state.set_current_scene(self._initial_scene)
80 initial_state.set_do_enter_leave()
81 return initial_state
82
83 def main(self):
84 opts = parse_args(sys.argv)
85 pygame.display.init()
86 pygame.font.init()
87 if opts.sound:
88 try:
89 pygame.mixer.init(FREQ, BITSIZE, CHANNELS, BUFFER)
90 except pygame.error, exc:
91 no_sound(exc)
92 else:
93 # Ensure get_sound returns nothing, so everything else just works
94 disable_sound()
95 if DEBUG:
96 if opts.scene is not None:
97 # debug the specified scene
98 self._initial_scene = opts.scene
99 self._debug_rects = opts.rects
100 display = pygame.display.set_mode(SCREEN, SWSURFACE)
101 pygame.display.set_icon(pygame.image.load(
102 data.filepath('icons/suspended_sentence24x24.png')))
103 pygame.display.set_caption("Suspended Sentence")
104 shell = MainShell(display, self.initial_state)
56 try: 105 try:
57 pygame.mixer.init(FREQ, BITSIZE, CHANNELS, BUFFER) 106 shell.run()
58 except pygame.error, exc: 107 except KeyboardInterrupt:
59 no_sound(exc) 108 pass
60 else:
61 # Ensure get_sound returns nothing, so everything else just works
62 disable_sound()
63 if DEBUG:
64 if opts.scene is not None:
65 # debug the specified scene
66 initial_scene = opts.scene
67 debug_rects = opts.rects
68 initial_state = state.initial_state_creator(initial_scene, scene_list,
69 debug_rects)
70 display = pygame.display.set_mode(SCREEN, SWSURFACE)
71 pygame.display.set_icon(pygame.image.load(
72 data.filepath('icons/suspended_sentence24x24.png')))
73 pygame.display.set_caption("Suspended Sentence")
74 shell = MainShell(display, initial_state)
75 try:
76 shell.run()
77 except KeyboardInterrupt:
78 pass