支持的植物類型:太陽花,豌豆射手,寒冰射手,堅果,櫻桃炸彈。新增長植物:雙重豌豆射手,三重豌豆射手。
支持的殭屍類型:普通殭屍,棋子殭屍,路障殭屍,鐵桶殭屍。
使用json文件保存關卡信息,設置殭屍出現的時間和位置。
新增長除草機。
下面是遊戲的截圖:
圖1python
圖2git
圖片顯示切換github
從圖1和圖2能夠看到,殭屍的行走和攻擊時的圖片顯示會有不一樣,這篇文章講下如何進行圖片顯示的切換。
以上面的路障殭屍爲例,一共有下面幾種圖片類型。json
圖3是路障殭屍的這7種圖片類型的示例bash
植物大戰殭屍的圖片資源比較特別,一種圖片類型的每個動做是一個單獨的圖片,如圖4是路障殭屍帶着路障攻擊的動做圖片,一共有11個圖片,因此加載圖片的代碼要作對應的修改。app
遊戲實現代碼的github連接 植物大戰殭屍
這邊是csdn的下載連接 植物大戰殭屍函數
圖片加載
在 source\tool.py 中 load_all_gfx 函數遍歷resources\graphics 目錄和子目錄。
代碼中作了一個簡單的區分:學習
1 def load_all_gfx(directory, colorkey=c.WHITE, accept=('.png', '.jpg', '.bmp', '.gif')):
2 graphics = {}
3 for name1 in os.listdir(directory):
4 # subfolders under the folder resources\graphics
5 dir1 = os.path.join(directory, name1)
6 if os.path.isdir(dir1):
7 for name2 in os.listdir(dir1):
8 dir2 = os.path.join(dir1, name2)
9 if os.path.isdir(dir2):
10 # e.g. subfolders under the folder resources\graphics\Zombies
11 for name3 in os.listdir(dir2):
12 dir3 = os.path.join(dir2, name3)
13 # e.g. subfolders or pics under the folder resources\graphics\Zombies\ConeheadZombie
14 if os.path.isdir(dir3):
15 # e.g. it's the folder resources\graphics\Zombies\ConeheadZombie\ConeheadZombieAttack
16 image_name, _ = os.path.splitext(name3)
17 graphics[image_name] = load_image_frames(dir3, image_name, colorkey, accept)
18 else:
19 # e.g. pics under the folder resources\graphics\Plants\Peashooter
20 image_name, _ = os.path.splitext(name2)
21 graphics[image_name] = load_image_frames(dir2, image_name, colorkey, accept)
22 break
23 else:
24 # e.g. pics under the folder resources\graphics\Screen
25 name, ext = os.path.splitext(name2)
26 if ext.lower() in accept:
27 img = pg.image.load(dir2)
28 if img.get_alpha():
29 img = img.convert_alpha()
30 else:
31 img = img.convert()
32 img.set_colorkey(colorkey)
33 graphics[name] = img
34 return graphics
35
36 GFX = load_all_gfx(os.path.join("resources","graphics"))複製代碼
load_image_frames 函數 將目錄中的全部圖片按照圖片名稱中的index值爲key,保存在tmp 字典中。好比圖片名稱爲"ConeheadZombieAttack_2",它的index值就爲2。
而後將圖片按index值依次加入到 frame_list 中。ui
1 def load_image_frames(directory, image_name, colorkey, accept):
2 frame_list = []
3 tmp = {}
4 # image_name is "Peashooter", pic name is 'Peashooter_1', get the index 1
5 index_start = len(image_name) + 1
6 frame_num = 0;
7 for pic in os.listdir(directory):
8 name, ext = os.path.splitext(pic)
9 if ext.lower() in accept:
10 index = int(name[index_start:])
11 img = pg.image.load(os.path.join(directory, pic))
12 if img.get_alpha():
13 img = img.convert_alpha()
14 else:
15 img = img.convert()
16 img.set_colorkey(colorkey)
17 tmp[index]= img
18 frame_num += 1
19
20 for i in range(frame_num):
21 frame_list.append(tmp[i])
22 return frame_list複製代碼
圖片顯示切換
在 source\component\zombie.py 中, Zombie 類是全部殭屍類的父類,初始化 函數調用loadImages函數加載全部支持的圖片類型,設置Sprite 精靈類顯示須要的成員變量 image和rect。
loadFrames函數給具體的子類來調用,獲取圖片。
spa
1 class Zombie(pg.sprite.Sprite):
2 def __init__(self, x, y, name, health, head_group=None, damage=1):
3 pg.sprite.Sprite.__init__(self)
4
5 self.name = name
6 self.frames = []
7 self.frame_index = 0
8 self.loadImages()
9 self.frame_num = len(self.frames)
10
11 self.image = self.frames[self.frame_index]
12 self.rect = self.image.get_rect()
13 self.rect.centerx = x
14 self.rect.bottom = y
15 ...
16
17 def loadFrames(self, frames, name, image_x):
18 frame_list = tool.GFX[name]
19 rect = frame_list[0].get_rect()
20 width, height = rect.w, rect.h
21 width -= image_x
22
23 for frame in frame_list:
24 frames.append(tool.get_image(frame, image_x, 0, width, height))複製代碼
基本的功能都在Zombie 父類中實現,若是子類有特殊需求,能夠重定義同名函數。
update 函數:每一個tick 都會調用的入口函數,用來更新殭屍的位置,切換狀態和更新圖片顯示。
handleState 函數:根據殭屍當前的狀態來執行不一樣的函數。
animation 函數:每隔指定的 animate_interval 時間會顯示圖片類型的下一個動做。
1 def update(self, game_info):
2 self.current_time = game_info[c.CURRENT_TIME]
3 self.handleState()
4 self.animation()
5
6 def handleState(self):
7 if self.state == c.WALK:
8 self.walking()
9 elif self.state == c.ATTACK:
10 self.attacking()
11 elif self.state == c.DIE:
12 self.dying()
13
14 def animation(self):
15 if (self.current_time - self.animate_timer) > self.animate_interval:
16 self.frame_index += 1
17 if self.frame_index >= self.frame_num:
18 if self.state == c.DIE:
19 self.kill()
20 return
21 self.frame_index = 0
22 self.animate_timer = self.current_time
23
24 self.image = self.frames[self.frame_index]複製代碼
下面四個函數是修改殭屍的當前狀態和圖片顯示。
1 def setWalk(self):
2 self.state = c.WALK
3 self.animate_interval = 150
4
5 if self.helmet:
6 self.changeFrames(self.helmet_walk_frames)
7 elif self.losHead:
8 self.changeFrames(self.losthead_walk_frames)
9 else:
10 self.changeFrames(self.walk_frames)
11
12 def setAttack(self, plant):
13 self.plant = plant
14 self.state = c.ATTACK
15 self.animate_interval = 100
16
17 if self.helmet:
18 self.changeFrames(self.helmet_attack_frames)
19 elif self.losHead:
20 self.changeFrames(self.losthead_attack_frames)
21 else:
22 self.changeFrames(self.attack_frames)
23
24 def setDie(self):
25 self.state = c.DIE
26 self.animate_interval = 200
27 self.changeFrames(self.die_frames)
28
29 def changeFrames(self, frames):
30 '''change image frames and modify rect position'''
31 self.frames = frames
32 self.frame_num = len(self.frames)
33 self.frame_index = 0
34
35 bottom = self.rect.bottom
36 centerx = self.rect.centerx
37 self.image = self.frames[self.frame_index]
38 self.rect = self.image.get_rect()
39 self.rect.bottom = bottom
40 self.rect.centerx = centerx複製代碼
路障殭屍類就比較簡單,只須要實現 loadImages 函數,調用loadFrames函數加載該種殭屍支持的圖片類型,這邊主要的差別在於不一樣種類殭屍的圖片類型的名稱會有區別。
1 class ConeHeadZombie(Zombie):
2 def __init__(self, x, y, head_group):
3 Zombie.__init__(self, x, y, c.CONEHEAD_ZOMBIE, c.CONEHEAD_HEALTH, head_group)
4 self.helmet = True
5
6 def loadImages(self):
7 self.helmet_walk_frames = []
8 self.helmet_attack_frames = []
9 self.walk_frames = []
10 self.attack_frames = []
11 self.losthead_walk_frames = []
12 self.losthead_attack_frames = []
13 self.die_frames = []
14
15 helmet_walk_name = self.name
16 helmet_attack_name = self.name + 'Attack'
17 walk_name = c.NORMAL_ZOMBIE
18 attack_name = c.NORMAL_ZOMBIE + 'Attack'
19 losthead_walk_name = c.NORMAL_ZOMBIE + 'LostHead'
20 losthead_attack_name = c.NORMAL_ZOMBIE + 'LostHeadAttack'
21 die_name = c.NORMAL_ZOMBIE + 'Die'
22
23 frame_list = [self.helmet_walk_frames, self.helmet_attack_frames,
24 self.walk_frames, self.attack_frames, self.losthead_walk_frames,
25 self.losthead_attack_frames, self.die_frames]
26 name_list = [helmet_walk_name, helmet_attack_name,
27 walk_name, attack_name, losthead_walk_name,
28 losthead_attack_name, die_name]
29
30 for i, name in enumerate(name_list):
31 self.loadFrames(frame_list[i], name, tool.ZOMBIE_RECT[name]['x'])
32
33 self.frames = self.helmet_walk_frames複製代碼
python3.7 + pygame1.9