コード例 #1
0
ファイル: common.py プロジェクト: edupals/omnitux
class Level():
    # a difficulty level contains one or more games

    # TODO : allow different rounds amount depending on level

    def __init__(self, globalvars, xml_level_node, game_set):

        self.globalvars = globalvars

        self.games = []
        self.xml_level_node = xml_level_node

        self.game_set = game_set

        self.game_rounds_amount = self.game_set.game_rounds_amount

        try:
            self.difficulty = self.xml_level_node.getAttribute("difficulty")
        except Exception, e:
            # not clean...
            # we arrive here if the xml_level_node happens to be a game (faked level for backwards compatibility)
            self.difficulty = "1"

        # amount of game rounds for this particular level
        self.rounds_amount_nodes = self.xml_level_node.getElementsByTagName(
            "rounds")
        if (len(self.rounds_amount_nodes) > 0):
            if (self.rounds_amount_nodes[0].parentNode == self.xml_level_node):
                # here, we override the game_rounds_amount value which came from the game_set
                # object
                self.game_rounds_amount = xml_funcs.getInt(
                    self.rounds_amount_nodes[0])

        self.xml_game_nodes = self.xml_level_node.getElementsByTagName("game")

        if (len(self.xml_game_nodes) == 0):
            error(" no game tag found")

            raise BadXMLException()

        for xml_game_node in self.xml_game_nodes:
            game = Game(self.globalvars, xml_game_node, self, None)
            self.games.append(game)
コード例 #2
0
    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)
コード例 #3
0
ファイル: associate.py プロジェクト: edupals/omnitux
    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)
コード例 #4
0
ファイル: common.py プロジェクト: Trilarion/omnitux2
	def __init__(self, globalvars, xml_game_node, level, score = None) :

		self.globalvars = globalvars

		self.xml_game_node = xml_game_node
		self.level = level
		
		self.game_type = ""
		self.xml_file_version = ""
		self.languages = []
		self.i18n_dict = self.level.game_set.i18n_dict
		self.author = ""
		self.titles = {}
		self.thumb = None


		if (score != None) :
			self.score = score
		else :
			self.score = Score(globalvars)


		# TODO : implement custom music for a given game
		self.music_files = constants.default_music_files
	
		self.game_type = self.xml_game_node.getAttribute("type")

		
		
		self.xml_file_version = self.xml_game_node.getAttribute("xml_version")
		self.author = self.xml_game_node.getAttribute("author")
		
		title_nodes = self.xml_game_node.getElementsByTagName("title")

		if (len(title_nodes) == 0) :
			# no title found, we try to get it from the game_set above
			title_nodes = self.xml_game_node.parentNode.getElementsByTagName("title")

			if (len(title_nodes) == 0) :
				# yet no title ? we are probably inside a level tag
				# so let's go up again
				title_nodes = self.xml_game_node.parentNode.parentNode.getElementsByTagName("title")
		
		for title_node in title_nodes :
			title = xml_funcs.getText(title_node)
			language = title_node.getAttribute("lang")
			
			self.languages.append(language)
			self.titles[language] = title
		
		# eventual thumbnail
		thumb_nodes = self.xml_game_node.getElementsByTagName("thumbnail")
		
		if (len(thumb_nodes) > 0) :
			self.thumb = xml_funcs.getText(thumb_nodes[0])
		
		background_nodes = self.xml_game_node.getElementsByTagName("background")
		
		self.backgrounds = []
		for background_node in background_nodes :

			background_file = xml_funcs.getText(background_node)
			
			background_mode = background_node.getAttribute("mode")

			if (background_mode == "") :
				# by default, the background will be stretched to fill all the screen
				background_mode = "FILL"

			self.backgrounds.append((background_file, background_mode))


		music_nodes = self.xml_game_node.getElementsByTagName("music")
		if (len(music_nodes) > 0) :
			# Only the first node is taken into account
			music_node = music_nodes[0]
			
			self.music_mode = music_node.getAttribute("mode")
			
			self.music_files = []
			
			music_files = music_node.getElementsByTagName("file")
			for music_file in music_files :
				self.music_files.append(music_files)
			
			if (len(self.music_files) == 0) :
				self.music_files = constants.default_music_files
			
		else :
			self.music_mode = "on"
			self.music_files = constants.default_music_files


		self.rounds_amount = self.xml_game_node.getElementsByTagName("rounds")
		if (len(self.rounds_amount) > 0) :
			self.rounds_amount = xml_funcs.getInt(self.rounds_amount[0])
		else :
			self.rounds_amount = constants.infinite
			
		self.rounds_done = 0
コード例 #5
0
ファイル: common.py プロジェクト: Trilarion/omnitux2
	def __init__(self, globalvars, xml_filename) :

		self.globalvars = globalvars

		self.levels = []

		self.xml_filename = xml_filename

		self.languages = []

		self.titles = {}
		self.thumb = None

		self.tags = []

		self.game_set_node = None


		try :
			self.xml_data = xml.dom.minidom.parse(xml_filename)

			# info("Loading file " + xml_filename)

		except :
			error("Impossible to read the contents of "+xml_filename)
			error("File format might be incorrect")

			raise BadXMLException()

		# trying to load an i18n dictionary
		self.i18n_dict = i18n.i18n_dict(self.xml_data)

		self.xml_game_set_nodes = self.xml_data.getElementsByTagName("game_set")

		if (len(self.xml_game_set_nodes) > 0) :
			# we enter there if the file is a game_set
			self.game_set_node = self.xml_game_set_nodes[0]

			# get the title of the game_set in different languages
			title_nodes = self.game_set_node.getElementsByTagName("title")
			
			for title_node in title_nodes :
				if (title_node.parentNode == self.game_set_node) :

					title = xml_funcs.getText(title_node)
					language = title_node.getAttribute("lang")
					
					self.languages.append(language)
					self.titles[language] = title

			# eventual thumbnail
			thumb_nodes = self.game_set_node.getElementsByTagName("thumbnail")
		
			if (len(thumb_nodes) > 0) :
				self.thumb = xml_funcs.getText(thumb_nodes[0])

			# amount of game rounds (applies by default to all levels)
			self.rounds_amount_nodes = self.game_set_node.getElementsByTagName("rounds")
			if (len(self.rounds_amount_nodes) > 0) :
				if (self.rounds_amount_nodes[0].parentNode == self.game_set_node) :
					self.game_rounds_amount = xml_funcs.getInt(self.rounds_amount_nodes[0])
				else :
					self.game_rounds_amount = constants.infinite
			else :
				self.game_rounds_amount = constants.infinite

		else :
			# we're here if the file was only made of a 'game' and not a 'game_set'
			self.game_rounds_amount = 1

			# we 'fake', the game node is considered a game_set
			self.game_set_node = self.xml_data


		self.xml_level_nodes = self.game_set_node.getElementsByTagName("level")

		if (len(self.xml_level_nodes) == 0) :
			# no level defined, so we let the game_set act like a level
			level = Level(self.globalvars, self.game_set_node, self)
			self.levels.append(level)

		else :

			for xml_level_node in self.xml_level_nodes :
				level = Level(self.globalvars, xml_level_node, self)
				self.levels.append(level)



		# TODO allow multiple types games
		self.game_type = self.levels[0].games[0].game_type



		# TODO put real tags

		xml_tag_nodes = self.game_set_node.getElementsByTagName("tag")

		if (len(xml_tag_nodes) > 0) :
			for xml_tag_node in xml_tag_nodes :
				tag = xml_funcs.getText(xml_tag_node).lower()
				self.tags.append(tag)

		else :
			# if no tags defined, the game type becomes a tag
			self.tags = [self.levels[0].games[0].game_type]



		# get the indexes for sorting in menu

		self.menu_index = {}

		menu_index_nodes = self.game_set_node.getElementsByTagName("menu_index")

		self.max_menu_index_len = 1

		if (len(menu_index_nodes) > 0) :
			for menu_index_node in menu_index_nodes :

				lang = menu_index_node.getAttribute("lang")
				value = menu_index_node.getAttribute("value")

				if (cmp(lang, "") != 0) :
					self.menu_index[lang] = value
				else :
					for lang in constants.supported_languages :
						if (lang not in self.menu_index.keys()) :

							self.menu_index[lang] = value

				if (len(value) > self.max_menu_index_len) :
					self.max_menu_index_len = len(value)
コード例 #6
0
	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()
コード例 #7
0
ファイル: transform.py プロジェクト: edupals/omnitux
	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)
コード例 #8
0
	def __init__(self, game, lang, 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]
		
			# 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])

			# eventual setup for placeholders
			self.goal_image_file = constants.puzzle_tux_image

			goal_nodes = xml_global_parameters_node.getElementsByTagName("goal")
			if (len(goal_nodes) > 0) :
				goal_node = goal_nodes[0]
				
				image_file_nodes = goal_node.getElementsByTagName("image")
		
				if (len(image_file_nodes) > 0) :
					self.goal_image_file = xml_funcs.getText(image_file_nodes[random.randint(0, len(image_file_nodes)-1)])

								
			# reading puzzle items
			
			main_puzzles = self.xml_game_node.getElementsByTagName("puzzles")[0]
			
			puzzle_nodes = main_puzzles.getElementsByTagName("puzzle")
			
			self.puzzles = []
			
			for puzzle_node in puzzle_nodes :
				
				puzzle = Puzzle(self)
				
				image_nodes = puzzle_node.getElementsByTagName("image")
				
				puzzle.image = xml_funcs.getText(image_nodes[0])
				
				sound_nodes = puzzle_node.getElementsByTagName("sound")
					
				if len(sound_nodes) > 0 :
					puzzle.sound = xml_funcs.getText(sound_nodes[0])

				text_nodes = puzzle_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
							puzzle.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
							puzzle.text = xml_funcs.getText(text_node)
							break
					
				if (puzzle.pieces_amount_x == 0) :
					puzzle.pieces_amount_x = self.pieces_amount_x
				
				if (puzzle.pieces_amount_y == 0) :
					puzzle.pieces_amount_y = self.pieces_amount_y
					
				self.puzzles.append(puzzle)
				
			self.puzzles = common.randomize_list(self.puzzles)
コード例 #9
0
    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)
コード例 #10
0
ファイル: memory_cards.py プロジェクト: Trilarion/omnitux2
	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)
コード例 #11
0
ファイル: learning.py プロジェクト: Trilarion/omnitux2
    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)
コード例 #12
0
ファイル: associate.py プロジェクト: Trilarion/omnitux2
	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)
コード例 #13
0
ファイル: common.py プロジェクト: edupals/omnitux
    def __init__(self, globalvars, xml_game_node, level, score=None):

        self.globalvars = globalvars

        self.xml_game_node = xml_game_node
        self.level = level

        self.game_type = ""
        self.xml_file_version = ""
        self.languages = []
        self.i18n_dict = self.level.game_set.i18n_dict
        self.author = ""
        self.titles = {}
        self.thumb = None

        if (score != None):
            self.score = score
        else:
            self.score = Score(globalvars)

        # TODO : implement custom music for a given game
        self.music_files = constants.default_music_files

        self.game_type = self.xml_game_node.getAttribute("type")

        self.xml_file_version = self.xml_game_node.getAttribute("xml_version")
        self.author = self.xml_game_node.getAttribute("author")

        title_nodes = self.xml_game_node.getElementsByTagName("title")

        if (len(title_nodes) == 0):
            # no title found, we try to get it from the game_set above
            title_nodes = self.xml_game_node.parentNode.getElementsByTagName(
                "title")

            if (len(title_nodes) == 0):
                # yet no title ? we are probably inside a level tag
                # so let's go up again
                title_nodes = self.xml_game_node.parentNode.parentNode.getElementsByTagName(
                    "title")

        for title_node in title_nodes:
            title = xml_funcs.getText(title_node)
            language = title_node.getAttribute("lang")

            self.languages.append(language)
            self.titles[language] = title

        # eventual thumbnail
        thumb_nodes = self.xml_game_node.getElementsByTagName("thumbnail")

        if (len(thumb_nodes) > 0):
            self.thumb = xml_funcs.getText(thumb_nodes[0])

        background_nodes = self.xml_game_node.getElementsByTagName(
            "background")

        self.backgrounds = []
        for background_node in background_nodes:

            background_file = xml_funcs.getText(background_node)

            background_mode = background_node.getAttribute("mode")

            if (background_mode == ""):
                # by default, the background will be stretched to fill all the screen
                background_mode = "FILL"

            self.backgrounds.append((background_file, background_mode))

        music_nodes = self.xml_game_node.getElementsByTagName("music")
        if (len(music_nodes) > 0):
            # Only the first node is taken into account
            music_node = music_nodes[0]

            self.music_mode = music_node.getAttribute("mode")

            self.music_files = []

            music_files = music_node.getElementsByTagName("file")
            for music_file in music_files:
                self.music_files.append(music_files)

            if (len(self.music_files) == 0):
                self.music_files = constants.default_music_files

        else:
            self.music_mode = "on"
            self.music_files = constants.default_music_files

        self.rounds_amount = self.xml_game_node.getElementsByTagName("rounds")
        if (len(self.rounds_amount) > 0):
            self.rounds_amount = xml_funcs.getInt(self.rounds_amount[0])
        else:
            self.rounds_amount = constants.infinite

        self.rounds_done = 0
コード例 #14
0
ファイル: common.py プロジェクト: edupals/omnitux
    def __init__(self, globalvars, xml_filename):

        self.globalvars = globalvars

        self.levels = []

        self.xml_filename = xml_filename

        self.languages = []

        self.titles = {}
        self.thumb = None

        self.tags = []

        self.game_set_node = None

        try:
            self.xml_data = xml.dom.minidom.parse(xml_filename)

            # info("Loading file " + xml_filename)

        except:
            error("Impossible to read the contents of " + xml_filename)
            error("File format might be incorrect")

            raise BadXMLException()

        # trying to load an i18n dictionary
        self.i18n_dict = i18n.i18n_dict(self.xml_data)

        self.xml_game_set_nodes = self.xml_data.getElementsByTagName(
            "game_set")

        if (len(self.xml_game_set_nodes) > 0):
            # we enter there if the file is a game_set
            self.game_set_node = self.xml_game_set_nodes[0]

            # get the title of the game_set in different languages
            title_nodes = self.game_set_node.getElementsByTagName("title")

            for title_node in title_nodes:
                if (title_node.parentNode == self.game_set_node):

                    title = xml_funcs.getText(title_node)
                    language = title_node.getAttribute("lang")

                    self.languages.append(language)
                    self.titles[language] = title

            # eventual thumbnail
            thumb_nodes = self.game_set_node.getElementsByTagName("thumbnail")

            if (len(thumb_nodes) > 0):
                self.thumb = xml_funcs.getText(thumb_nodes[0])

            # amount of game rounds (applies by default to all levels)
            self.rounds_amount_nodes = self.game_set_node.getElementsByTagName(
                "rounds")
            if (len(self.rounds_amount_nodes) > 0):
                if (self.rounds_amount_nodes[0].parentNode ==
                        self.game_set_node):
                    self.game_rounds_amount = xml_funcs.getInt(
                        self.rounds_amount_nodes[0])
                else:
                    self.game_rounds_amount = constants.infinite
            else:
                self.game_rounds_amount = constants.infinite

        else:
            # we're here if the file was only made of a 'game' and not a 'game_set'
            self.game_rounds_amount = 1

            # we 'fake', the game node is considered a game_set
            self.game_set_node = self.xml_data

        self.xml_level_nodes = self.game_set_node.getElementsByTagName("level")

        if (len(self.xml_level_nodes) == 0):
            # no level defined, so we let the game_set act like a level
            level = Level(self.globalvars, self.game_set_node, self)
            self.levels.append(level)

        else:

            for xml_level_node in self.xml_level_nodes:
                level = Level(self.globalvars, xml_level_node, self)
                self.levels.append(level)

        # TODO allow multiple types games
        self.game_type = self.levels[0].games[0].game_type

        # TODO put real tags

        xml_tag_nodes = self.game_set_node.getElementsByTagName("tag")

        if (len(xml_tag_nodes) > 0):
            for xml_tag_node in xml_tag_nodes:
                tag = xml_funcs.getText(xml_tag_node).lower()
                self.tags.append(tag)

        else:
            # if no tags defined, the game type becomes a tag
            self.tags = [self.levels[0].games[0].game_type]

        # get the indexes for sorting in menu

        self.menu_index = {}

        menu_index_nodes = self.game_set_node.getElementsByTagName(
            "menu_index")

        self.max_menu_index_len = 1

        if (len(menu_index_nodes) > 0):
            for menu_index_node in menu_index_nodes:

                lang = menu_index_node.getAttribute("lang")
                value = menu_index_node.getAttribute("value")

                if (cmp(lang, "") != 0):
                    self.menu_index[lang] = value
                else:
                    for lang in constants.supported_languages:
                        if (lang not in self.menu_index.keys()):

                            self.menu_index[lang] = value

                if (len(value) > self.max_menu_index_len):
                    self.max_menu_index_len = len(value)