source: skaapsteker/__main__.py

Last change on this file was 618:72865593bdc7, checked in by Jeremy Thurgood <firxen@…>, 12 years ago

Make DEBUG an environment variable.

File size: 2.5 KB
Line 
1"""Game main module.
2 """
3
4import os
5import sys
6import optparse
7
8import pygame
9from pygame.locals import SWSURFACE
10
11from . import options
12from .constants import SCREEN
13from .data import filepath
14from .engine import Engine
15from .levelscene import LevelScene
16from .menuscene import MenuScene
17from .sound import SoundSystem
18
19DEBUG = False
20if os.environ.get('DEBUG'):
21 DEBUG = True
22
23
24def _get_default_save_location():
25 """Return a default save game location."""
26 app = "ninetales"
27 if sys.platform.startswith("win") and "APPDATA" in os.environ:
28 return os.path.join(os.environ["APPDATA"], app)
29 else:
30 return os.path.join(os.path.expanduser("~"), ".%s" % app)
31
32
33def parse_args(args):
34 parser = optparse.OptionParser()
35 parser.add_option("--no-sound", action="store_false", default=True,
36 dest="sound", help="disable sound")
37 parser.add_option("--dvorak", action="store_true", default=False,
38 dest="dvorak", help="Dvorak keyboard layout")
39 parser.add_option("--save-location", action="store", default=_get_default_save_location(),
40 dest="save_location", help="Saved game location")
41 if DEBUG:
42 parser.add_option("--level", type="str", default=None,
43 dest="level", help="Initial level")
44 parser.add_option("--all-tails", action="store_true", default=False,
45 dest='all_tails', help="Start with all the tails")
46 parser.add_option("--show-rects", action="store_true", default=False,
47 dest="rects", help="Disable debugging rects")
48 opts, _ = parser.parse_args(args or [])
49 options['dvorak'] = opts.dvorak
50 options['sound'] = opts.sound
51 options['save_location'] = opts.save_location
52 if DEBUG:
53 options['debug_rects'] = opts.rects
54 options['all_tails'] = opts.all_tails
55 return opts.level
56
57
58def main():
59 """Launch Nine Tales.
60 """
61 level = parse_args(sys.argv)
62 pygame.display.init()
63 pygame.font.init()
64 soundsystem = SoundSystem(options['sound'])
65 pygame.display.set_mode(SCREEN, SWSURFACE)
66 pygame.display.set_icon(pygame.image.load(filepath('icons/program/icon_24.png')))
67 pygame.display.set_caption("Nine Tales")
68
69 engine = Engine(soundsystem)
70 if level is not None:
71 engine.game_state.new_game()
72 engine.change_scene(LevelScene(engine.game_state, soundsystem, level + '.starting'))
73 else:
74 engine.change_scene(MenuScene(engine.game_state, soundsystem))
75 try:
76 engine.run()
77 except KeyboardInterrupt:
78 pass
79
80
81if __name__ == '__main__':
82 main()
Note: See TracBrowser for help on using the repository browser.