先来一波效果图
文章来源:https://www.toymoban.com/news/detail-655978.html
文章来源地址https://www.toymoban.com/news/detail-655978.html
来看看如何设计游戏架构
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()
抛砖引玉,等待后续完善。。。。。
到了这里,关于Python面向对象植物大战僵尸的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!