def select_random_differences(self) : # the differences which will have to be found by the user self.differences_to_find = [] # the differences which will be masked (copy the rect from the original image) self.differences_to_mask = [] self.differences = common.randomize_list(self.differences) self.amount_differences = self.min_differences + random.randint(0, self.max_differences-self.min_differences) if (len(self.differences) < self.amount_differences) : common.error("Not enough differences available. Please check your XML setup : the value of the 'max_differences' tag is too high.") raise common.BadXMLException() for i in range(self.amount_differences) : difference = self.differences[0] self.differences.remove(self.differences[0]) self.differences_to_find.append(difference) # at this point, only differences which have to be masked are yet in the self.differences list self.differences_to_mask = self.differences
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 get_random_associations(self): # we position images and texts which might be displayed # (in case there would be more than one in some associations) for association in self.associations: association.select_random_items() # the amount of draggable items will be a random value between min_draggable and max_draggable if (self.score.get_arcade_mode() == False): amount_draggable = self.min_draggable + random.randint( 0, self.max_draggable - self.min_draggable) else: # in arcade mode, we force the maximum amount of drag objects amount_draggable = self.max_draggable # taking a random association # those will be the "good" answers good_associations = [] if (cmp(self.type_to_associate, "image_on_map") == 0): goals_to_find = amount_draggable else: goals_to_find = len(self.goals) for i in range(goals_to_find): for association in self.associations: if (association.done == False): if (association not in good_associations): good_associations.append(association) break else: # if all the associations were found, there's nothing more to associate ! common.info( "Not enough associations left to find. Activity ended") return (None, None) # select other "wrong" associations all_associations = copy.copy(good_associations) for i in range(amount_draggable - len(self.goals)): found = False if (len(all_associations) >= len(self.associations)): # we won't be able to find other associations (all are already selected) # so we have to stop, to avoid an infinite while loop below common.info( "Not enough associations available for 'bad' choices.") common.info( "Please check your xml file to add more associations or lower 'max_draggable' parameter" ) return (None, None) while (found == False): other_association = self.associations[random.randint( 0, len(self.associations) - 1)] if (other_association not in all_associations): all_associations.append(other_association) found = True # eventually sorting the entries if (self.sort == True): all_associations.sort() else: all_associations = common.randomize_list(all_associations) return (good_associations, all_associations)
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 find_tuxes(self) : tuxes = common.get_files(constants.folder_award_tuxes, constants.image_extensions) self.tuxes = common.randomize_list(tuxes)
def __init__(self, globalvars): common.Run_Screen.__init__(self, globalvars) tag_defs = common.load_tag_defs() self.current_page = Page_menu(globalvars, tag_defs) self.current_page.draw_back() pygame.display.flip() common.start_music(constants.default_music_files, globalvars) common.info("Looking for xml activities") for data_folder in constants.data_folders: self.current_page.games_list.append_from_folder(data_folder) common.info("Initialize igloo") self.current_page.igloo_page = igloo.create(self.globalvars) common.info("Display main menu") self.current_page.display_main_menu() self.current_page.draw() pygame.display.flip() main_page = self.current_page level = None while self.running: try: common.Run_Screen.run_pre(self) except common.EndActivityException, e: # although we should normally end after an EndActivityException, # here we'll ask for user confirmation self.running = True if (cmp(self.current_page.page_type, "MAIN") == 0): self.dirty_rects = self.current_page.display_quit() elif (cmp(self.current_page.page_type, "SELECT_LEVEL") == 0): self.mouse_clicked = False self.current_page = main_page self.current_page.draw() self.dirty_rects = [self.screen.get_rect()] else: self.dirty_rects = self.current_page.display_main_menu() self.current_page.draw() if (self.mouse_clicked == True): clicked = self.current_page.was_something_clicked( self.mouse_pos, self.mousebutton) if (clicked != None): (component_clicked, foo, some_dirty_rects) = clicked self.dirty_rects = self.dirty_rects + some_dirty_rects if (component_clicked != None): # we try to play an associated sound (if any) component_clicked.play_random_associated_sound() if (cmp(self.current_page.page_type, "DIALOG_BOX_QUIT") == 0): if (component_clicked == self.current_page.quit_yes): self.running = False elif (component_clicked == self.current_page.quit_no): self.dirty_rects = self.current_page.display_main_menu( ) self.current_page.draw() self.mouse_clicked = False elif (cmp(self.current_page.page_type, "MAIN") == 0): if (component_clicked == self.current_page.igloo_image ): self.current_page.run_igloo(False) self.mouse_clicked = False self.current_page = main_page self.current_page.draw() self.dirty_rects = [self.screen.get_rect()] elif (component_clicked == self.current_page.language_image): # changing main language self.current_page.remove_all() index_lang = constants.supported_languages.index( self.current_page.language) index_lang = index_lang + 1 if (index_lang >= len( constants.supported_languages)): index_lang = 0 self.current_page.language = constants.supported_languages[ index_lang] self.globalvars.main_language = constants.supported_languages[ index_lang] self.dirty_rects = self.current_page.display_main_menu( ) self.current_page.draw() self.mouse_clicked = False elif (component_clicked == self.current_page.quit_icon ): self.dirty_rects = self.current_page.display_quit() self.mouse_clicked = False elif (component_clicked in self.current_page.tag_icons): # TODO : allow multiple activities inside a game_set ! tag_icon = component_clicked tag_def = tag_icon.associated_object self.current_page.games_to_display = self.current_page.games_list.get_games_by_tag_and_language( tag_def.tag, self.current_page.language) self.dirty_rects = self.current_page.display_games_by_type( ) self.mouse_clicked = False elif (component_clicked == self.current_page.options_icon): self.current_page.fade_out() options.Run_options(globalvars) self.current_page.draw() pygame.display.flip() self.mouse_clicked = False elif (cmp(self.current_page.page_type, "GAME_SELECTION") == 0): if (component_clicked == self.current_page.go_back_icon ): self.current_page.fade_out() self.dirty_rects = self.current_page.display_main_menu( ) self.current_page.draw() self.mouse_clicked = False else: game_set = component_clicked.associated_object if (game_set != None): # TODO allow more levels !!!! levels = game_set.levels if (len(levels) > 1): level_page = Page_level( globalvars, self.current_page. second_background_image) self.current_page = level_page self.dirty_rects = self.current_page.select_level( levels) self.mouse_clicked = False else: # in case we have a single level activity level = levels[0] elif (cmp(self.current_page.page_type, "SELECT_LEVEL") == 0): if (component_clicked == self.current_page.go_back_icon ): # TODO : a implementer !!! self.mouse_clicked = False self.current_page = main_page self.current_page.draw() self.dirty_rects = [self.screen.get_rect()] else: associated_object = component_clicked.associated_object if (associated_object != None): self.current_page.fade_out() # TODO allow more levels !!!! level = associated_object else: # mouse is not clicked # but it may be going over elements (mouse_over_new, mouse_over_old) = self.current_page.components_mouse_over( pygame.mouse.get_pos()) if (mouse_over_old != None): self.dirty_rects = self.dirty_rects + self.current_page.remove_legend_text( ) if (mouse_over_new != None): self.dirty_rects = self.dirty_rects + self.current_page.add_legend_text( mouse_over_new) if (level != None): # then we'll go back to main page when game or highscore will end self.current_page = main_page if (self.mousebutton == 3): # right click means start highscore screen self.current_page.run_highscore(level) self.mouse_clicked = False else: self.current_page.fade_out() # the level is copied in order to change its variables copy_level = copy.copy(level) # shuffle the games copy_level.games = common.randomize_list( copy.copy(copy_level.games)) amount_games_played = 0 try: total_score = common.Score(globalvars) for game in copy_level.games: if (amount_games_played < copy_level.game_rounds_amount): if (cmp(game.game_type, "associate") == 0): # association run_activity = associate.Run_Associate( game, globalvars, total_score) if (cmp(game.game_type, "puzzle") == 0): # puzzle run_activity = puzzle.Run_Puzzle_Screen( game, globalvars, total_score) if (cmp(game.game_type, "memory_cards") == 0): # memory cards run_activity = memory_cards.Run_Memory_Cards_Screen( game, globalvars, total_score) if (cmp(game.game_type, "differences") == 0): # differences run_activity = differences.Run_Differences_Screen( game, globalvars, total_score) if (cmp(game.game_type, "transform") == 0): # image transformation run_activity = transform.Run_Transform_Screen( game, globalvars) if (cmp(game.game_type, "learning") == 0): # learning cards run_activity = learning.Run_Learning_Screen( game, globalvars) amount_games_played = amount_games_played + 1 else: # if all the games were played, there's nothing more to play ! common.info( "Not enough games left to play. Level completed." ) if (pygame.mixer.music.get_busy() == False): common.start_music(constants.default_music_files, globalvars) # we go to the highscore screen, if activated and if some score was indeed recorded if ((total_score.get_arcade_mode() == True) and (self.globalvars.highscore == True)): self.current_page.run_highscore( copy_level, total_score) # After a completed game_set, we get a Tux as a reward # (only in case the display_igloo global var is True if (self.globalvars.display_igloo == True): self.current_page.run_igloo(True) self.dirty_rects = [] except common.EndActivityException, e: common.info("Activity stopped by the user") except common.BadXMLException, e: common.error( "Had to stop the activity because of an error in XML setup" ) except Exception, e: common.error("Exception fired", e, traceback.format_exc())
def find_tuxes(self): tuxes = common.get_files(constants.folder_award_tuxes, constants.image_extensions) self.tuxes = common.randomize_list(tuxes)
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, globalvars) : common.Run_Screen.__init__(self, globalvars) tag_defs = common.load_tag_defs() self.current_page = Page_menu(globalvars, tag_defs) self.current_page.draw_back() pygame.display.flip() common.start_music(constants.default_music_files, globalvars) common.info("Looking for xml activities") for data_folder in constants.data_folders : self.current_page.games_list.append_from_folder(data_folder) common.info("Initialize igloo") self.current_page.igloo_page = igloo.create(self.globalvars) common.info("Display main menu") self.current_page.display_main_menu() self.current_page.draw() pygame.display.flip() main_page = self.current_page level = None while self.running : try : common.Run_Screen.run_pre(self) except common.EndActivityException, e : # although we should normally end after an EndActivityException, # here we'll ask for user confirmation self.running = True if (cmp(self.current_page.page_type, "MAIN") == 0) : self.dirty_rects = self.current_page.display_quit() elif (cmp(self.current_page.page_type, "SELECT_LEVEL") == 0) : self.mouse_clicked = False self.current_page = main_page self.current_page.draw() self.dirty_rects = [self.screen.get_rect()] else : self.dirty_rects = self.current_page.display_main_menu() self.current_page.draw() if (self.mouse_clicked == True) : clicked = self.current_page.was_something_clicked(self.mouse_pos, self.mousebutton) if (clicked != None) : (component_clicked, foo, some_dirty_rects) = clicked self.dirty_rects = self.dirty_rects + some_dirty_rects if (component_clicked != None) : # we try to play an associated sound (if any) component_clicked.play_random_associated_sound() if (cmp(self.current_page.page_type, "DIALOG_BOX_QUIT") == 0) : if (component_clicked == self.current_page.quit_yes) : self.running = False elif (component_clicked == self.current_page.quit_no): self.dirty_rects = self.current_page.display_main_menu() self.current_page.draw() self.mouse_clicked = False elif (cmp(self.current_page.page_type, "MAIN") == 0) : if (component_clicked == self.current_page.igloo_image) : self.current_page.run_igloo(False) self.mouse_clicked = False self.current_page = main_page self.current_page.draw() self.dirty_rects = [self.screen.get_rect()] elif (component_clicked == self.current_page.language_image) : # changing main language self.current_page.remove_all() index_lang = constants.supported_languages.index(self.current_page.language) index_lang = index_lang + 1 if (index_lang >= len(constants.supported_languages)) : index_lang = 0 self.current_page.language = constants.supported_languages[index_lang] self.globalvars.main_language = constants.supported_languages[index_lang] self.dirty_rects = self.current_page.display_main_menu() self.current_page.draw() self.mouse_clicked = False elif (component_clicked == self.current_page.quit_icon) : self.dirty_rects = self.current_page.display_quit() self.mouse_clicked = False elif (component_clicked in self.current_page.tag_icons ) : # TODO : allow multiple activities inside a game_set ! tag_icon = component_clicked tag_def = tag_icon.associated_object self.current_page.games_to_display = self.current_page.games_list.get_games_by_tag_and_language(tag_def.tag, self.current_page.language) self.dirty_rects = self.current_page.display_games_by_type() self.mouse_clicked = False elif (component_clicked == self.current_page.options_icon) : self.current_page.fade_out() options.Run_options(globalvars) self.current_page.draw() pygame.display.flip() self.mouse_clicked = False elif (cmp(self.current_page.page_type, "GAME_SELECTION") == 0) : if (component_clicked == self.current_page.go_back_icon) : self.current_page.fade_out() self.dirty_rects = self.current_page.display_main_menu() self.current_page.draw() self.mouse_clicked = False else : game_set = component_clicked.associated_object if (game_set != None) : # TODO allow more levels !!!! levels = game_set.levels if (len(levels) > 1) : level_page = Page_level(globalvars, self.current_page.second_background_image) self.current_page = level_page self.dirty_rects = self.current_page.select_level(levels) self.mouse_clicked = False else : # in case we have a single level activity level = levels[0] elif (cmp(self.current_page.page_type, "SELECT_LEVEL") == 0) : if (component_clicked == self.current_page.go_back_icon) : # TODO : a implementer !!! self.mouse_clicked = False self.current_page = main_page self.current_page.draw() self.dirty_rects = [self.screen.get_rect()] else : associated_object = component_clicked.associated_object if (associated_object != None) : self.current_page.fade_out() # TODO allow more levels !!!! level = associated_object else : # mouse is not clicked # but it may be going over elements (mouse_over_new, mouse_over_old) = self.current_page.components_mouse_over(pygame.mouse.get_pos()) if (mouse_over_old != None) : self.dirty_rects = self.dirty_rects + self.current_page.remove_legend_text() if (mouse_over_new != None) : self.dirty_rects = self.dirty_rects + self.current_page.add_legend_text(mouse_over_new) if (level != None) : # then we'll go back to main page when game or highscore will end self.current_page = main_page if (self.mousebutton == 3) : # right click means start highscore screen self.current_page.run_highscore(level) self.mouse_clicked = False else : self.current_page.fade_out() # the level is copied in order to change its variables copy_level = copy.copy(level) # shuffle the games copy_level.games = common.randomize_list(copy.copy(copy_level.games)) amount_games_played = 0 try : total_score = common.Score(globalvars) for game in copy_level.games : if (amount_games_played < copy_level.game_rounds_amount) : if (cmp(game.game_type, "associate") == 0) : # association run_activity = associate.Run_Associate(game, globalvars, total_score) if (cmp(game.game_type, "puzzle") == 0) : # puzzle run_activity = puzzle.Run_Puzzle_Screen(game, globalvars, total_score) if (cmp(game.game_type, "memory_cards") == 0) : # memory cards run_activity = memory_cards.Run_Memory_Cards_Screen(game, globalvars, total_score) if (cmp(game.game_type, "differences") == 0) : # differences run_activity = differences.Run_Differences_Screen(game, globalvars, total_score) if (cmp(game.game_type, "transform") == 0) : # image transformation run_activity = transform.Run_Transform_Screen(game, globalvars) if (cmp(game.game_type, "learning") == 0) : # learning cards run_activity = learning.Run_Learning_Screen(game, globalvars) amount_games_played = amount_games_played + 1 else : # if all the games were played, there's nothing more to play ! common.info("Not enough games left to play. Level completed.") if (pygame.mixer.music.get_busy() == False) : common.start_music(constants.default_music_files, globalvars) # we go to the highscore screen, if activated and if some score was indeed recorded if ((total_score.get_arcade_mode() == True) and (self.globalvars.highscore == True)) : self.current_page.run_highscore(copy_level, total_score) # After a completed game_set, we get a Tux as a reward # (only in case the display_igloo global var is True if (self.globalvars.display_igloo == True) : self.current_page.run_igloo(True) self.dirty_rects = [] except common.EndActivityException, e : common.info("Activity stopped by the user") except common.BadXMLException, e: common.error("Had to stop the activity because of an error in XML setup") except Exception, e : common.error("Exception fired", e, traceback.format_exc())
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)
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 get_random_associations(self) : # we position images and texts which might be displayed # (in case there would be more than one in some associations) for association in self.associations : association.select_random_items() # the amount of draggable items will be a random value between min_draggable and max_draggable if (self.score.get_arcade_mode() == False) : amount_draggable = self.min_draggable + random.randint(0, self.max_draggable-self.min_draggable) else : # in arcade mode, we force the maximum amount of drag objects amount_draggable = self.max_draggable # taking a random association # those will be the "good" answers good_associations = [] if (cmp(self.type_to_associate, "image_on_map") == 0): goals_to_find = amount_draggable else : goals_to_find = len(self.goals) for i in range(goals_to_find) : for association in self.associations : if (association.done == False) : if (association not in good_associations) : good_associations.append(association) break else : # if all the associations were found, there's nothing more to associate ! common.info("Not enough associations left to find. Activity ended") return (None, None) # select other "wrong" associations all_associations = copy.copy(good_associations) for i in range(amount_draggable - len(self.goals)) : found = False if (len(all_associations) >= len(self.associations)) : # we won't be able to find other associations (all are already selected) # so we have to stop, to avoid an infinite while loop below common.info("Not enough associations available for 'bad' choices.") common.info("Please check your xml file to add more associations or lower 'max_draggable' parameter") return (None, None) while (found == False) : other_association = self.associations[random.randint(0, len(self.associations)-1)] if (other_association not in all_associations) : all_associations.append(other_association) found = True # eventually sorting the entries if (self.sort == True) : all_associations.sort() else : all_associations = common.randomize_list(all_associations) return (good_associations, all_associations)
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)