统一越界判断函数

This commit is contained in:
星外之神 2022-08-14 11:03:55 +08:00
parent cf20f9733f
commit 57501a8f22

View File

@ -632,8 +632,7 @@ class Level(tool.State):
def shovelRemovePlant(self, mouse_pos): def shovelRemovePlant(self, mouse_pos):
x, y = mouse_pos x, y = mouse_pos
map_x, map_y = self.map.getMapIndex(x, y) map_x, map_y = self.map.getMapIndex(x, y)
if ((map_y < 0) or (map_y >= self.map_y_len) if not self.map.isValid(map_x, map_y):
or (map_x < 0) or (map_x >= self.map_x_len)):
return return
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
@ -1085,7 +1084,7 @@ class Level(tool.State):
# 默认为最右侧的一个植物 # 默认为最右侧的一个植物
target_plant = max(attackable_common_plants, key=lambda i: i.rect.x) target_plant = max(attackable_common_plants, key=lambda i: i.rect.x)
map_x, map_y = self.map.getMapIndex(target_plant.rect.centerx, target_plant.rect.centery) map_x, map_y = self.map.getMapIndex(target_plant.rect.centerx, target_plant.rect.centery)
if (0 <= map_x < self.map_x_len and 0 <= map_y < self.map_y_len): if self.map.isValid(map_x, map_y):
if c.PUMPKINHEAD in self.map.map[map_y][map_x][c.MAP_PLANT]: if c.PUMPKINHEAD in self.map.map[map_y][map_x][c.MAP_PLANT]:
for actual_target_plant in self.plant_groups[i]: for actual_target_plant in self.plant_groups[i]:
# 检测同一格的其他植物 # 检测同一格的其他植物
@ -1472,8 +1471,7 @@ class Level(tool.State):
# 铲子接近植物时会高亮提示 # 铲子接近植物时会高亮提示
map_x, map_y = self.map.getMapIndex(x, y) map_x, map_y = self.map.getMapIndex(x, y)
surface.blit(self.shovel, self.shovel_rect) surface.blit(self.shovel, self.shovel_rect)
if ((map_y < 0) or (map_y >= self.map_y_len) if not self.map.isValid(map_x, map_y):
or (map_x < 0) or (map_x >= self.map_x_len)):
return return
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