植物大战僵尸
import random
import time
student_id_mark = "2026"
print("学号后两位:02")
植物基础数据
plant_info = {
"向日葵": {"sun_cost": 50, "hp": 100, "damage": 0, "produce_sun": 25},
"豌豆射手": {"sun_cost": 100, "hp": 100, "damage": 20, "produce_sun": 0}
}
僵尸基础数据
zombie_info = {
"普通僵尸": {"hp": 100, "attack": 10},
"铁桶僵尸": {"hp": 250, "attack": 15}
}
class Plant:
def init(self, name):
self.name = name
self.hp = plant_info[name]["hp"]
self.damage = plant_info[name]["damage"]
self.sun_pro = plant_info[name]["produce_sun"]
class Zombie:
def init(self, name):
self.name = name
self.hp = zombie_info[name]["hp"]
self.atk = zombie_info[name]["attack"]
class Game:
def init(self):
self.sun = 150 # 初始阳光
self.plants = [] # 场上植物列表
self.zombies = [] # 场上僵尸列表
self.round = 1 # 回合数
def show_status(self):
"""打印当前游戏状态"""
print(f"\n----- 第{self.round}回合 | 当前阳光:{self.sun} -----")
print(f"场上植物:{[p.name for p in self.plants]}")
print(f"场上僵尸:{[z.name for z in self.zombies]}")
def plant_operate(self):
"""玩家种植植物操作"""
print("\n可种植植物:")
for k, v in plant_info.items():
print(f"{k} | 消耗阳光{v['sun_cost']} | 血量{v['hp']} | 攻击力{v['damage']}")
choice = input("输入要种植的植物名称,不种植请直接回车:")
if choice not in plant_info:
print("跳过种植或输入无效名称")
return
cost = plant_info[choice]["sun_cost"]
if self.sun >= cost:
self.sun -= cost
new_p = Plant(choice)
self.plants.append(new_p)
print(f"成功种下{choice}!剩余阳光{self.sun}")
else:
print("阳光不足,无法种植!")
def sun_generate(self):
"""向日葵产出阳光"""
total_sun = 0
for p in self.plants:
if p.sun_pro > 0:
total_sun += p.sun_pro
self.sun += total_sun
if total_sun > 0:
print(f"向日葵产出{total_sun}阳光!")
def plant_attack_zombie(self):
"""豌豆射手攻击僵尸"""
total_dmg = 0
for p in self.plants:
total_dmg += p.damage
if total_dmg > 0 and len(self.zombies) > 0:
target = self.zombies[0]
target.hp -= total_dmg
print(f"豌豆射手合力造成{total_dmg}伤害!{target.name}剩余血量{target.hp}")
if target.hp <= 0:
self.zombies.pop(0)
print(f"{target.name}被消灭!")
def zombie_attack_plant(self):
"""僵尸攻击前排植物"""
if len(self.zombies) == 0 or len(self.plants) == 0:
return
total_z_atk = sum(z.atk for z in self.zombies)
target = self.plants[0]
target.hp -= total_z_atk
print(f"僵尸群造成{total_z_atk}伤害!{target.name}剩余血量{target.hp}")
if target.hp <= 0:
self.plants.pop(0)
print(f"{target.name}被吃掉了!")
def spawn_zombie(self):
"""随机生成僵尸"""
if random.random() < 0.4:
z_name = random.choice(list(zombie_info.keys()))
new_z = Zombie(z_name)
self.zombies.append(new_z)
print(f"⚠️ 新僵尸[{z_name}]从右侧出现!")
def check_game_over(self):
"""判断游戏胜负"""
if len(self.plants) == 0 and len(self.zombies) > 0:
print("\n===== 游戏结束 | 僵尸吃掉所有植物,你输了 =")
return True
if self.round >= 15 and len(self.zombies) == 0:
print("\n= 游戏胜利 | 成功抵挡15波僵尸进攻! =====")
return True
return False
def game_loop(self):
"""主游戏循环"""
while True:
self.show_status()
if self.check_game_over():
break
# 玩家操作
self.plant_operate()
# 向日葵产阳光
self.sun_generate()
# 生成僵尸
self.spawn_zombie()
# 植物攻击僵尸
self.plant_attack_zombie()
# 僵尸攻击植物
self.zombie_attack_plant()
self.round += 1
time.sleep(1)
if name == "main":
game = Game()
game.game_loop()

浙公网安备 33010602011771号