以下是使用 Python 实现的猫狗大战模拟程序示例: ```python import random class Cat: def __init__(self, name, breed, attack_power, health): self.name = name self.breed = breed self.attack_power = attack_power self.health = health def attack_dog(self, dog): dog.health -= self.attack_power print(f"{self.name} 攻击了 {dog.name},{dog.name} 的生命值减少了 {self.attack_power},剩余生命值:{dog.health}") def eat(self): self.health += random.randint(10, 20) print(f"{self.name} 吃了东西,生命值增加了,现在生命值为:{self.health}") class Dog: def __init__(self, name, breed, attack_power, health): self.name = name self.breed = breed self.attack_power = attack_power self.health = health def attack_cat(self, cat): cat.health -= self.attack_power print(f"{self.name} 攻击了 {cat.name},{cat.name} 的生命值减少了 {self.attack_power},剩余生命值:{cat.health}") def eat(self): self.health += random.randint(10, 20) print(f"{self.name} 吃了东西,生命值增加了,现在生命值为:{self.health}") # 创建猫和狗的对象 cat1 = Cat("小花", "波斯猫", 15, 80) cat2 = Cat("小白", "布偶猫", 12, 90) dog1 = Dog("大黄", "拉布拉多", 20, 100) dog2 = Dog("小黑", "德国牧羊犬", 18, 110) # 模拟战斗 while True: # 猫攻击狗 cat1.attack_dog(dog1) if dog1.health <= 0: print(f"{dog1.name} 被 {cat1.name} 杀死了") break cat2.attack_dog(dog2) if dog2.health <= 0: print(f"{dog2.name} 被 {cat2.name} 杀死了") break # 狗攻击猫 dog1.attack_cat(cat1) if cat1.health <= 0: print(f"{cat1.name} 被 {dog1.name} 杀死了") break dog2.attack_cat(cat2) if cat2.health <= 0: print(f"{cat2.name} 被 {dog2.name} 杀死了") break # 猫和狗吃东西恢复生命值 cat1.eat() cat2.eat() dog1.eat() dog2.eat() ``` [2024-11-12 16:27:23 | AI写代码神器 | 663点数解答]