统一变量命名规则

This commit is contained in:
星外之神 2022-08-01 14:44:29 +08:00
parent 172b736e2f
commit ac3f7f56d7
2 changed files with 31 additions and 31 deletions

View File

@ -9,27 +9,27 @@ class Map():
if self.background_type in c.POOL_EQUIPPED_BACKGROUNDS: if self.background_type in c.POOL_EQUIPPED_BACKGROUNDS:
self.width = c.GRID_POOL_X_LEN self.width = c.GRID_POOL_X_LEN
self.height = c.GRID_POOL_Y_LEN self.height = c.GRID_POOL_Y_LEN
self.gridHeightSize = c.GRID_POOL_Y_SIZE self.grid_height_size = c.GRID_POOL_Y_SIZE
self.map = [[(self.initMapGrid(c.MAP_GRASS), self.initMapGrid(c.MAP_WATER))[y in {2, 3}] for x in range(self.width)] for y in range(self.height)] self.map = [[(self.initMapGrid(c.MAP_GRASS), self.initMapGrid(c.MAP_WATER))[y in {2, 3}] for x in range(self.width)] for y in range(self.height)]
elif self.background_type in c.ON_ROOF_BACKGROUNDS: elif self.background_type in c.ON_ROOF_BACKGROUNDS:
self.width = c.GRID_ROOF_X_LEN self.width = c.GRID_ROOF_X_LEN
self.height = c.GRID_ROOF_Y_LEN self.height = c.GRID_ROOF_Y_LEN
self.gridHeightSize = c.GRID_ROOF_Y_SIZE self.grid_height_size = c.GRID_ROOF_Y_SIZE
self.map = [[self.initMapGrid(c.MAP_TILE) for x in range(self.width)] for y in range(self.height)] self.map = [[self.initMapGrid(c.MAP_TILE) for x in range(self.width)] for y in range(self.height)]
elif self.background_type == c.BACKGROUND_SINGLE: elif self.background_type == c.BACKGROUND_SINGLE:
self.width = c.GRID_X_LEN self.width = c.GRID_X_LEN
self.height = c.GRID_Y_LEN self.height = c.GRID_Y_LEN
self.gridHeightSize = c.GRID_Y_SIZE self.grid_height_size = c.GRID_Y_SIZE
self.map = [[(self.initMapGrid(c.MAP_UNAVAILABLE), self.initMapGrid(c.MAP_GRASS))[y == 2] for x in range(self.width)] for y in range(self.height)] self.map = [[(self.initMapGrid(c.MAP_UNAVAILABLE), self.initMapGrid(c.MAP_GRASS))[y == 2] for x in range(self.width)] for y in range(self.height)]
elif self.background_type == c.BACKGROUND_TRIPLE: elif self.background_type == c.BACKGROUND_TRIPLE:
self.width = c.GRID_X_LEN self.width = c.GRID_X_LEN
self.height = c.GRID_Y_LEN self.height = c.GRID_Y_LEN
self.gridHeightSize = c.GRID_Y_SIZE self.grid_height_size = c.GRID_Y_SIZE
self.map = [[(self.initMapGrid(c.MAP_UNAVAILABLE), self.initMapGrid(c.MAP_GRASS))[y in {1, 2, 3}] for x in range(self.width)] for y in range(self.height)] self.map = [[(self.initMapGrid(c.MAP_UNAVAILABLE), self.initMapGrid(c.MAP_GRASS))[y in {1, 2, 3}] for x in range(self.width)] for y in range(self.height)]
else: else:
self.width = c.GRID_X_LEN self.width = c.GRID_X_LEN
self.height = c.GRID_Y_LEN self.height = c.GRID_Y_LEN
self.gridHeightSize = c.GRID_Y_SIZE self.grid_height_size = c.GRID_Y_SIZE
self.map = [[self.initMapGrid(c.MAP_GRASS) for x in range(self.width)] for y in range(self.height)] self.map = [[self.initMapGrid(c.MAP_GRASS) for x in range(self.width)] for y in range(self.height)]
def isValid(self, map_x, map_y): def isValid(self, map_x, map_y):
@ -47,14 +47,14 @@ class Map():
# 判断位置是否可用 # 判断位置是否可用
# 暂时没有写紫卡植物的判断方法 # 暂时没有写紫卡植物的判断方法
# 由于紫卡植物需要移除以前的植物,所以可用另外定义一个函数 # 由于紫卡植物需要移除以前的植物,所以可用另外定义一个函数
def isAvailable(self, map_x, map_y, plantName): def isAvailable(self, map_x, map_y, plant_name):
# 咖啡豆和墓碑吞噬者的判别最为特殊 # 咖啡豆和墓碑吞噬者的判别最为特殊
if plantName == c.COFFEEBEAN: if plant_name == c.COFFEEBEAN:
if self.map[map_y][map_x][c.MAP_SLEEP] and (plantName not in self.map[map_y][map_x][c.MAP_PLANT]): if self.map[map_y][map_x][c.MAP_SLEEP] and (plant_name not in self.map[map_y][map_x][c.MAP_PLANT]):
return True return True
else: else:
return False return False
if plantName == c.GRAVEBUSTER: if plant_name == c.GRAVEBUSTER:
if (c.GRAVE in self.map[map_y][map_x][c.MAP_PLANT]): if (c.GRAVE in self.map[map_y][map_x][c.MAP_PLANT]):
return True return True
else: else:
@ -64,13 +64,13 @@ class Map():
return False return False
if self.map[map_y][map_x][c.MAP_PLOT_TYPE] == c.MAP_GRASS: # 草地 if self.map[map_y][map_x][c.MAP_PLOT_TYPE] == c.MAP_GRASS: # 草地
# 首先需要判断植物是否是水生植物,水生植物不能种植在陆地上 # 首先需要判断植物是否是水生植物,水生植物不能种植在陆地上
if plantName not in c.WATER_PLANTS: if plant_name not in c.WATER_PLANTS:
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
elif (all((i in {"花盆(未实现)", c.PUMPKINHEAD}) for i in self.map[map_y][map_x][c.MAP_PLANT]) elif (all((i in {"花盆(未实现)", c.PUMPKINHEAD}) for i in self.map[map_y][map_x][c.MAP_PLANT])
and (plantName not in self.map[map_y][map_x][c.MAP_PLANT])): # 例外植物:集合中填花盆和南瓜头,只要这里没有这种植物就能种植 and (plant_name not in self.map[map_y][map_x][c.MAP_PLANT])): # 例外植物:集合中填花盆和南瓜头,只要这里没有这种植物就能种植
return True return True
elif (plantName == c.PUMPKINHEAD) and (c.PUMPKINHEAD not in self.map[map_y][map_x][c.MAP_PLANT]): # 没有南瓜头就能种南瓜头 elif (plant_name == c.PUMPKINHEAD) and (c.PUMPKINHEAD not in self.map[map_y][map_x][c.MAP_PLANT]): # 没有南瓜头就能种南瓜头
return True return True
else: else:
return False return False
@ -78,26 +78,26 @@ class Map():
return False return False
elif self.map[map_y][map_x][c.MAP_PLOT_TYPE] == c.MAP_TILE: # 屋顶 elif self.map[map_y][map_x][c.MAP_PLOT_TYPE] == c.MAP_TILE: # 屋顶
# 首先需要判断植物是否是水生植物,水生植物不能种植在陆地上 # 首先需要判断植物是否是水生植物,水生植物不能种植在陆地上
if plantName not in c.WATER_PLANTS: if plant_name not in c.WATER_PLANTS:
if "花盆(未实现)" in self.map[map_y][map_x][c.MAP_PLANT]: if "花盆(未实现)" in self.map[map_y][map_x][c.MAP_PLANT]:
if (all((i in {"花盆(未实现)", c.PUMPKINHEAD}) for i in self.map[map_y][map_x][c.MAP_PLANT]) if (all((i in {"花盆(未实现)", c.PUMPKINHEAD}) for i in self.map[map_y][map_x][c.MAP_PLANT])
and (plantName not in self.map[map_y][map_x][c.MAP_PLANT])): # 例外植物:集合中填花盆和南瓜头,只要这里没有这种植物就能种植 and (plant_name not in self.map[map_y][map_x][c.MAP_PLANT])): # 例外植物:集合中填花盆和南瓜头,只要这里没有这种植物就能种植
if plantName in {c.SPIKEWEED}: # 不能在花盆上种植的植物 if plant_name in {c.SPIKEWEED}: # 不能在花盆上种植的植物
return False return False
else: else:
return True return True
elif (plantName == c.PUMPKINHEAD) and (c.PUMPKINHEAD not in self.map[map_y][map_x][c.MAP_PLANT]): # 有花盆且没有南瓜头就能种南瓜头 elif (plant_name == c.PUMPKINHEAD) and (c.PUMPKINHEAD not in self.map[map_y][map_x][c.MAP_PLANT]): # 有花盆且没有南瓜头就能种南瓜头
return True return True
else: else:
return False return False
elif plantName == "花盆(未实现)": # 这一格本来没有花盆而且新来的植物是花盆,可以种 elif plant_name == "花盆(未实现)": # 这一格本来没有花盆而且新来的植物是花盆,可以种
return True return True
else: else:
return False return False
else: else:
return False return False
elif self.map[map_y][map_x][c.MAP_PLOT_TYPE] == c.MAP_WATER: # 水里 elif self.map[map_y][map_x][c.MAP_PLOT_TYPE] == c.MAP_WATER: # 水里
if plantName in c.WATER_PLANTS: # 是水生植物 if plant_name in c.WATER_PLANTS: # 是水生植物
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
else: else:
@ -105,12 +105,12 @@ class Map():
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 (all((i in {c.LILYPAD, c.PUMPKINHEAD}) for i in self.map[map_y][map_x][c.MAP_PLANT]) if (all((i in {c.LILYPAD, c.PUMPKINHEAD}) for i in self.map[map_y][map_x][c.MAP_PLANT])
and (plantName not in self.map[map_y][map_x][c.MAP_PLANT])): and (plant_name not in self.map[map_y][map_x][c.MAP_PLANT])):
if plantName in {c.SPIKEWEED, c.POTATOMINE, "花盆(未实现)"}: # 不能在睡莲上种植的植物 if plant_name in {c.SPIKEWEED, c.POTATOMINE, "花盆(未实现)"}: # 不能在睡莲上种植的植物
return False return False
else: else:
return True return True
elif (plantName == c.PUMPKINHEAD) and (c.PUMPKINHEAD not in self.map[map_y][map_x][c.MAP_PLANT]): # 在睡莲上且没有南瓜头就能种南瓜头 elif (plant_name == c.PUMPKINHEAD) and (c.PUMPKINHEAD not in self.map[map_y][map_x][c.MAP_PLANT]): # 在睡莲上且没有南瓜头就能种南瓜头
return True return True
else: else:
return False return False
@ -152,22 +152,22 @@ class Map():
def setMapGridType(self, map_x, map_y, plot_type): def setMapGridType(self, map_x, map_y, plot_type):
self.map[map_y][map_x][c.MAP_PLOT_TYPE] = plot_type self.map[map_y][map_x][c.MAP_PLOT_TYPE] = plot_type
def addMapPlant(self, map_x, map_y, plantName, sleep=False): def addMapPlant(self, map_x, map_y, plant_name, sleep=False):
self.map[map_y][map_x][c.MAP_PLANT].add(plantName) self.map[map_y][map_x][c.MAP_PLANT].add(plant_name)
self.map[map_y][map_x][c.MAP_SLEEP] = sleep 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, plant_name):
self.map[map_y][map_x][c.MAP_PLANT].discard(plantName) self.map[map_y][map_x][c.MAP_PLANT].discard(plant_name)
def getRandomMapIndex(self): def getRandomMapIndex(self):
map_x = random.randint(0, self.width-1) map_x = random.randint(0, self.width-1)
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 checkPlantToSeed(self, x, y, plantName): def checkPlantToSeed(self, x, y, plant_name):
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.isAvailable(map_x, map_y, plantName): if self.isValid(map_x, map_y) and self.isAvailable(map_x, map_y, plant_name):
pos = self.getMapGridPos(map_x, map_y) pos = self.getMapGridPos(map_x, map_y)
return pos return pos

View File

@ -841,9 +841,9 @@ class Level(tool.State):
# 能否种植物的判断: # 能否种植物的判断:
# 先判断位置是否合法 isValid(map_x, map_y) # 先判断位置是否合法 isValid(map_x, map_y)
# 再判断位置是否可用 isMovable(map_x, map_y) # 再判断位置是否可用 isMovable(map_x, map_y)
def canSeedPlant(self, plantName): def canSeedPlant(self, plant_name):
x, y = pg.mouse.get_pos() x, y = pg.mouse.get_pos()
return self.map.checkPlantToSeed(x, y, plantName) return self.map.checkPlantToSeed(x, y, plant_name)
# 种植物 # 种植物
def addPlant(self): def addPlant(self):
@ -913,7 +913,7 @@ class Level(tool.State):
elif self.plant_name == c.TANGLEKLEP: elif self.plant_name == c.TANGLEKLEP:
new_plant = plant.TangleKlep(x, y) new_plant = plant.TangleKlep(x, y)
elif self.plant_name == c.DOOMSHROOM: elif self.plant_name == c.DOOMSHROOM:
if self.map.gridHeightSize == c.GRID_Y_SIZE: if self.map.grid_height_size == c.GRID_Y_SIZE:
new_plant = plant.DoomShroom(x, y, self.map.map[map_y][map_x][c.MAP_PLANT], explode_y_range=2) new_plant = plant.DoomShroom(x, y, self.map.map[map_y][map_x][c.MAP_PLANT], explode_y_range=2)
else: else:
new_plant = plant.DoomShroom(x, y, self.map.map[map_y][map_x][c.MAP_PLANT], explode_y_range=3) new_plant = plant.DoomShroom(x, y, self.map.map[map_y][map_x][c.MAP_PLANT], explode_y_range=3)
@ -1150,7 +1150,7 @@ class Level(tool.State):
if self.map.map[i][0][c.MAP_PLOT_TYPE] != self.map.map[i + _move][0][c.MAP_PLOT_TYPE]: if self.map.map[i][0][c.MAP_PLOT_TYPE] != self.map.map[i + _move][0][c.MAP_PLOT_TYPE]:
_move = -(_move) _move = -(_move)
zombie.target_map_y = i + _move zombie.target_map_y = i + _move
zombie.target_y_change = _move * self.map.gridHeightSize zombie.target_y_change = _move * self.map.grid_height_size
else: else:
zombie.setAttack(target_plant) zombie.setAttack(target_plant)