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
Sword : Item
Player
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)