Python 植物大战僵尸游戏【含Python源码 MX_012期】

简介:

"植物大战僵尸"(Plants vs. Zombies)是一款由PopCap Games开发的流行塔防游戏,最初于2009年发布。游戏的概念是在僵尸入侵的情境下,玩家通过种植不同种类的植物来保护他们的房屋免受僵尸的侵袭。在游戏中,玩家需要通过收集阳光资源来种植植物,这些植物各有不同的特点和技能,比如射击、炸弹、冰冻等,以应对不同类型的僵尸。游戏通常分为多个关卡,每个关卡都有不同的地图和僵尸类型,玩家需要在每个关卡中选择合适的植物来应对不同的挑战。

界面图:

部分代码:

python 复制代码
#5 向日葵类
class Sunflower(Plant):
    def __init__(self,x,y):
        super(Sunflower, self).__init__()
        self.image = pygame.image.load('imgs/sunflower.png')
        self.rect = self.image.get_rect()
        self.rect.x = x
        self.rect.y = y
        self.price = 50
        self.hp = 100
        #5 时间计数器
        self.time_count = 0

    #5 新增功能:生成阳光
    def produce_money(self):
        self.time_count += 1
        if self.time_count == 25:
            MainGame.money += 5
            self.time_count = 0
    #5 向日葵加入到窗口中
    def display_sunflower(self):
        MainGame.window.blit(self.image,self.rect)
#6 豌豆射手类
class PeaShooter(Plant):
    def __init__(self,x,y):
        super(PeaShooter, self).__init__()
        # self.image 为一个 surface
        self.image = pygame.image.load('imgs/peashooter.png')
        self.rect = self.image.get_rect()
        self.rect.x = x
        self.rect.y = y
        self.price = 50
        self.hp = 200
        #6 发射计数器
        self.shot_count = 0

    #6 增加射击方法
    def shot(self):
        #6 记录是否应该射击
        should_fire = False
        for zombie in MainGame.zombie_list:
            if zombie.rect.y == self.rect.y and zombie.rect.x < 800 and zombie.rect.x > self.rect.x:
                should_fire = True
        #6 如果活着
        if self.live and should_fire:
            self.shot_count += 1
            #6 计数器到25发射一次
            if self.shot_count == 25:
                #6 基于当前豌豆射手的位置,创建子弹
                peabullet = PeaBullet(self)
                #6 将子弹存储到子弹列表中
                MainGame.peabullet_list.append(peabullet)
                self.shot_count = 0

完整代码:Python 植物大战僵尸游戏

相关推荐
亚里随笔12 小时前
GenEnv:让AI智能体像人一样在_游戏_中成长
人工智能·游戏·llm·rl·agentic
天人合一peng13 小时前
unity 3d 通过游戏对象的名子查到其对象
游戏·unity·游戏引擎
Zhichao_9714 小时前
【UE5.3 C++】ARPG游戏 05-准备角色攻击的武器和动画
游戏·ue5
GOSIM 全球开源创新汇18 小时前
科班出身+跨界双轨:陈郑豪用 AI 压缩技术,让 4K 游戏走进普通设备|Open AGI Forum
人工智能·游戏·agi
幽络源小助理1 天前
逆向工程系统学习资源图谱(2026):从 Windows 内核、安卓安全到游戏协议分析的全栈教程清单
学习·安全·游戏·逆向工程
i橡皮擦2 天前
TheIsle恐龙岛游戏管理员命令
游戏·恐龙岛·theisle
zhutoutoutousan2 天前
氛围数学学习:用游戏化思维征服抽象数学
学习·算法·游戏
上海云盾-小余2 天前
im即时通讯被攻击使用游戏盾高防方案有效解决
网络·网络协议·web安全·游戏·金融·ddos
电报号dapp1192 天前
钱包开发:在虚无中为数字自我筑巢
游戏·去中心化·区块链·智能合约
zhangx1234_2 天前
C语言 扫雷游戏
游戏