铲子优先对睡莲上的植物而非睡莲生效

This commit is contained in:
星外之神 2022-04-22 14:30:08 +08:00
parent 7acbb320d2
commit ccbfda85b5
6 changed files with 51 additions and 8 deletions

View File

@ -1,5 +1,5 @@
{ {
"background_type":1, "background_type":2,
"init_sun_value":5000, "init_sun_value":5000,
"shovel":1, "shovel":1,
"zombie_list":[ "zombie_list":[

View File

@ -63,7 +63,7 @@ class Map():
return False return False
else: else:
return False return False
else: # 水里 elif self.map[map_y][map_x][c.MAP_PLOT_TYPE] == c.MAP_WATER: # 水里
if plantName in {c.LILYPAD, '海蘑菇(未实现)', '缠绕水草(未实现)'}: # 是水生植物 if plantName in {c.LILYPAD, '海蘑菇(未实现)', '缠绕水草(未实现)'}: # 是水生植物
if not self.map[map_y][map_x][c.MAP_PLANT]: # 只有无植物时才能在水里种植水生植物 if not self.map[map_y][map_x][c.MAP_PLANT]: # 只有无植物时才能在水里种植水生植物
return True return True
@ -71,9 +71,9 @@ class Map():
return False return False
else: # 非水生植物,依赖睡莲 else: # 非水生植物,依赖睡莲
if c.LILYPAD in self.map[map_y][map_x][c.MAP_PLANT]: if c.LILYPAD in self.map[map_y][map_x][c.MAP_PLANT]:
if ((self.map[map_y][map_x][c.MAP_PLANT] | {c.LILYPAD, '花盆(未实现)', '南瓜头(未实现)'} == {c.LILYPAD, '花盆(未实现)', '南瓜头(未实现)'}) if ((self.map[map_y][map_x][c.MAP_PLANT] | {c.LILYPAD, '南瓜头(未实现)'} == {c.LILYPAD, '南瓜头(未实现)'})
and (plantName not in self.map[map_y][map_x][c.MAP_PLANT])): # 例外植物:集合中填花盆和南瓜头,只要这里没有这种植物就能种植;判断方法:并集 and (plantName not in self.map[map_y][map_x][c.MAP_PLANT])): # 例外植物:集合中填花盆和南瓜头,只要这里没有这种植物就能种植;判断方法:并集
if plantName in {c.SPIKEWEED}: # 不能在睡莲上种植的植物 if plantName in {c.SPIKEWEED, '花盆(未实现)'}: # 不能在睡莲上种植的植物
return False return False
else: else:
return True return True
@ -81,6 +81,8 @@ class Map():
return False return False
else: else:
return False return False
else: # 不可种植区域
return False
def getMapIndex(self, x, y): def getMapIndex(self, x, y):
# 引入新地图后需要增加这里的内容 # 引入新地图后需要增加这里的内容

View File

@ -107,6 +107,7 @@ class Bullet(pg.sprite.Sprite):
def draw(self, surface): def draw(self, surface):
surface.blit(self.image, self.rect) surface.blit(self.image, self.rect)
# 暂时是杨桃的子弹,以后可以扩展到非定行飞行的子弹
class StarBullet(Bullet): class StarBullet(Bullet):
def __init__(self, x, y, name): def __init__(self, x, y, name):
self.name = name self.name = name

View File

@ -299,6 +299,7 @@ class Zombie(pg.sprite.Sprite):
def setAttack(self, prey, is_plant=True): def setAttack(self, prey, is_plant=True):
self.prey = prey # prey can be plant or other zombies self.prey = prey # prey can be plant or other zombies
print(self.prey)
self.prey_is_plant = is_plant self.prey_is_plant = is_plant
self.state = c.ATTACK self.state = c.ATTACK
self.attack_timer = self.current_time self.attack_timer = self.current_time

View File

@ -1,4 +1,4 @@
START_LEVEL_NUM = 1 START_LEVEL_NUM = 0
START_LITTLE_GAME_NUM = 1 START_LITTLE_GAME_NUM = 1
ORIGINAL_CAPTION = 'pypvz' ORIGINAL_CAPTION = 'pypvz'
@ -98,6 +98,7 @@ MAP_PLOT_TYPE = 'plotType'
MAP_GRASS = 'grass' MAP_GRASS = 'grass'
MAP_WATER = 'water' MAP_WATER = 'water'
MAP_TILE = 'tile' # 指屋顶上的瓦片 MAP_TILE = 'tile' # 指屋顶上的瓦片
MAP_DAMAGED = 'damaged'
MAP_STATE_EMPTY = {MAP_PLANT:set(), MAP_SLEEP:False, MAP_PLOT_TYPE:MAP_GRASS} # 由于同一格显然不可能种两个相同的植物,所以用集合 MAP_STATE_EMPTY = {MAP_PLANT:set(), MAP_SLEEP:False, MAP_PLOT_TYPE:MAP_GRASS} # 由于同一格显然不可能种两个相同的植物,所以用集合
MAP_STATE_WATER = {MAP_PLANT:set(), MAP_SLEEP:False, MAP_PLOT_TYPE:MAP_WATER} MAP_STATE_WATER = {MAP_PLANT:set(), MAP_SLEEP:False, MAP_PLOT_TYPE:MAP_WATER}
MAP_STATE_TILE = {MAP_PLANT:set(), MAP_SLEEP:False, MAP_PLOT_TYPE:MAP_TILE} MAP_STATE_TILE = {MAP_PLANT:set(), MAP_SLEEP:False, MAP_PLOT_TYPE:MAP_TILE}

View File

@ -185,6 +185,10 @@ class Level(tool.State):
self.hint_image = None self.hint_image = None
self.hint_plant = False self.hint_plant = False
# 种植植物后应当刷新僵尸的攻击对象,当然,默认初始时不用刷新
self.refreshZombieAttack = False
# 0:白天 1:夜晚 2:泳池 3:浓雾 4:屋顶 5:月夜 6:坚果保龄球 # 0:白天 1:夜晚 2:泳池 3:浓雾 4:屋顶 5:月夜 6:坚果保龄球
# 还准备加入 7:单行草皮 8:三行草皮 但是目前没有找到图( # 还准备加入 7:单行草皮 8:三行草皮 但是目前没有找到图(
if self.background_type in {0, 2, 4, 7, 8} and self.bar_type == c.CHOOSEBAR_STATIC: if self.background_type in {0, 2, 4, 7, 8} and self.bar_type == c.CHOOSEBAR_STATIC:
@ -285,6 +289,14 @@ class Level(tool.State):
for i in self.plant_groups[map_y]: for i in self.plant_groups[map_y]:
if (x >= i.rect.x and x <= i.rect.right and if (x >= i.rect.x and x <= i.rect.right and
y >= i.rect.y and y <= i.rect.bottom): y >= i.rect.y and y <= i.rect.bottom):
# 优先移除花盆、睡莲上的植物而非花盆、睡莲本身
if len(self.map.map[map_y][map_x][c.MAP_PLANT]) >= 2:
if c.LILYPAD in self.map.map[map_y][map_x][c.MAP_PLANT]:
if i.name == c.LILYPAD:
continue
elif '花盆(未实现)' in self.map.map[map_y][map_x][c.MAP_PLANT]:
if i.name == '花盆(未实现)':
continue
self.killPlant(i, shovel=True) self.killPlant(i, shovel=True)
# 使用后默认铲子复原 # 使用后默认铲子复原
self.drag_shovel = not self.drag_shovel self.drag_shovel = not self.drag_shovel
@ -526,6 +538,8 @@ class Level(tool.State):
else: else:
mushroomSleep = False mushroomSleep = False
self.plant_groups[map_y].add(new_plant) self.plant_groups[map_y].add(new_plant)
# 种植植物后应当刷新僵尸的攻击对象
self.refreshZombieAttack = True
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)
self.menubar.setCardFrozenTime(self.plant_name) self.menubar.setCardFrozenTime(self.plant_name)
@ -618,6 +632,7 @@ class Level(tool.State):
hypo_zombies = [] hypo_zombies = []
for zombie in self.zombie_groups[i]: for zombie in self.zombie_groups[i]:
if zombie.state != c.WALK: if zombie.state != c.WALK:
if not self.refreshZombieAttack:
continue continue
plant = pg.sprite.spritecollideany(zombie, self.plant_groups[i], collided_func) plant = pg.sprite.spritecollideany(zombie, self.plant_groups[i], collided_func)
if plant: if plant:
@ -630,8 +645,23 @@ class Level(tool.State):
elif plant.name == c.REDWALLNUTBOWLING: elif plant.name == c.REDWALLNUTBOWLING:
if plant.state == c.IDLE: if plant.state == c.IDLE:
plant.setAttack() plant.setAttack()
elif plant.name != c.SPIKEWEED: elif plant.name == c.SPIKEWEED:
continue
# 在睡莲、花盆上有植物时应当优先攻击其上的植物
# 这一段代码目前未能生效
elif plant.name in {c.LILYPAD, '花盆(未实现)'}:
map_x, map_y = self.map.getMapIndex(plant.rect.centerx, plant.rect.bottom)
if len(self.map.map[map_y][map_x][c.MAP_PLANT]) >= 2:
# 这里暂时没有南瓜头优先攻击逻辑,这整个模块都没有对南瓜头的设计
for actualTargetPlant in self.plant_groups[i]:
# 检测同一格的其他植物
if self.map.getMapIndex(actualTargetPlant.rect.centerx, actualTargetPlant.rect.bottom) == (map_x, map_y):
if actualTargetPlant.name != c.LILYPAD:
zombie.setAttack(actualTargetPlant)
continue
zombie.setAttack(plant) zombie.setAttack(plant)
else:
self.refreshZombieAttack = False # 生效后需要解除刷新设置
for hypno_zombie in self.hypno_zombie_groups[i]: for hypno_zombie in self.hypno_zombie_groups[i]:
if hypno_zombie.health <= 0: if hypno_zombie.health <= 0:
@ -846,6 +876,14 @@ class Level(tool.State):
for i in self.plant_groups[map_y]: for i in self.plant_groups[map_y]:
if (x >= i.rect.x and x <= i.rect.right and if (x >= i.rect.x and x <= i.rect.right and
y >= i.rect.y and y <= i.rect.bottom): y >= i.rect.y and y <= i.rect.bottom):
# 优先选中睡莲、花盆上的植物
if len(self.map.map[map_y][map_x][c.MAP_PLANT]) >= 2:
if c.LILYPAD in self.map.map[map_y][map_x][c.MAP_PLANT]:
if i.name == c.LILYPAD:
continue
elif '花盆(未实现)' in self.map.map[map_y][map_x][c.MAP_PLANT]:
if i.name == '花盆(未实现)':
continue
i.highlightTime = self.current_time i.highlightTime = self.current_time
return return