Python面向对象植物大战僵尸

先来一波效果图

来看看如何设计游戏架构

python 复制代码
import sys

import pygame


class BaseSprite(pygame.sprite.Sprite):
    def __init__(self, name):
        super().__init__()
        self.image = pygame.image.load(name)
        self.rect = self.image.get_rect()


class AnimateSprite(BaseSprite):
    def __init__(self, default_name, names):
        super().__init__(default_name)
        self.images = [pygame.image.load(name) for name in names]
        self.current = 0

    def update(self):
        self.current += 1
        if self.current == len(self.images) * 2:
            self.current = 0
        self.image = self.images[self.current // 2]


class Enemy(AnimateSprite):
    def __init__(self, default_name, names, center, gm):
        super().__init__(default_name, names)
        self.rect.center = center
        self.time_count = 2
        self.gm = gm

        self.bullet_group = pygame.sprite.Group()

    def update(self):
        super().update()
        self.rect.left -= 1


class PeaShooter(AnimateSprite):
    def __init__(self, default_name, names, center, gm):
        super().__init__(default_name, names)
        self.rect.center = center
        self.time_count = 5
        self.gm = gm

        self.bullet_group = pygame.sprite.Group()

    def update(self):
        super().update()
        self.time_count -= 0.1
        if self.time_count <= 0:
            self.time_count = 5
            BulletSprite("bullet.png", (self.rect.right, self.rect.top + 15)).add(self.bullet_group)

        self.bullet_group.draw(self.gm.screen)
        self.bullet_group.update()

        print("当前子弹哟", len(self.bullet_group.sprites()))


class BulletSprite(BaseSprite):
    def __init__(self, name, center):
        super().__init__(name)
        self.rect.center = center

    def update(self):
        self.rect.left += 5
        if self.rect.left > 1400:
            self.kill()


class GameManage:
    def __init__(self):
        self.screen = pygame.display.set_mode((1400, 600))
        pygame.display.set_caption("植物大战僵尸")

        self.clock = pygame.time.Clock()

        self.bg_surface = pygame.image.load("background1.jpg")

        # 射手精灵组
        self.pea_shooter_group = pygame.sprite.Group()

        self.pea1 = PeaShooter("plants/peashooter/idle/idle_00.png",
                               [f"plants/peashooter/idle/idle_0{i}.png" for i in range(8)], (700, 200), self)
        self.pea1.add(self.pea_shooter_group)

        self.pea2 = PeaShooter("plants/peashooter/idle/idle_00.png",
                               [f"plants/peashooter/idle/idle_0{i}.png" for i in range(8)], (700, 300), self)
        self.pea2.add(self.pea_shooter_group)

        self.enemy_group = pygame.sprite.Group()

        images = [f"zombies/run/run_0{i}.png" for i in range(10)]
        for i in range(10, 31):
            images.append(f"zombies/run/run_{i}.png")

        self.enemy1 = Enemy("zombies/run/run_00.png", images, (1400, 200), self)
        self.enemy1.add(self.enemy_group)

        self.enemy2 = Enemy("zombies/run/run_00.png", images, (1100, 200), self)
        self.enemy2.add(self.enemy_group)

        self.enemy3 = Enemy("zombies/run/run_00.png", images, (1300, 300), self)
        self.enemy3.add(self.enemy_group)

        self.enemy4 = Enemy("zombies/run/run_00.png", images, (1200, 300), self)
        self.enemy4.add(self.enemy_group)

    def run(self):
        while True:
            self.clock.tick(20)

            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    pygame.quit()
                    sys.exit()

            self.screen.blit(self.bg_surface, (0, 0))

            self.pea_shooter_group.draw(self.screen)
            self.pea_shooter_group.update()

            self.enemy_group.draw(self.screen)
            self.enemy_group.update()


            for pea_sprite in self.pea_shooter_group.sprites():
                if pygame.sprite.groupcollide(pea_sprite.bullet_group, self.enemy_group, True, True):
                    print("打死了一个僵尸")

            pygame.display.flip()


gm = GameManage()
gm.run()

抛砖引玉,等待后续完善。。。。。

相关推荐
2401_857439692 小时前
SSM 架构下 Vue 电脑测评系统:为电脑性能评估赋能
开发语言·php
SoraLuna2 小时前
「Mac畅玩鸿蒙与硬件47」UI互动应用篇24 - 虚拟音乐控制台
开发语言·macos·ui·华为·harmonyos
xlsw_2 小时前
java全栈day20--Web后端实战(Mybatis基础2)
java·开发语言·mybatis
梧桐树04293 小时前
python常用内建模块:collections
python
Dream_Snowar3 小时前
速通Python 第三节
开发语言·python
高山我梦口香糖4 小时前
[react]searchParams转普通对象
开发语言·前端·javascript
信号处理学渣5 小时前
matlab画图,选择性显示legend标签
开发语言·matlab
红龙创客5 小时前
某狐畅游24校招-C++开发岗笔试(单选题)
开发语言·c++
蓝天星空5 小时前
Python调用open ai接口
人工智能·python
jasmine s5 小时前
Pandas
开发语言·python