前言
今天给大家推荐一个Gtihub开源项目:PythonPlantsVsZombies,翻译成中就是植物大战僵尸。
《植物大战僵尸》是一款极富策略性的小游戏。可怕的僵尸即将入侵,每种僵尸都有不同的特点,例如铁桶僵尸拥有极强的抗击打能力,矿工僵尸可以挖地道绕过种植在土壤表面的植物等。玩家防御僵尸的方式就是栽种植物。49种植物每种都有不同的功能,例如樱桃炸弹可以和周围一定范围内的所有僵尸同归于尽,而食人花可以吃掉最靠近自己的一只僵尸。玩家可以针对不同僵尸的弱点来合理地种植植物,这也是胜利的诀窍。
功能实现
支持的植物:向日葵、豌豆射手、胡桃、雪豌豆射手、樱桃炸弹、三豌豆射手、大嘴花、puffshroom、马铃薯胺、穗状杂草、南瓜、胆小菇、墨西哥胡椒、阳光菇、冰川菇、催眠蘑菇。
支持僵尸:普通僵尸,旗帜僵尸,路障僵尸,铁桶僵尸,报纸僵尸。
支持在关卡开始时选择植物卡片。
支持白天级别、夜间级别、移动卡选择级别和胡桃保龄球
环境要求
1、python3.7
2、Python-Pygame 1.9
展示部分素材
个性化定义
游戏的关卡数据,存储在json文件里的。具体目录:PythonPlantsVsZombies-master\source\data。我们可以进行自定义配置,例如僵尸的位置和时间,背景信息。
主要代码
__author__ = 'marble_xu' import pygame as pg from .. import tool from .. import constants as c class Menu(tool.State): def __init__(self): tool.State.__init__(self) def startup(self, current_time, persist): self.next = c.LEVEL self.persist = persist self.game_info = persist self.setupBackground() self.setupOption() def setupBackground(self): frame_rect = [80, 0, 800, 600] self.bg_image = tool.get_image(tool.GFX[c.MAIN_MENU_IMAGE], *frame_rect) self.bg_rect = self.bg_image.get_rect() self.bg_rect.x = 0 self.bg_rect.y = 0 def setupOption(self): self.option_frames = [] frame_names = [c.OPTION_ADVENTURE + '_0', c.OPTION_ADVENTURE + '_1'] frame_rect = [0, 0, 165, 77] for name in frame_names: self.option_frames.append(tool.get_image(tool.GFX[name], *frame_rect, c.BLACK, 1.7)) self.option_frame_index = 0 self.option_image = self.option_frames[self.option_frame_index] self.option_rect = self.option_image.get_rect() self.option_rect.x = 435 self.option_rect.y = 75 self.option_start = 0 self.option_timer = 0 self.option_clicked = False def checkOptionClick(self, mouse_pos): x, y = mouse_pos if(x >= self.option_rect.x and x <= self.option_rect.right and y >= self.option_rect.y and y <= self.option_rect.bottom): self.option_clicked = True self.option_timer = self.option_start = self.current_time return False def update(self, surface, current_time, mouse_pos, mouse_click): self.current_time = self.game_info[c.CURRENT_TIME] = current_time if not self.option_clicked: if mouse_pos: self.checkOptionClick(mouse_pos) else: if(self.current_time - self.option_timer) > 200: self.option_frame_index += 1 if self.option_frame_index >= 2: self.option_frame_index = 0 self.option_timer = self.current_time self.option_image = self.option_frames[self.option_frame_index] if(self.current_time - self.option_start) > 1300: self.done = True surface.blit(self.bg_image, self.bg_rect) surface.blit(self.option_image, self.option_rect)
__author__ = 'marble_xu' import pygame as pg from .. import tool from .. import constants as c class Screen(tool.State): def __init__(self): tool.State.__init__(self) self.end_time = 3000 def startup(self, current_time, persist): self.start_time = current_time self.next = c.LEVEL self.persist = persist self.game_info = persist name = self.getImageName() self.setupImage(name) self.next = self.set_next_state() def getImageName(self): pass def set_next_state(self): pass def setupImage(self, name): frame_rect = [0, 0, 800, 600] self.image = tool.get_image(tool.GFX[name], *frame_rect) self.rect = self.image.get_rect() self.rect.x = 0 self.rect.y = 0 def update(self, surface, current_time, mouse_pos, mouse_click): if(current_time - self.start_time) < self.end_time: surface.fill(c.WHITE) surface.blit(self.image, self.rect) else: self.done = True class GameVictoryScreen(Screen): def __init__(self): Screen.__init__(self) def getImageName(self): return c.GAME_VICTORY_IMAGE def set_next_state(self): return c.LEVEL class GameLoseScreen(Screen): def __init__(self): Screen.__init__(self) def getImageName(self): return c.GAME_LOOSE_IMAGE def set_next_state(self): return c.MAIN_MENU