Skip to content

Instantly share code, notes, and snippets.

@joncle
Created January 22, 2020 19:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joncle/f1a5f1c45f2085589da030eca3380823 to your computer and use it in GitHub Desktop.
Save joncle/f1a5f1c45f2085589da030eca3380823 to your computer and use it in GitHub Desktop.
from collections import Counter
class Player:
def __init__(self, name, **kwargs):
self.name = name
self.stats = kwargs.copy()
self.inventory = Counter()
def check_status(self):
pass
def pickup_item(self, what):
self.inventory[what] += 1
self.check_status()
def use_item(self, what):
available = self.inventory[what]
if available < 1:
return {}
what.apply_to(self)
self.inventory.subtract([what])
self.check_status()
class InventoryItem:
def __init__(self, main, sub=None, description='', **kwargs):
self.main = main
self.sub = sub
self.description = description
self.stats = kwargs.copy()
def __repr__(self):
return f'{self.main} [{self.sub}]'
def __hash__(self):
return hash((self.main, self.sub))
def apply_to(self, target):
pass
class SimpleHealthPotion(InventoryItem):
def apply_to(self, target):
target.stats['health'] += self.sub
class SuperHealthPotion(InventoryItem):
def apply_to(self, target):
target.stats['health'] *= 10
player = Player('cabbage', health=1, skill=3, luck=5)
health_potion = SimpleHealthPotion('Heal', 5)
player.pickup_item(health_potion)
player.use_item(health_potion)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment