欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

用 Python 实现植物大战僵尸代码!

程序员文章站 2023-09-07 08:33:24
前言 本文的文字及图片来源于网络,仅供学习、交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理。 作者: marble_xu GitHub地址:https://github.com/marblexu/PythonPlantsVsZombies PS:如有需要Python学 ......

前言

本文的文字及图片来源于网络,仅供学习、交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理。

作者: marble_xu

github地址:https://github.com/marblexu/pythonplantsvszombies

ps:如有需要python学习资料的小伙伴可以加点击下方链接自行获取

功能介绍

最近一直在给这个植物大战僵尸游戏添加新的植物和僵尸, 因为网上的图片资源有限,能加的植物和僵尸比较少, 目前进展如下。

功能实现如下:

  • 支持的植物类型:太阳花,豌豆射手,寒冰射手,坚果,樱桃炸弹。新增加植物:双重豌豆射手,三重豌豆射手,食人花 ,小喷菇,土豆地雷,倭瓜。

  • 支持的僵尸类型:普通僵尸,棋子僵尸,路障僵尸,铁桶僵尸。新增加读报僵尸。

  • 使用json文件保存关卡信息,设置僵尸出现的时间和位置。

  • 增加每关开始时选择上场植物。

  • 增加除草机。

下面是游戏的截图:

用 Python 实现植物大战僵尸代码! 用 Python 实现植物大战僵尸代码! 用 Python 实现植物大战僵尸代码!

植物卡片选择和种植

如图所示,游戏中可以种植物的方格一共有45个(有5行,每行9列)。

这篇文章要介绍的是:

  • 上方植物卡片栏的实现。

  • 点击植物卡片,鼠标切换为植物图片。

  • 鼠标移动时,判断当前在哪个方格中,并显示半透明的植物作为提示。

代码实现

所有的植物卡片的名称和属性都保存在单独的list中,每个list index都对应一种植物。

比如list index 0 就是太阳花:

  • card_name_list[0] 是太阳花卡片的名字,用来获取太阳花卡片的图片。

  • plant_name_list[0] 是太阳花的名字,用来获取太阳花卡片的图片。

  • plant_sun_list[0] 是种植太阳花需要花费的太阳点数。

  • plant_frozen_time_list[0] 是太阳花的冷却时间。

植物卡片类

每个植物卡片是一个单独的card类,用来显示这个植物。

  • checkmouseclick函数:判断鼠标是否点击到这个卡片;

  • canclick:判断这个卡片是否能种植(有没有足够的点数,是否还在冷却时间内);

  • update 函数:通过设置图片的透明度来表示这个卡片是否能选择。

卡片栏类

menubar类显示图3中的植物卡片栏:

  • self.sun_value:当前采集的太阳点数;

  • self.card_list: 植物卡片的list;

  • setupcards函数:遍历初始化init函数中传入这个关卡选好的植物卡片list,依次创建card类,设置每个卡片的显示位置;

  • checkcardclick函数:检查鼠标是否点击了卡片栏上的某个植物卡片,如果选择了一个可种植的卡片,返回结果。

代码:

  1 import pygame as pg
  2 from .. import tool
  3 from .. import constants as c
  4 ​
  5 panel_y_start = 87
  6 panel_x_start = 22
  7 panel_y_internal = 74
  8 panel_x_internal = 53
  9 card_list_num = 8
 10 ​
 11 card_name_list = [c.card_sunflower, c.card_peashooter, c.card_snowpeashooter, c.card_wallnut,
 12                   c.card_cherrybomb, c.card_threepeashooter, c.card_repeaterpea, c.card_chomper,
 13                   c.card_puffshroom, c.card_potatomine, c.card_squash, c.card_spikeweed,
 14                   c.card_jalapeno, c.card_scaredyshroom, c.card_sunshroom, c.card_iceshroom]
 15 plant_name_list = [c.sunflower, c.peashooter, c.snowpeashooter, c.wallnut,
 16                    c.cherrybomb, c.threepeashooter, c.repeaterpea, c.chomper,
 17                    c.puffshroom, c.potatomine, c.squash, c.spikeweed,
 18                    c.jalapeno, c.scaredyshroom, c.sunshroom, c.iceshroom]
 19 plant_sun_list = [50, 100, 175, 50, 150, 325, 200, 150, 0, 25, 50, 100, 125, 25, 25, 75]
 20 plant_frozen_time_list = [7500, 7500, 7500, 30000, 50000, 7500, 7500, 7500, 7500, 30000,
 21                           30000, 7500, 50000, 7500, 7500, 50000]
 22 all_card_list = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
 23 ​
 24 def getsunvalueimage(sun_value):
 25     font = pg.font.sysfont(none, 22)
 26     width = 32
 27     msg_image = font.render(str(sun_value), true, c.navyblue, c.lightyellow)
 28     msg_rect = msg_image.get_rect()
 29     msg_w = msg_rect.width
 30 ​
 31     image = pg.surface([width, 17])
 32     x = width - msg_w
 33 ​
 34     image.fill(c.lightyellow)
 35     image.blit(msg_image, (x, 0), (0, 0, msg_rect.w, msg_rect.h))
 36     image.set_colorkey(c.black)
 37     return image
 38 ​
 39 class card():
 40     def __init__(self, x, y, name_index, scale=0.78):
 41         self.loadframe(card_name_list[name_index], scale)
 42         self.rect = self.orig_image.get_rect()
 43         self.rect.x = x
 44         self.rect.y = y
 45         
 46         self.name_index = name_index
 47         self.sun_cost = plant_sun_list[name_index]
 48         self.frozen_time = plant_frozen_time_list[name_index]
 49         self.frozen_timer = -self.frozen_time
 50         self.refresh_timer = 0
 51         self.select = true
 52 ​
 53     def loadframe(self, name, scale):
 54         frame = tool.gfx[name]
 55         rect = frame.get_rect()
 56         width, height = rect.w, rect.h
 57 ​
 58         self.orig_image = tool.get_image(frame, 0, 0, width, height, c.black, scale)
 59         self.image = self.orig_image
 60 ​
 61     def checkmouseclick(self, mouse_pos):
 62         x, y = mouse_pos
 63         if(x >= self.rect.x and x <= self.rect.right and
 64            y >= self.rect.y and y <= self.rect.bottom):
 65             return true
 66         return false
 67 ​
 68     def canclick(self, sun_value, current_time):
 69         if self.sun_cost <= sun_value and (current_time - self.frozen_timer) > self.frozen_time:
 70             return true
 71         return false
 72 ​
 73     def canselect(self):
 74         return self.select
 75 ​
 76     def setselect(self, can_select):
 77         self.select = can_select
 78         if can_select:
 79             self.image.set_alpha(255)
 80         else:
 81             self.image.set_alpha(128)
 82 ​
 83     def setfrozentime(self, current_time):
 84         self.frozen_timer = current_time
 85 ​
 86     def createshowimage(self, sun_value, current_time):
 87         '''create a card image to show cool down status
 88            or disable status when have not enough sun value'''
 89         time = current_time - self.frozen_timer
 90         if time < self.frozen_time: #cool down status
 91             image = pg.surface([self.rect.w, self.rect.h])
 92             frozen_image = self.orig_image.copy()
 93             frozen_image.set_alpha(128)
 94             frozen_height = (self.frozen_time - time)/self.frozen_time * self.rect.h
 95             
 96             image.blit(frozen_image, (0,0), (0, 0, self.rect.w, frozen_height))
 97             image.blit(self.orig_image, (0,frozen_height),
 98                        (0, frozen_height, self.rect.w, self.rect.h - frozen_height))
 99         elif self.sun_cost > sun_value: #disable status
100             image = self.orig_image.copy()
101             image.set_alpha(192)
102         else:
103             image = self.orig_image
104         return image
105 ​
106     def update(self, sun_value, current_time):
107         if (current_time - self.refresh_timer) >= 250:
108             self.image = self.createshowimage(sun_value, current_time)
109             self.refresh_timer = current_time
110 ​
111     def draw(self, surface):
112         surface.blit(self.image, self.rect)
113 ​
114 class menubar():
115     def __init__(self, card_list, sun_value):
116         self.loadframe(c.menubar_background)
117         self.rect = self.image.get_rect()
118         self.rect.x = 10
119         self.rect.y = 0
120         
121         self.sun_value = sun_value
122         self.card_offset_x = 32
123         self.setupcards(card_list)
124 ​
125     def loadframe(self, name):
126         frame = tool.gfx[name]
127         rect = frame.get_rect()
128         frame_rect = (rect.x, rect.y, rect.w, rect.h)
129 ​
130         self.image = tool.get_image(tool.gfx[name], *frame_rect, c.white, 1)
131 ​
132     def update(self, current_time):
133         self.current_time = current_time
134         for card in self.card_list:
135             card.update(self.sun_value, self.current_time)
136 ​
137     def createimage(self, x, y, num):
138         if num == 1:
139             return
140         img = self.image
141         rect = self.image.get_rect()
142         width = rect.w
143         height = rect.h
144         self.image = pg.surface((width * num, height)).convert()
145         self.rect = self.image.get_rect()
146         self.rect.x = x
147         self.rect.y = y
148         for i in range(num):
149             x = i * width
150             self.image.blit(img, (x,0))
151         self.image.set_colorkey(c.black)
152     
153     def setupcards(self, card_list):
154         self.card_list = []
155         x = self.card_offset_x
156         y = 8
157         for index in card_list:
158             x += 55
159             self.card_list.append(card(x, y, index))
160 ​
161     def checkcardclick(self, mouse_pos):
162         result = none
163         for card in self.card_list:
164             if card.checkmouseclick(mouse_pos):
165                 if card.canclick(self.sun_value, self.current_time):
166                     result = (plant_name_list[card.name_index], card.sun_cost)
167                 break
168         return result
169     
170     def checkmenubarclick(self, mouse_pos):
171         x, y = mouse_pos
172         if(x >= self.rect.x and x <= self.rect.right and
173            y >= self.rect.y and y <= self.rect.bottom):
174             return true
175         return false
176 ​
177     def decreasesunvalue(self, value):
178         self.sun_value -= value
179 ​
180     def increasesunvalue(self, value):
181         self.sun_value += value
182 ​
183     def setcardfrozentime(self, plant_name):
184         for card in self.card_list:
185             if plant_name_list[card.name_index] == plant_name:
186                 card.setfrozentime(self.current_time)
187                 break
188 ​
189     def drawsunvalue(self):
190         self.value_image = getsunvalueimage(self.sun_value)
191         self.value_rect = self.value_image.get_rect()
192         self.value_rect.x = 21
193         self.value_rect.y = self.rect.bottom - 21
194         
195         self.image.blit(self.value_image, self.value_rect)
196 ​
197     def draw(self, surface):
198         self.drawsunvalue()
199         surface.blit(self.image, self.rect)
200         for card in self.card_list:
201             card.draw(surface)
202 ​
203 class panel():
204     def __init__(self, card_list, sun_value):
205         self.loadimages(sun_value)
206         self.selected_cards = []
207         self.selected_num = 0
208         self.setupcards(card_list)
209 ​
210     def loadframe(self, name):
211         frame = tool.gfx[name]
212         rect = frame.get_rect()
213         frame_rect = (rect.x, rect.y, rect.w, rect.h)
214 ​
215         return tool.get_image(tool.gfx[name], *frame_rect, c.white, 1)
216 ​
217     def loadimages(self, sun_value):
218         self.menu_image = self.loadframe(c.menubar_background)
219         self.menu_rect = self.menu_image.get_rect()
220         self.menu_rect.x = 0
221         self.menu_rect.y = 0
222 ​
223         self.panel_image = self.loadframe(c.panel_background)
224         self.panel_rect = self.panel_image.get_rect()
225         self.panel_rect.x = 0
226         self.panel_rect.y = panel_y_start
227 ​
228         
229         self.value_image = getsunvalueimage(sun_value)
230         self.value_rect = self.value_image.get_rect()
231         self.value_rect.x = 21
232         self.value_rect.y = self.menu_rect.bottom - 21
233 ​
234         self.button_image =  self.loadframe(c.start_button)
235         self.button_rect = self.button_image.get_rect()
236         self.button_rect.x = 155
237         self.button_rect.y = 547
238 ​
239     def setupcards(self, card_list):
240         self.card_list = []
241         x = panel_x_start - panel_x_internal
242         y = panel_y_start + 43 - panel_y_internal
243         for i, index in enumerate(card_list):
244             if i % 8 == 0:
245                 x = panel_x_start - panel_x_internal
246                 y += panel_y_internal
247             x += panel_x_internal
248             self.card_list.append(card(x, y, index, 0.75))
249 ​
250     def checkcardclick(self, mouse_pos):
251         delete_card = none
252         for card in self.selected_cards:
253             if delete_card: # when delete a card, move right cards to left
254                 card.rect.x -= 55
255             elif card.checkmouseclick(mouse_pos):
256                 self.deletecard(card.name_index)
257                 delete_card = card
258 ​
259         if delete_card:
260             self.selected_cards.remove(delete_card)
261             self.selected_num -= 1
262 ​
263         if self.selected_num == card_list_num:
264             return
265 ​
266         for card in self.card_list:
267             if card.checkmouseclick(mouse_pos):
268                 if card.canselect():
269                     self.addcard(card)
270                 break
271 ​
272     def addcard(self, card):
273         card.setselect(false)
274         y = 8
275         x = 78 + self.selected_num * 55
276         self.selected_cards.append(card(x, y, card.name_index))
277         self.selected_num += 1
278 ​
279     def deletecard(self, index):
280         self.card_list[index].setselect(true)
281 ​
282     def checkstartbuttonclick(self, mouse_pos):
283         if self.selected_num < card_list_num:
284             return false
285 ​
286         x, y = mouse_pos
287         if (x >= self.button_rect.x and x <= self.button_rect.right and
288             y >= self.button_rect.y and y <= self.button_rect.bottom):
289            return true
290         return false
291 ​
292     def getselectedcards(self):
293         card_index_list = []
294         for card in self.selected_cards:
295             card_index_list.append(card.name_index)
296         return card_index_list
297 ​
298     def draw(self, surface):
299         self.menu_image.blit(self.value_image, self.value_rect)
300         surface.blit(self.menu_image, self.menu_rect)
301         surface.blit(self.panel_image, self.panel_rect)
302         for card in self.card_list:
303             card.draw(surface)
304         for card in self.selected_cards:
305             card.draw(surface)
306 ​
307         if self.selected_num == card_list_num:
308             surface.blit(self.button_image, self.button_rect)

 

鼠标图片切换

setupmouseimage 函数实现鼠标图片切换为选中的植物:

  • self.mouse_image :根据 plant_name 获取选中的植物图片;

  • self.mouse_rect:选中植物图片的位置,在drawmouseshow函数中,需要将植物图片的位置设置成当前鼠标的位置;

  • pg.mouse.set_visible(false):隐藏默认的鼠标显示,这样效果就是鼠标图片切换为选中的植物了。

 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 ​
11 ​
12         if plant_name == c.potatomine or plant_name == c.squash:
13             color = c.white
14         else:
15             color = c.black
16         self.mouse_image = tool.get_image(frame_list[0], x, y, width, height, color, 1)
17         self.mouse_rect = self.mouse_image.get_rect()
18         pg.mouse.set_visible(false)
19         self.drag_plant = true
20         self.plant_name = plant_name
21         self.plant_cost = plant_cost
22 ​
23 ​
24     def drawmouseshow(self, surface):
25         if self.hint_plant:
26             surface.blit(self.hint_image, self.hint_rect)
27         x, y = pg.mouse.get_pos()
28         self.mouse_rect.centerx = x
29         self.mouse_rect.centery = y
30         surface.blit(self.mouse_image, self.mouse_rect)

 

提示种在哪个方格中

先看下map类,代码在source\component\map.py 中:

  • self.map:二维list,用来保存每个方格的状态。每个entry初始化为 0, 表示可以种植物,值为1时表示这个方格已经种了植物。

  • getmapindex 函数:传入参数是游戏中的坐标位置(比如当前鼠标的位置),返回该位置在地图的哪个方格中。

  • getmapgridpos 函数:传入一个方格的index,返回在该方格中种植物的坐标位置。

  • showplant 函数:根据传入的坐标位置,判断该位置所在的方格是否能种植物,如果能种,就返回返回在该方格中种植物的坐标位置。

 1 map_empty = 0
 2 map_exist = 1
 3 ​
 4 ​
 5 class map():
 6     def __init__(self, width, height):
 7         self.width = width
 8         self.height = height
 9         self.map = [[0 for x in range(self.width)] for y in range(self.height)]
10 ​
11 ​
12     def isvalid(self, map_x, map_y):
13         if (map_x < 0 or map_x >= self.width or
14             map_y < 0 or map_y >= self.height):
15             return false
16         return true
17 ​
18     def ismovable(self, map_x, map_y):
19         return (self.map[map_y][map_x] == c.map_empty)
20 ​
21     def getmapindex(self, x, y):
22         x -= c.map_offset_x
23         y -= c.map_offset_y
24         return (x // c.grid_x_size, y // c.grid_y_size)
25 ​
26     def getmapgridpos(self, map_x, map_y):
27         return (map_x * c.grid_x_size + c.grid_x_size//2 + c.map_offset_x,
28                 map_y * c.grid_y_size + c.grid_y_size//5 * 3 + c.map_offset_y)
29 ​
30     def setmapgridtype(self, map_x, map_y, type):
31         self.map[map_y][map_x] = type
32 ​
33 ​
34     def getrandommapindex(self):
35         map_x = random.randint(0, self.width-1)
36         map_y = random.randint(0, self.height-1)
37         return (map_x, map_y)
38 ​
39 ​
40     def showplant(self, x, y):
41         pos = none
42         map_x, map_y = self.getmapindex(x, y)
43         if self.isvalid(map_x, map_y) and self.ismovable(map_x, map_y):
44             pos = self.getmapgridpos(map_x, map_y)
45         return pos

 

代码在source\state\level.py中:

  • canseedplant 函数:判断当前鼠标位置能否种植物;

  • setuphintimage 函数:如果当前鼠标位置能种植物,且有选择了一个植物卡片,则设置self.hint_image 显示当前会在哪一个方格中种植物,self.hint_rect 是植物种的坐标位置。

 1     def canseedplant(self):
 2         x, y = pg.mouse.get_pos()
 3         return self.map.showplant(x, y)
 4 ​
 5     def setuphintimage(self):
 6         pos = self.canseedplant()
 7         if pos and self.mouse_image:
 8             if (self.hint_image and pos[0] == self.hint_rect.x and
 9                 pos[1] == self.hint_rect.y):
10                 return
11             width, height = self.mouse_rect.w, self.mouse_rect.h
12             image = pg.surface([width, height])
13             image.blit(self.mouse_image, (0, 0), (0, 0, width, height))
14             image.set_colorkey(c.black)
15             image.set_alpha(128)
16             self.hint_image = image
17             self.hint_rect = image.get_rect()
18             self.hint_rect.centerx = pos[0]
19             self.hint_rect.bottom = pos[1]
20             self.hint_plant = true
21         else:
22             self.hint_plant = false