Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

How do i make a text shop - Python 3

+3 votes
asked Jan 7 by ARTY (160 points)
So I need to make a text shop in python for a game and I want to add a sword in it that let's you defeat enemies that you could not defeat before. I also need to add the sword to the inventory to show the player he has it

1 Answer

+2 votes
answered Jan 8 by Peter Minarik (15,580 points)
edited Jan 8 by Peter Minarik

Think about what you need for your game.

  • What classes do you need for this?
  • What functionality would these classes offer?
  • What relations would be there between the classes?

I could think of the following.

Shop

  • ListItems()
  • Sell(ItemId)

Sword : Item

  • Use()

Player

  • Equip(Item)

Give it a go, think about how you'd do it. Implement it and come back and show us how it went.

Update

I was a bit bored, so I did quick implementation. Feel free to make it better:

class Hero:
    def __init__(self):
        self.items = []

    def Add(self, item):
        self.items.append(item)
    
    def Has(self, itemName):
        for item in self.items:
            if (item.name == itemName):
                return True
        return False
    
    def Die(self):
        print('The hero has died a terrible death')

class Monster:
    def Fight(self, hero):
        if (hero.Has('Sword')):
            self.Die()
        else:
            hero.Die()
    
    def Die(self):
        print('The monster has been slain.')

class Sword:
    def __init__(self):
        self.name = 'Sword'

class HealingPotion:
    def __init__(self):
        self.name = "Healing potion"

class Shop:
    def __init__(self):
        self.items = [Sword(), HealingPotion()]
    
    def ShowItems(self):
        print('Available items in store:')
        index = 0
        for item in self.items:
            print(str(index) + '.\t' + item.name)
            index = index + 1
    
    def Purchase(self, itemIndex): # itemIndex is 0-based
        if (itemIndex < 0):
            print('Index must be >= 0')
            return None
        length = len(self.items)
        if (itemIndex >= length):
            print('Index must be < ' + str(length))
            return None
        return self.items[itemIndex]


hero = Hero()
shop = Shop()
shop.ShowItems()
itemId = int(input('Which item would you like to purchase (enter ID only): '))
item = shop.Purchase(itemId) # 0 --> Sword; 1 --> Healing potion
if (item != None):
    print('Purchased a ' + item.name + '.')
    hero.Add(item)
else:
    print('Nothing purchased.')
monster = Monster()
monster.Fight(hero)
commented Jan 8 by ARTY (160 points)
Thanks it's perfect
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...