当前位置:   article > 正文

Python面向对象植物大战僵尸_python写植物大战僵尸

python写植物大战僵尸

先来一波效果图

 

 来看看如何设计游戏架构

  1. import sys
  2. import pygame
  3. class BaseSprite(pygame.sprite.Sprite):
  4. def __init__(self, name):
  5. super().__init__()
  6. self.image = pygame.image.load(name)
  7. self.rect = self.image.get_rect()
  8. class AnimateSprite(BaseSprite):
  9. def __init__(self, default_name, names):
  10. super().__init__(default_name)
  11. self.images = [pygame.image.load(name) for name in names]
  12. self.current = 0
  13. def update(self):
  14. self.current += 1
  15. if self.current == len(self.images) * 2:
  16. self.current = 0
  17. self.image = self.images[self.current // 2]
  18. class Enemy(AnimateSprite):
  19. def __init__(self, default_name, names, center, gm):
  20. super().__init__(default_name, names)
  21. self.rect.center = center
  22. self.time_count = 2
  23. self.gm = gm
  24. self.bullet_group = pygame.sprite.Group()
  25. def update(self):
  26. super().update()
  27. self.rect.left -= 1
  28. class PeaShooter(AnimateSprite):
  29. def __init__(self, default_name, names, center, gm):
  30. super().__init__(default_name, names)
  31. self.rect.center = center
  32. self.time_count = 5
  33. self.gm = gm
  34. self.bullet_group = pygame.sprite.Group()
  35. def update(self):
  36. super().update()
  37. self.time_count -= 0.1
  38. if self.time_count <= 0:
  39. self.time_count = 5
  40. BulletSprite("bullet.png", (self.rect.right, self.rect.top + 15)).add(self.bullet_group)
  41. self.bullet_group.draw(self.gm.screen)
  42. self.bullet_group.update()
  43. print("当前子弹哟", len(self.bullet_group.sprites()))
  44. class BulletSprite(BaseSprite):
  45. def __init__(self, name, center):
  46. super().__init__(name)
  47. self.rect.center = center
  48. def update(self):
  49. self.rect.left += 5
  50. if self.rect.left > 1400:
  51. self.kill()
  52. class GameManage:
  53. def __init__(self):
  54. self.screen = pygame.display.set_mode((1400, 600))
  55. pygame.display.set_caption("植物大战僵尸")
  56. self.clock = pygame.time.Clock()
  57. self.bg_surface = pygame.image.load("background1.jpg")
  58. # 射手精灵组
  59. self.pea_shooter_group = pygame.sprite.Group()
  60. self.pea1 = PeaShooter("plants/peashooter/idle/idle_00.png",
  61. [f"plants/peashooter/idle/idle_0{i}.png" for i in range(8)], (700, 200), self)
  62. self.pea1.add(self.pea_shooter_group)
  63. self.pea2 = PeaShooter("plants/peashooter/idle/idle_00.png",
  64. [f"plants/peashooter/idle/idle_0{i}.png" for i in range(8)], (700, 300), self)
  65. self.pea2.add(self.pea_shooter_group)
  66. self.enemy_group = pygame.sprite.Group()
  67. images = [f"zombies/run/run_0{i}.png" for i in range(10)]
  68. for i in range(10, 31):
  69. images.append(f"zombies/run/run_{i}.png")
  70. self.enemy1 = Enemy("zombies/run/run_00.png", images, (1400, 200), self)
  71. self.enemy1.add(self.enemy_group)
  72. self.enemy2 = Enemy("zombies/run/run_00.png", images, (1100, 200), self)
  73. self.enemy2.add(self.enemy_group)
  74. self.enemy3 = Enemy("zombies/run/run_00.png", images, (1300, 300), self)
  75. self.enemy3.add(self.enemy_group)
  76. self.enemy4 = Enemy("zombies/run/run_00.png", images, (1200, 300), self)
  77. self.enemy4.add(self.enemy_group)
  78. def run(self):
  79. while True:
  80. self.clock.tick(20)
  81. for event in pygame.event.get():
  82. if event.type == pygame.QUIT:
  83. pygame.quit()
  84. sys.exit()
  85. self.screen.blit(self.bg_surface, (0, 0))
  86. self.pea_shooter_group.draw(self.screen)
  87. self.pea_shooter_group.update()
  88. self.enemy_group.draw(self.screen)
  89. self.enemy_group.update()
  90. for pea_sprite in self.pea_shooter_group.sprites():
  91. if pygame.sprite.groupcollide(pea_sprite.bullet_group, self.enemy_group, True, True):
  92. print("打死了一个僵尸")
  93. pygame.display.flip()
  94. gm = GameManage()
  95. gm.run()

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

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/586100
推荐阅读
相关标签
  

闽ICP备14008679号