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

相关推荐
阿阳微客11 小时前
Steam 搬砖项目深度拆解:从抵触到真香的转型之路
前端·笔记·学习·游戏
m0_5522008217 小时前
《UE5_C++多人TPS完整教程》学习笔记37 ——《P38 变量复制(Variable Replication)》
c++·游戏·ue5
量子炒饭大师3 天前
项目实战——C语言扫雷游戏
c语言·开发语言·游戏
开开心心就好3 天前
高效视频倍速播放插件推荐
python·学习·游戏·pdf·计算机外设·电脑·音视频
DanmF--4 天前
C#面向对象实践项目--贪吃蛇
开发语言·游戏·c#·游戏程序
Code_流苏4 天前
Python趣学篇:从零打造智能AI井字棋游戏(Python + Tkinter + Minimax算法)
python·算法·游戏·tkinter·智能井字棋·minimax算法
RedJACK~5 天前
【Go语言】Ebiten游戏库开发者文档 (v2.8.8)
开发语言·游戏·golang
Sui_Network5 天前
从公开到私密:重新思考 Web3 的数据安全
人工智能·游戏·web3·去中心化·区块链
土豆宝5 天前
AI玩游戏的一点尝试(5)—— 多样化的数字识别
人工智能·游戏
陈哥聊测试5 天前
游戏公司如何同时管好上百个游戏项目?
游戏·程序员·游戏开发