how to create a draggroup in a python function?

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
Sevlac
Newbie
Posts: 1
Joined: Wed Sep 13, 2023 5:00 pm
Contact:

how to create a draggroup in a python function?

#1 Post by Sevlac »

Hi, I try to make a cross math minigame that should look like this.
Capture d'écran 2023-09-13 075429.png
but right now it look like this.
Capture d'écran 2023-09-13 200634.png
So, my problem is that the drags don't go on the correct position and they are not draggable.
What should I change?

(I tried to change the position of my drags after adding draggroup in the screen but it does nothing.)

Here is my code

Code: Select all

init python:
    level1field = [["0","+","7","=","17","n","n","n","n"],["+","n","+","n","n","n","n","n","n"],["0","n","0","n","0","+","3","=","0"],["=","n","=","n","-","n","n","n","n"],["19","-","0","=","7","n","n","n","n"],["n","n","n","n","=","n","n","n","n"],["n","n","n","n","10","n","n","n","n"],["n","n","n","n","n","n","n","n","n"],["n","n","n","n","n","n","n","n","n"]]
    level1answers = ["10","9","5","17","20","12"]
    square_list = []
    square_answer_list = []
    blank_square_list = []

    class square_info:
        def __init__(self, name, xposition, yposition, value, ison=None):
            self.name = name
            self.xposition = xposition
            self.yposition = yposition
            self.value = value
            self.ison = ison

    def create_draggroup(levelfield,levelanswers):
        square_answer_list.clear()
        blank_square_list.clear()
        for truc in square_list:
            my_draggroup.remove(my_draggroup.get_child_by_name(truc.name))
        square_list.clear()
        #gap between each square
        gap = 48

        top_gap = 200
        left_gap = 50
        xsquare_pos = left_gap
        ysquare_pos = top_gap
        answer = 0
        cpt = 0
        for line in levelfield:
            for case in line:
                if case != "n":
                    # case with 0 value is a blank square
                    if case != "0":
                        my_draggroup.add(Drag(d = Fixed("square.png",Text(case, color = "#000000ff", size = 25, align=(0.5, 0.5))), drag_name = cpt, draggable = False, droppable = False, xypos = (xsquare_pos,ysquare_pos))) 
                        tmp_square = square_info(name = cpt, xposition=xsquare_pos, yposition=ysquare_pos, value=case)
                        square_list.append(tmp_square) 
                    else:
                        my_draggroup.add(Drag(d = Fixed("square.png"), drag_name = cpt, draggable = False, droppable = True, xypos = (xsquare_pos,ysquare_pos)))
                        tmp_square = square_info(name = cpt, xposition=xsquare_pos, yposition=ysquare_pos, value=levelanswers[answer])
                        square_list.append(tmp_square) 
                        blank_square_list.append(tmp_square) 
                        answer += 1
                    cpt += 1
                xsquare_pos += gap
            ysquare_pos += gap
            xsquare_pos = left_gap
        xsquare_pos = 500
        ysquare_pos = 500
        for square_answer in levelanswers:
            my_draggroup.add(Drag(d = Fixed("squareanswer.png",Text(square_answer, color = "#002affff", size = 25, align=(0.5, 0.5))), drag_name = cpt, draggable = True, droppable = False, dragged = dragged_func, xypos = (xsquare_pos,ysquare_pos)))
            tmp_square = square_info(name = cpt, xposition=xsquare_pos, yposition=ysquare_pos, value=square_answer)
            square_list.append(tmp_square) 
            square_answer_list.append(tmp_square)   
            xsquare_pos += 100
            cpt += 1  
        print(len(square_list)) 

    def dragged_func(dragged_items, dropped_on):
        selected_drag = dragged_items[0]
        if dropped_on is not None:
            selected_drag.snap(dropped_on.x, dropped_on.y)

default my_draggroup = DragGroup()

screen drag_drop:

    frame:
        align((0.5, 0.5))
        xysize(1536,972)
        background "#ffffffff"
        text Fixed(Text("Math test", color = "#000000ff", size = 50, align=(0.5, 0.1)))
        
        

        add my_draggroup

        $create_draggroup(level1field,level1answers)

        #python:
            #for item in square_list:
                #my_draggroup.get_child_by_name(item.name).x = item.xposition
                #my_draggroup.get_child_by_name(item.name).y = item.yposition
            
            
label  start:

    scene classroomside

    $levelfield = level1field
    $levelanswers = level1answers
    
    call screen drag_drop

    scene onehourlater

    return

Post Reply

Who is online

Users browsing this forum: No registered users