diff --git a/resources/data/map/level_4.json b/resources/data/map/level_4.json new file mode 100644 index 0000000..edcd955 --- /dev/null +++ b/resources/data/map/level_4.json @@ -0,0 +1,21 @@ +{ + "background_type":2, + "init_sun_value":50, + "shovel":1, + "zombie_list":[ + {"time":20000, "name":"Zombie"}, + {"time":40000, "name":"ConeheadZombie"}, + {"time":50000, "name":"BucketheadZombie"}, + {"time":70000, "name":"BucketheadZombie"}, + {"time":72000, "name":"FlagZombie"}, + {"time":74000, "name":"ConeheadZombie"}, + {"time":90000, "name":"BucketheadZombie"}, + {"time":91000, "name":"ConeheadZombie"}, + {"time":92000, "name":"Zombie"}, + {"time":93000, "name":"BucketheadZombie"}, + {"time":94000, "name":"Zombie"}, + {"time":95000, "name":"FlagZombie"}, + {"time":96000, "name":"BucketheadZombie"}, + {"time":97000, "name":"FlagZombie"} + ] +} diff --git a/resources/music/poolLevel.opus b/resources/music/poolLevel.opus new file mode 100644 index 0000000..82ccedc Binary files /dev/null and b/resources/music/poolLevel.opus differ diff --git a/source/component/map.py b/source/component/map.py index 0366b3f..93bf6ba 100755 --- a/source/component/map.py +++ b/source/component/map.py @@ -4,12 +4,20 @@ from .. import tool from .. import constants as c class Map(): - def __init__(self, width, height): - self.width = width - self.height = height + def __init__(self, background_type): + self.background_type = background_type + if self.background_type in {c.BACKGROUND_POOL, c.BACKGROUND_FOG}: + self.width = c.GRID_POOL_X_LEN + self.height = c.GRID_POOL_Y_LEN + elif self.background_type in {c.BACKGROUND_ROOF, c.BACKGROUND_ROOFNIGHT}: + self.width = c.GRID_ROOF_X_LEN + self.height = c.GRID_ROOF_Y_LEN + else: + self.width = c.GRID_X_LEN + self.height = c.GRID_Y_LEN # 要把记录信息改成元组的话这里又得改 # 而且不同场地还不一样 - self.map = [[c.MAP_EMPTY for x in range(self.width)] for y in range(self.height)] + self.map = [[c.MAP_STATE_EMPTY for x in range(self.width)] for y in range(self.height)] def isValid(self, map_x, map_y): if (map_x < 0 or map_x >= self.width or @@ -25,17 +33,38 @@ class Map(): # 当然,不用元组的话字符串也行,但是得把判断植物写在母函数中,并且需要更多参数 # 这样返回的就是一个具体信息,而非bool值了 # 到时候还要改一下变量名,还叫isMovable不合适 - return (self.map[map_y][map_x] == c.MAP_EMPTY) + return (self.map[map_y][map_x] == c.MAP_STATE_EMPTY) def getMapIndex(self, x, y): # 引入新地图后需要增加这里的内容 - x -= c.MAP_OFFSET_X - y -= c.MAP_OFFSET_Y - return (x // c.GRID_X_SIZE, y // c.GRID_Y_SIZE) + if self.background_type in {c.BACKGROUND_POOL, c.BACKGROUND_FOG}: + x -= c.MAP_POOL_OFFSET_X + y -= c.MAP_POOL_OFFSET_Y + return (x // c.GIRD_POOL_X_SIZE, y // c.GRID_POOL_Y_SIZE) + elif self.background_type in {c.BACKGROUND_ROOF, c.BACKGROUND_ROOFNIGHT}: + x -= c.MAP_ROOF_OFFSET_X + y -= c.MAP_ROOF_OFFSET_X + girdX = x // c.GRID_ROOF_X_SIZE + if girdX >= 5: + gridY = y // c.GRID_ROOF_Y_SIZE + else: + gridY = (y - 20*(6 - girdX)) // 85 + return (girdX, gridY) + else: + x -= c.MAP_OFFSET_X + y -= c.MAP_OFFSET_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) + if self.background_type in {c.BACKGROUND_POOL, c.BACKGROUND_FOG}: + return (map_x * c.GRID_ROOF_X_SIZE + c.GRID_X_SIZE//2 + c.MAP_ROOF_OFFSET_X, + map_y * c.GRID_ROOF_Y_SIZE + c.GRID_Y_SIZE//5 * 3 + c.MAP_ROOF_OFFSET_Y) + elif self.background_type in {c.BACKGROUND_ROOF, c.BACKGROUND_ROOFNIGHT}: + return (map_x * c.GRID_POOL_X_SIZE + c.GRID_X_SIZE//2 + c.MAP_POOL_OFFSET_X, + map_y * c.GRID_POOL_Y_SIZE + 20 * max(0, (6 - map_y)) + c.GRID_Y_SIZE//5 * 3 + c.MAP_POOL_OFFSET_Y) + else: + 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 diff --git a/source/constants.py b/source/constants.py index 79ee618..c7ce216 100755 --- a/source/constants.py +++ b/source/constants.py @@ -1,16 +1,31 @@ -START_LEVEL_NUM = 1 +START_LEVEL_NUM = 4 START_LITTLE_GAME_NUM = 1 ORIGINAL_CAPTION = 'pypvz' +# 游戏模式 +MODE_ADVENTURE = 'adventure' +MODE_LITTLEGAME = 'littleGame' + SCREEN_WIDTH = 800 SCREEN_HEIGHT = 600 SCREEN_SIZE = (SCREEN_WIDTH, SCREEN_HEIGHT) +# 一般 GRID_X_LEN = 9 GRID_Y_LEN = 5 GRID_X_SIZE = 80 GRID_Y_SIZE = 100 +# 带有泳池 +GRID_POOL_X_LEN = GRID_X_LEN +GRID_POOL_Y_LEN = 6 +GIRD_POOL_X_SIZE = GRID_X_SIZE +GRID_POOL_Y_SIZE = 85 +# 屋顶 +GRID_ROOF_X_LEN = GRID_X_LEN +GRID_ROOF_Y_LEN = GRID_Y_LEN +GRID_ROOF_X_SIZE = GRID_X_SIZE +GRID_ROOF_Y_SIZE = 85 # 游戏速度倍率(调试用) GAME_RATE = 1 @@ -66,7 +81,19 @@ BACKGROUND_TYPE = 'background_type' INIT_SUN_NAME = 'init_sun_value' ZOMBIE_LIST = 'zombie_list' -MAP_EMPTY = 0 +#BACKGROUND +BACKGROUND_DAY = 0 +BACKGROUND_NIGHT = 1 +BACKGROUND_POOL = 2 +BACKGROUND_FOG = 3 +BACKGROUND_ROOF = 4 +BACKGROUND_ROOFNIGHT = 5 +BACKGROUND_WALLNUTBOWLING = 6 +BACKGROUND_SINGLE = 7 +BACKGROUND_TRIPLE = 8 + + +MAP_STATE_EMPTY = 0 MAP_COMMON_PLANT = 1 # 只有南瓜头 MAP_PUMPKIN_ONLY = 2 @@ -79,6 +106,10 @@ MAP_PUMPKIN_WITH = 3 BACKGROUND_OFFSET_X = 220 MAP_OFFSET_X = 35 MAP_OFFSET_Y = 100 +MAP_POOL_OFFSET_X = 35 # 暂时还不清楚数据 +MAP_POOL_OFFSET_Y = 115 # 暂时还不清楚数据 +MAP_ROOF_OFFSET_X = 35 # 暂时还不清楚数据 +MAP_ROOF_OFFSET_Y = 105 # 暂时还不清楚数据 #MENUBAR CHOOSEBAR_TYPE = 'choosebar_type' @@ -208,11 +239,3 @@ SLEEP = 'sleep' #LEVEL STATE CHOOSE = 'choose' PLAY = 'play' - -#BACKGROUND -BACKGROUND_DAY = 0 -BACKGROUND_NIGHT = 1 -BACKGROUND_POOL = 2 -BACKGROUND_FOG = 3 -BACKGROUND_ROOF = 4 -BACKGROUND_ROOFNIGHT = 5 \ No newline at end of file diff --git a/source/state/level.py b/source/state/level.py index ecc48fe..75fb824 100644 --- a/source/state/level.py +++ b/source/state/level.py @@ -15,9 +15,6 @@ class Level(tool.State): self.game_info = persist self.persist = self.game_info self.game_info[c.CURRENT_TIME] = current_time - self.map_y_len = c.GRID_Y_LEN - # 可以给map加一个地图类型参数 - self.map = map.Map(c.GRID_X_LEN, self.map_y_len) # 暂停状态 self.pause = False @@ -30,14 +27,17 @@ class Level(tool.State): self.setupBackground() self.initState() + # 可以给map加一个地图类型参数 + self.map = map.Map(self.map_data[c.BACKGROUND_TYPE]) + self.map_y_len = self.map.height + def loadMap(self): - modeList = ['adventure', 'littleGame'] if c.LITTLEGAME_BUTTON in self.game_info and self.game_info[c.LITTLEGAME_BUTTON]: map_file = 'littleGame_' + str(self.game_info[c.LITTLEGAME_NUM]) + '.json' - mode = 'littleGame' + self.mode = c.MODE_LITTLEGAME else: map_file = 'level_' + str(self.game_info[c.LEVEL_NUM]) + '.json' - mode = 'adventure' + self.mode = c.MODE_ADVENTURE file_path = os.path.join(os.path.dirname(os.path.dirname(os.path.dirname(__file__))),'resources' , 'data', 'map', map_file) # 最后一关之后应该结束了 try: @@ -46,12 +46,15 @@ class Level(tool.State): f.close() except Exception as e: print("游戏结束") + if self.mode == c.MODE_ADVENTURE: + self.game_info[c.LEVEL_NUM] = c.START_LEVEL_NUM + elif self.mode == c.MODE_LITTLEGAME: + self.game_info[c.LITTLEGAME_NUM] = c.START_LITTLE_GAME_NUM self.done = True self.next = c.MAIN_MENU pg.mixer.music.stop() pg.mixer.music.load(os.path.join(os.path.dirname(os.path.dirname(os.path.dirname(__file__))) ,"resources", "music", "intro.opus")) pg.mixer.music.play(-1, 0) - # 这里应该设置有复原状态的操作,避免完成一轮后无法再玩 return if self.map_data[c.SHOVEL] == 0: self.hasShovel = False @@ -60,12 +63,14 @@ class Level(tool.State): # 同时播放音乐 global bgm - if mode == modeList[0]: # 冒险模式 + if self.mode == c.MODE_ADVENTURE: # 冒险模式 if self.game_info[c.LEVEL_NUM] in {0, 1, 2}: # 白天关卡 bgm = 'dayLevel.opus' elif self.game_info[c.LEVEL_NUM] in {3}: # 夜晚关卡 bgm = 'nightLevel.opus' - elif mode == modeList[1]: # 小游戏模式 + elif self.game_info[c.LEVEL_NUM] in {4}: + bgm = 'poolLevel.opus' + elif self.mode == c.MODE_LITTLEGAME: # 小游戏模式 if self.game_info[c.LITTLEGAME_NUM] in {1}: # 传送带大战 bgm = 'battle.opus' elif self.game_info[c.LITTLEGAME_NUM] in {2}: # 坚果保龄球 @@ -183,7 +188,7 @@ class Level(tool.State): self.hint_plant = False # 0:白天 1:夜晚 2:泳池 3:浓雾 4:屋顶 5:月夜 6:坚果保龄球 # 还准备加入 7:单行草皮 8:三行草皮 但是目前没有找到图( - if self.background_type in {0, 2, 4, -1, -2} and self.bar_type == c.CHOOSEBAR_STATIC: + if self.background_type in {0, 2, 4, 7, 8} and self.bar_type == c.CHOOSEBAR_STATIC: self.produce_sun = True else: self.produce_sun = False @@ -458,7 +463,7 @@ class Level(tool.State): # 调用self.map.showPlant(x, y) # 先判断位置是否合法 isValid(map_x, map_y) # 再判断位置是否可用 isMovable(map_x, map_y) - # 因为现在还没有做南瓜头,所以目前判断的是map[map_y][map_x]是否为空(c.MAP_EMPTY,即0) + # 因为现在还没有做南瓜头,所以目前判断的是map[map_y][map_x]是否为空(c.MAP_STATE_EMPTY,即0) # 写了南瓜头需要改这个验证 def canSeedPlant(self): x, y = pg.mouse.get_pos() @@ -515,7 +520,7 @@ class Level(tool.State): elif self.plant_name == c.REDWALLNUTBOWLING: new_plant = plant.RedWallNutBowling(x, y) - if new_plant.can_sleep and self.background_type == c.BACKGROUND_DAY: + 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() self.plant_groups[map_y].add(new_plant) if self.bar_type == c.CHOOSEBAR_STATIC: @@ -666,7 +671,7 @@ class Level(tool.State): map_x, map_y = self.map.getMapIndex(x, y) if self.bar_type != c.CHOSSEBAR_BOWLING: # 更改地图类型、添加南瓜头、睡莲、花盆后可能也需要改这里 - self.map.setMapGridType(map_x, map_y, c.MAP_EMPTY) + self.map.setMapGridType(map_x, map_y, c.MAP_STATE_EMPTY) # 用铲子铲不用触发植物功能 if not shovel: if (plant.name == c.CHERRYBOMB or plant.name == c.JALAPENO or