用python写一个2、 定义一个三国人物的类,实现简单的三国杀小游戏,要求包括 1)私有属性,“角色总数” 2) 构造方法,初始化属性有“角色名”,“技能名称”,“生命值”,“攻击力”,可自定义增加其他的属性; 3)公有方法,实现攻击某个角色并造成伤害,接收1个参数target(被攻击的角色),并打印相关信息, 要求结合异常处理来判断生命值是否耗尽,伤害值可依据“攻击力”随机生成也可直接使用“攻击力”属性; 4)创建3个实例对象,结合循环与选择结构所学内容,实现3个实例对象之间随机地进行互相攻击,打印出相关过程,并输出最终获胜者 3、继承类并进行扩展: 1)继承2中的三国人物类; 2)增加属性:人物所属“阵营” 3)改写或增加一个方法:实现A阵营的人物对B阵营人物造成伤害的总值到达一定程度时,随机选择A阵营人物的生命值恢复部分; 4)创建实例:创建4-6个人物实例(包括至少2个阵营,每个阵营至少2位人物),使用循环实现实例之间的相互攻击,打印出相关过程,并输出最终获胜者。
时间: 2024-04-02 07:36:05 浏览: 100
以下是代码实现:
```python
import random
class ThreeKingdoms:
__total_characters = 0
def __init__(self, name, skill_name, hp, attack, **kwargs):
self.name = name
self.skill_name = skill_name
self.hp = hp
self.attack = attack
self.__class__.__total_characters += 1
self.__dict__.update(kwargs)
def attack_target(self, target):
print(f"{self.name}正在攻击{target.name}...")
try:
damage = random.randint(1, self.attack)
target.hp -= damage
if target.hp <= 0:
target.hp = 0
self.__class__.__total_characters -= 1
print(f"{target.name}已经被{self.name}击败,当前角色总数为{self.__class__.__total_characters}")
except Exception as e:
print(f"{e}")
class ThreeKingdomsExtended(ThreeKingdoms):
def __init__(self, name, skill_name, hp, attack, camp, **kwargs):
super().__init__(name, skill_name, hp, attack, **kwargs)
self.camp = camp
def attack_target(self, target):
print(f"{self.name}({self.camp})正在攻击{target.name}({target.camp})...")
try:
damage = random.randint(1, self.attack)
target.hp -= damage
if target.hp <= 0:
target.hp = 0
self.__class__.__total_characters -= 1
print(f"{target.name}({target.camp})已经被{self.name}({self.camp})击败,当前角色总数为{self.__class__.__total_characters}")
if self.camp == "A":
total_damage = sum([char.attack for char in character_list_A])
if total_damage >= 20:
print("A阵营总伤害已达到20,随机选择一个角色恢复生命值")
character_list_A[random.randint(0, len(character_list_A)-1)].hp += 5
except Exception as e:
print(f"{e}")
# 创建三个实例
character1 = ThreeKingdoms("曹操", "奸雄", 20, 10)
character2 = ThreeKingdoms("刘备", "仁德", 18, 8)
character3 = ThreeKingdoms("孙权", "妙计", 22, 12)
# 创建一个列表,方便循环
character_list = [character1, character2, character3]
# 三个实例随机互相攻击
while ThreeKingdoms._ThreeKingdoms__total_characters > 1:
attacker = random.choice(character_list)
target = random.choice([char for char in character_list if char != attacker])
attacker.attack_target(target)
# 输出最终获胜者
for char in character_list:
if char.hp > 0:
print(f"{char.name}获得了胜利!")
# 创建4-6个实例
character1 = ThreeKingdomsExtended("曹操", "奸雄", 20, 10, "A")
character2 = ThreeKingdomsExtended("张辽", "突袭", 18, 8, "A")
character3 = ThreeKingdomsExtended("刘备", "仁德", 18, 8, "B")
character4 = ThreeKingdomsExtended("关羽", "武圣", 22, 12, "B")
character5 = ThreeKingdomsExtended("孙权", "妙计", 22, 12, "C")
# 创建阵营列表,方便循环
character_list_A = [char for char in [character1, character2] if char.camp == "A"]
character_list_B = [char for char in [character3, character4] if char.camp == "B"]
character_list_C = [char for char in [character5] if char.camp == "C"]
# 创建一个总列表方便循环
character_list = [character1, character2, character3, character4, character5]
# 实例随机互相攻击
while ThreeKingdomsExtended._ThreeKingdoms__total_characters > 1:
attacker = random.choice(character_list)
target_list = []
if attacker.camp == "A":
target_list = [char for char in character_list_B + character_list_C if char.hp > 0]
elif attacker.camp == "B":
target_list = [char for char in character_list_A + character_list_C if char.hp > 0]
elif attacker.camp == "C":
target_list = [char for char in character_list_A + character_list_B if char.hp > 0]
if target_list:
target = random.choice(target_list)
attacker.attack_target(target)
# 输出最终获胜者
for char in character_list:
if char.hp > 0:
print(f"{char.name}({char.camp})获得了胜利!")
```
以上代码实现了三国杀小游戏的基本功能,并通过继承类的方式进行了扩展,实现了不同阵营之间的相互攻击和生命值恢复。
阅读全文