[Solved]Change item in list

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
bonnie_641
Regular
Posts: 133
Joined: Sat Jan 13, 2018 10:57 pm
Projects: Código C.O.C.I.N.A.
Deviantart: rubymoonlily
Contact:

[Solved]Change item in list

#1 Post by bonnie_641 »

Hello everyone.
I have been practicing about lists. However, I have this problem.

The list, at the beginning, shows the cards face down.

Code: Select all

$ items_list = [
        {"name":"uno", "child":"images/d.png", "default_x_pos":197, "default_y_pos":202, "x_pos":197, "y_pos":202},
        {"name":"dos", "child":"images/d.png", "default_x_pos":197, "default_y_pos":202, "x_pos":197, "y_pos":202},
        {"name":"tres", "child":"images/d.png", "default_x_pos":197, "default_y_pos":202, "x_pos":197, "y_pos":202},
        ]
When you drag the card to a location, it should change to face up.
For example,

Code: Select all

	# pseudocode
$ item_list[0] = # Suppose you change the first row (change name, appearance, and location). 
	{"name":"Decisión", "child":"images/01.png", "default_x_pos":197, "default_y_pos":202, "x_pos":197, "y_pos":202},
#to:
$ slots_list[0] = [ 
        {"name":"1", "child":"images/d1.png", "x_pos":750, "y_pos":202, "var_name":"itemone"},
        
   ####     card 1 [in deck] -> a slot -> any card (random) [shows content of the card].
I don't know how to make the changes :C

Code: Select all

default interaccion = False
default itemUno = False
default itemDos = False
default itemTres = False

image esperanza = "images/01.png"
image logica = "images/02.png"

init python:

    def my_dragged (drags, drop):
        
        ####
        # If dragged item wasn't dropped over droppable item.
        #
        if not drop:
            
            # Let's find dragged item in items_list,
            # set its x_pos and y_pos to default values
            # and move it to its default position.
            for i in items_list:
                if i["name"] == drags[0].drag_name:
                    ind = items_list.index(i)
                    items_list[ind]["x_pos"] = i["default_x_pos"]
                    items_list[ind]["y_pos"] = i["default_y_pos"]
                    drags[0].snap(i["default_x_pos"], i["default_y_pos"], 0.1)
            
            # (in case of item been dragged out of the slot)
            # If dragged item was already placed in one of the slots,
            # the corresponding variable was set to the name of this item.
            # Let's find this variable and delete it.
            for s in slots_list:
                if hasattr(store, s["var_name"]):
                    if getattr(store, s["var_name"]) == drags[0].drag_name:
                        delattr(store, s["var_name"])
                        renpy.restart_interaction()   # This command used to refresh variables values shown onscreen (delete it if no need to show values) 
            return
        
        
        ####
        # If dragged item was dropped onto droppable item.
        #
        
        # Let's find the dragged item index in items_list.
        for i in items_list:
                if i["name"] == drags[0].drag_name:
                    ind = items_list.index(i)
                    
        # Default value for variable dropped_over_slot
        # (we don't know yet if item was dropped over slot or not).
        dropped_over_slot = False
        
        # Let's check if one of variables has value of dragged item name.
        # If so - delete this variable.
        for s in slots_list:
            if hasattr(store, s["var_name"]):
                if getattr(store, s["var_name"]) == drags[0].drag_name:
                        delattr(store, s["var_name"])
            
            # Also, let's check if the position of an item 
            # we dropped our dragged item onto match the
            # position of one of the slots.
            # If so - set dragged items x_pos and y_pos to the values of (x,y)
            # of item dropped onto,
            # set the corresponding variable value to the name of dragged item,
            # and set the variable dropped_over_slot to True.
            if s["x_pos"] == drop.x and s["y_pos"] == drop.y:
                items_list[ind]["x_pos"] = drop.x
                items_list[ind]["y_pos"] = drop.y
                setattr(store, s["var_name"], drags[0].drag_name)
                dropped_over_slot = True
                
        # If dragged item was dropped onto another item (not onto the slot)
        # let's move dragged item to its default position.
        if not dropped_over_slot:
            drags[0].snap(items_list[ind]["default_x_pos"], items_list[ind]["default_y_pos"], 0.1)
            return
            

        # Let's place dragged item over item it was dropped onto.
        drags[0].snap(drop.x, drop.y, 0.1)
        
        # Let's check if another item was already placed over this slot
        # and if so - move this another item to its default position.
        for j in items_list:
            if j["name"] == drop.drag_name:
                ind = items_list.index(j)
                items_list[ind]["x_pos"] = j["default_x_pos"]
                items_list[ind]["y_pos"] = j["default_y_pos"]
                drop.snap(j["default_x_pos"], j["default_y_pos"], 0.1)
        
        renpy.restart_interaction()  # This command used to refresh variables values shown onscreen (delete it if no need to show values)
        return
    


screen shop_screen:

    # An image as background.
    add "bg.jpg"
    
    ####
    # This part shows variables values (just delete this lines if not needed).
    #
    vbox:
        align (0.95, 0.05)
        if hasattr(store, "itemone"):
            text "Primera carta: [itemone]"
            $ itemUno = True
        if hasattr(store, "itemtwo"):
            text "Segunda carta: [itemtwo]"
            $ itemDos = True
        if hasattr(store, "itemthree"):
            text "Tercera carta: [itemthree]"
            $ itemTres = True
    #
    ####
        

    # A drag group ensures that items can be dragged to BUY
    draggroup:
        
        # At first create slots from their description.
        for each_slot in slots_list:
            drag:
                drag_name each_slot["name"]
                draggable False
                child each_slot["child"]
                xpos each_slot["x_pos"] ypos each_slot["y_pos"]
        
        # And then - items from their descriptions.
        for each_item in items_list:
            drag:
                drag_name each_item["name"]
                child each_item["child"]
                droppable True
                dragged my_dragged
                xpos each_item["x_pos"] ypos each_item["y_pos"]
    
    if itemUno:
        $ persistent.items_list[0,"child"]= "images/01.png"

            
# The game starts here.
label start:
    
    
    # The descriptions of items and slots (one easily can add more items or slots).
    # We need them to create items and slots automaticaly
    # and also to be able to properly store the game state
    # if it will be saved during the interactions with the screen.
    $ items_list = [
        {"name":"uno", "child":"images/d.png", "default_x_pos":197, "default_y_pos":202, "x_pos":197, "y_pos":202},
        {"name":"dos", "child":"images/d.png", "default_x_pos":197, "default_y_pos":202, "x_pos":197, "y_pos":202},
        {"name":"tres", "child":"images/d.png", "default_x_pos":197, "default_y_pos":202, "x_pos":197, "y_pos":202},
        {"name":"cuatro", "child":"images/d.png", "default_x_pos":197, "default_y_pos":202, "x_pos":197, "y_pos":202},
        # {"name":"Decisión", "child":"images/01.png", "default_x_pos":100, "default_y_pos":100, "x_pos":100, "y_pos":100},
        # {"name":"Imaginación", "child":"images/02.png", "default_x_pos":150, "default_y_pos":100, "x_pos":150, "y_pos":100},
        # {"name":"Naturaleza", "child":"images/03.png", "default_x_pos":170, "default_y_pos":100, "x_pos":150, "y_pos":100},
        # {"name":"Fortaleza", "child":"images/04.png", "default_x_pos":170, "default_y_pos":100, "x_pos":150, "y_pos":100},
        # {"name":"Esperanza", "child":"images/05.png", "default_x_pos":170, "default_y_pos":100, "x_pos":150, "y_pos":100},
        # {"name":"Inicio", "child":"images/06.png", "default_x_pos":170, "default_y_pos":100, "x_pos":150, "y_pos":100},
        # {"name":"Certeza", "child":"images/07.png", "default_x_pos":150, "default_y_pos":100, "x_pos":150, "y_pos":100},
        # {"name":"Emoción", "child":"images/08.png", "default_x_pos":170, "default_y_pos":100, "x_pos":150, "y_pos":100},
        # {"name":"Cooperación", "child":"images/09.png", "default_x_pos":170, "default_y_pos":100, "x_pos":150, "y_pos":100},
            ]
    
    $ slots_list = [ 
        {"name":"1", "child":"images/d1.png", "x_pos":750, "y_pos":202, "var_name":"itemone"},
        {"name":"2", "child":"images/d2.png", "x_pos":549, "y_pos":202, "var_name":"itemtwo"},
        {"name":"3", "child":"images/d3.png", "x_pos":950, "y_pos":202, "var_name":"itemthree"},
        ]
    

        # Another line that is neccessary for proper storing the game state
        # while interacting with the screen.
    $ renpy.retain_after_load()

    while True:

        $ interaccion = True
        call screen shop_screen

        if _return == "UnO":
            $ renpy.pause(0.001, hard=True) #<-- obligatorio para que no salte el primer diálogo.
            $ interaccion = False
            show screen shop_screen
            # --> I don't know if the code I need goes here or somewhere else (and that I write there)

Thank you in advance
Last edited by bonnie_641 on Tue Sep 14, 2021 3:44 pm, edited 1 time in total.
I speak and write in Spanish. I use an English-Spanish translator to express myself in this forum. If I make any mistakes, please forgive me.
I try my best to give an answer according to your question. :wink:

User avatar
Alex
Lemma-Class Veteran
Posts: 3090
Joined: Fri Dec 11, 2009 5:25 pm
Contact:

Re: Change item in list

#2 Post by Alex »

bonnie_641 wrote: Mon Aug 16, 2021 9:52 pm ...When you drag the card to a location, it should change to face up....
Try to change card description a bit - make "child" a dictionary that has keys "current", "back" and "face". So, you could show the current image for each card and be able to change it to back/face. Also, it's better to use predefined images instead of paths to files.

Code: Select all

image card_back = "images/d.png"
image card_face_1 = "images/f_1.png"
image card_face_2 = "images/f_2.png"
image card_face_3 = "images/f_3.png"
image card_face_4 = "images/f_4.png"

    $ items_list = [
        {"name":"uno", "child":{"current":"card_back", "back":"card_back", "face":"card_face_1"}, "default_x_pos":197, "default_y_pos":202, "x_pos":197, "y_pos":202},
        {"name":"dos", "child":{"current":"card_back", "back":"card_back", "face":"card_face_2"}, "default_x_pos":197, "default_y_pos":202, "x_pos":197, "y_pos":202},
        {"name":"tres", "child":{"current":"card_back", "back":"card_back", "face":"card_face_3"}, "default_x_pos":197, "default_y_pos":202, "x_pos":197, "y_pos":202},
        {"name":"cuatro", "child":{"current":"card_back", "back":"card_back", "face":"card_face_4"}, "default_x_pos":197, "default_y_pos":202, "x_pos":197, "y_pos":202},
        ]
in shop_screen:

Code: Select all

        # And then - items from their descriptions.
        for each_item in items_list:
            drag:
                drag_name each_item["name"]
                child each_item["child"]["current"] # <---
                droppable True
                dragged my_dragged
                xpos each_item["x_pos"] ypos each_item["y_pos"]

And in my_dragged function:

Code: Select all

        # Let's place dragged item over item it was dropped onto.
        items_list[ind]["child"]["current"] = items_list[ind]["child"]["face"] # <--- change card's image
        #renpy.restart_interaction() # <--- to redraw card, but you already have it some lines below 
        drags[0].snap(drop.x, drop.y, 0.1)

User avatar
bonnie_641
Regular
Posts: 133
Joined: Sat Jan 13, 2018 10:57 pm
Projects: Código C.O.C.I.N.A.
Deviantart: rubymoonlily
Contact:

Re: Change item in list

#3 Post by bonnie_641 »

Alex wrote: Tue Aug 17, 2021 5:38 pm (...)
And in my_dragged function:

Code: Select all

        # Let's place dragged item over item it was dropped onto.
        items_list[ind]["child"]["current"] = items_list[ind]["child"]["face"] # <--- change card's image
        #renpy.restart_interaction() # <--- to redraw card, but you already have it some lines below 
        drags[0].snap(drop.x, drop.y, 0.1)
Thank you very much Alex.
You are very kind to answer my question.
I edited the code as you explained.
bonnie_641 wrote: Mon Aug 16, 2021 9:52 pm
When you drag the card to a location, it should change to face up.
For example,

Code: Select all

	# pseudocode
$ item_list[0] = # Suppose you change the first row (change name, appearance, and location). 
	{"name":"Decisión", "child":"images/01.png", "default_x_pos":197, "default_y_pos":202, "x_pos":197, "y_pos":202},
#to:
$ slots_list[0] = [ 
        {"name":"1", "child":"images/d1.png", "x_pos":750, "y_pos":202, "var_name":"itemone"}, ### <--- Done  :D 
        
   ####     card 1 [in deck] -> a slot -> any card (random) [shows content of the card]. ### <----- Missing this one
I try to make the cards appear randomly when it reaches one of the sectors of the table.

Code: Select all

python:
        for elemento in items_list: #### <---- Fail XD
            items_list[elemento["name"]] = renpy.random.randint(1, 4) #### <---- Fail XD
But an error is displayed :oops:
Edit:
To randomly draw the cards:

Code: Select all

# Another line that is neccessary for proper storing the game state
    # while interacting with the screen.
    $ renpy.retain_after_load()
    $ renpy.random.shuffle(items_list) ####<----------- add code here :D
My other question: events

- When the card "f_1" appears on the table, a message appears.
- When the 3 cards appear on the table, it should give a meaning of what it is in general (tarot style).
I speak and write in Spanish. I use an English-Spanish translator to express myself in this forum. If I make any mistakes, please forgive me.
I try my best to give an answer according to your question. :wink:

User avatar
Alex
Lemma-Class Veteran
Posts: 3090
Joined: Fri Dec 11, 2009 5:25 pm
Contact:

Re: Change item in list

#4 Post by Alex »

bonnie_641 wrote: Tue Aug 17, 2021 8:53 pm ...My other question: events

- When the card "f_1" appears on the table, a message appears.
- When the 3 cards appear on the table, it should give a meaning of what it is in general (tarot style).
That depends of how the whole system you got works.
If your shop_screen returns some value (like the name of dragged card) you can use this value.
In my_dragged function, when card was flipped, instead of just 'return' make it return the name of this card

Code: Select all

return drags[0].drag_name
then in game label

Code: Select all

    while True:
        $ interaccion = True
        call screen shop_screen

        if _return == "uno":
            $ renpy.pause(0.001, hard=True) #<-- obligatorio para que no salte el primer diálogo.
            $ interaccion = False
            "You got Uno card"

Similar way you can have a list of opened cards and can fills it with cards, then check if it has 3 items, and if so, show some message.

Code: Select all

default opened_cards_list = [] # an empty list
in my_dragged function

Code: Select all

        # If dragged item was dropped onto another item (not onto the slot)
        # let's move dragged item to its default position.
        if not dropped_over_slot:
            drags[0].snap(items_list[ind]["default_x_pos"], items_list[ind]["default_y_pos"], 0.1)
            return
            

        # Let's place dragged item over item it was dropped onto.
        items_list[ind]["child"]["current"] = items_list[ind]["child"]["face"] # <--- change card's image
        store.opened_cards_list.append(drags[0].drag_name) # <--- add the name of the card to a list
        drags[0].snap(drop.x, drop.y, 0.1)
        
        # Let's check if another item was already placed over this slot
        # and if so - move this another item to its default position.
        for j in items_list:
            if j["name"] == drop.drag_name:
                ind = items_list.index(j)
                items_list[ind]["x_pos"] = j["default_x_pos"]
                items_list[ind]["y_pos"] = j["default_y_pos"]
                drop.snap(j["default_x_pos"], j["default_y_pos"], 0.1)
        
        renpy.restart_interaction()  # This command used to refresh variables values shown onscreen (delete it if no need to show values)
        return drags[0].drag_name # <--- return card's name
then in game label

Code: Select all

    while True:
        $ interaccion = True
        call screen shop_screen

        if _return == "uno":
            $ renpy.pause(0.001, hard=True) #<-- obligatorio para que no salte el primer diálogo.
            $ interaccion = False
            "You got Uno card"

        if len(opened_cards_list) == 3:
            # check the name of cards in a list and show some message
            # don't forget to move cards out of table and set opened_cards_list to an empty list

User avatar
bonnie_641
Regular
Posts: 133
Joined: Sat Jan 13, 2018 10:57 pm
Projects: Código C.O.C.I.N.A.
Deviantart: rubymoonlily
Contact:

Re: Change item in list

#5 Post by bonnie_641 »

All the time I spent absent from the forum was to resolve this issue.
I tried hard. On the one hand, I managed to get her to identify each deck the letter that arrives to her (the corresponding message comes out). But I forgot a detail that is important: each card that arrives to a section cannot move again (that's where it generates (one or several) error message).
Alex wrote: Wed Aug 18, 2021 1:03 pm (...)
then in game label

Code: Select all

   
        if len(opened_cards_list) == 3:
            # check the name of cards in a list and show some message
            # don't forget to move cards out of table and set opened_cards_list to an empty list
Thank you Alex for helping me. Although I consider myself too clumsy to find a solution. :oops:
As the cards are not blocked, the counter increases and arrives earlier than budgeted (either by moving more than once the card between decks).
The only thing I could do was:

Code: Select all



default interaccion = False
default itemUno = False
default itemDos = False
default itemTres = False
default can_move = True

image card_back = "images/d.png"

#image card_face_1 = renpy.random.choice(["images/f_1.png", "images/f_2.png", "images/f_3.png", "images/f_4.png"])

image card_1 = (["card_face_1", "card_face_2", "card_face_3", "card_face_4"])
default card_face_1 = renpy.random.choice("card_1")
image card_face_1 = "images/f_1.png"
image card_face_2 = "images/f_2.png"
image card_face_3 = "images/f_3.png"
#image card_face_1 = "images/f_1.png"
image card_face_2 = "images/f_5.png"
image card_face_3 = "images/f_6.png"
image card_face_4 = "images/f_7.png"

image esperanza = "images/01.png"
image logica = "images/02.png"

default opened_cards_list = [] # an empty list ##<---- I added what you recommended

init python:

    def my_dragged (drags, drop):
        # Variables globales boleanos
        global uno_cant_move, dos_cant_move, tres_cant_move #<------ I don't know if it is the right thing or not
        ####
        # If dragged item wasn't dropped over droppable item.
        #
        if not drop:

#######################################################################
            # I don't know if this is the wrong code.
            # Once you check that the card is in place (1, 2 or 3) it is not supposed to move anymore? What did I do wrong?
            
            if drags[0].drag_name == "uno" and drop.drag_name == "1":
                uno_cant_move = True
                drags[0].snap(drop.x,drop.y, delay=0.1)
                drags[0].draggable = False # to not let player move it elsewhere

            elif drags[0].drag_name == "dos" and drop.drag_name == "2":
                dos_cant_move = True
                drags[0].snap(drop.x,drop.y, delay=0.1)
                drags[0].draggable = False

            elif drags[0].drag_name == "tres" and drop.drag_name == "3":
                tres_cant_move = True
                drags[0].snap(drop.x,drop.y, delay=0.1)
                drags[0].draggable = False
                
            else:
                pass
######################################################################
        
            
            # Let's find dragged item in items_list,
            # set its x_pos and y_pos to default values
            # and move it to its default position.
            for i in items_list:
                if i["name"] == drags[0].drag_name:
                    ind = items_list.index(i)
                    items_list[ind]["x_pos"] = i["default_x_pos"]
                    items_list[ind]["y_pos"] = i["default_y_pos"]
                    drags[0].snap(i["default_x_pos"], i["default_y_pos"], 0.1)
                    # drags[0].draggable = False # to not let player move it elsewhere
                    # can_move = False
            
            # (in case of item been dragged out of the slot)
            # If dragged item was already placed in one of the slots,
            # the corresponding variable was set to the name of this item.
            # Let's find this variable and delete it.
            for s in slots_list:
                if hasattr(store, s["var_name"]):
                    if getattr(store, s["var_name"]) == drags[0].drag_name:
                        delattr(store, s["var_name"])
                        renpy.restart_interaction()   # This command used to refresh variables values shown onscreen (delete it if no need to show values) 
            return
        
        
        ####
        # If dragged item was dropped onto droppable item.
        #
        
        # Let's find the dragged item index in items_list.
        for i in items_list:
                if i["name"] == drags[0].drag_name:
                    ind = items_list.index(i)
                    
        # Default value for variable dropped_over_slot
        # (we don't know yet if item was dropped over slot or not).
        dropped_over_slot = False
        
        # Let's check if one of variables has value of dragged item name.
        # If so - delete this variable.
        for s in slots_list:
            if hasattr(store, s["var_name"]):
                if getattr(store, s["var_name"]) == drags[0].drag_name:
                        delattr(store, s["var_name"])
            
            # Also, let's check if the position of an item 
            # we dropped our dragged item onto match the
            # position of one of the slots.
            # If so - set dragged items x_pos and y_pos to the values of (x,y)
            # of item dropped onto,
            # set the corresponding variable value to the name of dragged item,
            # and set the variable dropped_over_slot to True.
            if s["x_pos"] == drop.x and s["y_pos"] == drop.y:
                items_list[ind]["x_pos"] = drop.x
                items_list[ind]["y_pos"] = drop.y
                setattr(store, s["var_name"], drags[0].drag_name)
                dropped_over_slot = True
                
        # If dragged item was dropped onto another item (not onto the slot)
        # let's move dragged item to its default position.
        if not dropped_over_slot:
            drags[0].snap(items_list[ind]["default_x_pos"], items_list[ind]["default_y_pos"], 0.1)
            return
            

        # Let's place dragged item over item it was dropped onto.
        items_list[ind]["child"]["current"] = items_list[ind]["child"]["face"] # <--- change card's image
        store.opened_cards_list.append(drags[0].drag_name) # <--- add the name of the card to a list
        drags[0].snap(drop.x, drop.y, 0.1)
        
        # Let's check if another item was already placed over this slot
        # and if so - move this another item to its default position.
        for j in items_list:
            if j["name"] == drop.drag_name:
                ind = items_list.index(j)
                items_list[ind]["x_pos"] = j["default_x_pos"]
                items_list[ind]["y_pos"] = j["default_y_pos"]
                drop.snap(j["default_x_pos"], j["default_y_pos"], 0.1)

                

        
        renpy.restart_interaction()  # This command used to refresh variables values shown onscreen (delete it if no need to show values)
        return drags[0].drag_name # <--- return card's name


screen shop_screen:

    # An image as background.
    add "bg.jpg"
    
    ####
    # This part shows variables values (just delete this lines if not needed).
    #
    vbox:
        align (0.95, 0.05)
        if hasattr(store, "itemone"):
            text "Primera carta: [itemone]"
            $ itemUno = True
        if hasattr(store, "itemtwo"):
            text "Segunda carta: [itemtwo]"
            $ itemDos = True
        if hasattr(store, "itemthree"):
            text "Tercera carta: [itemthree]"
            $ itemTres = True
    #
    ####
        

    # A drag group ensures that items can be dragged to BUY
    draggroup:
        
        # At first create slots from their description.
        for each_slot in slots_list:
            drag:
                drag_name each_slot["name"]
                draggable False
                child each_slot["child"]
                xpos each_slot["x_pos"] ypos each_slot["y_pos"]
        
        # And then - items from their descriptions.
        for each_item in items_list:
            drag:
                drag_name each_item["name"]
                child each_item["child"]["current"] 
                droppable True
                dragged my_dragged
                xpos each_item["x_pos"] ypos each_item["y_pos"]
    
    # if itemUno:
    #     $ persistent.items_list[0,"child"]= "images/01.png"

            
# The game starts here.
label start:
    # The descriptions of items and slots (one easily can add more items or slots).
    # We need them to create items and slots automaticaly
    # and also to be able to properly store the game state
    # if it will be saved during the interactions with the screen.
    $ items_list = [
        {"name":"uno", "child":{"current":"card_back", "back":"card_back", "face":"card_face_1"}, "default_x_pos":197, "default_y_pos":202, "x_pos":197, "y_pos":202, "uno_cant_move": False},
        {"name":"dos", "child":{"current":"card_back", "back":"card_back", "face":"card_face_2"}, "default_x_pos":197, "default_y_pos":202, "x_pos":197, "y_pos":202, "dos_cant_move": False},
        {"name":"tres", "child":{"current":"card_back", "back":"card_back", "face":"card_face_3"}, "default_x_pos":197, "default_y_pos":202, "x_pos":197, "y_pos":202, "tres_cant_move": False},
        #{"name":"cuatro", "child":{"current":"card_back", "back":"card_back", "face":"card_face_4"}, "default_x_pos":197, "default_y_pos":202, "x_pos":197, "y_pos":202},
        ]
    
    $ slots_list = [
        {"name":"1", "child":"images/d1.png", "x_pos":750, "y_pos":202, "var_name":"itemone"},
        {"name":"2", "child":"images/d2.png", "x_pos":549, "y_pos":202, "var_name":"itemtwo"},
        {"name":"3", "child":"images/d3.png", "x_pos":950, "y_pos":202, "var_name":"itemthree"},
        ]
    

        # Another line that is neccessary for proper storing the game state
        # while interacting with the screen.
    $ renpy.retain_after_load()
    $ renpy.random.shuffle(items_list) 

    while True:

        $ interaccion = True
        call screen shop_screen

        if _return == "uno": ##<-------- it works
            $ renpy.pause(0.001, hard=True) #<-- obligatorio para que no salte el primer diálogo.
            $ interaccion = False
            $ uno_cant_move = True
            show screen shop_screen
            "unoo card"

        if _return == "dos": ##<-------- it works
            $ renpy.pause(0.001, hard=True) #<-- obligatorio para que no salte el primer diálogo.
            $ interaccion = False
            $ dos_cant_move = True
            show screen shop_screen
            "dos card"

        if _return == "tres": ##<-------- it works
            $ renpy.pause(0.001, hard=True) 
            $ interaccion = False
            $ tres_cant_move = True
            show screen shop_screen
            "tres card"

            
        if len(opened_cards_list) == 3: ##<------------- if I accidentally move the card that is on the table... it never happens.
            if uno_cant_move == True and dos_cant_move == True and tres_cant_move == True:
                "Cartas desbloqueadas"
                return True

I speak and write in Spanish. I use an English-Spanish translator to express myself in this forum. If I make any mistakes, please forgive me.
I try my best to give an answer according to your question. :wink:

User avatar
bonnie_641
Regular
Posts: 133
Joined: Sat Jan 13, 2018 10:57 pm
Projects: Código C.O.C.I.N.A.
Deviantart: rubymoonlily
Contact:

Re: [Solved]Change item in list

#6 Post by bonnie_641 »

I finally did it!
I made several changes to the original code.

For this, I applied:
- LiveComposite
- DynamicDisplayable
- SetVariable
- viewport
- vbox

Shortly, I will upload the code for backup
I speak and write in Spanish. I use an English-Spanish translator to express myself in this forum. If I make any mistakes, please forgive me.
I try my best to give an answer according to your question. :wink:

User avatar
bonnie_641
Regular
Posts: 133
Joined: Sat Jan 13, 2018 10:57 pm
Projects: Código C.O.C.I.N.A.
Deviantart: rubymoonlily
Contact:

Re: [Solved]Change item in list

#7 Post by bonnie_641 »

Code: Select all

# Screen size: 1280x720 px
#█▓▒░ Capas del juego ░▒▓███████████████████████████
define config.layers = ['bg', 'under', 'master', 'transient', 'screens', 'overlay']

init python:
    config.tag_layer['screens'] = 'screens'
    config.tag_layer['adivina'] = 'screens'

#############################################
###### randomly generated cards: draw card function.
    def draw_card(st, at):
        return LiveComposite(
            (1200,600), #Size of the original image
            (546,200), "images/f_%s.png"%card, #card 1 is the first layer on the bottom (0,0) are the coordinate within (596, 996)
            (752,200), "images/f_%s.png"%cardo, #Card 2
            (950,200), "images/f_%s.png"%carto, #Card 3
        ), .1
#############################################

default interaccion = False
default itemUno = False
default itemDos = False
default itemTres = False
default ucan_move = True

default dinero = 4000

#############################################
###### cartas generados al azar: variables.
default card = 0  #
default cardo = 0
default carto = 0
default tablero = DynamicDisplayable(draw_card) 
default angel = 0
default random_contador = 0
default var = 0
default posx = 520
default posy = 420

default ca = ""
default ka = ""
default qa = ""


#############################################

image card_back = "images/d.png"

default opened_cards_list = [] # an empty list


init python:

    def my_dragged (drags, drop):
        ####
        # If dragged item wasn't dropped over droppable item.
        #
        if not drop:
        
            for i in items_list:
                if i["name"] == drags[0].drag_name:
                    ind = items_list.index(i)
                    items_list[ind]["x_pos"]
                    items_list[ind]["y_pos"]
                    drags[0].snap(i["x_pos"], i["y_pos"], 0.1)
            
            for s in slots_list:
                if hasattr(store, s["var_name"]):
                    if getattr(store, s["var_name"]) == drags[0].drag_name:
                        delattr(store, s["var_name"])

                        renpy.restart_interaction()

        for i in items_list:
                if i["name"] == drags[0].drag_name:
                    ind = items_list.index(i)
                    

        dropped_over_slot = False
        
        for s in slots_list:
            if hasattr(store, s["var_name"]):
                if getattr(store, s["var_name"]) == drags[0].drag_name:
                        delattr(store, s["var_name"])
            

            if s["x_pos"] == drop.x and s["y_pos"] == drop.y:
                items_list[ind]["x_pos"] = drop.x
                items_list[ind]["y_pos"] = drop.y
                setattr(store, s["var_name"], drags[0].drag_name)
                dropped_over_slot = True #si es falso el slot rechaza la carta 

        if not dropped_over_slot:
            drags[0].snap(items_list[ind]["x_pos"], items_list[ind]["y_pos"], 0.1)
            return
            

        items_list[ind]["child"]
        store.opened_cards_list.append(drags[0].drag_name) 
        drags[0].snap(drop.x, drop.y, 0.1)

        items_list[ind]["can_move"] = False # <---

        for j in items_list:
            if j["name"] == drop.drag_name:
                ind = items_list.index(j)
                items_list[ind]["x_pos"]
                items_list[ind]["y_pos"]
                drop.snap(j["x_pos"], j["y_pos"], 0.1)
                

        renpy.restart_interaction()
        return drags[0].drag_name
        
##################################################################################

screen tablero1():
    add "bg.jpg"
    add tablero

    frame:
        xpos 0.61
        ypos 0.755
        textbutton "Random" action [
            SetVariable('card', renpy.random.randint(1,17)),
            SetVariable('cardo', renpy.random.randint(18,35)),
            SetVariable('carto', renpy.random.randint(36,52)),
            SetVariable("angel", renpy.random.randint(0,5)),
            SetScreenVariable("random_contador", 1)
            ]

    if random_contador == 1:
        vbox:
            area(0.5,0.81,454,455)
            frame:
                textbutton "Muestra significado de las cartas" action Show ("muestra_carta_ya")
        textbutton "Random" action NullAction() xpos 0.613 ypos 0.76

screen muestra_carta_ya():
    add "images/bg1.png"
    
################################################

    if card ==1:
        $ ca = "{size=-3}Las personas cuestionan tus decisiones. \nRelájate y abre tu corazón antes de juzgar una situación.\nTen en cuenta que el optimismo atrae la buena suerte. Si estás contento, transmitirás esa energía a mucha gente y ellas te seguirán.{/size}"

    if card ==2:
        $ ca = "{size=-3}Te sentirás a gusto si expresas lo que piensas y sientes.\nGanarás experiencia si te concentras en la actividad que realizas. \nSigue tu intuición y no te restrinjas por las normas sociales.{/size}"

# (... rest of code)
    if cardo ==18:
        $ ka = "{size=-3}Ahorra todo lo que puedas. Necesitas tener objetivos a largo plazo para emprender. Invierte sabiamente.{/size}"
    if cardo ==19:
        $ ka = "{size=-3}Escribe todos los logros que quieras obtener y también aquellas que desees obtener a futuro. Reserva para tí estos anhelos de emprendimiento.{/size}"
        
# (... rest of code)
    if carto ==36:
        $ qa = "{size=-3}Cumple las promesas que hiciste, asi que no ignores este mensaje. Tu suerte se incrementará a medida que seas correcto en tus acciones.{/size}"
    if carto ==37:
        $ qa = "{size=-3}El arrepentimiento es un don que pocas personas cultivan en su vida. Si lo haces a tiempo, evitarás momentos dolorosos.{/size}"
        
# (... rest of code)
    if carto ==52:
        $ qa = "{size=-3}El amor te espera pacientemente. Acéptalo. Él es la persona más importante para tí.{/size}"

    ############################################
    if angel == 0:
        $ destreza +=7
        $ experiencia +=3

        add "images/bg.png"
        frame:
            background "images/angel_0.png"
            area (600,150,350,400)
            viewport:
                scrollbars "vertical"
                vbox:
                    spacing 10
                    xsize 318
                    xfill True
                    text "{b}Guardián Apocalíptico{/b}"
                    text "{size=-3}Anuncia problemas de larga duración. Al obtener esta carta, te da la posibilidad de salir adelante más fuerte y más sabio.\nIncrementa: \n-> Destreza +7\n-> Experiencia +3{/size}":
                        justify True
                    null width 10
                    text ca:   ######<---------------- 'ca' is the variable for storing text. It is used in viewport
                        justify True
                    null width 10
                    text ka:   ######<---------------- 'ka' is the variable for storing text. It is used in viewport
                        justify True
                    null width 10
                    text qa:   ######<---------------- 'qa' is the variable for storing text. It is used in viewport
                        justify True
                    null width 10
                    textbutton "Cerrar" action Hide("muestra_carta_ya")

####################################
# add what you want here respecting what it says in 'if angel== 0:'
# add more 'angel' with the same format (to avoid errors) up to 'if angel==5:'. 
# If you want to add more, edit 
'SetVariable("angel", renpy.random.randint(0,5)),', where 0 and 5 are the minimum and maximum quantities respectively, from angel_0 to angel_5.
####################################
                    
                    
screen shop_screen():

    # An image as background.
    add "bg.jpg"

    vbox:
        xpos 150
        ypos 40
        ###########################
        ### Helps to detect cards in deck
        ###########################
        if hasattr(store, "itemone"):
            #text "Primera carta: [itemone]"
            $ itemUno = True
        if hasattr(store, "itemtwo"):
            #text "Primera carta: [itemone]"
            $ itemDos = True
        if hasattr(store, "itemthree"):
            #text "Primera carta: [itemone]"
            $ itemTres = True

###################################################################################
# block the cards (you don't need 'len')
###################
    if itemUno == True and itemDos == True and itemTres == True :
        $ ucan_move = False

        timer 0.002 action Jump("fin_de_shop_screen")
        
############################################################################################

    # A drag group ensures that items can be dragged to BUY
    draggroup:
        
        # At first create slots from their description.
        for each_slot in slots_list:
            drag:
                drag_name each_slot["name"]
                draggable False
                child each_slot["child"]
                xpos each_slot["x_pos"] ypos each_slot["y_pos"]
            
        
        # And then - items from their descriptions.
        for each_item in items_list:
            drag:
                drag_name each_item["name"]
                child each_item["child"]#["current"]
                droppable True
                draggable ucan_move # and each_item["can_move"])
                dragged my_dragged
                xpos each_item["x_pos"] ypos each_item["y_pos"]
################################################################
# Start game
###############################################################
label start:
    
    $ items_list = [
        {"name":"uno", "child":"card_back", "x_pos":197, "y_pos":202},
        {"name":"dos", "child":"card_back", "x_pos":197, "y_pos":202},
        {"name":"tres", "child":"card_back", "x_pos":197, "y_pos":202},
        ]

    $ slots_list = [
        {"name":"1", "child":"images/d1.png", "x_pos":750, "y_pos":202, "var_name":"itemone"},
        {"name":"2", "child":"images/d2.png", "x_pos":549, "y_pos":202, "var_name":"itemtwo"},
        {"name":"3", "child":"images/d3.png", "x_pos":950, "y_pos":202, "var_name":"itemthree"},
        ]
    

        # Another line that is neccessary for proper storing the game state
        # while interacting with the screen.
    $ renpy.retain_after_load()

    while True:
        $ interaccion = True
        call screen shop_screen()

 # ###### You can add the "_returns" here ############
 
 ################################################

label fin_de_shop_screen:
    hide screen shop_screen
    call screen tablero1() 

Card size (even that of the angel)
Card size (even that of the angel)
f_0.png (43.95 KiB) Viewed 388 times
I speak and write in Spanish. I use an English-Spanish translator to express myself in this forum. If I make any mistakes, please forgive me.
I try my best to give an answer according to your question. :wink:

Post Reply

Who is online

Users browsing this forum: Bing [Bot]