Skip to content

Commit

Permalink
feat(examples): add drag_and_drop_inventory
Browse files Browse the repository at this point in the history
  • Loading branch information
remarkablemark committed Aug 31, 2024
1 parent b8b75c6 commit 5a20bc1
Show file tree
Hide file tree
Showing 2 changed files with 186 additions and 20 deletions.
40 changes: 20 additions & 20 deletions game/examples/drag_and_drop.rpy
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
# https://www.renpy.org/doc/html/drag_drop.html
label drag_and_drop:
# https://github.com/VimislikArt/dragdropcode/blob/main/dragdrop.rpy
init python:
def drag_placed(drags, drop) -> bool:
if not drop:
return False

# https://github.com/VimislikArt/dragdropcode/blob/main/dragdrop.rpy
init python:
def drag_placed(drags, drop):
if not drop:
return
store.draggable = drags[0].drag_name
store.droppable = drop.drag_name

store.draggable = drags[0].drag_name
store.droppable = drop.drag_name
return True

return True
# https://www.renpy.org/doc/html/drag_drop.html
label drag_and_drop:

show screen drag_sample1
call screen drag_sample2
show screen drag_sample3
show screen drag_example1
call screen drag_example2
show screen drag_example3

if droppable == "The Left Circle":
$ xpos_var = 150
Expand All @@ -35,13 +35,13 @@ label drag_and_drop:

"The [draggable] was put in [droppable]"

hide screen drag_sample1
hide screen drag_sample2
hide screen drag_sample3
hide screen drag_example1
hide screen drag_example2
hide screen drag_example3

jump start
jump drag_and_drop_inventory

screen drag_sample1():
screen drag_example1():
draggroup:
drag:
xpos 0.25
Expand Down Expand Up @@ -73,7 +73,7 @@ screen drag_sample1():
ypadding 20
text "Draggable"

screen drag_sample2():
screen drag_example2():
draggroup:
drag:
drag_name "circle"
Expand Down Expand Up @@ -121,7 +121,7 @@ screen drag_sample2():
draggable False
droppable True

screen drag_sample3():
screen drag_example3():
draggroup:
drag:
drag_name "The Left Circle"
Expand Down
166 changes: 166 additions & 0 deletions game/examples/drag_and_drop_inventory.rpy
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
# 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:
global characters
if item == characters[character]["preferences"]:
characters[character]["expression"] = "happy"
else:
characters[character]["expression"] = "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.
def character_dragged(drags, drop) -> bool:
global currentCharacter, lastGivenItem

if not drop:
return False

item = drags[0].drag_name
character = drop.drag_name

if character in characters and item in inventory and inventory[item]["available"]:
lastGivenItem = item

character_expression(character, item)

inventory[item]["available"] = False
return True

return False

default inventory = {
"gem": {
"name": "Gem",
"image": "inventory/2.png",
"description": "A gem that Sylvie will love!",
"available": True,
"xpos": 0.1,
"ypos": 0.8,
},
"book": {
"name": "Book",
"image": "inventory/5.png",
"description": "A book that Eileen will appreciate!",
"available": True,
"xpos": 0.1,
"ypos": 0.8,
},
}

default characters = {
"Sylvie": {
"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 selected_info = ""
default selected_item = ""

# screen for the main character interaction, displaying available items
screen interact_with_characters():
zorder 1

if not items_are_available():
vbox:
label "{color=#fff}No items to give at the moment."
textbutton "Return":
action Return()

else:
frame:
xalign 0.0
yalign 0.0
xsize 400
ysize 600

vpgrid:
cols 2
spacing 10
draggable False
mousewheel True

scrollbars "vertical"
xalign 0.5

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)]
else:
text "??? - ???"

if selected_item:
draggroup:
drag:
drag_name selected_item
droppable False
dragged character_dragged
xalign 0.1
yalign 0.8
image selected_info["image"]

drag:
drag_name currentCharacter
draggable False
xalign 0.5
yalign 1.0
image characters[currentCharacter][characters[currentCharacter]["expression"]]

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"]

$ character_image = characters[currentCharacter][expression]

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:

while items_are_available():
jump drag_and_drop_inventory

hide expression character_image

"Congratulations, you gave all the items!"

jump start

0 comments on commit 5a20bc1

Please sign in to comment.