예제 #1
0
 def __init__(self, config, scene):
     from zombie.constants import COLORS
     
     from cocos.director import director
     from cocos.menu import ColorMenuItem, EntryMenuItem, ImageMenuItem, MenuItem, MultipleMenuItem
     
     super(SettingsMenu, self).__init__(scene, 'Settings')
     
     self.nickname = EntryMenuItem('Nickname:', self.validate_nickname, config['player']['nickname'], max_length=10)
     color_options = tuple(COLORS.values())
     color_index = color_options.index(config['player']['color'])
     self.color = ColorMenuItem('Color: ', self.select_color, color_options, default_item=color_index)
     self.player_icon = ImageMenuItem('player-icon-001.png', self.select_player_icon)
     # XXX: MultipleMenuItems requires a space?
     
     self.resolutions = [(320, 240), (640, 480), (800, 600)]
     resolution_index = self.resolutions.index(director.get_window_size())
     resolution_options = ['%dx%d' % (w, h) for w, h in self.resolutions]
     self.resolution = MultipleMenuItem('Resolution: ', self.change_resolution, resolution_options, default_item=resolution_index)
     # TODO: Handle unknown resolution.
     apply = MenuItem('Apply', self.apply)
     cancel = MenuItem('Cancel', self.cancel)
     
     items = (self.nickname, self.color, self.player_icon, self.resolution, apply, cancel)
     
     self.create_animated_menu(items)
     
     # Handle escape.
     self.on_quit = self.cancel
     
     self.config = config
예제 #2
0
 def validate_player(config):
     from random import choice
     
     from zombie.common import load_player_icons
     from zombie.constants import COLORS, DEFAULT_CONFIG, VALID_IP_HOSTNAME_CHARS
     
     section_name = 'player'
     default = DEFAULT_CONFIG[section_name]
     
     # Section: player
     if section_name not in config:
         config[section_name] = {}
     
     player = config[section_name]
     
     # player/nickname
     if 'nickname' in player:
         nickname = player['nickname']
         if isinstance(nickname, str):
             nickname = nickname.encode('ascii')
         for nickname_stop_index, char in enumerate(nickname, start=1):
             if nickname_stop_index == 10:
                 break
             elif char not in VALID_IP_HOSTNAME_CHARS:
                 break
         
         player['nickname'] = nickname[:nickname_stop_index]
     
     else:
         player['nickname'] = default['nickname']
     
     # player/icon
     player_icons = load_player_icons()
     
     if 'icon' not in player or int(player['icon']) not in player_icons:
         player['icon'] = next(iter(player_icons.keys()))
     else:
         player['icon'] = int(player['icon'])
     
     # player/color
     
     #random_light = lambda: randint(0, 255)
     #random_color = lambda: (random_light(), random_light(), random_light())
     color_options = tuple(COLORS.values())
     if 'color' not in player or player['color'] not in color_options:
         #color = player['color']
         #if not isinstance(color, tuple) or not len(color) == 3 or not all(0 <= int(x) <= 255 for x in color):
         player['color'] = choice(color_options)