#🔒 inventory system in text game

16 messages · Page 1 of 1 (latest)

calm scroll
#
class Player:
    def __init__(self,hp):
        self.hp = hp
        self.Player_Inventory = {}

    def attack(self, target):
        target.hp -= self.Player_Inventory['Rusty_Sword']

    def show_inventory(self):
        if self.Player_Inventory:
            for i, (item,value) in enumerate(self.Player_Inventory.items()):
                print(f"{i} {item}:{value}")
        else:
            print("Your inventory is empty!")
            
                

class Goblin:
    def __init__(self,hp,damage):
        self.hp = hp
        self.damage = damage
    
    def attack(self, target):
        target.hp -= self.damage

class Potions:
    def __init__(self,hp):
        self.heal_hp = 75
        

def handle_player_input(player):
    while True:
        action = input("Enter your action: ").lower()
        if action == 'i':
            player.show_inventory()
        elif action == 'e':
            return "e"
        elif action == 'm':
            return 'm'
        elif action == 'i':
            if action == '2':
                ...#im stuck
lament pantherBOT
#

@calm scroll

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

calm scroll
#
player = Player(150)

def Game():
    weapon_choice = input("You just stumbled upon a chest,Open (y/n): ")
    if weapon_choice == 'y':
        player.Player_Inventory['Rusty_Sword'] = 25
        print(f"{player.Player_Inventory}")
        print("This sword does 25 Damage!")

    time.sleep(1)
    total_goblins = 4
    goblin_counter = 0
    while goblin_counter <= total_goblins:
        goblin = Goblin(75,10)
        while goblin.hp > 0:
            if player.hp <= 0:
                print("You have fallen in battle. Game Over")
                return

            action = handle_player_input(player)
            if action == 'e':
                player.attack(goblin)
                if goblin.hp > 0:
                    goblin.attack(player)
                print(f"Goblin HP: {goblin.hp}")
                print(f"Your HP: {player.hp}")
                
        goblin_counter += 1
        print(f"{goblin_counter} Goblins down")
        if player.hp <= 0:
            print("You have fallen in battle. Game Over")
            return

    if goblin_counter >= total_goblins and player.hp > 0:
        print("Congrats! You defeated all the goblins.")
        time.sleep(1)
        print("You just obtained a healing potion and health buff. Check your inventory for the potion.")
        player.Player_Inventory['Healing_Potion'] = 75
#
action = handle_player_input(player)
    if action == 'm':
        print("Going further into the dungeon...")
        time.sleep(1.2)
        print("Oh hey, The name’s Callum. Been wandering these dungeons for a while now. You?")
        time.sleep(1.2)
        print("You: Hey, you look like you know your way around here. What’s your name?")
        time.sleep(1.2)
        print(f"You:My name is {name}. This place looks dangerous. What are you doing down here alone? ")
        time.sleep(1.2)
        print(f"Callum: Looking for treasure, thrills, and maybe a bit of revenge.")
        time.sleep(1.2)
        print("These monstors took something from me...")
        time.sleep(1.2)
        print("You: I’m here to clear this dungeon and put an end to whatever’s lurking deeper inside.")
        time.sleep(1.2)
        print("You: Agreed. I could use some backup. You any good with that sword of yours?")
        time.sleep(1.2)
        print("Callum: Good enough to keep us both alive. Let’s just hope you can keep up.")
        time.sleep(1.2)
        print("You: We'll see whos carrying who...")
        time.sleep(1.2)
        print("You: Oh hell, that is a big guy")

    elif action == 'e':
        print("There are no enemies nearby! ")

    elif action == '2':
        if player.Player_Inventory['Healing_Potion']:
            player.hp += player.Player_Inventory['Healing_Potion']
            print(f"Your HP: {player.hp}")
Game()
#

So this a text based rpg game

#

i need to implement an inventory system

#
def handle_player_input(player):
    while True:
        action = input("Enter your action: ").lower()
        if action == 'i':
            player.show_inventory()
        elif action == 'e':
            return "e"
        elif action == 'm':
            return 'm'
        elif action == 'i':
            if action == '2':
                ...#im stuck
```What I want to do here is that lets say player presses i and then they press 2 i want it like the player uses whatever is at 2
#

if its a potion the player gets that buff if its a sword the player gets the sword equiped

#
elif action == '2':
        if player.Player_Inventory['Healing_Potion']:
            player.hp += player.Player_Inventory['Healing_Potion']
            print(f"Your HP: {player.hp}")
```but as u can see here im only targeting the healing potion i have no idea how to do it the way i want
sudden lantern
#

Where is the issue

calm scroll
#

...

#

the whole code is the issue

#

specifically in these 2 boxes

#

!close

lament pantherBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.