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 植物大战僵尸游戏

相关推荐
Buling_019 小时前
游戏中的设计模式——第一篇 设计模式简介
游戏·设计模式
lingran__1 天前
C语言制作扫雷游戏(拓展版赋源码)
c语言·算法·游戏
D155540880581 天前
电竞护航小程序成品搭建三角洲行动护航小程序开发俱乐部点单小程序成品游戏派单小程序定制
游戏·小程序
nightunderblackcat2 天前
新手向:Python制作贪吃蛇游戏(Pygame)
python·游戏·pygame
王家视频教程图书馆2 天前
2025年最新 unityHub游戏引擎开发2d手机游戏和桌面游戏教程
游戏·unity·游戏引擎
点金石游戏出海2 天前
每周资讯 | 中国游戏市场将在2025年突破500亿美元;《恋与深空》收入突破50亿元
游戏·网易游戏·海外市场·恋与深空·手游市场
一点都不方女士2 天前
《无畏契约》游戏报错“缺少DirectX”?5种解决方案(附DirectX修复工具)
windows·游戏·microsoft·动态链接库·directx·运行库
wanhengidc2 天前
云手机可以息屏挂手游吗?
运维·网络·安全·游戏·智能手机
wanhengidc3 天前
云手机的空间会占用本地内存吗
科技·游戏·智能手机
wanhengidc3 天前
网页版的云手机都有哪些优势?
运维·网络·安全·游戏·智能手机