用python帮我写一个植物大战僵尸代码
时间: 2023-08-28 11:54:19 浏览: 65
当然可以!下面是一个简单的植物大战僵尸游戏的代码示例:
```python
import random
# 定义植物类
class Plant:
def __init__(self, name, attack_power, health):
self.name = name
self.attack_power = attack_power
self.health = health
def attack(self, zombie):
zombie.health -= self.attack_power
print(f"{self.name}攻击了僵尸,造成了{self.attack_power}点伤害")
# 定义僵尸类
class Zombie:
def __init__(self, name, attack_power, health):
self.name = name
self.attack_power = attack_power
self.health = health
def attack(self, plant):
plant.health -= self.attack_power
print(f"{self.name}攻击了植物,造成了{self.attack_power}点伤害")
# 创建植物和僵尸实例
sunflower = Plant("向日葵", 0, 10)
peashooter = Plant("豌豆射手", 2, 10)
zombie = Zombie("普通僵尸", 1, 10)
# 游戏主循环
while sunflower.health > 0 and zombie.health > 0:
# 植物攻击僵尸
sunflower.attack(zombie)
if zombie.health <= 0:
print("僵尸被击败!")
break
# 僵尸攻击植物
zombie.attack(peashooter)
if peashooter.health <= 0:
print("植物被击败!")
break
```
这只是一个简单的示例代码,你可以根据自己的需求进行扩展和修改。希望对你有帮助!