Page 2 of 2

Re: Drag and Drop Tutorial-ish thing

Posted: Sat Jun 22, 2013 11:44 pm
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.

Re: Drag and Drop Tutorial-ish thing

Posted: Sun Jun 23, 2013 12:43 pm
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?

Re: Drag and Drop Tutorial-ish thing

Posted: Sun Jun 23, 2013 2:09 pm
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.

Re: Drag and Drop Tutorial-ish thing

Posted: Wed Aug 16, 2017 3:17 am
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

Re: Drag and Drop Tutorial-ish thing

Posted: Sat Sep 16, 2017 2:20 pm
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! :•)