当前位置:   article > 正文

植物大战僵尸—python版_植物大战僵尸python

植物大战僵尸python
  1. # 0 - 模块区
  2. import datetime
  3. import math
  4. import os
  5. import random
  6. import sys
  7. import pygame
  8. from pygame.locals import *
  9. class Begger(object):
  10. def __init__(self, img, rect, speed):
  11. self.ful_img = img
  12. self.imgs = [self.ful_img.subsurface(Rect((i*83, 0), (83, 93)))
  13. for i in range(3)]
  14. self.rect = rect
  15. self.speed = speed
  16. self.num = 0
  17. def update(self, screen, press_keys):
  18. if press_keys[K_a]:
  19. self.rect.left -= self.speed
  20. if self.rect.left <= 0:
  21. self.rect.left = 0
  22. if press_keys[K_d]:
  23. self.rect.left += self.speed
  24. if self.rect.right >= 200:
  25. self.rect.right = 200
  26. if press_keys[K_w]:
  27. self.rect.top -= self.speed
  28. if self.rect.top <= 0:
  29. self.rect.top = 0
  30. if press_keys[K_s]:
  31. self.rect.top += self.speed
  32. if self.rect.bottom >= 640:
  33. self.rect.bottom = 640
  34. self.num += 1
  35. if self.num % 3 == 0:
  36. self.num = 0
  37. return [(self.rect.left + self.rect.right)/2, (self.rect.top + self.rect.bottom)/2], self.imgs[self.num]
  38. # 主要的工作区域
  39. if __name__ == '__main__':
  40. # 1 - 设置区
  41. # 1.1 - 窗口设置区
  42. white = (255, 255, 255)
  43. screen_width, screen_height = 960, 640
  44. os.environ['SDL_VIDEO_CENTERED'] = '1'
  45. screen = pygame.display.set_mode((screen_width, screen_height))
  46. pygame.display.set_caption("Pas_Game")
  47. # 1.2 - 基础设置区
  48. pygame.init()
  49. start = 0
  50. win = 0
  51. running = 0
  52. distance = 200
  53. health_value = 194
  54. health_value_max = 194
  55. fin_time = 90
  56. start_time = datetime.datetime.now()
  57. rect_bg = Rect(50, 50, 133, 142)
  58. bg_speed = 2
  59. begger_pos = []
  60. monsters = []
  61. monster_speed = 2
  62. wave_set = []
  63. wave_speed = 3.0
  64. wave_max = 20
  65. fig_path = r'C:\Users\ASUS\Desktop\pack/'#自己运行时根据源文件的保存目录修改地址,另外这是win系统的读取方式
  66. paper = pygame.image.load(fig_path + 'paper.png').convert_alpha()
  67. wall = pygame.image.load(fig_path + 'walls.png').convert_alpha()
  68. wall_width = wall.get_width()
  69. wall_height = wall.get_height()
  70. begger = pygame.image.load(fig_path + 'beggers.png').convert_alpha()
  71. begger_width = begger.get_width()
  72. begger_height = begger.get_height()
  73. waves = pygame.image.load(fig_path + 'waves.png').convert_alpha()
  74. sub_wave = waves.subsurface(Rect((0, 0), (waves.get_width() / 5, waves.get_height())))
  75. sub_wave_width = sub_wave.get_width()
  76. sub_wave_height = sub_wave.get_height()
  77. monster_img1 = pygame.image.load(fig_path + 'monster1.PNG').convert_alpha()#
  78. monster_width = monster_img1.get_width()
  79. monster_height = monster_img1.get_height()
  80. monster_img = monster_img1
  81. health_bar_img = pygame.image.load(fig_path + "health_bar.png")
  82. health_bar_height = health_bar_img.get_height()
  83. health_img = pygame.image.load(fig_path + "health.png")
  84. health_height = health_img.get_height()
  85. victory = pygame.image.load(fig_path + 'victory.png')
  86. game_over = pygame.image.load(fig_path + 'game_over.png')
  87. start_img = pygame.image.load(fig_path + 'start.png').convert_alpha()
  88. # 2 - 游戏区
  89. while not start:
  90. screen.fill(white)
  91. screen.blit(start_img, (160, 0))
  92. pygame.font.init()
  93. font = pygame.font.Font(None, 84)
  94. text = font.render("Press Space to Start !",
  95. True, (250, 50, 200))
  96. text_Rect = text.get_rect()
  97. text_Rect.centerx = screen.get_rect().centerx
  98. text_Rect.centery = screen.get_rect().centery + 200
  99. screen.blit(text, text_Rect)
  100. pygame.display.flip()
  101. for event in pygame.event.get():
  102. if event.type == pygame.QUIT:
  103. pygame.quit()
  104. sys.exit()
  105. if event.type == pygame.KEYDOWN:
  106. if event.key == K_SPACE:
  107. start = 1
  108. running = 1
  109. pygame.mixer.music.load(fig_path + "Checkpoint.mp3")
  110. pygame.mixer.music.play(1, 0.0)
  111. pygame.mixer.music.set_volume(0.15)
  112. start_time = datetime.datetime.now()
  113. bg = Begger(begger, rect_bg, bg_speed)
  114. # 2.2 - 游戏进行区
  115. while running:
  116. # 2.2.1 - 游戏显示区
  117. screen.fill(white)
  118. screen.blit(paper, (0, 0))
  119. for height in range(0, screen_height, wall_height):
  120. screen.blit(wall, (distance, height))
  121. press_keys = pygame.key.get_pressed()
  122. begger_pos, begger_img = bg.update(screen, press_keys)
  123. position = pygame.mouse.get_pos()
  124. angle = math.atan2(position[1] - (begger_pos[1] + begger_height),
  125. position[0] - (begger_pos[0] + begger_width))
  126. begger_rot = pygame.transform.rotate(begger_img, 360 - angle * 57.29)
  127. begger_pos1 = (begger_pos[0] - begger_rot.get_rect().width / 2,
  128. begger_pos[1] - begger_rot.get_rect().height / 2)
  129. screen.blit(begger_rot, begger_pos1)
  130. for wave in wave_set:
  131. index = 0
  132. vel_x = math.cos(wave[0]) * wave_speed
  133. vel_y = math.sin(wave[0]) * wave_speed
  134. wave[1] += vel_x
  135. wave[2] += vel_y
  136. if wave[1] < - sub_wave_width or wave[1] > screen_width \
  137. or wave[2] < - sub_wave_height or wave[2] > screen_height:
  138. wave_set.pop(index)
  139. index += 1
  140. for projectile in wave_set:
  141. wave1 = pygame.transform.rotate(sub_wave, 360 - projectile[0] * 57.29)
  142. screen.blit(wave1, (projectile[1], projectile[2]))
  143. monster_timer = random.choice(range(200))
  144. if monster_timer < 1:
  145. monsters.append([screen_width,
  146. random.randint(monster_height, screen_height - monster_height)])
  147. index = 0
  148. for monster in monsters:
  149. if monster[0] < - monster_width:
  150. monsters.pop(index)
  151. monster[0] -= monster_speed
  152. monster_rect = pygame.Rect(monster_img.get_rect())
  153. monster_rect.top = monster[1]
  154. monster_rect.left = monster[0]
  155. if monster_rect.left < wall_width + distance:
  156. health_value -= random.randint(20, 50)
  157. monsters.pop(index)
  158. index1 = 0
  159. for wave in wave_set:
  160. wave_rect = pygame.Rect(sub_wave.get_rect())
  161. wave_rect.left = wave[1]
  162. wave_rect.top = wave[2]
  163. # 检查两个矩形块是否交叉
  164. if monster_rect.colliderect(wave_rect):
  165. wave_set.pop(index1)
  166. try:
  167. monsters.pop(index)
  168. except IndexError as error:
  169. print("IndexError: " + str(error))
  170. index1 += 1
  171. index += 1
  172. for monster in monsters:
  173. screen.blit(monster_img, monster)
  174. font = pygame.font.Font(None, 42)
  175. cur_time = datetime.datetime.now()
  176. play_time = (cur_time - start_time).seconds
  177. if play_time % 60 < 10:
  178. time_str = ":0"
  179. else:
  180. time_str = ":"
  181. survived_text = font.render(
  182. str(play_time // 60) +
  183. time_str +
  184. str(play_time % 60),
  185. True, (0, 0, 0)
  186. )
  187. text_Rect = survived_text.get_rect()
  188. text_Rect.topright = [screen_width - 5, 5]
  189. screen.blit(survived_text, text_Rect)
  190. health_bar_img = pygame.transform.scale(health_bar_img,
  191. (health_value_max, health_bar_height))
  192. screen.blit(health_bar_img, [0, 5])
  193. if health_value < 0:
  194. health_value = 0
  195. health_img = pygame.transform.scale(health_img,
  196. (health_value, health_height))
  197. screen.blit(health_img, [0, 5])
  198. pygame.display.flip()
  199. # 2.2.2 - 游戏操作区
  200. for event in pygame.event.get():
  201. if event.type == pygame.QUIT:
  202. pygame.quit()
  203. sys.exit()
  204. if event.type == pygame.MOUSEBUTTONDOWN and len(wave_set) < wave_max:
  205. position = pygame.mouse.get_pos()
  206. wave_set.append([math.atan2(position[1] - (begger_pos1[1] + begger_height),
  207. position[0] - (begger_pos1[0] + begger_width)),
  208. begger_pos1[0], begger_pos1[1]])
  209. if pygame.time.get_ticks() >= fin_time * 1000:
  210. running = 0
  211. win = 1
  212. if health_value == 0:
  213. running = 0
  214. win = 0
  215. while not running and start:
  216. pygame.mixer.music.stop()
  217. if win:
  218. screen.blit(victory, (100, 0))
  219. pygame.font.init()
  220. font = pygame.font.Font(None, 84)
  221. text = font.render("Victory !",
  222. True, (250, 50, 200))
  223. text_Rect = text.get_rect()
  224. text_Rect.centerx = screen.get_rect().centerx + 10
  225. text_Rect.centery = screen.get_rect().centery + 50
  226. screen.blit(text, text_Rect)
  227. if not win:
  228. screen.blit(game_over, (100, 0))
  229. pygame.font.init()
  230. font = pygame.font.Font(None, 100)
  231. text = font.render("GAME OVER",
  232. True, (250, 50, 200))
  233. text_Rect = text.get_rect()
  234. text_Rect.centerx = screen.get_rect().centerx
  235. text_Rect.centery = screen.get_rect().centery + 80
  236. screen.blit(text, text_Rect)
  237. pygame.display.flip()
  238. for event in pygame.event.get():
  239. if event.type == pygame.QUIT:
  240. pygame.quit()
  241. sys.exit()

这是Py版的植物大战僵尸,实现了最基本的功能,但是因为对pygame 的了解不深,所以做的可能有点缺陷,比如僵尸不是动态的,豌豆抖动严重

另外自己运行时要修改下图片的地址,在源码中已经标明


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

闽ICP备14008679号