“你的脑子被僵尸吃掉啦”,关于Python与植物大战僵尸的故事……

技术

“你的🧠被僵尸吃掉啦!”

这是一篇关于Python与植物大战僵尸的故事……

🎮 大家一定都玩过《植物大战僵尸》叭~

💻小编发现了一个简易版的植物大战僵尸

👀一起来看看吧!

Part1:关于《植物大战僵尸》

《植物大战僵尸》是一款由美国宝开游戏公司(PopCap Games)开发并发行的知名益智策略类塔防游戏,于2009年5月5日正式上线。在游戏中,玩家置身于自家后院,面对成群结队、形态各异的僵尸入侵,必须巧妙利用不同特性的植物构建防线进行抵抗。

游戏中,玩家通过收集阳光资源种植各类植物,每种植物都具有独特的攻击或防御能力。例如,豌豆射手可以发射豌豆攻击僵尸,向日葵则负责生产阳光以供种植更多的植物;而樱桃炸弹能够一次性炸飞一定范围内的僵尸,倭瓜则可直接压倒行进中的敌人。此外,还有诸如寒冰射手冻结僵尸、坚果墙阻挡攻击等多样化的战术选择。

这款游戏不仅玩法简单易上手,更富含策略元素,随着关卡推进,僵尸种类增多,挑战不断升级,要求玩家灵活调整战术布局。除了基础的冒险模式外,《植物大战僵尸》还包含多种附加模式,如解谜模式、生存模式以及特殊小游戏,极大地丰富了游戏体验,并因此深受全球玩家喜爱,成为一款老少皆宜的经典之作。

📇要不要来主页瞅瞅吖(●ˇ∀ˇ●)

Part2:编写代码 实现游戏

💻完整代码如下,由于需要用到一些图片,小编把文件放在公众号里喽,在文末可以下载代码哈(●ˇ∀ˇ●)


                  
import pygame
                  
import random
                  
IMAGE_PATH = 'imgs/'
                  
scrrr_width = 800
                  
scrrr_height = 560
                  
GAMEOVER = False
                  
class Map()
                  
class Plant(pygame.sprite.Sprite)
                  
class Sunflower(Plant)
                  
class PeaShooter(Plant)
                  
class PeaBullet(pygame.sprite.Sprite)
                  
class Zombie(pygame.sprite.Sprite)
                  
class MainGame():
                  
    shaoguan = 1
                  
    score = 0
                  
    remnant_score = 100
                  
    money = 200
                  
    map_points_list = []
                  
    map_list = []
                  
    plants_list = []
                  
    peabullet_list = []
                  
    zombie_list = []
                  
    count_zombie = 0
                  
    produce_zombie = 100
                  
    def init_window(self):
                  
        pygame.display.init()
                  
        MainGame.window = pygame.display.set_mode([scrrr_width, scrrr_height])
                  
    def draw_text(self, content, size, color):
                  
        pygame.font.init()
                  
        font = pygame.font.SysFont('kaiti', size)
                  
        text = font.render(content, True, color)
                  
        return text
                  
    def load_help_text(self):
                  
        text1 = self.draw_text('1.按左键创建向日葵 2.按右键创建豌豆射手', 26, (255, 0, 0))
                  
        MainGame.window.blit(text1, (5, 5))
                  
    def init_plant_points(self):
                  
        for y in range(1, 7):
                  
            points = []
                  
            for x in range(10):
                  
                point = (x, y)
                  
                points.append(point)
                  
            MainGame.map_points_list.append(points)
                  
            print("MainGame.map_points_list", MainGame.map_points_list)
                  
    def init_map(self):
                  
        for points in MainGame.map_points_list:
                  
            temp_map_list = list()
                  
            for point in points:
                  
                # map = None
                  
                if (point[0] + point[1]) % 2 == 0:
                  
                    map = Map(point[0] * 80, point[1] * 80, 0)
                  
                else:
                  
                    map = Map(point[0] * 80, point[1] * 80, 1)
                  
                # 将地图块加入到窗口中
                  
                temp_map_list.append(map)
                  
                print("temp_map_list", temp_map_list)
                  
            MainGame.map_list.append(temp_map_list)
                  
        print("MainGame.map_list", MainGame.map_list)
                  
    def load_map(self):
                  
        for temp_map_list in MainGame.map_list:
                  
            for map in temp_map_list:
                  
                map.load_map()
                  
    def load_plants(self):
                  
        for plant in MainGame.plants_list:
                  
            if plant.live:
                  
                if isinstance(plant, Sunflower):
                  
                    plant.display_sunflower()
                  
                    plant.produce_money()
                  
                elif isinstance(plant, PeaShooter):
                  
                    plant.display_peashooter()
                  
                    plant.shot()
                  
            else:
                  
                MainGame.plants_list.remove(plant)
                  
    def load_peabullets(self):
                  
        for b in MainGame.peabullet_list:
                  
            if b.live:
                  
                b.display_peabullet()
                  
                b.move_bullet()
                  
                b.hit_zombie()
                  
            else:
                  
                MainGame.peabullet_list.remove(b)
                  
    def deal_events(self):
                  
        eventList = pygame.event.get()
                  
        for e in eventList:
                  
            if e.type == pygame.QUIT:
                  
                self.gameOver()
                  
            elif e.type == pygame.MOUSEBUTTONDOWN:
                  
                print(e.pos)
                  
                # print(e.button)#左键1  按下滚轮2 上转滚轮为4 下转滚轮为5  右键 3
                  
                x = e.pos[0] // 80
                  
                y = e.pos[1] // 80
                  
                print(x, y)
                  
                map = MainGame.map_list[y - 1][x]
                  
                print(map.position)
                  
                if e.button == 1:
                  
                    if map.can_grow and MainGame.money >= 50:
                  
                        sunflower = Sunflower(map.position[0], map.position[1])
                  
                        MainGame.plants_list.append(sunflower)
                  
                        print('当前植物列表长度:{}'.format(len(MainGame.plants_list)))
                  
                        map.can_grow = False
                  
                        MainGame.money -= 50
                  
                elif e.button == 3:
                  
                    if map.can_grow and MainGame.money >= 50:
                  
                        peashooter = PeaShooter(map.position[0], map.position[1])
                  
                        MainGame.plants_list.append(peashooter)
                  
                        print('当前植物列表长度:{}'.format(len(MainGame.plants_list)))
                  
                        map.can_grow = False
                  
                        MainGame.money -= 50
                  
    def init_zombies(self):
                  
        for i in range(1, 7):
                  
            dis = random.randint(1, 5) * 200
                  
            zombie = Zombie(800 + dis, i * 80)
                  
            MainGame.zombie_list.append(zombie)
                  
    def load_zombies(self):
                  
        for zombie in MainGame.zombie_list:
                  
            if zombie.live:
                  
                zombie.display_zombie()
                  
                zombie.move_zombie()
                  
                zombie.hit_plant()
                  
            else:
                  
                MainGame.zombie_list.remove(zombie)
                  
    def start_game(self):
                  
        self.init_window()
                  
        self.init_plant_points()
                  
        self.init_map()
                  
        self.init_zombies()
                  
        while not GAMEOVER:
                  
            MainGame.window.fill((255, 255, 255))
                  
            MainGame.window.blit(self.draw_text('当前钱数$: {}'.format(MainGame.money), 26, (255, 0, 0)), (500, 40))
                  
            MainGame.window.blit(self.draw_text('当前关数{},得分{},距离下关还差{}分'.format(MainGame.shaoguan, MainGame.score, MainGame.remnant_score),26,(255, 0, 0)), (5, 40))
                  
            self.load_help_text()
                  
            self.load_map()
                  
            self.load_plants()
                  
            self.load_peabullets()
                  
            self.deal_events()
                  
            self.load_zombies()
                  
            MainGame.count_zombie += 1
                  
            if MainGame.count_zombie == MainGame.produce_zombie:
                  
                self.init_zombies()
                  
                MainGame.count_zombie = 0
                  
            pygame.time.wait(10)
                  
            pygame.display.update()
                  
    def gameOver(self):
                  
        MainGame.window.blit(self.draw_text('游戏结束', 50, (255, 0, 0)), (300, 200))
                  
        print('游戏结束')
                  
        pygame.time.wait(400)
                  
        global GAMEOVER
                  
        GAMEOVER = True
                  
if __name__ == '__main__':
                  
    game = MainGame()
                  
    game.start_game()
              

💡 这段代码是一个使用Pygame实现的简易版植物大战僵尸,具体分析如下。

1.导入模块并定义全局变量

  1. 导入了pygame和random模块。

  2. 定义了图片路径IMAGE_PATH。

  3. 定义了窗口的宽高scrrr_width和scrrr_height。

  4. 定义了一个全局变量GAMEOVER用来控制游戏结束状态。

  5. 定义了日志记录变量LOG,用于记录图片加载错误。

2.定义地图类Map

  1. 定义了一个地图类,包含地图加载和初始化的方法。

  2. 地图类存储了两种不同颜色地图块的图片名称。

  3. 地图初始化时加载了指定的图片,并记录了位置和是否可种植的状态。

3.定义植物类Plant及其子类

  1. 定义了植物类,包含基本的属性和加载图片的方法。

  2. 子类包括向日葵类Sunflower和豌豆射手类PeaShooter。

  3. 植物类和子类有加载图片和显示的方法,并且具有一些与游戏逻辑相关的功能,如向日葵生成阳光,豌豆射手发射子弹等。

4.定义豌豆子弹类PeaBullet

  1. 定义了豌豆子弹类,包含子弹的属性和移动、碰撞检测等方法。

  2. 子弹可以移动并检测与僵尸的碰撞,当碰撞时对僵尸造成伤害。

5.定义僵尸类Zombie

  1. 定义了僵尸类,包含僵尸的属性和移动、攻击植物等方法。

  2. 僵尸可以移动并检测与植物的碰撞,当碰撞时攻击植物。

6.主程序MainGame

  1. 定义了主程序类,包含了游戏的各种初始化、加载和处理事件的方法。

  2. 主程序初始化了窗口、地图、坐标点等,并且实时更新游戏画面。

  3. 游戏循环中包括了地图、植物、子弹、僵尸的加载和展示,并处理了鼠标事件。

  4. 游戏结束时展示游戏结束文字,并设置了全局变量GAMEOVER为True。

7.启动游戏

最后创建MainGame实例并启动游戏。

总的来说,这段代码实现了一个基于Pygame的简化版植物大战僵尸游戏。它包括地图、植物(向日葵和豌豆射手)、豌豆子弹、僵尸等多个类的定义与功能实现。游戏主程序通过循环实时更新画面,并处理鼠标事件,实现植物的种植、攻击,子弹的发射与命中检测,僵尸的移动与攻击。游戏具有简单的界面展示、交互操作和游戏结束判定,为玩家提供了一次简单而有趣的游戏体验。

Part3:更多精彩内容

🐇 欢迎来到《休闲一刻》o( ̄▽ ̄)ブ

📚OpenAI突然宣布:不需要注册也可以使用ChatGPT!

📚“你的电脑已崩溃”,仅需20行Python代码,祝你愚人节快乐!

📚解放双手!仅20行Python代码,让AI替你回复微信消息!

📚Python李峋同款跳动的爱心代码(可写字版)

📚我用python实现了一只呆萌的喜羊羊!

Part4:最后想说

💡公众号内回复“ python113 ”即可下载代码

(●'◡'●)如果你也喜欢这篇文章

可以留下👍嘛o( ̄▽ ̄)ブ

0
0
0
0
评论
未登录
看完啦,登录分享一下感受吧~
暂无评论