python创建三个游戏人物
时间: 2023-08-15 09:06:22 浏览: 189
利用python制作一个游戏
好的,以下是三个游戏人物的代码:
```python
class Character:
def __init__(self, name, health, strength):
self.name = name
self.health = health
self.strength = strength
def attack(self, target):
target.health -= self.strength
class Warrior(Character):
def __init__(self, name, health, strength, weapon):
super().__init__(name, health, strength)
self.weapon = weapon
def attack(self, target):
target.health -= (self.strength + self.weapon.damage)
class Mage(Character):
def __init__(self, name, health, strength, magic):
super().__init__(name, health, strength)
self.magic = magic
def cast_spell(self, target):
target.health -= (self.magic.damage + self.strength)
class Thief(Character):
def __init__(self, name, health, strength, agility):
super().__init__(name, health, strength)
self.agility = agility
def attack(self, target):
if self.agility >= target.agility:
target.health -= (2 * self.strength)
else:
target.health -= self.strength
```
这里我们定义了一个基类`Character`和三个子类`Warrior`、`Mage`和`Thief`。我们为每个角色定义了名字、生命值和力量,但是每个角色的攻击方式都不同。战士有武器,法师有魔法,盗贼有敏捷度。我们在每个子类中重新定义了`attack`方法,以适应不同的攻击方式。
阅读全文