2
\$\begingroup\$

What is the best way to go about making user defined classes (monsters, items, loot, armor, weapons, spell types, etc) and then calling them later during an encounter? Looking at the entire card list for a trading card game is intimidating, and I would love some advice on tackling this before I go about it the wrong way.

class Card(object):
def __init__(self, name, value, type, power):
    self.name = name
    self.value = value
    self.type = type
    self.power = power
    self.expan = 'Base'


class Monster(Card):
def __init__(self, name, value, type, power):
    self.active = False
    Card.__init__(self, name, value, type, power)
    #monsterlist.append((self.name, self.value, self.type, self.power))

Is it ideal to just make a random number and create the monster like this (there's over a thousand cards, so this doesn't seem optimal to me):

def pickMonster():
    chance = random.randint(1,2)
    if chance == 1:
        goblin = Monster('goblin', 8, 'animal', 'run')
    if chance = 2:
        troll == Monster('troll', 8, 'troll', 'smash')

Can I define monsters in a list/dictionary somehow and pull them at random from there? I was trying to use this method to make the code easier to read, but I'm having a hard time figuring out exactly how to do it that way (as I'm not even sure it's possible to put more than 2 values into a list, and dictionaries are the bane of my existence -- I'm fairly new to python).

\$\endgroup\$
1
  • \$\begingroup\$ The tittle of your question is the right answer. "Picking random monster cards aka element from a deck aka list/dict" Think logically, the deck is a containter for cards, then use a containter in you code too? Then let the random integer be the index of the element. \$\endgroup\$ – dragons Mar 18 '14 at 9:03
2
\$\begingroup\$

define them in a list/dictionary somehow and pull them at random from there?

Take this approach; not sure why you were having trouble figure out how to make a list for this. Also you may want your Monster initializer to simply take a dictionary passed to it, so that the list is a list of dictionaries you can pass that factory class. For example:

monsterList = [{'name': 'goblin', 'health': 8, 'type': 'animal', 'ability': 'run'},
               {'name': 'troll', 'health': 8, 'type': 'troll', 'ability': 'smash'}]

def pickMonster():
    monster = Monster(random.choice(monsterList))
\$\endgroup\$
1
  • \$\begingroup\$ Note that you can use dictionary unpacking to simplify this, without changing Monster.__init__: monster = Monster(**random.choice(monsterList)). Here the ** unpacks the dictionary keys and values into keyword arguments. \$\endgroup\$ – jonrsharpe Mar 24 '14 at 10:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.