|
| 1 | +import random |
| 2 | + |
| 3 | +# classes are blueprints of the objects we want to create |
| 4 | +# every method has the __init__ self, assign new fields (characteristics) with self |
| 5 | +class Creature: |
| 6 | + def __init__(self, name, level): |
| 7 | + self.name = name |
| 8 | + self.level = level |
| 9 | + # add new behaviors (methods) with new functions within the class |
| 10 | + def defensive_roll(self): |
| 11 | + roll = random.randint(1, 12) |
| 12 | + return roll * self.level |
| 13 | + |
| 14 | +# pass the Creature class as parameter so that you can inherit Creature characteristics |
| 15 | +class Dragon(Creature): |
| 16 | + def __init__(self, name, level, scaliness, breaths_fire): |
| 17 | + # use super() to reference Creature characteristics |
| 18 | + super().__init__(name, level) |
| 19 | + # storing additional characteristics |
| 20 | + self.scaliness = scaliness |
| 21 | + self.breaths_fire = breaths_fire |
| 22 | + |
| 23 | + def defensive_roll(self): |
| 24 | + # can also use super() to call methods from Creature class |
| 25 | + roll = super().defensive_roll() |
| 26 | + value = roll * self.scaliness |
| 27 | + if self.breaths_fire: |
| 28 | + value = value * 2 |
| 29 | + |
| 30 | + return value |
| 31 | + |
| 32 | +class Wizard(Creature): |
| 33 | + |
| 34 | + def attack(self, creature): |
| 35 | + my_roll = self.defensive_roll() |
| 36 | + their_roll = creature.defensive_roll() |
| 37 | + |
| 38 | + return my_roll >= their_roll |
| 39 | + |
| 40 | + |
0 commit comments