Page 1 of 1

[help] inventory with images

Posted: Mon Feb 10, 2020 3:59 pm
by elproxxgamer
Hello sorry for the inconvenience someone would tell me how I make this inventory be with images, that objects appear with images, not in the form of text. Thank you very much.

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Code: Select all

screen inventory_screen():
    imagebutton:
        idle "frame"
        action Show ("inventario")
    vbox:
        align (0.9, 0.0)
        text "Inventory:"
      
        for item in myBackpack.mySet:
            text ("[item.name], [item.amount]")
           
        
screen inventario:
    add "inventory"
   
    
init -1 python:
    class Item(object):
        def __init__(self, name, cost, wt, damage, amount=0, **kwargs):
            self.name = name
            self.cost = cost
            self.wt = wt
            self.damage = damage
            self.amount = amount
            
    class Backpack(object):
        def __init__(self, maxwt, gold, **kwargs):
            self.maxwt = maxwt
            self.space_left = maxwt
            self.mySet = set()
            self.gold = gold
            
        def add_Item(self, item, found=False):
            if item.wt <= self.space_left and (item.cost <= self.gold or found):
                self.mySet.add(item)
                self.space_left -= item.wt
                item.amount += 1
                if not found:
                    self.gold -= item.cost
                return "{0} added to inventory.".format(item.name)
            else:
                if item.wt > self.space_left:
                    return "There is not enough space in your inventory!"
                elif item.cost > self.gold:
                    return "You don't have enough gold!"
                else:
                    return "There is some problem here."
        
        def remove_Item(self, item):
            if item in self.mySet:
                item.amount -= 1
                self.space_left += item.wt
                if item.amount == 0:
                    self.mySet.remove(item)
                return "{0} removed.".format(item.name)
            else:
                return "{0} not in inventory!".format(item.name)
      
#Variable declarations
default backpack = set()
default myBackpack = Backpack(50, 50)
default money = 50
default rose = Item("Rose", 10, 0, 0, )
default horse = Item("Horse", 500, 0, 0)
      
# The game starts here.
label start:    
    show screen inventory_screen()
    $result = myBackpack.add_Item(rose)
    $result = myBackpack.add_Item(rose)
    "hello"

                          
    return

Re: [help] inventory with images

Posted: Mon Feb 10, 2020 4:31 pm
by RicharDann
Add an image attribute to your Item class, then when you create your items, fill it with the corresponding image:

Code: Select all

    class Item(object):
        def __init__(self, name, image, **kwargs):
            self.name = name
            self.image = image

default rose = Item("Rose", "rose.png")
Then you can show it in your inventory_screen with add statement:

Code: Select all

        for item in myBackpack.mySet:
	    add item.image

Re: [help] inventory with images

Posted: Mon Feb 10, 2020 4:41 pm
by Per K Grok
elproxxgamer wrote:
Mon Feb 10, 2020 3:59 pm
Hello sorry for the inconvenience someone would tell me how I make this inventory be with images, that objects appear with images, not in the form of text. Thank you very much.

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Code: Select all

screen inventory_screen():
    imagebutton:
        idle "frame"
        action Show ("inventario")
    vbox:
        align (0.9, 0.0)
        text "Inventory:"
      
        for item in myBackpack.mySet:
            text ("[item.name], [item.amount]")
           
        
screen inventario:
    add "inventory"
   
    
init -1 python:
    class Item(object):
        def __init__(self, name, cost, wt, damage, amount=0, **kwargs):
            self.name = name
            self.cost = cost
            self.wt = wt
            self.damage = damage
            self.amount = amount
            
    class Backpack(object):
        def __init__(self, maxwt, gold, **kwargs):
            self.maxwt = maxwt
            self.space_left = maxwt
            self.mySet = set()
            self.gold = gold
            
        def add_Item(self, item, found=False):
            if item.wt <= self.space_left and (item.cost <= self.gold or found):
                self.mySet.add(item)
                self.space_left -= item.wt
                item.amount += 1
                if not found:
                    self.gold -= item.cost
                return "{0} added to inventory.".format(item.name)
            else:
                if item.wt > self.space_left:
                    return "There is not enough space in your inventory!"
                elif item.cost > self.gold:
                    return "You don't have enough gold!"
                else:
                    return "There is some problem here."
        
        def remove_Item(self, item):
            if item in self.mySet:
                item.amount -= 1
                self.space_left += item.wt
                if item.amount == 0:
                    self.mySet.remove(item)
                return "{0} removed.".format(item.name)
            else:
                return "{0} not in inventory!".format(item.name)
      
#Variable declarations
default backpack = set()
default myBackpack = Backpack(50, 50)
default money = 50
default rose = Item("Rose", 10, 0, 0, )
default horse = Item("Horse", 500, 0, 0)
      
# The game starts here.
label start:    
    show screen inventory_screen()
    $result = myBackpack.add_Item(rose)
    $result = myBackpack.add_Item(rose)
    "hello"

                          
    return


Not tested, but you could give this a try

-----

for item in myBackpack.mySet:
add item.pix

-----

class Item(object):
def __init__(self, pix, name, cost, wt, damage, amount=0, **kwargs):
self.pix = pix

---------

default rose = Item("roseIcon.png", "Rose", 10, 0, 0, )
default horse = Item("horseIcon.png", "Horse", 500, 0, 0)

Re: [help] inventory with images

Posted: Mon Feb 10, 2020 5:25 pm
by elproxxgamer
Thank you very much now the problem I have is that the items in the inventory come out anywhere, do you know any way of putting in what positions they can appear?

Re: [help] inventory with images

Posted: Tue Feb 11, 2020 2:40 pm
by Per K Grok
elproxxgamer wrote:
Mon Feb 10, 2020 5:25 pm
Thank you very much now the problem I have is that the items in the inventory come out anywhere, do you know any way of putting in what positions they can appear?
I did a test-run of the code and it seems to work fine.

I think that you might not understand what the code does. Therefore you do not know what result to expect.

In "your" code the backpack has one rose added and then a second rose. As the code is set up this is one item with the amount set to 2, not two items.

In the original setting you would now in the inventory have the text; Rose, 2

When replacing 'text ("[item.name], [item.amount]")' with 'add item.pix', you will only get the one icon for a rose.

In the code below I have put both in. That way you will get the icon and 'Rose, 2'.

Another thing the code does is that you have a certain amount of gold (50 to start with). Adding stuff to the backpack costs gold.

A rose costs 10 gold so you can add that. But a horse costs 500 so you can not afford that (Adding a horse to the backpack seems a bit strange).

myBackpack.add_Item(horse) will fail.

But if you find the horse it is free.

myBackpack.add_Item(horse, True) will succed.


In the inventory I have also added a line that show how much gold you have.
text "Gold: [myBackpack.gold]"

Under start I have added some more lines for adding stuff.
["result"] will return messages on whether an attempt was successful or not.

I've also added a line for adding gold to the backpack.

With this start you should see if you can analyze the code and see what different parts does (I have not cracked all of it).
You really have to be able to understand the code to make it work.

Code: Select all


screen inventory_screen():
    #imagebutton:
    #    idle "frame"

    vbox:
        align (0.9, 0.0)

        text "Gold: [myBackpack.gold]"
        text "Inventory:"
        for item in myBackpack.mySet:
            add item.pix
            text ("[item.name], [item.amount]")



screen inventario:
    add "inventory"


init -1 python:
    class Item(object):
        def __init__(self, pix, name, cost, wt, damage, amount=0, **kwargs):
            self.pix = pix
            self.name = name
            self.cost = cost
            self.wt = wt
            self.damage = damage
            self.amount = amount

    class Backpack(object):
        def __init__(self, maxwt, gold, **kwargs):
            self.maxwt = maxwt
            self.space_left = maxwt
            self.mySet = set()
            self.gold = gold

        def add_Item(self, item, found=False):
            if item.wt <= self.space_left and (item.cost <= self.gold or found):
                self.mySet.add(item)
                self.space_left -= item.wt
                item.amount += 1
                if not found:
                    self.gold -= item.cost
                return "{0} added to inventory.".format(item.name)
            else:
                if item.wt > self.space_left:
                    return "There is not enough space in your inventory!"
                elif item.cost > self.gold:
                    return "You don't have enough gold!"
                else:
                    return "There is some problem here."

        def remove_Item(self, item):
            if item in self.mySet:
                item.amount -= 1
                self.space_left += item.wt
                if item.amount == 0:
                    self.mySet.remove(item)
                return "{0} removed.".format(item.name)
            else:
                return "{0} not in inventory!".format(item.name)

#Variable declarations
default backpack = set()
default myBackpack = Backpack(50, 50)
default money = 50
default rose = Item("rose.png","Rose", 10, 0, 0, )
default horse = Item("horse.png","Horse", 500, 0, 0)

# The game starts here.
label start:
    show screen inventory_screen()
    $result =  myBackpack.add_Item(horse)
    "[result]"
    $result =  myBackpack.add_Item(horse, True)
    "[result]"
    $result =  myBackpack.add_Item(rose)
    "[result]"
    $ myBackpack.gold+= 500
    "+ 500 gold"
    $result =  myBackpack.add_Item(rose)
    "[result]"
    "hello"


    return