当前位置:首页 > 游戏资讯

plantsvszombies2国际版下载 植物大战僵尸国际版2下载

2023-09-15 10:56:02

plantsvszombies2国际版下载 植物大战僵尸国际版2下载

plantsvszombies2国际版下载 植物大战僵尸国际版2下载

编辑|毛中正

出品| CSDN博客

功能介绍

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

该功能实现如下:

支持的植物类型:向日葵,豌豆射手,雪豌豆,坚果,樱桃炸弹。新增植物:双豌豆射手,三豌豆射手,食人花,小蘑菇,土豆矿,南瓜。

支持的僵尸类型:普通僵尸、僵尸、路障僵尸、铁桶僵尸。新加入的读报僵尸。

使用json文件保存检查点信息,设置僵尸的时间和位置。

在每一关开始时增加植物的选择。

加一台割草机。

下面是游戏截图:

图1:新植物和僵尸

图2:在每一关开始时选择植物卡。

图3:选择种植植物的地方。

植物卡片的选择与种植

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

本文将介绍:

上层植物卡列的实现。

点击植物卡片,鼠标会切换到植物图片。

当鼠标移动时,确定其当前所在的网格,并显示半透明的植物作为提示。

完全码

游戏实现代码Github链接:

https://github.com/marblexu/PythonPlantsVsZombies

以下是csdn的下载链接:

https://download.csdn.net/download/marble_xu/11982275

代码实现

所有植物卡片的名称和属性存储在一个单独的列表中,每个列表索引对应一个植物。

例如,列表索引0是向日葵:

Card_name_list[0]是向日葵卡片的名称,用来获取向日葵卡片的图片。

Plant_name_list[0]是向日葵的名字,用来获取向日葵卡片的图片。

Plant_sun_list[0]是种植向日葵所需的太阳点数。

Plant_frozen_time_list[0]是向日葵的冷却时间。

代码在source\component\menubar.py中:

CARD _ name _ list =[c . CARD _ SUNFLOWER,c.CARD_PEASHOOTER,c.CARD_SNOWPEASHOOTER,c.CARD_WALLNUT,

c.卡_CHERRYBOMB,c .卡_THREEPEASHOOTER,c .卡_REPEATERPEA,c .卡_CHOMPER,

c.卡_泡芙,卡_土豆宁,卡_南瓜]

plant_name_list = [c .向日葵,c .豌豆射手,c .雪地豌豆射手,c.WALLNUT,

c.CHERRYBOMB,c.THREEPEASHOOTER,c.REPEATERPEA,c.CHOMPER,

c.马勃菌,马铃薯,南瓜]

plant_sun_list = [50,100,175,50,150,325,200,150,0,25,50]

plant_frozen_time_list = [0,5000,5000,10000,5000,5000,5000,8000,8000,8000,8000]

all_card_list = [0,1,2,3,4,5,6,7,8,9,10]

植物卡片

代码在source\component\menubar.py中,每个植物卡片都是一个单独的卡片类来显示这个植物。

CheckMouseClick功能:判断鼠标是否点击这张卡;

CanClick:判断这张卡是否可以种(点数是否足够,是否还在冷却时间);

更新功能:通过设置图片的透明度来表示这张卡是否可以被选中。

班级卡:

def __init__(self,x,y,name_index,scale=0.78):

self . load frame(card _ name _ list[name _ index],秤)

self.rect = self.image.get_rect

self.rect.x = x

self.rect.y = y

self.name_index =名称_索引

self . sun _ cost = plant _ sun _ list[name _ index]

self . frozen _ time = plant _ frozen _ time _ list[name _ index]

self . frozen _ timer =-self . frozen _ time

self.select = True

定义负载框架(自身、名称、比例):

框架=工具。GFX[名字]

rect = frame.get_rect

宽度,高度=矩形宽度,矩形高度

self . image = tool . get _ image(frame,0,0,宽度,高度,c.BLACK,scale)

def checkMouseClick(self,mouse_pos):

x,y =鼠标位置

if(x & gt;= self.rect.x和x & lt= self.rect.right和

y & gt= self.rect.y和y & lt= self.rect.bottom):

返回True

返回False

def canClick(自身,太阳值,当前时间):

if self.sun _ cost & lt= sun _ value and(current _ time-self . frozen _ timer)& gt;self.frozen_time:

返回True

返回False

def canSelect(self):

返回自我选择

def setSelect(self,can_select):

self.select = can_select

如果可以选择:

self.image.set_alpha(255)

否则:

self.image.set_alpha(128)

def setFrozenTime(self,current_time):

self.frozen_timer =当前时间

定义更新(自身,太阳值,当前时间):

if(self . sun _ cost & gt;sun_value或

(current _ time-self . frozen _ timer)& lt;= self.frozen_time):

self.image.set_alpha(128)

否则:

self.image.set_alpha(255)

定义绘制(自身、表面):

surface.blit(self.image,self.rect)

卡片栏类别

代码在source\component\MenuBar.py中,MenuBar类显示图3中的植物卡片栏:

Self.sun_value:当前收集的太阳点数;

Self.card_list:植物卡片列表;

SetupCards函数:遍历初始化__init__函数传入本级选中的植物卡片列表,依次创建卡片类,设置每张卡片的显示位置;

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

类菜单栏:

def __init__(self,card_list,sun_value):

self.loadFrame(c .菜单栏_BACKGROUND)

self.rect = self.image.get_rect

self.rect.x = 10

self.rect.y = 0

self.sun_value = sun_value

self.card_offset_x = 32

自助设置卡(card_list)

定义负载框架(自身,名称):

框架=工具。GFX[名字]

rect = frame.get_rect

frame_rect =(矩形x,矩形y,矩形w,矩形h)

self.image = tool.get_image(tool。GFX[姓名],*frame_rect,c.WHITE,1)

定义更新(自身,当前时间):

self.current_time =当前时间

对于self.card_list中的卡:

card.update(self.sun_value,self.current_time)

def createImage(self,x,y,num):

如果数量== 1:

返回

img = self.image

rect = self.image.get_rect

宽度=矩形

高度=矩形高度

self.image = pg。曲面((宽度*数量,高度))。皈依者

self.rect = self.image.get_rect

self.rect.x = x

self.rect.y = y

对于范围内的I(数量):

x = i *宽度

self.image.blit(img, (x,0))self.image.blit(img,(x,0))

self . image . set _ color key(c . BLACK)

def setupCards(self,card_list):

self.card_list =

x = self.card_offset_x

y = 8

对于card_list中的索引:

x += 55

self.card_list.append(Card(x,y,index))

def checkCardClick(self,mouse_pos):

结果=无

对于self.card_list中的卡:

if card . check mouse click(mouse _ pos):

if card.canClick(self.sun_value,self.current_time):

结果=(植物名称列表[卡片名称索引],卡片名称成本)

破裂

回送结果

def checkMenuBarClick(self,mouse_pos):

x,y =鼠标位置

if(x & gt;= self.rect.x和x & lt= self.rect.right和

y & gt= self.rect.y和y & lt= self.rect.bottom):

返回True

返回False

def decreaseSunValue(自我,价值):

self.sun_value -= value

def increaseSunValue(自身,值):

self.sun_value += value

def setCardFrozenTime(self,plant_name):

对于self.card_list中的卡:

if植物名称列表[卡片名称索引] ==植物名称:

card . setfrozentime(self . current _ time)

破裂

定义drawSunValue(自身):

self . value _ image = getSunValueImage(self . sun _ value)

self . value _ rect = self . value _ image . get _ rect

self.value_rect.x = 21

self . value _ rect . y = self . rect . bottom-21

self . image . blit(self . value _ image,self.value_rect)

定义绘制(自身、表面):

self.drawSunValue

surface.blit(self.image,self.rect)

对于self.card_list中的卡:

card.draw(表面)

鼠标图片切换

代码在source\state\level.py中,setupMouseImage的函数可以将鼠标画面切换到选中的植物上:

Self.mouse_image:根据plant_name获取选中的植物图像;

Self.mouse_rect:选择植物图片的位置。在drawMouseShow函数中,需要将植物图片的位置设置为当前鼠标位置;

Pg.mouse.set_visible(False):隐藏默认的鼠标显示,这样鼠标画面就会切换到选中的植物上。

def setupMouseImage(self,plant_name,plant_cost):

frame_list =工具。GFX[植物名称]

如果工具中有工厂名称。植物_RECT:

数据=工具。RECT植物[植物名称]

x,y,width,height = data[‘x’],data[‘y’],data[‘width’],data[‘height’]

否则:

x,y = 0,0

rect = frame_list[0]。get_rect

宽度,高度=矩形宽度,矩形高度

如果植物名称==马铃薯或植物名称==南瓜:

颜色= c .白色

否则:

颜色= c .黑色

self . mouse _ image = tool . get _ image(frame _ list[0],x,y,宽度,高度,颜色,1)

self.mouse_rect = self .鼠标_图像. get_rect

pg.mouse.set_visible(False)

self.drag_plant = True

self.plant_name =植物名称

self.plant_cost =工厂成本

def drawMouseShow(自身,表面):

if self.hint_plant:

surface.blit(self.hint_image,self.hint_rect)

x,y = pg.mouse.get_pos

self . mouse _ rect . centex = x

self.mouse_rect.centery = y

surface.blit(self.mouse_image,self.mouse_rect)

建议种在哪个盒子里。

先看地图类。代码在source\component\map.py中:

Self.map:二维列表,用于保存每个网格的状态。每个条目初始化为0,表示可以种植植物,值为1表示该框中已经种植了植物。

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

GetMapGridPos函数:传入一个网格的索引,返回种植在网格中的植物的坐标位置。

ShowPlant函数:根据传入的坐标位置,判断该位置所在的网格是否可以种植植物,如果可以,则返回该网格中种植的植物的坐标位置。

MAP_EMPTY = 0

MAP_EXIST = 1

类别映射:

def __init__(自身,宽度,高度):

自身宽度=宽度

自我高度=高度

self.map = [[0表示范围内的x(self . width)]表示范围内的y(self . height)]

def isValid(self,map_x,map_y):

if(map _ x & lt;0或map _ x & gt= self.width或

map _ y & lt0或map _ y & gt= self.height):

返回False

返回True

def iovable(self,map_x,map_y):

return(self . MAP[MAP _ y][MAP _ x]= = c . MAP _ EMPTY)

def getMapIndex(self,x,y):

x -= c.MAP_OFFSET_X

y -= c .地图_偏移_Y

return (x // c.GRID_X_SIZE,y // c.GRID_Y_SIZE)

def getMapGridPos(self,map_x,map_y):

return(MAP _ X * c . GRID _ X _ SIZE+c . GRID _ X _ SIZE//2+c . MAP _ OFFSET _ X

MAP _ Y * c . GRID _ Y _ SIZE+c . GRID _ Y _ SIZE//5 * 3+c . MAP _ OFFSET _ Y)

def setMapGridType(self,map_x,map_y,Type):

self.map[map_y][map_x] = type

def getRandomMapIndex(self):

map_x = random.randint(0,self.width-1)

map_y = random.randint(0,self.height-1)

返回(地图x,地图y)

def showPlant(自身,x,y):

pos =无

map_x,map_y = self.getMapIndex(x,y)

如果self.isValid(map_x,map_y)和self.iovable(map_x,map_y):

pos = self.getMapGridPos(map_x,map_y)

退货位置

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

CanSeedPlant功能:判断当前鼠标位置是否可以种植植物;

SetupHintImage函数:如果当前鼠标位置可以种植植物,并且选择了一张植物卡片,则设置self.hint_image显示当前将要种植哪些网格植物,self.hint_rect为植物种类的坐标位置。

def canSeedPlant(self):

x,y = pg.mouse.get_pos

return self.map.showPlant(x,y)

定义设置提示(自我):

pos = self.canSeedPlant

if pos和self.mouse_image:

if(self . hint _ image and pos[0]= = self . hint _ rect . x and

pos[1] == self.hint_rect.y):

返回

宽度,高度= self.mouse_rect.w,self.mouse_rect.h

image = pg。表面([宽度,高度])

image.blit(self.mouse_image,(0,0),(0,0,width,height))

image.set_colorkey(c.BLACK)

image.set_alpha(128)

self.hint_image = image

self.hint_rect = image.get_rect

self . hint _ rect . centex = pos[0]

self.hint_rect.bottom = pos[1]

self.hint_plant = True

否则:

self.hint_plant = False

编译环境:python3.7+pygame1.9。

原文链接:https://blog.csdn.net/marble_xu/article/details/103100614

版权声明:本文为博主“弹珠_徐”原创文章。

标签 国际版   下载   僵尸   大战   植物
ID:1 黄金广告位
ID:2 黄金广告位
ID:3 黄金广告位
今日推荐
《最终幻想16》PC中文版本预告片震撼发布
《最终幻想16》PC中文版本预告片震撼发布

最近,SE公司发布了《最终幻想16》PC版的中文预告片,向玩...[详细]

独家专栏
精彩推荐
ID:4 黄金广告位
热门排行
热门标签