请用Python实现游戏植物大战僵尸
时间: 2023-07-11 09:09:59 浏览: 111
好的,以下是Python实现植物大战僵尸的简化版代码:
```python
import random
# 植物与僵尸的基类
class Entity:
def __init__(self, name, hp, damage):
self.name = name
self.hp = hp
self.damage = damage
def attack(self, target):
target.hp -= self.damage
def is_alive(self):
return self.hp > 0
# 植物类
class Plant(Entity):
def __init__(self, name, hp, damage, cost):
super().__init__(name, hp, damage)
self.cost = cost
# 僵尸类
class Zombie(Entity):
def __init__(self, name, hp, damage, speed):
super().__init__(name, hp, damage)
self.speed = speed
# 植物和僵尸的工厂类
class EntityFactory:
@staticmethod
def create_plant(plant_type):
if plant_type == 'PeaShooter':
return Plant('PeaShooter', 100, 20, 50)
elif plant_type == 'SunFlower':
return Plant('SunFlower', 50, 0, 25)
@staticmethod
def create_zombie(zombie_type):
if zombie_type == 'NormalZombie':
return Zombie('NormalZombie', 200, 10, 1)
elif zombie_type == 'ConeheadZombie':
return Zombie('ConeheadZombie', 300, 20, 1)
# 游戏管理类
class GameManager:
def __init__(self):
self.plants = []
self.zombies = []
def add_plant(self, plant_type):
plant = EntityFactory.create_plant(plant_type)
if plant.cost <= self.sun:
self.sun -= plant.cost
self.plants.append(plant)
return True
else:
return False
def add_zombie(self, zombie_type):
zombie = EntityFactory.create_zombie(zombie_type)
self.zombies.append(zombie)
def game_loop(self):
while True:
# 随机生成僵尸
if random.randint(0, 9) < 2:
self.add_zombie('NormalZombie')
else:
self.add_zombie('ConeheadZombie')
# 植物攻击僵尸
for plant in self.plants:
zombie = random.choice(self.zombies)
plant.attack(zombie)
if not zombie.is_alive():
self.zombies.remove(zombie)
# 僵尸攻击植物
for zombie in self.zombies:
plant = random.choice(self.plants)
zombie.attack(plant)
if not plant.is_alive():
self.plants.remove(plant)
# 判断游戏是否结束
if not self.plants:
print('Game over! Zombies win!')
break
elif not self.zombies:
print('Game over! Plants win!')
break
# 游戏入口
if __name__ == '__main__':
game = GameManager()
game.sun = 100
game.add_plant('PeaShooter')
game.add_plant('SunFlower')
game.game_loop()
```
这个游戏的实现还可以更加完善,例如添加更多的植物和僵尸类型、增加关卡等。但是这个简化版的代码已经可以让你了解到如何用Python实现植物大战僵尸的游戏逻辑。
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)