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

相关推荐
Footprint_Analytics12 分钟前
2024 年 8 月公链行业研报:Layer 1、比特币 Layer 2 和以太坊 Layer 2 趋势分析
游戏·web3·区块链
这是我5838 分钟前
C++黑暗迷宫
c++·其他·游戏·visual studio·随机·迷宫·黑暗
乐思智能科技有限公司2 小时前
C语言编写一个五子棋游戏-代码实例讲解与分析
c语言·开发语言·嵌入式硬件·算法·游戏
奶香滴小馒头4 小时前
Day101 代码随想录打卡|动态规划篇--- 分割等和子集
数据结构·算法·leetcode·游戏·动态规划
Thomas_YXQ9 小时前
Unity3D PostLateUpdate为何突然占用大量时间详解
开发语言·数码相机·游戏·unity·架构·unity3d
向宇it10 小时前
【unity进阶知识1】最详细的单例模式的设计和应用,继承和不继承MonoBehaviour的单例模式,及泛型单例基类的编写
游戏·unity·单例模式·设计模式·游戏引擎·框架
Thomas_YXQ10 小时前
Unity3D 中构建行为树插件详解
游戏·unity·架构·unity3d·游戏开发
timidcatt14 小时前
独立游戏《Project:Survival》UE5C++开发日志0——游戏介绍
c++·游戏·ue5
yingxiao8882 天前
手游和应用出海资讯:三七新游首月收入突破700万元;领英尝试推出游戏功能以增加用户使用时长
游戏·手游出海·应用出海·手游资讯
一只蜗牛儿2 天前
基于饥饿游戏搜索优化随机森林的数据回归预测 MATLAB 程序 HGS-RF
游戏·随机森林·回归