当前位置:   article > 正文

python项目:僵尸大战僵尸(植物大战僵尸改版)_python打僵尸

python打僵尸

        当问你塔防游戏时,大多数人第一时间想到的可能是《植物大战僵尸》,这是一款经典的塔防游戏,在其中你需要种植植物来抵御僵尸的入侵。但除了这款经典之作,是否还能有其他更多的塔防游戏呢?今天我们就来介绍一款基于 Python 和 Pygame 开发的塔防游戏(demo)——《僵尸大战僵尸》。

        首先,让我们来了解一下这款游戏的背景和玩法。《僵尸大战僵尸》是一款以僵尸为主题的塔防游戏,玩家需要在游戏中布置不同种类的僵尸来抵御敌方的进攻。游戏分为左右两方,每一方都有自己的阳光资源和僵尸种类,玩家需要合理利用资源,选择不同的僵尸种类,并根据敌方的情况进行布局,最终击败对方获得胜利。下面是僵尸类的代码片段:

  1. # 僵尸类
  2. class Zombie(pygame.sprite.Sprite):
  3. def __init__(self, kind, camp, ai, ny):
  4. super().__init__()
  5. # 阵营分配
  6. self.camp = camp
  7. self.ai = ai
  8. self.kind = kind
  9. if camp == 0:
  10. self.x = random.randint(1300, 1600) # 默认出生位置
  11. else:
  12. self.x = random.randint(-300, 0)
  13. if not self.ai:
  14. self.y = ny
  15. else:
  16. self.y = random.choice(random_y)
  17. # 僵尸种类
  18. if self.kind == 0: # 普通僵尸
  19. self.speed = 0.5 # 基础速度
  20. self.HP = 500
  21. self.image = 0 # 普通僵尸对应的图片组
  22. elif self.kind == 1: # 路障僵尸(更快)
  23. self.speed = 1 #
  24. self.HP = 600
  25. self.image = 1
  26. elif self.kind == 2: # 铁桶僵尸(更肉)
  27. self.speed = 0.5 #
  28. self.HP = 1200
  29. self.image = 2
  30. # 进入房间的僵尸数量计数一个僵尸只记一次的标志位
  31. self.cont_added = False
  32. # 两种帧
  33. self.frame = None
  34. self.frames = []
  35. self.currentFrame = 0
  36. self.frame1 = None
  37. self.frames1 = []
  38. self.currentFrame1 = 0
  39. if self.camp == 0:
  40. if self.image == 0:
  41. for i in range(1, 23):
  42. self.frame = pygame.image.load(f'Zombies/Zombie/Zombie_{i}.png').convert_alpha()
  43. self.frames.append(self.frame)
  44. elif self.image == 1:
  45. for i in range(1, 22):
  46. self.frame = pygame.image.load(f'Zombies/ConeheadZombie/ConeheadZombie_{i}.png').convert_alpha()
  47. self.frames.append(self.frame)
  48. elif self.image == 2:
  49. for i in range(1, 16):
  50. self.frame = pygame.image.load(f'Zombies/BucketheadZombie/BucketheadZombie_{i}.png').convert_alpha()
  51. self.frames.append(self.frame)
  52. elif self.camp == 1:
  53. if self.image == 0:
  54. for i in range(1, 23):
  55. self.frame = pygame.image.load(f'Zombies/Zombie/Zombie_{i}.png').convert_alpha()
  56. self.frame = pygame.transform.flip(self.frame, True, False)
  57. self.frames.append(self.frame)
  58. elif self.image == 1:
  59. for i in range(1, 22):
  60. self.frame = pygame.image.load(f'Zombies/ConeheadZombie/ConeheadZombie_{i}.png').convert_alpha()
  61. self.frame = pygame.transform.flip(self.frame, True, False)
  62. self.frames.append(self.frame)
  63. elif self.image == 2:
  64. for i in range(1, 16):
  65. self.frame = pygame.image.load(f'Zombies/BucketheadZombie/BucketheadZombie_{i}.png').convert_alpha()
  66. self.frame = pygame.transform.flip(self.frame, True, False)
  67. self.frames.append(self.frame)
  68. if self.camp == 0:
  69. if self.image == 0:
  70. for i in range(1, 22):
  71. self.frame1 = pygame.image.load(f'Zombies/Zombie/ZombieAttack_{i}.png').convert_alpha()
  72. self.frames1.append(self.frame1)
  73. elif self.image == 1:
  74. for i in range(1, 12):
  75. self.frame1 = pygame.image.load(
  76. f'Zombies/ConeheadZombie/ConeheadZombieAttack_{i}.png').convert_alpha()
  77. self.frames1.append(self.frame1)
  78. elif self.image == 2:
  79. for i in range(1, 12):
  80. self.frame1 = pygame.image.load(
  81. f'Zombies/BucketheadZombie/BucketheadZombieAttack_{i}.png').convert_alpha()
  82. self.frames1.append(self.frame1)
  83. elif self.camp == 1:
  84. if self.image == 0:
  85. for i in range(1, 22):
  86. self.frame1 = pygame.image.load(f'Zombies/Zombie/ZombieAttack_{i}.png').convert_alpha()
  87. self.frame1 = pygame.transform.flip(self.frame1, True, False)
  88. self.frames1.append(self.frame1)
  89. elif self.image == 1:
  90. for i in range(1, 12):
  91. self.frame1 = pygame.image.load(
  92. f'Zombies/ConeheadZombie/ConeheadZombieAttack_{i}.png').convert_alpha()
  93. self.frame1 = pygame.transform.flip(self.frame1, True, False)
  94. self.frames1.append(self.frame1)
  95. elif self.image == 2:
  96. for i in range(1, 12):
  97. self.frame1 = pygame.image.load(
  98. f'Zombies/BucketheadZombie/BucketheadZombieAttack_{i}.png').convert_alpha()
  99. self.frame1 = pygame.transform.flip(self.frame1, True, False)
  100. self.frames1.append(self.frame1)
  101. # 僵尸移动
  102. def walking(self):
  103. if self.camp == 0:
  104. self.x = self.x - self.speed
  105. screen.blit(self.frames[self.currentFrame],
  106. (self.x, self.y))
  107. # 更新当前帧
  108. self.currentFrame = (self.currentFrame + 1) % len(self.frames)
  109. else:
  110. self.x = self.x + self.speed
  111. screen.blit(self.frames[self.currentFrame],
  112. (self.x, self.y))
  113. # 更新当前帧
  114. self.currentFrame = (self.currentFrame + 1) % len(self.frames)
  115. if self.camp == 0:
  116. if self.kind == 0:
  117. pygame.draw.rect(screen, (255, 255, 255), (self.x + 75, self.y, 50, 10))
  118. pygame.draw.rect(screen, (255, 0, 0), (self.x + 75, self.y, self.HP / 10, 10))
  119. elif self.kind == 1:
  120. pygame.draw.rect(screen, (255, 255, 255), (self.x + 100, self.y, 50, 10))
  121. pygame.draw.rect(screen, (255, 0, 0), (self.x + 100, self.y, self.HP / 12, 10))
  122. elif self.kind == 2:
  123. pygame.draw.rect(screen, (255, 255, 255), (self.x + 75, self.y, 50, 10))
  124. pygame.draw.rect(screen, (255, 0, 0), (self.x + 75, self.y, self.HP / 24, 10))
  125. elif self.camp == 1:
  126. if self.kind == 0:
  127. pygame.draw.rect(screen, (255, 255, 255), (self.x + 25, self.y, 50, 10))
  128. pygame.draw.rect(screen, (255, 0, 0), (self.x + 25, self.y, self.HP / 10, 10))
  129. elif self.kind == 1:
  130. pygame.draw.rect(screen, (255, 255, 255), (self.x + 25, self.y, 50, 10))
  131. pygame.draw.rect(screen, (255, 0, 0), (self.x + 25, self.y, self.HP / 12, 10))
  132. elif self.kind == 2:
  133. pygame.draw.rect(screen, (255, 255, 255), (self.x + 25, self.y, 50, 10))
  134. pygame.draw.rect(screen, (255, 0, 0), (self.x + 25, self.y, self.HP / 24, 10))
  135. def eating(self):
  136. screen.blit(self.frames1[self.currentFrame1],
  137. (self.x, self.y))
  138. # 更新当前帧
  139. self.currentFrame1 = (self.currentFrame1 + 1) % len(self.frames1)
  140. if self.camp == 0:
  141. if self.kind == 0:
  142. pygame.draw.rect(screen, (255, 255, 255), (self.x + 75, self.y, 50, 10))
  143. pygame.draw.rect(screen, (255, 0, 0), (self.x + 75, self.y, self.HP / 10, 10))
  144. elif self.kind == 1:
  145. pygame.draw.rect(screen, (255, 255, 255), (self.x + 100, self.y, 50, 10))
  146. pygame.draw.rect(screen, (255, 0, 0), (self.x + 100, self.y, self.HP / 12, 10))
  147. elif self.kind == 2:
  148. pygame.draw.rect(screen, (255, 255, 255), (self.x + 75, self.y, 50, 10))
  149. pygame.draw.rect(screen, (255, 0, 0), (self.x + 75, self.y, self.HP / 24, 10))
  150. elif self.camp == 1:
  151. if self.kind == 0:
  152. pygame.draw.rect(screen, (255, 255, 255), (self.x + 25, self.y, 50, 10))
  153. pygame.draw.rect(screen, (255, 0, 0), (self.x + 25, self.y, self.HP / 10, 10))
  154. elif self.kind == 1:
  155. pygame.draw.rect(screen, (255, 255, 255), (self.x + 25, self.y, 50, 10))
  156. pygame.draw.rect(screen, (255, 0, 0), (self.x + 25, self.y, self.HP / 12, 10))
  157. elif self.kind == 2:
  158. pygame.draw.rect(screen, (255, 255, 255), (self.x + 25, self.y, 50, 10))
  159. pygame.draw.rect(screen, (255, 0, 0), (self.x + 25, self.y, self.HP / 24, 10))
  160. def game(self):
  161. global rcont
  162. global lcont
  163. if self.camp == 0:
  164. if not self.cont_added:
  165. rcont = rcont + 1
  166. self.cont_added = True
  167. elif self.camp == 1:
  168. if not self.cont_added:
  169. lcont = lcont + 1
  170. self.cont_added = True

        在游戏中,玩家可以通过点击不同的按钮来选择不同种类的僵尸,比如普通僵尸、路障僵尸和铁桶僵尸等。每个僵尸种类都有自己的特点和属性,需要玩家根据实际情况进行选择。另外,游戏还提供了不同的难度选择,玩家可以根据自己的实力来选择简单、普通或困难模式,增加了游戏的可玩性。目前僵尸种类不多,在后续会加入更多的僵尸,实现更加复杂的交互。

        游戏中的僵尸移动、攻击等行为都经过碰撞算法进行设计,通过 Pygame 实现了生动的动画效果(能动),给玩家带来了更加真实的游戏体验。同时,游戏中还加入了音乐和阳光资源等元素,丰富了游戏的内容,为玩家带来更加丰富的游戏体验。(使用阳光召唤僵尸,定时生产阳光,消灭敌方僵尸获取阳光)

        除此之外,游戏还设置了五条不同的路径供玩家进行选择,这样玩家可以根据实际情况进行布局,增加了游戏的策略性和趣味性。(目前是初版,后续会改成卡片拖拽指定分路生成)

        总的来说,这款《僵尸大战僵尸》是一款非常有趣的塔防游戏,通过精心设计的僵尸种类、动画效果和游戏元素,为玩家带来了不错的游戏体验。如果你对塔防游戏感兴趣,不妨来试试这款游戏吧

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

闽ICP备14008679号