def map_exists(mapname, asset_type=AssetType.MAP): try: map_path = get_asset(asset_type, mapname) with open(map_path): return True except IOError: return False
def save_settings_to_file(self, settings): try: settings_path = get_asset(AssetType.ATTRIBUTES, "settings.cfg") with open(settings_path, 'w') as settings_file: for key, value in settings.items(): settings_file.write("{} {}\n".format(key.value, value)) except IOError as e: print("Unable to save settings file. Exception: {}".format(e))
def load_map_from_file(mapname, asset_type=AssetType.MAP): try: map_path = get_asset(asset_type, mapname) with open(map_path) as mapfile: return parse_map_from_string(mapfile.read()) except IOError as e: print("Unable to load file {}. Generating new map. Exception: {}".format(mapname, e)) return generate_map()
def read_upgrade_attributes_from_file(): try: attributes_path = get_asset(AssetType.ATTRIBUTES, "upgradeattributes.cfg") with open(attributes_path) as attributes_file: return json.loads(attributes_file.read().replace('\n', ''), object_hook=as_enum) except IOError as e: print("Unable to load upgrade attributes. Exception: {}".format(e)) return None
def get_logger(name, level, filename): handler = logging.FileHandler(get_asset(AssetType.LOG, filename)) handler.setFormatter(formatter) logger = logging.getLogger(name) logger.setLevel(level) logger.addHandler(handler) return logger
def load_campaign_progress(): try: all_maps = get_loadable_maps(AssetType.CAMPAIGN_MAP) progress_path = get_asset(AssetType.ATTRIBUTES, "campaign.cfg") progress = [] with open(progress_path) as progress_file: lines = progress_file.read() for line in StringIO(lines): # Format: <mapname> values = line.rstrip().split(' ') if len(values) == 1 and values[0] in all_maps: progress.append(values[0]) return progress except (IOError, KeyError) as e: print("Unable to load campaign file. Exception: {}".format(e))
def load_settings_from_file(self): try: settings_path = get_asset(AssetType.ATTRIBUTES, "settings.cfg") file_settings = {} with open(settings_path) as settings_file: lines = settings_file.read() for line in StringIO(lines): values = line.split(' ') if len(values) == 2: setting = Setting[values[0]] if setting in numeric_settings: file_settings[setting] = int(values[1]) else: file_settings[setting] = values[1].rstrip() return file_settings except (IOError, KeyError) as e: print("Unable to load settings file. Using defaults. Exception: {}".format(e)) return default_settings.copy()
def save_campaign_progress(map_name): all_maps = get_loadable_maps(AssetType.CAMPAIGN_MAP) progress = load_campaign_progress() # Trim the map name if necessary (.sav -> .map) if map_name.endswith(".sav"): map_name = map_name[:-4] + ".map" # Update the progress structure if map_name in all_maps: progress.append(map_name) # Make the items unique progress = list(set(progress)) # Serialize and save the progress structure back to the file try: progress_path = get_asset(AssetType.ATTRIBUTES, "campaign.cfg") with open(progress_path, 'w') as progress_file: for mapname in progress: progress_file.write("{}\n".format(mapname)) except IOError as e: print("Unable to save campaign file. Exception: {}".format(e))
def get_loadable_maps(asset_type=AssetType.MAP): maps = [] for (_, _, filenames) in walk(get_asset(asset_type, "")): maps.extend([filename for filename in filenames if filename.endswith((".map", ".sav"))]) return maps
def check_log_dir_exists(): path = Path(get_asset(AssetType.LOG)) if not path.exists(): path.mkdir(exist_ok=True)
def load_sound(resource_name): sound = pygame.mixer.Sound(get_asset(AssetType.SOUND, resource_name)) sound.set_volume(SETTINGS.get(Setting.SFX_VOLUME) / 100) all_sounds.append(sound) return sound
# Load a sound and properly set it up def load_sound(resource_name): sound = pygame.mixer.Sound(get_asset(AssetType.SOUND, resource_name)) sound.set_volume(SETTINGS.get(Setting.SFX_VOLUME) / 100) all_sounds.append(sound) return sound pygame.mixer.init() # General unit_palette = { Team.RED: generate_palette_list( pygame.image.load( get_asset(AssetType.SPRITE, "palettes/Red-Palette.png"))), Team.BLUE: generate_palette_list( pygame.image.load( get_asset(AssetType.SPRITE, "palettes/Blue-Palette.png"))), Team.GREEN: generate_palette_list( pygame.image.load( get_asset(AssetType.SPRITE, "palettes/Green-Palette.png"))), Team.YELLOW: generate_palette_list( pygame.image.load( get_asset(AssetType.SPRITE, "palettes/Yellow-Palette.png"))), Team.NONE: generate_palette_list( pygame.image.load(