Reputation: 83
I don't really know how to word this other than: does this work, and could I add more to it like the bush statement?
class Enemy():
def __init__(self, name, HP, skills, attack, defense, loot, expget, flvrtxt):
self.name = name
self.HP = health
self.skills = skills
self.attack = attack
self.defense = defense
self.loot = loot
self.expget = expget
self.flvrtxt = text
bush = Enemy()
bush.name = "Bush"
bush.HP = 10
bush.skills = "none"
bush.attack = 0
bush.defense = 5
bush.loot = "A stick"
bush.expget = 50
bush.flvrtxt = "Just a bush, it cannot attack"
And by adding more, I mean can I basically copy paste the bush definitions, change the stats around and make a brand new enemy? For example, could I add this?:
imp = Enemy():
imp.name = "Imp"
imp.HP = 50
imp.skills = "none"
imp.attack = 10
imp.defense = 10
imp.loot = "gold"
imp.expget = 150
imp.flvrtxt = "Cliche RPG enemy"
Upvotes: 0
Views: 1112
Reputation: 1297
Off the top of my head, I can think of two ways that you could go about this depending on what you are trying to achieve.
The way that you have your code structured now, you can create instances of the Enemy
class, that are defined by their arguments. As @Patrick Haugh pointed out in the comments section, you could explicitly set all of the attributes of Enemy
at construction if you update your method to be titled __init__(self, name, HP, skills, attack, defense, loot, expget, flvrtxt)
:
class Enemy(object):
def __init__(self, name, HP, skills, attack, defense, loot, expget, flvrtxt):
self.name = name
self.HP = HP
self.skills = skills
self.attack = attack
self.defense = defense
self.loot = loot
self.expget = expget
self.flvrtxt = flvrtext
bush = Enemy("Bush", 10, "none", 0, 5, "A Stick", 50, "Just a bush, it cannot attack")
imp = Enemy("Imp", 50, "none", 10, 10, "gold", 150, "Cliche RPG enemy")
Alternately, if you intend to instantiate specific types of Bush
es and Imp
s, you might want to create them as their own classes that sub-class the Enemy
class but have their own default implementations for their associated fields:
class Bush(Enemy):
def __init__(self):
super(Bush, self).__init__("Bush", 10, "none", 0, 5, "A Stick", 50, "Just a bush, it cannot attack")
class Imp(Enemy):
def __init__(self):
super(Imp, self).__init__("Imp", 50, "none", 10, 10, "gold", 150, "Cliche RPG enemy")
This way would allow you to write code like the following, to instantiate a Bush
that would come pre-poulated with all of the fields that you define for a Bush
in its class:
bush = Bush();
Upvotes: 1