From 7ace78295213f51ccbb45ba065ecc6c1a723af6e Mon Sep 17 00:00:00 2001 From: Mark Date: Sat, 31 Aug 2024 16:33:41 -0400 Subject: [PATCH] refactor(examples): tidy drag_and_drop_inventory.rpy --- game/examples/drag_and_drop_inventory.rpy | 60 ++++++++++------------- 1 file changed, 25 insertions(+), 35 deletions(-) diff --git a/game/examples/drag_and_drop_inventory.rpy b/game/examples/drag_and_drop_inventory.rpy index 9ea7296..a49fa62 100644 --- a/game/examples/drag_and_drop_inventory.rpy +++ b/game/examples/drag_and_drop_inventory.rpy @@ -1,28 +1,26 @@ # https://discover-with-mia.itch.io/renpy-tutorial-drag-and-drop-inventory init python: - # update the expression of a given character based on preference for a particular item. - def character_expression(character, item) -> None: + def get_character_expression(character, item) -> str: global characters if item == characters[character]["preferences"]: - characters[character]["expression"] = "happy" + return "happy" else: - characters[character]["expression"] = "normal" + return "normal" - # check if there are items in the inventory currently available for use. def items_are_available() -> bool: return any(item["available"] for item in inventory.values()) - # select the first available item from the inventory as a default choice. def select_default_item() -> tuple[any, any]: for item_id, item_info in inventory.items(): if item_info["available"]: return item_id, item_info return None, None - # handle the drag and drop interaction. Update global state when an item is dropped onto a character. + # handle the drag and drop interaction + # update global state when an item is dropped onto a character def character_dragged(drags, drop) -> bool: - global currentCharacter, lastGivenItem + global current_character, last_given_item if not drop: return False @@ -31,10 +29,7 @@ init python: character = drop.drag_name if character in characters and item in inventory and inventory[item]["available"]: - lastGivenItem = item - - character_expression(character, item) - + last_given_item = item inventory[item]["available"] = False return True @@ -64,18 +59,16 @@ default characters = { "normal": "sylvie/sylvie blue normal.png", "happy": "sylvie/sylvie blue smile.png", "preferences": "gem", - "expression": "normal", }, "Eileen": { "normal": "eileen/eileen happy.png", "happy": "eileen/eileen vhappy.png", "preferences": "book", - "expression": "normal", }, } -default currentCharacter = "Sylvie" -default lastGivenItem = None +default current_character = "Sylvie" +default last_given_item = None default selected_info = "" default selected_item = "" @@ -109,7 +102,12 @@ screen interact_with_characters(): vbox: for item_id, item_info in inventory.items(): if item_info["available"]: - textbutton f"{item_info['name']} - {item_info['description']}" action [SetVariable('selected_item', item_id), SetVariable('selected_info', item_info)] + textbutton f"{item_info['name']} - {item_info['description']}": + action [ + SetVariable('selected_item', item_id), + SetVariable('selected_info', item_info), + ] + else: text "??? - ???" @@ -124,37 +122,29 @@ screen interact_with_characters(): image selected_info["image"] drag: - drag_name currentCharacter + drag_name current_character draggable False xalign 0.5 yalign 1.0 - image characters[currentCharacter][characters[currentCharacter]["expression"]] + image characters[current_character]["normal"] label drag_and_drop_inventory: $ selected_item, selected_info = select_default_item() - $ item_given = False call screen interact_with_characters - if lastGivenItem: - $ character_expression(currentCharacter, lastGivenItem) - $ expression = characters[currentCharacter]["expression"] + if last_given_item: + $ character_expression = get_character_expression(current_character, last_given_item) + $ character_image = characters[current_character][character_expression] - $ character_image = characters[currentCharacter][expression] + if character_expression == "happy": + show expression character_image + "[current_character] is happy with her favorite gift." - if expression == "happy": - show expression character_image at center - "[currentCharacter] is happy with her favorite gift." else: - show expression character_image at center - "[currentCharacter]: 'Thank you, but this isn't what I prefer.'" - - $ item_given = True - - jump suite_story - -label suite_story: + show expression character_image + "[current_character]: 'Thank you, but this isn't what I prefer.'" while items_are_available(): jump drag_and_drop_inventory