Drag and Drop Tutorial-ish thing

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.
Message
Author
TrickWithAKnife
Eileen-Class Veteran
Posts: 1261
Joined: Fri Mar 16, 2012 11:38 am
Projects: Rika
Organization: Solo (for now)
IRC Nick: Trick
Location: Tokyo, Japan
Contact:

Re: Drag and Drop Tutorial-ish thing

#16 Post by TrickWithAKnife »

Nevermind, there is a fix on the way. Just wanted to mention that in case someone else is curious. And thanks for the reply KimiYoriBaka.
"We must teach them through the tools with which they are comfortable."
The #renpy IRC channel is a great place to chat with other devs. Due to the nature of IRC and timezone differences, people probably won't reply right away.

If you'd like to view or use any code from my VN PM me. All code is freely available without restriction, but also without warranty or (much) support.

User avatar
Stapper
Regular
Posts: 96
Joined: Wed Feb 27, 2013 9:54 pm
Contact:

Re: Drag and Drop Tutorial-ish thing

#17 Post by Stapper »

I tried this and it works very nicely. Thanks for the tutorial! :D

I have however one optimization question regarding the way the pieces are placed next to the board at the start of the game:

Code: Select all

    python:
        coorlistx = [10, 130, 250, 370]
        coorlisty = [10, 217, 424]
        piecelist = [[0,0],[0,0],[0,0],[0,0],[0,0],[0,0],[0,0],[0,0],[0,0],[0,0],[0,0],[0,0]]
        for i in range(12):
            x = renpy.random.randint(0, 59) + 621
            y = renpy.random.randint(0, 480)
            piecelist[i] = [x,y]
        movedpiece = 0
        movedplace = [0, 0]
This way they are placed in the proper order and it becomes very easy to complete the puzzle. Is there a way to shuffle the pieces, so that they appear in a random order?

KimiYoriBaka
Miko-Class Veteran
Posts: 636
Joined: Thu May 14, 2009 8:15 pm
Projects: Castle of Arhannia
Contact:

Re: Drag and Drop Tutorial-ish thing

#18 Post by KimiYoriBaka »

as in, the z order they're placed in? cause changing that would require either explicitly defining zorder using random depths (<--I've never tried this) or having the screen that shows the puzzle use a random order the first time it's displayed.

I guess you could create a list of piece indices, shuffle it, then pass it to the screen as the order the pieces should be added in. this wouldn't be hard if you used a for loop to add the pieces to the screen, but if you coded each one individually you would need to wrap the whole thing in a for loop using the list of indices and then add an if statement to each piece to check if it's time to add it.

another possibility is to randomly move the piece after they've been added to the screen using the snap() function. I'm not sure how well that would work though.

yung
Newbie
Posts: 2
Joined: Thu Jul 27, 2017 11:05 pm
Contact:

Re: Drag and Drop Tutorial-ish thing

#19 Post by yung »

sorry to bring out an old thread, but I am wondering if there's a way to detect if a slot is being dragged out of? Right now the code knows when a draggable is dropped into a defined area. What I want to do is detect if a piece that is dropped on an area, which is then dragged out of the drop area and do something with it. Hope this makes sense

greenchpal
Newbie
Posts: 1
Joined: Sat Sep 16, 2017 2:03 pm
Contact:

Re: Drag and Drop Tutorial-ish thing

#20 Post by greenchpal »

This is a really old thread, but since I am using the code from this, I figure I should maybe ask something about it over here.

So what I want to do is have 3 empty spaces, and have 4 puzzle pieces. However, only 3 of the four puzzle pieces will create the winning solution. Basically, I want to have "fake pieces", as mentioned before in this thread. I am not sure how to get the code working for that though-- I have been fiddling with the code for days now trying to figure out what to do, all to no avail.

Here is my code as it is right now (note that I will change the images used once I lay out a whole, complete, working code for the game!):

Code: Select all

init python:
    
    def piece_dragged(drags, drop):
        
        if not drop:
            
            store.piecelist[(int(drags[0].drag_name[0]) * 10 + int(drags[0].drag_name[1]))][0] = drags[0].x
            
            store.piecelist[(int(drags[0].drag_name[0]) * 10 + int(drags[0].drag_name[1]))][1] = drags[0].y
            
            return
            
        store.movedpiece = int(drags[0].drag_name[0]) * 10 + int(drags[0].drag_name[1])
        
        store.movedplace = [int(drop.drag_name[0]), int(drop.drag_name[1])]
        
        
        return True

screen jigsaw:

    

    draggroup:

        

        drag:

            drag_name "00"

            child "empty space.png"

            draggable False

            xpos coorlistx[0] ypos coorlisty[0]

            

        drag:

            drag_name "01"

            child "empty space.png"

            draggable False

            xpos coorlistx[0] ypos coorlisty[1]
            

        drag:

            drag_name "02"

            child "empty space.png"

            draggable False

            xpos coorlistx[0] ypos coorlisty[2]


        drag:

            drag_name "00 piece"
            
            child im.Crop("ShizukaClassroom_0001.jpg", 0,0, 120, 207)

            droppable False

            dragged piece_dragged

            xpos piecelist[0][0] ypos piecelist[0][1]

            

        drag:

            drag_name "01 piece"

            child im.Crop("ShizukaClassroom_0001.jpg", 120,0, 120, 207)

            droppable False

            dragged piece_dragged

            xpos piecelist[1][0] ypos piecelist[1][1]

            

        drag:

            drag_name "02 piece"

            child im.Crop("ShizukaClassroom_0001.jpg", 240,0, 120, 207)

            droppable False

            dragged piece_dragged

            xpos piecelist[2][0] ypos piecelist[2][1]

            

        drag:

            drag_name "03 piece"

            child im.Crop("ShizukaClassroom_0001.jpg", 360,0, 120, 207)

            droppable False

            dragged piece_dragged
            
            xpos piecelist[3][0] ypos piecelist[3][1]
            
label puzzle:

    call screen jigsaw

    if ([coorlistx[movedplace[0]], coorlisty[movedplace[1]]] in piecelist):

        python:

            t1 = piecelist[movedpiece]

            t2 = piecelist.index([coorlistx[movedplace[0]], coorlisty[movedplace[1]]])

            piecelist[movedpiece] = [coorlistx[movedplace[0]],coorlisty[movedplace[1]]]

            piecelist[t2] = t1

    else:

        $ piecelist[movedpiece] = [coorlistx[movedplace[0]],coorlisty[movedplace[1]]]

        
    if piecelist == [[coorlistx[0],coorlisty[0]],
                    [coorlistx[0],coorlisty[1]],
                    [coorlistx[0],coorlisty[2]]]:
                    

 
        jump win

    jump puzzle

label start:

    scene black

    #image whole = "ShizukaClassroom_0001.jpg"
    image whole = "fur.png"

    python:

        coorlistx = [10, 130, 250,370]
        
        #coorlistx = [10, 130, 250, 370]

        coorlisty = [10, 217, 424]

        #piecelist = [[0,0],[0,0],[0,0],[0,0],[0,0],[0,0],[0,0],[0,0],[0,0],[0,0],[0,0],[0,0]]
        
        piecelist = [[0,0],[0,0],[0,0],[0,0]]


        for i in range(4):
        #for i in range(12):

            x = renpy.random.randint(0, 59) + 621

            y = renpy.random.randint(0, 480)

            piecelist[i] = [x,y]

        movedpiece = 0

        movedplace = [0, 0]

    jump puzzle

    


label win:    
     scene black

     show whole

     "you win"

     menu:

        "Play again?"

        

        "yes":

            jump start

            

        "no":

            return


return

Any help is certainly, as one would expect, most appreciated! Thank you! :•)

Post Reply

Who is online

Users browsing this forum: Bing [Bot], Classy_Lemon