def __init__(self, game, main_language, score): xml_game_node = game.xml_game_node self.globalvars = game.globalvars # here we create a new game object to play with # thus the original game won't be altered common.Game.__init__(self, self.globalvars, xml_game_node, game.level, score) self.score.enable_count_good() self.score.enable_count_wrong() self.score.enable_record_time() self.xml_game_node = xml_game_node # reading global parameters xml_global_parameters_node = self.xml_game_node.getElementsByTagName( "global_parameters")[0] # first of all, game setup xml_game_setup_node = self.xml_game_node.getElementsByTagName( "game_setup")[0] # possible values for types : # image # image_on_map (only to associate) # text self.type_to_associate = xml_funcs.getText( xml_game_setup_node.getElementsByTagName("type_to_associate")[0]) type_to_drag_node = xml_game_setup_node.getElementsByTagName( "type_to_drag")[0] self.type_to_drag = xml_funcs.getText(type_to_drag_node) sort = type_to_drag_node.getAttribute("sort") if (sort == None): self.sort = False elif (cmp(sort.lower(), "yes") == 0): self.sort = True else: self.sort = False # the min and max amounts of associations (good + bad ones) which will become draggable items self.min_draggable = xml_funcs.getInt( xml_game_setup_node.getElementsByTagName("min_draggable")[0]) self.max_draggable = xml_funcs.getInt( xml_game_setup_node.getElementsByTagName("max_draggable")[0]) item_to_associate_parameters_nodes = xml_global_parameters_node.getElementsByTagName( "item_to_associate") self.item_to_associate_parameters = [] for item_to_associate_parameters_node in item_to_associate_parameters_nodes: self.item_to_associate_parameters.append( xml_funcs.get_box(item_to_associate_parameters_node)) draggable_items_area_nodes = xml_global_parameters_node.getElementsByTagName( "draggable_items_area") self.draggable_items_areas = [] for draggable_items_area_node in draggable_items_area_nodes: spacing_x_nodes = draggable_items_area_node.getElementsByTagName( "spacing_x") spacing_y_nodes = draggable_items_area_node.getElementsByTagName( "spacing_y") if (len(spacing_x_nodes) > 0): spacing_x = xml_funcs.getFloat(spacing_x_nodes[0]) else: spacing_x = 0 if (len(spacing_y_nodes) > 0): spacing_y = xml_funcs.getFloat(spacing_y_nodes[0]) else: spacing_y = 0 draggable_items_area = ( xml_funcs.get_box(draggable_items_area_node), xml_funcs.getInt( draggable_items_area_node.getElementsByTagName("amount_x") [0]), xml_funcs.getInt( draggable_items_area_node.getElementsByTagName("amount_y") [0]), spacing_x, spacing_y) # TODO: make this under each area text_height_nodes = draggable_items_area_node.getElementsByTagName( "font_height") if (len(text_height_nodes) > 0): self.text_height = xml_funcs.getInt(text_height_nodes[0]) else: self.text_height = None self.draggable_items_areas.append(draggable_items_area) # global placeholders where to drag items # only present for non-map associations goal_nodes = xml_global_parameters_node.getElementsByTagName("goal") self.goals = [] for goal_node in goal_nodes: goal = Goal(goal_node) self.goals.append(goal) # space to display text legends text_legend_nodes = xml_global_parameters_node.getElementsByTagName( "text_legend_area") if (len(text_legend_nodes) > 0): self.text_legend_area = xml_funcs.get_box(text_legend_nodes[0]) else: self.text_legend_area = None # Map information (only present if type_to_associate is "on_map") map_nodes = xml_global_parameters_node.getElementsByTagName("map") if (len(map_nodes) > 0): map_node = map_nodes[0] (self.map_pos, self.map_size) = xml_funcs.get_box(map_node) map_filenames = map_node.getElementsByTagName("image") self.map_filename = xml_funcs.getText(map_filenames[0]) # reading associations associations_node = self.xml_game_node.getElementsByTagName( "associations")[0] associations = associations_node.getElementsByTagName("association") self.associations = [] for association_node in associations: association = Association(self) image_nodes = association_node.getElementsByTagName("image") for image_node in image_nodes: if (image_node.parentNode == association_node): # we ignore images which are not direct children # of the association (ie: images inside goal for instance) image_filename = xml_funcs.getText(image_node) if (cmp(image_node.getAttribute("type"), "") == 0): if (cmp(self.type_to_associate, "image") == 0): association.images_to_associate.append( image_filename) if (cmp(self.type_to_drag, "image") == 0): association.images_to_drag.append(image_filename) elif (cmp(image_node.getAttribute("type"), "to_associate") == 0): if ((cmp(self.type_to_associate, "image") == 0) or (cmp(self.type_to_associate, "image_on_map") == 0)): association.images_to_associate.append( image_filename) if (cmp(self.type_to_associate, "image_on_map") == 0): association.image_to_associate_pos_size = xml_funcs.get_box( image_node) else: common.warn( image_filename + " is supposed to be associated, but the game is not supposed to associate images" ) elif (cmp(image_node.getAttribute("type"), "to_drag") == 0): if ((cmp(self.type_to_drag, "image") == 0) or (cmp( self.type_to_associate, "image_on_map") == 0)): association.images_to_drag.append(image_filename) else: common.warn( image_filename + " is supposed to be dragged and dropped, but the game is not supposed to drag an drop images" ) # find potential associated sounds sound_nodes = image_node.getElementsByTagName("sound") for sound_node in sound_nodes: sound_node_lang = sound_node.getAttribute("lang") if ((cmp(sound_node_lang, "") == 0) or (cmp(sound_node_lang, main_language) == 0)): association.append_image_sound( image_filename, xml_funcs.getText(sound_node)) # find potential associated text legends # only texts with no lang tag or with lang tag = main_language are used text_legend_nodes = image_node.getElementsByTagName("text") for text_legend_node in text_legend_nodes: if ((cmp(text_legend_node.getAttribute("lang"), main_language) == 0) or (cmp(text_legend_node.getAttribute("key"), "") != 0)): association.append_image_text_legend( image_filename, xml_funcs.getText(text_legend_node, self.i18n_dict, main_language)) text_nodes = association_node.getElementsByTagName("text") for text_node in text_nodes: if (text_node.parentNode == association_node): text_lang = text_node.getAttribute("lang") text_should_be_added = False if (text_lang == ""): # if no lang attribute defined, the text is included text_should_be_added = True else: # if there is a lang attribute, we add the text only # if this language is the main language if (cmp(text_lang, main_language) == 0): text_should_be_added = True # the text node might be a dictionary key, in this case we also add it : if (cmp(text_node.getAttribute("key"), "") != 0): text_should_be_added = True if (text_should_be_added == True): text = xml_funcs.getText(text_node, self.i18n_dict, main_language) association.texts.append(text) sound_nodes = text_node.getElementsByTagName("sound") for sound_node in sound_nodes: sound_node_lang = sound_node.getAttribute("lang") if ((cmp(sound_node_lang, "") == 0) or (cmp(sound_node_lang, main_language) == 0)): association.append_text_sound( text, xml_funcs.getText(sound_node)) # goals local to only one association goal_nodes = association_node.getElementsByTagName("goal") if (len(goal_nodes) > 0): # TODO : allow for more than a goal ? goal_node = goal_nodes[0] if (cmp(self.type_to_associate, "image_on_map") == 0): goal = Goal(goal_node) # TODO : remove from here ? self.goals.append(goal) # TODO : put more than one goal association.associated_goal = goal else: common.warn( "<goal> found inside an association whereas type to associate is not image_on_map" ) self.associations.append(association) self.associations = common.randomize_list(self.associations)
def __init__(self, game, score): self.globalvars = game.globalvars # creation of a new game common.Game.__init__(self, self.globalvars, game.xml_game_node, game.level, score) self.score.enable_record_time() if (self.xml_game_node != None): # reading global parameters xml_global_parameters_node = self.xml_game_node.getElementsByTagName( "global_parameters")[0] # the amount of identical cards to find self.amount_identical_cards = xml_funcs.getInt( xml_global_parameters_node.getElementsByTagName( "identical_cards")[0]) # Grid setup grid_node = xml_global_parameters_node.getElementsByTagName( "grid")[0] self.grid_box = xml_funcs.get_box(grid_node) self.grid_amount_x = xml_funcs.getInt( grid_node.getElementsByTagName("amount_x")[0]) self.grid_amount_y = xml_funcs.getInt( grid_node.getElementsByTagName("amount_y")[0]) self.amount_different_cards = int( (1.0 * self.grid_amount_x * self.grid_amount_y) / self.amount_identical_cards) if (self.amount_different_cards != round( self.amount_different_cards)): common.error( "the amount of cards is not a multiple of the amount of cards to group" ) raise common.BadXMLException() spacing_x_nodes = grid_node.getElementsByTagName("spacing_x") spacing_y_nodes = grid_node.getElementsByTagName("spacing_y") if (len(spacing_x_nodes) > 0): self.spacing_x = xml_funcs.getFloat(spacing_x_nodes[0]) else: self.spacing_x = 0 if (len(spacing_y_nodes) > 0): self.spacing_y = xml_funcs.getFloat(spacing_y_nodes[0]) else: self.spacing_y = 0 # card setup card_node = xml_global_parameters_node.getElementsByTagName( "card")[0] front_card_node = card_node.getElementsByTagName("front")[0] self.front_card_file = xml_funcs.getText(front_card_node) back_card_node = card_node.getElementsByTagName("back")[0] self.back_card_file = xml_funcs.getText(back_card_node) image_area_nodes = xml_global_parameters_node.getElementsByTagName( "image_area") if (len(image_area_nodes) > 0): image_area_node = image_area_nodes[0] ratio_x_node = image_area_node.getElementsByTagName( "ratio_x")[0] self.ratio_x = xml_funcs.getFloat(ratio_x_node) ratio_y_node = image_area_node.getElementsByTagName( "ratio_y")[0] self.ratio_y = xml_funcs.getFloat(ratio_y_node) else: self.ratio_x = 100.0 self.ratio_y = 100.0 # reading associations # (here, only images with associated sounds are supported) associations_node = self.xml_game_node.getElementsByTagName( "associations")[0] associations = associations_node.getElementsByTagName( "association") self.associations = [] for association_node in associations: association = Association(self) image_nodes = association_node.getElementsByTagName("image") for image_node in image_nodes: image_filename = xml_funcs.getText(image_node) association.images.append(image_filename) sound_nodes = image_node.getElementsByTagName("sound") for sound_node in sound_nodes: association.append_image_sound( image_filename, xml_funcs.getText(sound_node)) self.associations.append(association) if (len(self.associations) < self.amount_different_cards): common.error("The XML file contains " + str(len(self.associations)) + " associations") common.error("But at least " + str(self.amount_different_cards) + " associations are required") raise common.BadXMLException() self.associations = common.randomize_list(self.associations)
def __init__(self, game, score) : self.globalvars = game.globalvars # creation of a new game common.Game.__init__(self, self.globalvars, game.xml_game_node, game.level, score) self.score.enable_record_time() if (self.xml_game_node != None) : # reading global parameters xml_global_parameters_node = self.xml_game_node.getElementsByTagName("global_parameters")[0] # the min and max amounts of differences to find self.min_differences = xml_funcs.getInt(xml_global_parameters_node.getElementsByTagName("min_differences")[0]) self.max_differences = xml_funcs.getInt(xml_global_parameters_node.getElementsByTagName("max_differences")[0]) # original image original_node = xml_global_parameters_node.getElementsByTagName("original")[0] self.original_box = xml_funcs.get_box(original_node) original_image_file_node = original_node.getElementsByTagName("image")[0] self.original_image_file = xml_funcs.getText(original_image_file_node) # modified image modified_node = xml_global_parameters_node.getElementsByTagName("modified")[0] self.modified_box = xml_funcs.get_box(modified_node) modified_image_file_node = modified_node.getElementsByTagName("image")[0] self.modified_image_file = xml_funcs.getText(modified_image_file_node) # original image size (useful for resized .light images) image_resolution_nodes = xml_global_parameters_node.getElementsByTagName("image_resolution") if (len(image_resolution_nodes) > 0) : image_resolution_node = image_resolution_nodes[0] self.original_width = float(image_resolution_node.getAttribute("width")) self.original_height = float(image_resolution_node.getAttribute("height")) else : # will be useful in case of an XML file with difference pos and size in percents and not pixels self.original_width = None self.original_height = None # images 'showing' the differences found_nodes = xml_global_parameters_node.getElementsByTagName("found")[0] self.found_file_names = [] found_file_nodes = found_nodes.getElementsByTagName("image") for found_file_node in found_file_nodes : self.found_file_names.append(xml_funcs.getText(found_file_node)) ratio_x_nodes = xml_global_parameters_node.getElementsByTagName("ratio_x") if (len(ratio_x_nodes) > 0) : self.ratio_x = (xml_funcs.getFloat(ratio_x_nodes[0])) / 100.0 else : self.ratio_x = 1.0 ratio_y_nodes = xml_global_parameters_node.getElementsByTagName("ratio_y") if (len(ratio_y_nodes) > 0) : self.ratio_y = (xml_funcs.getFloat(ratio_y_nodes[0])) / 100.0 else : self.ratio_y = 1.0 # reading differences main_differences_node = self.xml_game_node.getElementsByTagName("differences")[0] differences_nodes = main_differences_node.getElementsByTagName("difference") self.differences = [] for difference_node in differences_nodes : difference = Difference(self, difference_node) self.differences.append(difference) if (len(self.differences) < self.max_differences) : common.error("The XML file contains " + str(len(self.differences)) + " differences") common.error("But at least "+self.max_differences + " differences are required") common.error("set up more differences or change the max_differences value") raise common.BadXMLException()
def __init__(self, game, difference_node) : self.game = game self.difference_box = xml_funcs.get_box(difference_node)
def __init__(self, game, lang) : self.globalvars = game.globalvars # creation of a new game common.Game.__init__(self, self.globalvars, game.xml_game_node, game.level) if (self.xml_game_node != None) : # reading global parameters xml_global_parameters_node = self.xml_game_node.getElementsByTagName("global_parameters")[0] # first of all, game setup xml_game_setup_node = self.xml_game_node.getElementsByTagName("game_setup")[0] # the default amount of pieces on x and y axis self.pieces_amount_x = xml_funcs.getInt(xml_game_setup_node.getElementsByTagName("pieces_amount_x")[0]) self.pieces_amount_y = xml_funcs.getInt(xml_game_setup_node.getElementsByTagName("pieces_amount_y")[0]) # area for image : image_area_node = xml_global_parameters_node.getElementsByTagName("image_area")[0] (self.image_pos, self.image_size) = xml_funcs.get_box(image_area_node) # reading transformation items main_transformations = self.xml_game_node.getElementsByTagName("transformations")[0] transformation_nodes = main_transformations.getElementsByTagName("transformation") self.transformations = [] for transformation_node in transformation_nodes : transformation = Transformation(self) image_nodes = transformation_node.getElementsByTagName("image") # TODO : make this a bit more robust ? transformation.transformed_image_filename = xml_funcs.getText(image_nodes[0]) transformation.original_image_filename = xml_funcs.getText(image_nodes[1]) text_nodes = transformation_node.getElementsByTagName("text") if len(text_nodes) > 0 : for text_node in text_nodes : lang_attr = text_node.getAttribute("lang") if (lang_attr == "") : # if no "lang" attribute, we assume # the language of the text is the one expected transformation.text = xml_funcs.getText(text_node) if (cmp(lang_attr, lang) == 0) : # if we have the correct language, we # take the text and exit the loop transformation.text = xml_funcs.getText(text_node) break if (transformation.pieces_amount_x == 0) : transformation.pieces_amount_x = self.pieces_amount_x if (transformation.pieces_amount_y == 0) : transformation.pieces_amount_y = self.pieces_amount_y self.transformations.append(transformation) self.transformations = common.randomize_list(self.transformations)
def __init__(self, game, main_language): xml_game_node = game.xml_game_node self.globalvars = game.globalvars # here we create a new game object to play with # thus the original game won't be altered common.Game.__init__(self, self.globalvars, xml_game_node, game.level) self.xml_game_node = xml_game_node # reading global parameters xml_global_parameters_node = self.xml_game_node.getElementsByTagName( "global_parameters")[0] # first of all, game setup xml_game_setup_node = self.xml_game_node.getElementsByTagName( "game_setup")[0] # the min and max amounts of learning cards to display (which also happen to be draggable) self.min_draggable = xml_funcs.getInt( xml_game_setup_node.getElementsByTagName("min_display")[0]) self.max_draggable = xml_funcs.getInt( xml_game_setup_node.getElementsByTagName("max_display")[0]) # global placeholders where to draw learning cards item_area_nodes = xml_global_parameters_node.getElementsByTagName( "item_to_display_area") self.item_areas = [] for item_area_node in item_area_nodes: self.item_areas.append(Item_area(item_area_node)) # area to display text legends text_legend_nodes = xml_global_parameters_node.getElementsByTagName( "text_legend_area") if (len(text_legend_nodes) > 0): self.text_legend_area = xml_funcs.get_box(text_legend_nodes[0]) else: self.text_legend_area = None # reading learning_cards learning_cards_node = self.xml_game_node.getElementsByTagName( "learning_cards")[0] learning_cards = learning_cards_node.getElementsByTagName( "learning_card") self.learning_cards = [] for learning_card_node in learning_cards: learning_card = Learning_card() image_nodes = learning_card_node.getElementsByTagName("image") for image_node in image_nodes: if (image_node.parentNode == learning_card_node): # we ignore images which are not direct children # of the learning_card image_filename = xml_funcs.getText(image_node) learning_card.append_image_filename(image_filename) sound_nodes = learning_card_node.getElementsByTagName("sound") for sound_node in sound_nodes: sound_node_lang = sound_node.getAttribute("lang") if ((cmp(sound_node_lang, "") == 0) or (cmp(sound_node_lang, main_language) == 0)): learning_card.append_sound_filename( xml_funcs.getText(sound_node)) text_nodes = learning_card_node.getElementsByTagName("text") for text_node in text_nodes: if (text_node.parentNode == learning_card_node): text_lang = text_node.getAttribute("lang") text_should_be_added = False if (text_lang == ""): # if no lang attribute defined, the text is included text_should_be_added = True else: # if there is a lang attribute, we add the text only # if this language is the main language if (cmp(text_lang, main_language) == 0): text_should_be_added = True # the text node might be a dictionary key, in this case we also add it : if (cmp(text_node.getAttribute("key"), "") != 0): text_should_be_added = True if (text_should_be_added == True): text = xml_funcs.getText(text_node, self.i18n_dict, main_language) learning_card.append_text(text) self.learning_cards.append(learning_card) self.learning_cards = common.randomize_list(self.learning_cards)
def __init__(self, game, score) : self.globalvars = game.globalvars # creation of a new game common.Game.__init__(self, self.globalvars, game.xml_game_node, game.level, score) self.score.enable_record_time() if (self.xml_game_node != None) : # reading global parameters xml_global_parameters_node = self.xml_game_node.getElementsByTagName("global_parameters")[0] # the amount of identical cards to find self.amount_identical_cards = xml_funcs.getInt(xml_global_parameters_node.getElementsByTagName("identical_cards")[0]) # Grid setup grid_node = xml_global_parameters_node.getElementsByTagName("grid")[0] self.grid_box = xml_funcs.get_box(grid_node) self.grid_amount_x = xml_funcs.getInt(grid_node.getElementsByTagName("amount_x")[0]) self.grid_amount_y = xml_funcs.getInt(grid_node.getElementsByTagName("amount_y")[0]) self.amount_different_cards = int((1.0 * self.grid_amount_x * self.grid_amount_y) / self.amount_identical_cards) if (self.amount_different_cards != round(self.amount_different_cards)) : common.error("the amount of cards is not a multiple of the amount of cards to group") raise common.BadXMLException() spacing_x_nodes = grid_node.getElementsByTagName("spacing_x") spacing_y_nodes = grid_node.getElementsByTagName("spacing_y") if (len(spacing_x_nodes) > 0) : self.spacing_x = xml_funcs.getFloat(spacing_x_nodes[0]) else : self.spacing_x = 0 if (len(spacing_y_nodes) > 0) : self.spacing_y = xml_funcs.getFloat(spacing_y_nodes[0]) else : self.spacing_y = 0 # card setup card_node = xml_global_parameters_node.getElementsByTagName("card")[0] front_card_node = card_node.getElementsByTagName("front")[0] self.front_card_file = xml_funcs.getText(front_card_node) back_card_node = card_node.getElementsByTagName("back")[0] self.back_card_file = xml_funcs.getText(back_card_node) image_area_nodes = xml_global_parameters_node.getElementsByTagName("image_area") if (len(image_area_nodes) > 0) : image_area_node = image_area_nodes[0] ratio_x_node = image_area_node.getElementsByTagName("ratio_x")[0] self.ratio_x = xml_funcs.getFloat(ratio_x_node) ratio_y_node = image_area_node.getElementsByTagName("ratio_y")[0] self.ratio_y = xml_funcs.getFloat(ratio_y_node) else : self.ratio_x = 100.0 self.ratio_y = 100.0 # reading associations # (here, only images with associated sounds are supported) associations_node = self.xml_game_node.getElementsByTagName("associations")[0] associations = associations_node.getElementsByTagName("association") self.associations = [] for association_node in associations : association = Association(self) image_nodes = association_node.getElementsByTagName("image") for image_node in image_nodes : image_filename = xml_funcs.getText(image_node) association.images.append(image_filename) sound_nodes = image_node.getElementsByTagName("sound") for sound_node in sound_nodes : association.append_image_sound(image_filename, xml_funcs.getText(sound_node)) self.associations.append(association) if (len(self.associations) < self.amount_different_cards) : common.error("The XML file contains " + str(len(self.associations)) + " associations") common.error("But at least "+str(self.amount_different_cards) + " associations are required") raise common.BadXMLException() self.associations = common.randomize_list(self.associations)
def __init__(self, game, main_language): xml_game_node = game.xml_game_node self.globalvars = game.globalvars # here we create a new game object to play with # thus the original game won't be altered common.Game.__init__(self, self.globalvars, xml_game_node, game.level) self.xml_game_node = xml_game_node # reading global parameters xml_global_parameters_node = self.xml_game_node.getElementsByTagName("global_parameters")[0] # first of all, game setup xml_game_setup_node = self.xml_game_node.getElementsByTagName("game_setup")[0] # the min and max amounts of learning cards to display (which also happen to be draggable) self.min_draggable = xml_funcs.getInt(xml_game_setup_node.getElementsByTagName("min_display")[0]) self.max_draggable = xml_funcs.getInt(xml_game_setup_node.getElementsByTagName("max_display")[0]) # global placeholders where to draw learning cards item_area_nodes = xml_global_parameters_node.getElementsByTagName("item_to_display_area") self.item_areas = [] for item_area_node in item_area_nodes: self.item_areas.append(Item_area(item_area_node)) # area to display text legends text_legend_nodes = xml_global_parameters_node.getElementsByTagName("text_legend_area") if len(text_legend_nodes) > 0: self.text_legend_area = xml_funcs.get_box(text_legend_nodes[0]) else: self.text_legend_area = None # reading learning_cards learning_cards_node = self.xml_game_node.getElementsByTagName("learning_cards")[0] learning_cards = learning_cards_node.getElementsByTagName("learning_card") self.learning_cards = [] for learning_card_node in learning_cards: learning_card = Learning_card() image_nodes = learning_card_node.getElementsByTagName("image") for image_node in image_nodes: if image_node.parentNode == learning_card_node: # we ignore images which are not direct children # of the learning_card image_filename = xml_funcs.getText(image_node) learning_card.append_image_filename(image_filename) sound_nodes = learning_card_node.getElementsByTagName("sound") for sound_node in sound_nodes: sound_node_lang = sound_node.getAttribute("lang") if (cmp(sound_node_lang, "") == 0) or (cmp(sound_node_lang, main_language) == 0): learning_card.append_sound_filename(xml_funcs.getText(sound_node)) text_nodes = learning_card_node.getElementsByTagName("text") for text_node in text_nodes: if text_node.parentNode == learning_card_node: text_lang = text_node.getAttribute("lang") text_should_be_added = False if text_lang == "": # if no lang attribute defined, the text is included text_should_be_added = True else: # if there is a lang attribute, we add the text only # if this language is the main language if cmp(text_lang, main_language) == 0: text_should_be_added = True # the text node might be a dictionary key, in this case we also add it : if cmp(text_node.getAttribute("key"), "") != 0: text_should_be_added = True if text_should_be_added == True: text = xml_funcs.getText(text_node, self.i18n_dict, main_language) learning_card.append_text(text) self.learning_cards.append(learning_card) self.learning_cards = common.randomize_list(self.learning_cards)
def __init__(self, game, main_language, score) : xml_game_node = game.xml_game_node self.globalvars = game.globalvars # here we create a new game object to play with # thus the original game won't be altered common.Game.__init__(self, self.globalvars, xml_game_node, game.level, score) self.score.enable_count_good() self.score.enable_count_wrong() self.score.enable_record_time() self.xml_game_node = xml_game_node # reading global parameters xml_global_parameters_node = self.xml_game_node.getElementsByTagName("global_parameters")[0] # first of all, game setup xml_game_setup_node = self.xml_game_node.getElementsByTagName("game_setup")[0] # possible values for types : # image # image_on_map (only to associate) # text self.type_to_associate = xml_funcs.getText(xml_game_setup_node.getElementsByTagName("type_to_associate")[0]) type_to_drag_node = xml_game_setup_node.getElementsByTagName("type_to_drag")[0] self.type_to_drag = xml_funcs.getText(type_to_drag_node) sort = type_to_drag_node.getAttribute("sort") if (sort == None) : self.sort = False elif (cmp(sort.lower(), "yes") == 0) : self.sort = True else : self.sort = False # the min and max amounts of associations (good + bad ones) which will become draggable items self.min_draggable = xml_funcs.getInt(xml_game_setup_node.getElementsByTagName("min_draggable")[0]) self.max_draggable = xml_funcs.getInt(xml_game_setup_node.getElementsByTagName("max_draggable")[0]) item_to_associate_parameters_nodes = xml_global_parameters_node.getElementsByTagName("item_to_associate") self.item_to_associate_parameters = [] for item_to_associate_parameters_node in item_to_associate_parameters_nodes : self.item_to_associate_parameters.append(xml_funcs.get_box(item_to_associate_parameters_node)) draggable_items_area_nodes = xml_global_parameters_node.getElementsByTagName("draggable_items_area") self.draggable_items_areas = [] for draggable_items_area_node in draggable_items_area_nodes : spacing_x_nodes = draggable_items_area_node.getElementsByTagName("spacing_x") spacing_y_nodes = draggable_items_area_node.getElementsByTagName("spacing_y") if (len(spacing_x_nodes) > 0) : spacing_x = xml_funcs.getFloat(spacing_x_nodes[0]) else : spacing_x = 0 if (len(spacing_y_nodes) > 0) : spacing_y = xml_funcs.getFloat(spacing_y_nodes[0]) else : spacing_y = 0 draggable_items_area = (xml_funcs.get_box(draggable_items_area_node), xml_funcs.getInt(draggable_items_area_node.getElementsByTagName("amount_x")[0]), xml_funcs.getInt(draggable_items_area_node.getElementsByTagName("amount_y")[0]), spacing_x, spacing_y) # TODO: make this under each area text_height_nodes = draggable_items_area_node.getElementsByTagName("font_height") if (len(text_height_nodes) > 0) : self.text_height = xml_funcs.getInt(text_height_nodes[0]) else : self.text_height = None self.draggable_items_areas.append(draggable_items_area) # global placeholders where to drag items # only present for non-map associations goal_nodes = xml_global_parameters_node.getElementsByTagName("goal") self.goals = [] for goal_node in goal_nodes : goal = Goal(goal_node) self.goals.append(goal) # space to display text legends text_legend_nodes = xml_global_parameters_node.getElementsByTagName("text_legend_area") if (len(text_legend_nodes) > 0) : self.text_legend_area = xml_funcs.get_box(text_legend_nodes[0]) else : self.text_legend_area = None # Map information (only present if type_to_associate is "on_map") map_nodes = xml_global_parameters_node.getElementsByTagName("map") if (len(map_nodes) > 0) : map_node = map_nodes[0] (self.map_pos, self.map_size) = xml_funcs.get_box(map_node) map_filenames = map_node.getElementsByTagName("image") self.map_filename = xml_funcs.getText(map_filenames[0]) # reading associations associations_node = self.xml_game_node.getElementsByTagName("associations")[0] associations = associations_node.getElementsByTagName("association") self.associations = [] for association_node in associations : association = Association(self) image_nodes = association_node.getElementsByTagName("image") for image_node in image_nodes : if (image_node.parentNode == association_node) : # we ignore images which are not direct children # of the association (ie: images inside goal for instance) image_filename = xml_funcs.getText(image_node) if (cmp(image_node.getAttribute("type"), "") == 0) : if (cmp(self.type_to_associate, "image") == 0) : association.images_to_associate.append(image_filename) if (cmp(self.type_to_drag, "image") == 0) : association.images_to_drag.append(image_filename) elif (cmp(image_node.getAttribute("type"), "to_associate") == 0) : if ((cmp(self.type_to_associate, "image") == 0) or (cmp(self.type_to_associate, "image_on_map") == 0)) : association.images_to_associate.append(image_filename) if (cmp(self.type_to_associate, "image_on_map") == 0) : association.image_to_associate_pos_size = xml_funcs.get_box(image_node) else : common.warn(image_filename + " is supposed to be associated, but the game is not supposed to associate images") elif (cmp(image_node.getAttribute("type"), "to_drag") == 0) : if ((cmp(self.type_to_drag, "image") == 0) or (cmp(self.type_to_associate, "image_on_map") == 0)) : association.images_to_drag.append(image_filename) else : common.warn(image_filename + " is supposed to be dragged and dropped, but the game is not supposed to drag an drop images") # find potential associated sounds sound_nodes = image_node.getElementsByTagName("sound") for sound_node in sound_nodes : sound_node_lang = sound_node.getAttribute("lang") if ((cmp(sound_node_lang, "") == 0) or (cmp(sound_node_lang, main_language) == 0)) : association.append_image_sound(image_filename, xml_funcs.getText(sound_node)) # find potential associated text legends # only texts with no lang tag or with lang tag = main_language are used text_legend_nodes = image_node.getElementsByTagName("text") for text_legend_node in text_legend_nodes : if ((cmp(text_legend_node.getAttribute("lang"), main_language) == 0) or (cmp(text_legend_node.getAttribute("key"), "") != 0)) : association.append_image_text_legend(image_filename, xml_funcs.getText(text_legend_node, self.i18n_dict, main_language)) text_nodes = association_node.getElementsByTagName("text") for text_node in text_nodes : if (text_node.parentNode == association_node) : text_lang = text_node.getAttribute("lang") text_should_be_added = False if (text_lang == "") : # if no lang attribute defined, the text is included text_should_be_added = True else : # if there is a lang attribute, we add the text only # if this language is the main language if (cmp(text_lang, main_language) == 0) : text_should_be_added = True # the text node might be a dictionary key, in this case we also add it : if (cmp(text_node.getAttribute("key"), "") != 0) : text_should_be_added = True if (text_should_be_added == True) : text = xml_funcs.getText(text_node, self.i18n_dict, main_language) association.texts.append(text) sound_nodes = text_node.getElementsByTagName("sound") for sound_node in sound_nodes : sound_node_lang = sound_node.getAttribute("lang") if ((cmp(sound_node_lang, "") == 0) or (cmp(sound_node_lang, main_language) == 0)) : association.append_text_sound(text, xml_funcs.getText(sound_node)) # goals local to only one association goal_nodes = association_node.getElementsByTagName("goal") if (len(goal_nodes) > 0) : # TODO : allow for more than a goal ? goal_node = goal_nodes[0] if (cmp(self.type_to_associate, "image_on_map") == 0) : goal = Goal(goal_node) # TODO : remove from here ? self.goals.append(goal) # TODO : put more than one goal association.associated_goal = goal else : common.warn("<goal> found inside an association whereas type to associate is not image_on_map") self.associations.append(association) self.associations = common.randomize_list(self.associations)