很高兴接受你的挑战!以下是使用Python编写植物大战僵尸游戏的简化版本代码:

import random

# 创建植物类
class Plant:
    def __init__(self, name, cost, damage):
        self.name = name
        self.cost = cost
        self.damage = damage

    def attack(self):
        return self.damage

# 创建僵尸类
class Zombie:
    def __init__(self, name, health, attack):
        self.name = name
        self.health = health
        self.attack = attack

    def take_damage(self, damage):
        self.health -= damage

    def is_alive(self):
        return self.health > 0

# 创建游戏类
class Game:
    def __init__(self):
        self.plants = [
            Plant('Peashooter', 50, 10),
            Plant('Sunflower', 25, 0),
            Plant('Cherry Bomb', 150, 100)
        ]
        self.zombies = [
            Zombie('Regular Zombie', 50, 10),
            Zombie('Conehead Zombie', 100, 20),
            Zombie('Buckethead Zombie', 150, 30)
        ]
        self.sun_points = 50

    def print_plants(self):
        print('Available Plants:')
        for index, plant in enumerate(self.plants):
            print(f'{index + 1}. {plant.name} (Cost: {plant.cost} sun points, Damage: {plant.damage})')

    def print_zombies(self):
        print('Incoming Zombies:')
        for index, zombie in enumerate(self.zombies):
            print(f'{index + 1}. {zombie.name} (Health: {zombie.health}, Attack: {zombie.attack})')

    def select_plant(self):
        self.print_plants()
        plant_index = int(input('Select a plant to grow: ')) - 1
        if plant_index < 0 or plant_index >= len(self.plants):
            print('Invalid plant selection. Please try again.')
            return None
        selected_plant = self.plants[plant_index]
        if selected_plant.cost > self.sun_points:
            print('Not enough sun points to grow this plant. Please try again.')
            return None
        self.sun_points -= selected_plant.cost
        return selected_plant

    def select_zombie(self):
        zombie_index = random.randint(0, len(self.zombies) - 1)
        selected_zombie = self.zombies[zombie_index]
        print(f'A {selected_zombie.name} is approaching!')
        return selected_zombie

    def play(self):
        while True:
            self.print_zombies()
            selected_plant = self.select_plant()
            if selected_plant is None:
                continue
            selected_zombie = self.select_zombie()
            while selected_zombie.is_alive():
                selected_zombie.take_damage(selected_plant.attack())
                if selected_zombie.is_alive():
                    print(f'The {selected_zombie.name} has {selected_zombie.health} health points left.')
                else:
                    print(f'The {selected_zombie.name} has been defeated!')
                    break
            if self.sun_points <= 0:
                print('Game Over! You ran out of sun points.')
                break

# 游戏开始
game = Game()
game.play()

请注意,这只是一个简单的命令行版本的植物大战僵尸游戏。它允许您在可用的植物中选择一种,并随机生成一只僵尸进行战斗。您可以根据自己的需求和喜好扩展该代码,例如添加更多植物、僵尸、关卡等。祝你编写游戏的过程愉快!

Python 植物大战僵尸游戏代码实现

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

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