更改检查是否可以种植的机制
This commit is contained in:
parent
fdc86eb0c3
commit
88c09f6a9d
@ -26,16 +26,55 @@ class Map():
|
|||||||
return False
|
return False
|
||||||
return True
|
return True
|
||||||
|
|
||||||
# 判断能否种植
|
# 判断位置是否可用
|
||||||
def isMovable(self, map_x, map_y):
|
# 暂时没有写紫卡植物的判断方法
|
||||||
# 目前没有南瓜头,所以用是否为空判断
|
# 由于紫卡植物需要移除以前的植物,所以可用另外定义一个函数
|
||||||
# 可将南瓜头新定义一个状态(如:2),基于此进一步判断
|
# 注意咖啡豆生效后需要同时将植物的睡眠状态和格子的睡眠记录改变
|
||||||
# 应当改成元组,保存南瓜头、花盆、睡莲等状态(字典也可,还更方便)
|
def isAvailable(self, map_x, map_y, plantName):
|
||||||
# 当然,不用元组的话字符串也行,但是得把判断植物写在母函数中,并且需要更多参数
|
if self.map[map_y][map_x][c.MAP_PLOT_TYPE] == c.MAP_GRASS: # 草地
|
||||||
# 这样返回的就是一个具体信息,而非bool值了
|
# 首先需要判断植物是否是水生植物,水生植物不能种植在陆地上
|
||||||
# 到时候还要改一下变量名,还叫isMovable不合适
|
if plantName not in {'睡莲(未实现)', '海蘑菇(未实现)', '缠绕水草(未实现)'}: # 这里的集合也可以换成存储在某一文件中的常数的表达
|
||||||
#if self.map[map_y][map_x][c.]
|
if not self.map[map_y][map_x][c.MAP_PLANT]: # 没有植物肯定可以种植
|
||||||
return (self.map[map_y][map_x] == c.MAP_STATE_EMPTY)
|
return True
|
||||||
|
elif ((self.map[map_y][map_x][c.MAP_PLANT] | {'花盆(未实现)', '南瓜头(未实现)'} == {'花盆(未实现)', '南瓜头(未实现)'})
|
||||||
|
and (plantName not in self.map[map_y][map_x][c.MAP_PLANT])): # 例外植物:集合中填花盆和南瓜头,只要这里没有这种植物就能种植;判断方法:并集
|
||||||
|
return True
|
||||||
|
elif plantName == '咖啡豆(未实现)' and self.map[map_y][map_x][c.MAP_SLEEP]:
|
||||||
|
return True
|
||||||
|
else:
|
||||||
|
return False
|
||||||
|
else:
|
||||||
|
return False
|
||||||
|
elif self.map[map_y][map_x][c.MAP_PLOT_TYPE] == c.MAP_TILE: # 屋顶
|
||||||
|
# 首先需要判断植物是否是水生植物,水生植物不能种植在陆地上
|
||||||
|
if plantName not in {'睡莲(未实现)', '海蘑菇(未实现)', '缠绕水草(未实现)'}: # 这里的集合也可以换成存储在某一文件中的常数的表达
|
||||||
|
if '花盆(未实现)' in self.map[map_y][map_x][c.MAP_PLANT]:
|
||||||
|
if ((self.map[map_y][map_x][c.MAP_PLANT] | {'花盆(未实现)', '南瓜头(未实现)'} == {'花盆(未实现)', '南瓜头(未实现)'})
|
||||||
|
and (plantName not in self.map[map_y][map_x][c.MAP_PLANT])): # 例外植物:集合中填花盆和南瓜头,只要这里没有这种植物就能种植;判断方法:并集
|
||||||
|
return True
|
||||||
|
elif plantName == '花盆(未实现)': # 这一格本来没有花盆而且新来的植物是花盆,可以种
|
||||||
|
return True
|
||||||
|
elif plantName == '咖啡豆(未实现)' and self.map[map_y][map_x][c.MAP_SLEEP]:
|
||||||
|
return True
|
||||||
|
else:
|
||||||
|
return False
|
||||||
|
else:
|
||||||
|
return False
|
||||||
|
else: # 水里
|
||||||
|
if plantName in {'睡莲(未实现)', '海蘑菇(未实现)', '缠绕水草(未实现)'}: # 是水生植物
|
||||||
|
if not self.map[map_y][map_x][c.MAP_PLANT]: # 只有无植物时才能在水里种植水生植物
|
||||||
|
return True
|
||||||
|
else:
|
||||||
|
return False
|
||||||
|
else: # 非水生植物,依赖睡莲
|
||||||
|
if '睡莲(未实现)' in self.map[map_y][map_x][c.MAP_PLANT]:
|
||||||
|
if ((self.map[map_y][map_x][c.MAP_PLANT] | {'睡莲(未实现)', '花盆(未实现)', '南瓜头(未实现)'} == {'睡莲(未实现)', '花盆(未实现)', '南瓜头(未实现)'})
|
||||||
|
and (plantName not in self.map[map_y][map_x][c.MAP_PLANT])): # 例外植物:集合中填花盆和南瓜头,只要这里没有这种植物就能种植;判断方法:并集
|
||||||
|
return True
|
||||||
|
else:
|
||||||
|
return False
|
||||||
|
else:
|
||||||
|
return False
|
||||||
|
|
||||||
def getMapIndex(self, x, y):
|
def getMapIndex(self, x, y):
|
||||||
# 引入新地图后需要增加这里的内容
|
# 引入新地图后需要增加这里的内容
|
||||||
@ -71,8 +110,9 @@ class Map():
|
|||||||
def setMapGridType(self, map_x, map_y, type):
|
def setMapGridType(self, map_x, map_y, type):
|
||||||
self.map[map_y][map_x] = type
|
self.map[map_y][map_x] = type
|
||||||
|
|
||||||
def addMapPlant(self, map_x, map_y, plantName):
|
def addMapPlant(self, map_x, map_y, plantName, sleep=False):
|
||||||
self.map[map_y][map_x][c.MAP_PLANT].add(plantName)
|
self.map[map_y][map_x][c.MAP_PLANT].add(plantName)
|
||||||
|
self.map[map_y][map_x][c.MAP_SLEEP] = sleep
|
||||||
|
|
||||||
def removeMapPlant(self, map_x, map_y, plantName):
|
def removeMapPlant(self, map_x, map_y, plantName):
|
||||||
self.map[map_y][map_x][c.MAP_PLANT].remove(plantName)
|
self.map[map_y][map_x][c.MAP_PLANT].remove(plantName)
|
||||||
@ -82,9 +122,9 @@ class Map():
|
|||||||
map_y = random.randint(0, self.height-1)
|
map_y = random.randint(0, self.height-1)
|
||||||
return (map_x, map_y)
|
return (map_x, map_y)
|
||||||
|
|
||||||
def showPlant(self, x, y):
|
def checkPlantToSeed(self, x, y, plantName):
|
||||||
pos = None
|
pos = None
|
||||||
map_x, map_y = self.getMapIndex(x, y)
|
map_x, map_y = self.getMapIndex(x, y)
|
||||||
if self.isValid(map_x, map_y) and self.isMovable(map_x, map_y):
|
if self.isValid(map_x, map_y) and self.isAvailable(map_x, map_y, plantName):
|
||||||
pos = self.getMapGridPos(map_x, map_y)
|
pos = self.getMapGridPos(map_x, map_y)
|
||||||
return pos
|
return pos
|
||||||
|
|||||||
@ -460,18 +460,15 @@ class Level(tool.State):
|
|||||||
self.zombie_groups[map_y].add(zombie.NewspaperZombie(c.ZOMBIE_START_X, y, self.head_group))
|
self.zombie_groups[map_y].add(zombie.NewspaperZombie(c.ZOMBIE_START_X, y, self.head_group))
|
||||||
|
|
||||||
# 能否种植物的判断:
|
# 能否种植物的判断:
|
||||||
# 调用self.map.showPlant(x, y)
|
|
||||||
# 先判断位置是否合法 isValid(map_x, map_y)
|
# 先判断位置是否合法 isValid(map_x, map_y)
|
||||||
# 再判断位置是否可用 isMovable(map_x, map_y)
|
# 再判断位置是否可用 isMovable(map_x, map_y)
|
||||||
# 因为现在还没有做南瓜头,所以目前判断的是map[map_y][map_x]是否为空(c.MAP_STATE_EMPTY,即0)
|
def canSeedPlant(self, plantName):
|
||||||
# 写了南瓜头需要改这个验证
|
|
||||||
def canSeedPlant(self):
|
|
||||||
x, y = pg.mouse.get_pos()
|
x, y = pg.mouse.get_pos()
|
||||||
return self.map.showPlant(x, y)
|
return self.map.checkPlantToSeed(x, y, plantName)
|
||||||
|
|
||||||
# 种植物
|
# 种植物
|
||||||
def addPlant(self):
|
def addPlant(self):
|
||||||
pos = self.canSeedPlant()
|
pos = self.canSeedPlant(self.plant_name)
|
||||||
if pos is None:
|
if pos is None:
|
||||||
return
|
return
|
||||||
|
|
||||||
@ -522,6 +519,9 @@ class Level(tool.State):
|
|||||||
|
|
||||||
if new_plant.can_sleep and self.background_type in {c.BACKGROUND_DAY, c.BACKGROUND_POOL, c.BACKGROUND_ROOF, c.BACKGROUND_WALLNUTBOWLING, c.BACKGROUND_SINGLE, c.BACKGROUND_TRIPLE}:
|
if new_plant.can_sleep and self.background_type in {c.BACKGROUND_DAY, c.BACKGROUND_POOL, c.BACKGROUND_ROOF, c.BACKGROUND_WALLNUTBOWLING, c.BACKGROUND_SINGLE, c.BACKGROUND_TRIPLE}:
|
||||||
new_plant.setSleep()
|
new_plant.setSleep()
|
||||||
|
mushroomSleep = True
|
||||||
|
else:
|
||||||
|
mushroomSleep = False
|
||||||
self.plant_groups[map_y].add(new_plant)
|
self.plant_groups[map_y].add(new_plant)
|
||||||
if self.bar_type == c.CHOOSEBAR_STATIC:
|
if self.bar_type == c.CHOOSEBAR_STATIC:
|
||||||
self.menubar.decreaseSunValue(self.select_plant.sun_cost)
|
self.menubar.decreaseSunValue(self.select_plant.sun_cost)
|
||||||
@ -530,7 +530,7 @@ class Level(tool.State):
|
|||||||
self.menubar.deleateCard(self.select_plant)
|
self.menubar.deleateCard(self.select_plant)
|
||||||
|
|
||||||
if self.bar_type != c.CHOSSEBAR_BOWLING:
|
if self.bar_type != c.CHOSSEBAR_BOWLING:
|
||||||
self.map.addMapPlant(map_x, map_y, self.plant_name)
|
self.map.addMapPlant(map_x, map_y, self.plant_name, sleep=mushroomSleep)
|
||||||
self.removeMouseImage()
|
self.removeMouseImage()
|
||||||
#print('addPlant map[%d,%d], grid pos[%d, %d] pos[%d, %d]' % (map_x, map_y, x, y, pos[0], pos[1]))
|
#print('addPlant map[%d,%d], grid pos[%d, %d] pos[%d, %d]' % (map_x, map_y, x, y, pos[0], pos[1]))
|
||||||
|
|
||||||
@ -672,6 +672,9 @@ class Level(tool.State):
|
|||||||
if self.bar_type != c.CHOSSEBAR_BOWLING:
|
if self.bar_type != c.CHOSSEBAR_BOWLING:
|
||||||
# 更改地图类型、添加南瓜头、睡莲、花盆后可能也需要改这里
|
# 更改地图类型、添加南瓜头、睡莲、花盆后可能也需要改这里
|
||||||
self.map.removeMapPlant(map_x, map_y, plant.name)
|
self.map.removeMapPlant(map_x, map_y, plant.name)
|
||||||
|
# 将睡眠植物移除后更新睡眠状态
|
||||||
|
if plant.state == c.SLEEP:
|
||||||
|
self.map[map_y][map_x][c.MAP_SLEEP] = False
|
||||||
# 用铲子铲不用触发植物功能
|
# 用铲子铲不用触发植物功能
|
||||||
if not shovel:
|
if not shovel:
|
||||||
if (plant.name == c.CHERRYBOMB or plant.name == c.JALAPENO or
|
if (plant.name == c.CHERRYBOMB or plant.name == c.JALAPENO or
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user