Python植物大战僵尸源码分享
前言
今天給大家推薦一個(gè)Gtihub開源項(xiàng)目:PythonPlantsVsZombies,翻譯成中就是植物大戰(zhàn)僵尸。
《植物大戰(zhàn)僵尸》是一款極富策略性的小游戲。可怕的僵尸即將入侵,每種僵尸都有不同的特點(diǎn),例如鐵桶僵尸擁有極強(qiáng)的抗擊打能力,礦工僵尸可以挖地道繞過種植在土壤表面的植物等。玩家防御僵尸的方式就是栽種植物。49種植物每種都有不同的功能,例如櫻桃炸彈可以和周圍一定范圍內(nèi)的所有僵尸同歸于盡,而食人花可以吃掉最靠近自己的一只僵尸。玩家可以針對(duì)不同僵尸的弱點(diǎn)來合理地種植植物,這也是勝利的訣竅。
功能實(shí)現(xiàn)
-
支持的植物:向日葵、豌豆射手、胡桃、雪豌豆射手、櫻桃炸彈、三豌豆射手、大嘴花、puffshroom、馬鈴薯胺、穗狀雜草、南瓜、膽小菇、墨西哥胡椒、陽光菇、冰川菇、催眠蘑菇。
-
支持僵尸:普通僵尸,旗幟僵尸,路障僵尸,鐵桶僵尸,報(bào)紙僵尸。
-
支持在關(guān)卡開始時(shí)選擇植物卡片。
-
支持白天級(jí)別、夜間級(jí)別、移動(dòng)卡選擇級(jí)別和胡桃保齡球
環(huán)境要求
1、python3.7
2、Python-Pygame 1.9
展示部分素材
游戲界面
個(gè)性化定義
游戲的關(guān)卡數(shù)據(jù),存儲(chǔ)在json文件里的。具體目錄:PythonPlantsVsZombies-master\source\data。我們可以進(jìn)行自定義配置,例如僵尸的位置和時(shí)間,背景信息。
?主要代碼
__author__ = 'marble_xu'import pygame as pg from .. import tool from .. import constants as cclass 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 = persistself.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 = 0def 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 = 75self.option_start = 0 self.option_timer = 0 self.option_clicked = Falsedef checkOptionClick(self, mouse_pos):x, y = mouse_pos if(x >= self.option_rect.x and x <= self.option_rect.right andy >= self.option_rect.y and y <= self.option_rect.bottom): self.option_clicked = True self.option_timer = self.option_start = self.current_time return Falsedef update(self, surface, current_time, mouse_pos, mouse_click): self.current_time = self.game_info[c.CURRENT_TIME] = current_timeif 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 = Truesurface.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 cclass Screen(tool.State): def __init__(self):tool.State.__init__(self) self.end_time = 3000def startup(self, current_time, persist): self.start_time = current_time self.next = c.LEVEL self.persist = persist self.game_info = persistname = self.getImageName() self.setupImage(name) self.next = self.set_next_state()def getImageName(self):passdef set_next_state(self):passdef 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 = 0def 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 = Trueclass GameVictoryScreen(Screen): def __init__(self):Screen.__init__(self)def getImageName(self): return c.GAME_VICTORY_IMAGEdef set_next_state(self): return c.LEVELclass GameLoseScreen(Screen): def __init__(self):Screen.__init__(self)def getImageName(self): return c.GAME_LOOSE_IMAGEdef set_next_state(self): return c.MAIN_MENU總結(jié)
以上是生活随笔為你收集整理的Python植物大战僵尸源码分享的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 数据库设计原则
- 下一篇: C++ 多重继承之内存存储