Drag and Drop Puzzle

Discuss how to use the Ren'Py engine to create visual novels and story-based games. New releases are announced in this section.
Forum rules
This is the right place for Ren'Py help. Please ask one question per thread, use a descriptive subject like 'NotFound error in option.rpy' , and include all the relevant information - especially any relevant code and traceback messages. Use the code tag to format scripts.
Post Reply
Message
Author
User avatar
Luka_01
Newbie
Posts: 1
Joined: Wed Mar 27, 2024 3:44 pm
Contact:

Drag and Drop Puzzle

#1 Post by Luka_01 »

I'm new in renpy and I'm trying to do a puzzle but have trouble the pieces doesn't drop on the spots I don't know if it's because the pieces have specific forms or what, also, it constantly appears error so the code might seem weird beacause all the changes I had to do...Please help me I've been a whole week trying to solve this

Code: Select all

default pieces = 6
default full_size = (1128, 1317)
default piece_coordinates = [(1040, 882), (1285, 894), (1064, 390), (912, 563), (1395, 563), (1060, 63)]
default initial_piece_coordinates = [ ]
default finished_pieces = 0

init python:
    dragged_piece = None
    dropped_on = None 
    pieces = dragged_piece
    finished_pieces = 0

    def piece_drop(dropped_on, dragged_piece):
    global finished_pieces
    dragged_piece = dragged_piece[0]
    
    if dropped_on is not None:
        if dragged_piece[0].drag_name == dropped_on.drag_name:
            dragged_piece[0].snap(dropped_on.x, dropped_on.y, 0.5)

    if finished_pieces == pieces:
            renpy.jump(complete)


screen jun: 

    draggroup:
        drag: 
            drag_name "yback2"
            child "yback2.png" 
            draggable False
            droppable True
        drag:
            drag_name "yleg1"
            child "yleg1.png"
            draggable True
            droppable False
        drag:
            drag_name "yleg2"
            child "yleg2.png"
            draggable True
            droppable False 
        drag:
            drag_name "ytorso2"
            child "ytorso2.png"
            draggable False 
            droppable True
            xpos 1064 ypos 390 
        drag:
            drag_name "yarm1"
            child "yarm1.png"
            draggable True
            droppable False
        drag: 
            drag_name "yarm2"
            child "yarm2.png"
            draggable True
            droppable False
        drag:
            drag_name "yhead"
            child "yhead.png"
            draggable True
            droppable False 
Attachments
screenshot0002.png
(776.28 KiB) Not downloaded yet
screenshot0001.png
(847.74 KiB) Not downloaded yet

jeffster
Veteran
Posts: 409
Joined: Wed Feb 03, 2021 9:55 pm
Contact:

Re: Drag and Drop Puzzle

#2 Post by jeffster »

Luka_01 wrote: Wed Mar 27, 2024 6:07 pm I'm new in renpy and I'm trying to do a puzzle but have trouble the pieces doesn't drop on the spots I don't know if it's because the pieces have specific forms or what, also, it constantly appears error so the code might seem weird beacause all the changes I had to do...Please help me I've been a whole week trying to solve this

Code: Select all

default pieces = 6
default full_size = (1128, 1317)
default piece_coordinates = [(1040, 882), (1285, 894), (1064, 390), (912, 563), (1395, 563), (1060, 63)]
default initial_piece_coordinates = [ ]
default finished_pieces = 0

init python:
    dragged_piece = None
    dropped_on = None 
    pieces = dragged_piece
    finished_pieces = 0

    def piece_drop(dropped_on, dragged_piece):
    global finished_pieces
    dragged_piece = dragged_piece[0]
    
    if dropped_on is not None:
        if dragged_piece[0].drag_name == dropped_on.drag_name:
            dragged_piece[0].snap(dropped_on.x, dropped_on.y, 0.5)

    if finished_pieces == pieces:
            renpy.jump(complete)


screen jun: 

    draggroup:
        drag: 
            drag_name "yback2"
            child "yback2.png" 
            draggable False
            droppable True
        drag:
            drag_name "yleg1"
            child "yleg1.png"
            draggable True
            droppable False
        drag:
            drag_name "yleg2"
            child "yleg2.png"
            draggable True
            droppable False 
        drag:
            drag_name "ytorso2"
            child "ytorso2.png"
            draggable False 
            droppable True
            xpos 1064 ypos 390 
        drag:
            drag_name "yarm1"
            child "yarm1.png"
            draggable True
            droppable False
        drag: 
            drag_name "yarm2"
            child "yarm2.png"
            draggable True
            droppable False
        drag:
            drag_name "yhead"
            child "yhead.png"
            draggable True
            droppable False 
First of all you have to understand the logic of what you are doing.
Please read the documentation
https://renpy.org/doc/html/drag_drop.html

Check "Examples" and understand how dragging and dropping works.
In particular, there are elements that you drag, and there are elements that you drop on.
(They all would belong to the same draggroup).
snap() can fine-tune the final position of a drag that you dropped.

Note that you define function "piece_drop", but its body should be indented.
This probably causes error messages:

Code: Select all

    def piece_drop(dropped_on, dragged_piece):
    global finished_pieces
With correct indentation, it should be:

Code: Select all

    def piece_drop(dropped_on, dragged_piece):
        global finished_pieces
(Its parameters order and its code are incorrect BTW).

And if you want to use that function it should be set as "dragged" parameter for the drags that use it.
(See the second example there).

In the future, to get useful help it's advisable to explain how exactly things don't work, what are the exact error messages and the like.

Post Reply

Who is online

Users browsing this forum: LuckyT