用Python写一个完整的'植物大战僵尸'游戏是一个相当庞大的项目,可能需要很多时间和技术知识。以下是一个简化版本的示例,供您参考:

import random

class Plant:
    def __init__(self, name, health, attack):
        self.name = name
        self.health = health
        self.attack = attack

    def __str__(self):
        return self.name

class Zombie:
    def __init__(self, name, health, attack):
        self.name = name
        self.health = health
        self.attack = attack

    def __str__(self):
        return self.name

def attack(plant, zombie):
    zombie.health -= plant.attack
    if zombie.health <= 0:
        print(f'{zombie}被击败了!')
        return True

    plant.health -= zombie.attack
    if plant.health <= 0:
        print(f'{plant}被击败了!')
        return True

    return False

def main():
    plants = [
        Plant('Peashooter', 100, 20),
        Plant('Sunflower', 50, 0),
        Plant('Cherry Bomb', 50, 100),
    ]

    zombies = [
        Zombie('Normal Zombie', 100, 10),
        Zombie('Conehead Zombie', 200, 20),
        Zombie('Buckethead Zombie', 300, 30),
    ]

    while True:
        plant = random.choice(plants)
        zombie = random.choice(zombies)

        print(f'一只{zombie}正在接近...')
        print(f'请选择您要使用的植物:')
        for i, p in enumerate(plants):
            print(f'{i+1}. {p}')

        choice = int(input('请输入您的选择:')) - 1
        selected_plant = plants[choice]

        print(f'您选择了:{selected_plant}')
        print(f'战斗开始!')
        while True:
            if attack(selected_plant, zombie):
                break

        replay = input('是否再次进行游戏?(Y/N)')
        if replay.lower() != 'y':
            break

if __name__ == '__main__':
    main()

这只是一个简化的示例,可能与原版的'植物大战僵尸'游戏有所不同。您可以根据自己的需求和技能进行修改和扩展。

用Python编写简易版'植物大战僵尸'游戏

原文地址: https://www.cveoy.top/t/topic/qvBz 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录