Python 模拟猫狗大战:多猫多狗激战,生命值与攻击力的较量!
以下是使用 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() ``` ######[AI写代码神器 | 663点数解答 | 2024-11-12 16:27:23]
- Python 运行报错:urllib3 网络连接创建失败,原因及排查建议(字节豆包 | 161点数解答 | 2024-10-31 00:39:36)445
- Python 实战:学生信息列表转字典、遍历、深拷贝及数据修改全流程(GPT | 422点数解答 | 2024-10-29 15:43:54)404
- Python实战:学生成绩列表转字典,深拷贝与数据修改操作全解析(字节豆包 | 254点数解答 | 2024-10-29 16:01:39)392
- 易语言实现复杂文本信息提取:解析游戏装备任务文本代码全揭秘(字节豆包 | 854点数解答 | 2025-06-02 21:47:02)170
- 易语言:轻松处理游戏文本,提取装备属性与去除颜色标记(DeepSeek | 492点数解答 | 2025-06-02 21:49:03)178
- Python 模拟猫狗大战:多猫多狗激战,生命值攻防大比拼!(GPT | 1149点数解答 | 2024-11-12 16:17:42)141
- Python 模拟猫狗大战:多猫多狗激战,生命值与攻击力的较量!(字节豆包 | 663点数解答 | 2024-11-12 16:27:23)182
- 超燃!Python模拟猫狗大战,属性、战斗、进食全模拟!(字节豆包 | 558点数解答 | 2024-12-20 10:13:15)134
- Python 模拟猫狗大战:从属性设定到生死对决(讯飞星火 | 453点数解答 | 2024-12-20 10:14:18)107
- C++实现戴夫后院抵御僵尸的植物选择策略(DeepSeek | 773点数解答 | 2026-01-19 19:20:57)33
- Java调用Python接口中文乱码?设置UTF - 8编码一招解决!(讯飞星火 | 263点数解答 | 2024-06-06 17:07:59)380
- 解决Java调用Python接口中文乱码问题:设置UTF - 8编码全攻略(讯飞星火 | 160点数解答 | 2024-06-06 17:18:39)406