当前位置:   article > 正文

Pygame实战:据说—这是一款还原度超高的植物大战僵尸游戏,你感受下......_python植物大战僵尸游戏图片

python植物大战僵尸游戏图片

导语

哈喽!大家好,我是木木子!

又到了每日游戏更新系列,看到这么如下.gif是不是让你想起来了童年吖~

植物大战僵尸的人气可谓是经久不衰,晃着脑袋生产阳光的向日葵,突突突吐着子弹的豌豆射手!​

行动迟缓种类丰富的僵尸……印象最深的是“僵尸吃掉了你的脑子!”还有疯狂的戴夫,无一不唤醒着我们的童年记忆

山民们闯到哪一关了?解锁了哪些植物?

对!没错,就是这个大工程今天带大家做一款Python版本的植物大战僵尸游戏!

正文

首先准备好相应的素材如下:【超多仅展示小部分】

定义所有的植物卡片的名称和属性都保存在单独的list中,每个list index都对应一种植物,每个植物卡片是一个单独的Card类,用来显示这个植物,然后每个植设置植物的卡片栏目。

  1. import pygame as pg
  2. from .. import tool
  3. from .. import constants as c
  4. PANEL_Y_START = 87
  5. PANEL_X_START = 22
  6. PANEL_Y_INTERNAL = 74
  7. PANEL_X_INTERNAL = 53
  8. CARD_LIST_NUM = 8
  9. card_name_list = [c.CARD_SUNFLOWER, c.CARD_PEASHOOTER, c.CARD_SNOWPEASHOOTER, c.CARD_WALLNUT,
  10. c.CARD_CHERRYBOMB, c.CARD_THREEPEASHOOTER, c.CARD_REPEATERPEA, c.CARD_CHOMPER,
  11. c.CARD_PUFFMUSHROOM, c.CARD_POTATOMINE, c.CARD_SQUASH]
  12. plant_name_list = [c.SUNFLOWER, c.PEASHOOTER, c.SNOWPEASHOOTER, c.WALLNUT,
  13. c.CHERRYBOMB, c.THREEPEASHOOTER, c.REPEATERPEA, c.CHOMPER,
  14. c.PUFFMUSHROOM, c.POTATOMINE, c.SQUASH]
  15. plant_sun_list = [50, 100, 175, 50, 150, 325, 200, 150, 0, 25, 50]
  16. plant_frozen_time_list = [0, 5000, 5000, 10000, 5000, 5000, 5000, 5000, 8000, 8000, 8000]
  17. all_card_list = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
  18. def getSunValueImage(sun_value):
  19. font = pg.font.SysFont(None, 22)
  20. width = 32
  21. msg_image = font.render(str(sun_value), True, c.NAVYBLUE, c.LIGHTYELLOW)
  22. msg_rect = msg_image.get_rect()
  23. msg_w = msg_rect.width
  24. image = pg.Surface([width, 17])
  25. x = width - msg_w
  26. image.fill(c.LIGHTYELLOW)
  27. image.blit(msg_image, (x, 0), (0, 0, msg_rect.w, msg_rect.h))
  28. image.set_colorkey(c.BLACK)
  29. return image
  30. class Card():
  31. def __init__(self, x, y, name_index, scale=0.78):
  32. self.loadFrame(card_name_list[name_index], scale)
  33. self.rect = self.image.get_rect()
  34. self.rect.x = x
  35. self.rect.y = y
  36. self.name_index = name_index
  37. self.sun_cost = plant_sun_list[name_index]
  38. self.frozen_time = plant_frozen_time_list[name_index]
  39. self.frozen_timer = -self.frozen_time
  40. self.select = True
  41. def loadFrame(self, name, scale):
  42. frame = tool.GFX[name]
  43. rect = frame.get_rect()
  44. width, height = rect.w, rect.h
  45. self.image = tool.get_image(frame, 0, 0, width, height, c.BLACK, scale)
  46. def checkMouseClick(self, mouse_pos):
  47. x, y = mouse_pos
  48. if(x >= self.rect.x and x <= self.rect.right and
  49. y >= self.rect.y and y <= self.rect.bottom):
  50. return True
  51. return False
  52. def canClick(self, sun_value, current_time):
  53. if self.sun_cost <= sun_value and (current_time - self.frozen_timer) > self.frozen_time:
  54. return True
  55. return False
  56. def canSelect(self):
  57. return self.select
  58. def setSelect(self, can_select):
  59. self.select = can_select
  60. if can_select:
  61. self.image.set_alpha(255)
  62. else:
  63. self.image.set_alpha(128)
  64. def setFrozenTime(self, current_time):
  65. self.frozen_timer = current_time
  66. def update(self, sun_value, current_time):
  67. if (self.sun_cost > sun_value or
  68. (current_time - self.frozen_timer) <= self.frozen_time):
  69. self.image.set_alpha(128)
  70. else:
  71. self.image.set_alpha(255)
  72. def draw(self, surface):
  73. surface.blit(self.image, self.rect)
  74. class MenuBar():
  75. def __init__(self, card_list, sun_value):
  76. self.loadFrame(c.MENUBAR_BACKGROUND)
  77. self.rect = self.image.get_rect()
  78. self.rect.x = 10
  79. self.rect.y = 0
  80. self.sun_value = sun_value
  81. self.card_offset_x = 32
  82. self.setupCards(card_list)
  83. def loadFrame(self, name):
  84. frame = tool.GFX[name]
  85. rect = frame.get_rect()
  86. frame_rect = (rect.x, rect.y, rect.w, rect.h)
  87. self.image = tool.get_image(tool.GFX[name], *frame_rect, c.WHITE, 1)
  88. def update(self, current_time):
  89. self.current_time = current_time
  90. for card in self.card_list:
  91. card.update(self.sun_value, self.current_time)
  92. def createImage(self, x, y, num):
  93. if num == 1:
  94. return
  95. img = self.image
  96. rect = self.image.get_rect()
  97. width = rect.w
  98. height = rect.h
  99. self.image = pg.Surface((width * num, height)).convert()
  100. self.rect = self.image.get_rect()
  101. self.rect.x = x
  102. self.rect.y = y
  103. for i in range(num):
  104. x = i * width
  105. self.image.blit(img, (x,0))
  106. self.image.set_colorkey(c.BLACK)
  107. def setupCards(self, card_list):
  108. self.card_list = []
  109. x = self.card_offset_x
  110. y = 8
  111. for index in card_list:
  112. x += 55
  113. self.card_list.append(Card(x, y, index))
  114. def checkCardClick(self, mouse_pos):
  115. result = None
  116. for card in self.card_list:
  117. if card.checkMouseClick(mouse_pos):
  118. if card.canClick(self.sun_value, self.current_time):
  119. result = (plant_name_list[card.name_index], card.sun_cost)
  120. break
  121. return result
  122. def checkMenuBarClick(self, mouse_pos):
  123. x, y = mouse_pos
  124. if(x >= self.rect.x and x <= self.rect.right and
  125. y >= self.rect.y and y <= self.rect.bottom):
  126. return True
  127. return False
  128. def decreaseSunValue(self, value):
  129. self.sun_value -= value
  130. def increaseSunValue(self, value):
  131. self.sun_value += value
  132. def setCardFrozenTime(self, plant_name):
  133. for card in self.card_list:
  134. if plant_name_list[card.name_index] == plant_name:
  135. card.setFrozenTime(self.current_time)
  136. break
  137. def drawSunValue(self):
  138. self.value_image = getSunValueImage(self.sun_value)
  139. self.value_rect = self.value_image.get_rect()
  140. self.value_rect.x = 21
  141. self.value_rect.y = self.rect.bottom - 21
  142. self.image.blit(self.value_image, self.value_rect)
  143. def draw(self, surface):
  144. self.drawSunValue()
  145. surface.blit(self.image, self.rect)
  146. for card in self.card_list:
  147. card.draw(surface)
  148. class Panel():
  149. def __init__(self, card_list, sun_value):
  150. self.loadImages(sun_value)
  151. self.selected_cards = []
  152. self.selected_num = 0
  153. self.setupCards(card_list)
  154. def loadFrame(self, name):
  155. frame = tool.GFX[name]
  156. rect = frame.get_rect()
  157. frame_rect = (rect.x, rect.y, rect.w, rect.h)
  158. return tool.get_image(tool.GFX[name], *frame_rect, c.WHITE, 1)
  159. def loadImages(self, sun_value):
  160. self.menu_image = self.loadFrame(c.MENUBAR_BACKGROUND)
  161. self.menu_rect = self.menu_image.get_rect()
  162. self.menu_rect.x = 0
  163. self.menu_rect.y = 0
  164. self.panel_image = self.loadFrame(c.PANEL_BACKGROUND)
  165. self.panel_rect = self.panel_image.get_rect()
  166. self.panel_rect.x = 0
  167. self.panel_rect.y = PANEL_Y_START
  168. self.value_image = getSunValueImage(sun_value)
  169. self.value_rect = self.value_image.get_rect()
  170. self.value_rect.x = 21
  171. self.value_rect.y = self.menu_rect.bottom - 21
  172. self.button_image = self.loadFrame(c.START_BUTTON)
  173. self.button_rect = self.button_image.get_rect()
  174. self.button_rect.x = 155
  175. self.button_rect.y = 547
  176. def setupCards(self, card_list):
  177. self.card_list = []
  178. x = PANEL_X_START - PANEL_X_INTERNAL
  179. y = PANEL_Y_START + 43 - PANEL_Y_INTERNAL
  180. for i, index in enumerate(card_list):
  181. if i % 8 == 0:
  182. x = PANEL_X_START - PANEL_X_INTERNAL
  183. y += PANEL_Y_INTERNAL
  184. x += PANEL_X_INTERNAL
  185. self.card_list.append(Card(x, y, index, 0.75))
  186. def checkCardClick(self, mouse_pos):
  187. delete_card = None
  188. for card in self.selected_cards:
  189. if delete_card: # when delete a card, move right cards to left
  190. card.rect.x -= 55
  191. elif card.checkMouseClick(mouse_pos):
  192. self.deleteCard(card.name_index)
  193. delete_card = card
  194. if delete_card:
  195. self.selected_cards.remove(delete_card)
  196. self.selected_num -= 1
  197. if self.selected_num == CARD_LIST_NUM:
  198. return
  199. for card in self.card_list:
  200. if card.checkMouseClick(mouse_pos):
  201. if card.canSelect():
  202. self.addCard(card)
  203. break
  204. def addCard(self, card):
  205. card.setSelect(False)
  206. y = 8
  207. x = 78 + self.selected_num * 55
  208. self.selected_cards.append(Card(x, y, card.name_index))
  209. self.selected_num += 1
  210. def deleteCard(self, index):
  211. self.card_list[index].setSelect(True)
  212. def checkStartButtonClick(self, mouse_pos):
  213. if self.selected_num < CARD_LIST_NUM:
  214. return False
  215. x, y = mouse_pos
  216. if (x >= self.button_rect.x and x <= self.button_rect.right and
  217. y >= self.button_rect.y and y <= self.button_rect.bottom):
  218. return True
  219. return False
  220. def getSelectedCards(self):
  221. card_index_list = []
  222. for card in self.selected_cards:
  223. card_index_list.append(card.name_index)
  224. return card_index_list
  225. def draw(self, surface):
  226. self.menu_image.blit(self.value_image, self.value_rect)
  227. surface.blit(self.menu_image, self.menu_rect)
  228. surface.blit(self.panel_image, self.panel_rect)
  229. for card in self.card_list:
  230. card.draw(surface)
  231. for card in self.selected_cards:
  232. card.draw(surface)
  233. if self.selected_num == CARD_LIST_NUM:
  234. surface.blit(self.button_image, self.button_rect)

植物卡片栏目截图如下:

设置的几种植物截图如下:

然后使用setupMouseImage 函数实现鼠标图片切换为选中的植物。

  1. def setupMouseImage(self, plant_name, plant_cost):
  2. frame_list = tool.GFX[plant_name]
  3. if plant_name in tool.PLANT_RECT:
  4. data = tool.PLANT_RECT[plant_name]
  5. x, y, width, height = data['x'], data['y'], data['width'], data['height']
  6. else:
  7. x, y = 0, 0
  8. rect = frame_list[0].get_rect()
  9. width, height = rect.w, rect.h
  10. if plant_name == c.POTATOMINE or plant_name == c.SQUASH:
  11. color = c.WHITE
  12. else:
  13. color = c.BLACK
  14. self.mouse_image = tool.get_image(frame_list[0], x, y, width, height, color, 1)
  15. self.mouse_rect = self.mouse_image.get_rect()
  16. pg.mouse.set_visible(False)
  17. self.drag_plant = True
  18. self.plant_name = plant_name
  19. self.plant_cost = plant_cost
  20. def removeMouseImage(self):
  21. pg.mouse.set_visible(True)
  22. self.drag_plant = False
  23. self.mouse_image = None
  24. self.hint_image = None
  25. self.hint_plant = False

效果图如下:开始界面只有一个关卡,but一个关卡有不同的关。

​嗯哼~好啦其实后面关卡有惊喜的啦~这里就不透露了~

总结

这款植物大战僵尸游戏界面还是蛮不错的啦 还原度很高~

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

推荐阅读
相关标签