def create_map(name, config_path, textures_path, zone): """Creates a new 'Map' object and returns a reference to its class. Parameters: 'name' - name of the map 'config_path' - name of the config image of the map 'textures_parh' - name of the textures image of the map 'zone' - map's initial zone """ try: if not isinstance(name, str): raise TypeError("error! 'name' parameter must be a string") elif not isinstance(config_path, str): raise TypeError("error! 'config_path' parameter must be a string") elif not isinstance(textures_path, str): raise TypeError("error! 'textures_path' parameter must be a string") elif not type(zone) is int: raise TypeError("error! 'zone' parameter must be an integer") except TypeError as err: windows.show_error(err, traceback.extract_stack()[-1]) else: return maps.Map(PATH_MAPS + config_path, PATH_MAPS + textures_path, zone)
def place_unit(unit, x, y): """Places a unit at the specified coordinates on the map. Parameters: 'unit' - one of the game's units 'x' - x-coordinate on the map 'y' - y-coordinate on the map """ try: if not isinstance(unit, characters.Character) \ and not isinstance(unit, creatures.Creature): raise Exception("error! invalid 'unit' parameter") elif not type(x) is int or not x in range(0, 1025 - unit.rect.width): raise Exception("error! invalid 'x' parameter") elif not type(y) is int or not y in range(0, 769 - unit.rect.height): raise Exception("error! invalid 'y' parameter") elif unit in globals_.units_in_game: raise Exception("error! the unit is already in game") except Exception as err: windows.show_error(err, traceback.extract_stack()[-1]) else: unit.rect.x = x unit.rect.y = y globals_.units_in_game.add(unit)
def move_unit_steps(unit, steps, direction): """Moves a unit a specified amount of steps in a given direction. Parameters: 'unit' - one of the game's unit types (i.e. Character, Creature etc.) 'distance' - amount of steps by which the object will be moved 'direction' - side towards which the object will be moved """ try: if not isinstance(unit, characters.Character) \ and not isinstance(unit, creatures.Creature): raise Exception("error! invalid 'unit' parameter") elif not steps or not type(steps) is int: raise Exception("error! 'steps' parameter must be an integer") elif not direction or not type(direction) is int: raise Exception("error! 'direction' parameter must be an integer") elif not unit in globals_.units_in_game: raise Exception("error! the unit is not on the map") except Exception as err: windows.show_error(err, traceback.extract_stack()[-1]) else: if _possible_to_move(unit, steps * unit.speed, direction): if direction == UP or direction == DOWN: globals_.units_to_move.append([unit, steps * unit.speed, direction, unit.rect.y]) elif direction == LEFT or direction == RIGHT: globals_.units_to_move.append([unit, steps * unit.speed, direction, unit.rect.x])
def can_move(unit, distance, direction): """Checks whether a unit can move a specified distance in a given direction. Parameters: 'unit' - one of the game's unit types (i.e. Character, Creature etc.) 'distance' - amount of pixels by which the object will be moved 'direction' - side towards which the object will be moved """ try: if not isinstance(unit, characters.Character) \ and not isinstance(unit, creatures.Creature): raise TypeError("error! invalid 'unit' type") elif not type(distance) is int: raise TypeError("error! 'distance' parameter must be an integer") elif not direction in (8, 2, 4, 6): raise Exception("error! invalid 'direction' parameter") elif not unit in globals_.units_in_game: raise Exception("error! the unit has to be \ placed on the map.") except (Exception, TypeError) as err: windows.show_error(err, traceback.extract_stack()[-1]) else: if _possible_to_move(unit, distance, direction): return True else: return False
def create_human(name, spritesheet, armor=None, inventory=None, weapon=None): """Creates a new 'Human' object and returns a reference to its class. Parameters: 'name' - name of the unit 'spritesheet' - name of the file containing character's animations 'armor' - Armor object to be used by the unit 'inventory' - Inventory object to be used by the unit 'weapon' - Weapon object to be used by the unit """ try: if not isinstance(name, str) or not len(name) <= 15: raise Exception("error! invalid 'name' parameter") elif not isinstance(spritesheet, str): raise Exception("error! invalid 'spritesheet' parameter") elif not armor is None and not isinstance(armor, armors.Armor): raise Exception("error! invalid 'armor' parameter") elif not inventory is None and not isinstance(inventory, windows.Inventory): raise Exception("error! invalid 'inventory' parameter") elif not weapon is None and not isinstance(weapon, weapons.Weapon): raise Exception("error! invalid 'weapon' parameter") except Exception as err: windows.show_error(err, traceback.extract_stack()[-1]) else: return characters.Human(name, PATH_CHARACTERS + spritesheet, armor, inventory, weapon)
def units_in_range(unit1, unit2, range_=48): """Checks whether two units are in range. Parameters: 'unit1' - one of the game's units 'unit2' - one of the game's units 'range_' - maximum distance between the units """ try: if not isinstance(unit1, characters.Character) \ or not isinstance(unit2, creatures.Creature): raise TypeError("error! invalid 'unit1' parameter") elif not isinstance(unit2, characters.Character) \ or not isinstance(unit2, creatures.Creature): raise TypeError("error! invalid 'unit2' parameter") except TypeError as err: windows.show_error(err, traceback.extract_stack()[-1]) else: if unit2.rect.x in range(unit1.rect.x - range_, unit1.rect.x + unit1.rect.width + \ range_ + 1) \ and unit2.rect.y in range(unit1.rect.y - range_, unit1.rect.y + unit1.rect.height + \ range_ + 1): return True else: return False
def save_game_state(data): """Saves game's current state to a file.""" try: # Saves data to a file. with open(PATH_DATA + "data2.epic", "wb") as state: cPickle.dump(data, state) except IOError as err: # Displays an error on the screen. windows.show_error(err, traceback.extract_stack()[-1])
def load_game_state(): """Loads and returns game's previously saved state.""" try: with open(PATH_DATA + "data3.epic", "rb") as data: state = cPickle.load(data) # validate the files except IOError as err: windows.show_error(err, traceback.extract_stack()[-1]) else: return state
def save_game_files(data): """Saves game's files, such as characters, items and maps to a file. """ try: # Saves data to a file. with open(PATH_DATA + "data1.epic", "wb") as files: cPickle.dump(data, files) except IOError as err: # Displays an error on the screen. windows.show_error(err, traceback.extract_stack()[-1])
def load_game_files(): """Loads and returns game's objects, such as characters, items, maps etc. """ try: with open(PATH_DATA + "data1.epic", "rb") as data: files = cPickle.load(data) # validate the files except IOError as err: windows.show_error(err, traceback.extract_stack()[-1]) else: return files
def event(code): """Adds an event to the game's stack. Parameters 'code' - Python code to be executed """ try: if not type(code) is str: raise TypeError("error! 'code' parameter must be a string") except TypeError as err: windows.show_error(err, traceback.extract_stack()[-1]) else: globals_.events.append(code)
def set_map(map_): """Sets a map to be currently displayed on the screen. Parameters: "map_" - a Map object """ try: if not isinstance(map_, maps.Map): raise Exception("error! invalid 'map_' parameter") except Exception as err: windows.show_error(err, traceback.extract_stack()[-1]) else: globals_.current_map = map_
def place_item(item_id, x, y): try: if not item_id in ITEMS: raise Exception("error! incorrect 'item_id' parameter") except Exception as err: windows.show_error(err, traceback.extract_stack()[-1]) else: if item_id == MEDICINE: item = items.Medicine() elif item_id == JEWEL: item = items.Jewel() item.rect.x = x item.rect.y = y globals_.objects_in_game.add(item)
def set_background_sound(sound): """Sets a background sound to be played during the game. Parameters: 'sound' - name of the song to be played """ try: if not isinstance(sound, str): raise TypeError("error! 'music' parameter must be a string") except TypeError as err: windows.show_error(err, traceback.extract_stack()[-1]) else: if not PATH_MUSIC + sound == globals_.current_bg_sound[0]: globals_.current_bg_sound[0] = PATH_MUSIC + sound globals_.current_bg_sound[1] = True
def set_music(music): """Sets the current music to be played during the game. Parameters: 'music' - name of the song to be played """ try: if not isinstance(music, str): raise TypeError("error! 'music' parameter must be a string") except TypeError as err: windows.show_error(err, traceback.extract_stack()[-1]) else: if not PATH_MUSIC + music == globals_.current_music[0]: globals_.current_music[0] = PATH_MUSIC + music globals_.current_music[1] = True
def set_player_character(character): """Sets a character to be controllable by the player. Parameters: 'character' - one of the game's characters """ try: if not isinstance(character, characters.Character): raise Exception("error! invalid 'unit' parameter") elif not character in globals_.units_in_game: raise Exception("error! given character is not placed on the map") except Exception as err: windows.show_error(err, traceback.extract_stack()[-1]) else: globals_.units_in_game.remove(character) globals_.player_unit = character
def load_human(name): """Loads a Human from data and returns a reference to is class. Parameters 'name' - name of the unit, specified in the data """ class FileError(Exception): pass class UnitNameError(Exception): pass try: if not type(name) is str or len(name) == 0: raise TypeError("error! incorrect 'name' parameter") unit = None for first in globals_.game_files: if first == "Human": for second in globals_.game_files[first]: if second.get("name") == name: unit = second break else: raise UnitNameError("error! '" + name + "' does not exist") break else: raise FileError("error! 'data1.epic' file is corrupted") except (FileError, TypeError, UnitNameError) as err: windows.show_error(err, traceback.extract_stack()[-1]) else: human = characters.Human(unit.get("name"), PATH_CHARACTERS + unit.get("image"), None, None, unit.get("weapon")) human.defense = unit.get("defense") human.speed = unit.get("speed") human.agility = unit.get("agility") human.skills = unit.get("skills") return human
def load_dialogs(text_file): try: dialogs = {} with open(PATH_TEXTS + text_file, "r") as fh: while True: tmp = fh.readline() if tmp == "": break id_ = tmp[2:-1] dialog = fh.readline()[2:-1] dialogs[id_] = dialog except IOError as err: windows.show_error(err, traceback.extract_stack()[-1]) else: return dialogs
def turn_unit(unit, direction): """Turns a unit to a specified direction. Parameters: 'unit' - one of the game's unit types (i.e. Character, Creature etc.) 'direction' - side towards which the unit should turn to """ try: if not isinstance(unit, characters.Character) \ or not isinstance(unit, creatures.Creature): raise TypeError("error! incorrect 'unit' parameter") elif not direction or not type(direction) is int \ or not direction in (UP, DOWN, LEFT, RIGHT): raise TypeError("error! incorrect 'direction' parameter") except TypeError as err: windows.show_error(err, traceback.extract_stack()[-1]) else: unit.turn(direction)
def load_map(name): """Loads a map from data and returns a reference to its class. Parameters: 'name' - name of the map, specified in the data """ class FileError(Exception): pass class UnitNameError(Exception): pass try: if not type(name) is str or len(name) == 0: raise TypeError("error! incorrect 'name' parameter") tmp_map = None for first in globals_.game_files: if first == "Map": for second in globals_.game_files[first]: if second.get("name") == name: tmp_map = second break else: raise UnitNameError("error! '" + name + "' does not exist") break else: raise FileError("error! 'data1.epic' file is corrupted") except (FileError, TypeError, UnitNameError) as err: windows.show_error(err, traceback.extract_stack()[-1]) else: map_ = maps.Map(PATH_MAPS + tmp_map.get("config"), PATH_MAPS + tmp_map.get("textures"), tmp_map.get("zone")) return map_
def attack_character(character1, character2): try: raise NotImplementedError("error! 'attack_character' function \ has not been implemented yet") except NotImplementedError as err: windows.show_error(err, traceback.extract_stack()[-1])
def create_titan(name, spritesheet): try: raise NotImplementedError("error! 'create_titan' function \ has not been implemented yet") except NotImplementedError as err: windows.show_error(err, traceback.extract_stack()[-1])
def say(character, text_id): try: raise NotImplementedError("error! 'say' function \ has not been implemented yet") except NotImplementedError as err: windows.show_error(err, traceback.extract_stack()[-1])