From cdf992095e8f5637dff7561552c274baec80e357 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=98=9F=E5=A4=96=E4=B9=8B=E7=A5=9E?= Date: Fri, 27 May 2022 15:09:53 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A4=A7=E8=92=9C=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 1 + source/component/map.py | 5 +++++ source/component/menubar.py | 4 ++++ source/component/zombie.py | 44 ++++++++++++++++++++++++++++++++++--- source/constants.py | 4 ++-- source/state/level.py | 17 +++++++++++++- 6 files changed, 69 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index aa6ba72..6cafb79 100644 --- a/README.md +++ b/README.md @@ -49,6 +49,7 @@ python main.py - 使用MSVC编译 - 每次提交均会更新,保证更新及时 - 未进行任何测试,存在bug的概率高于前者 + - 可看作本软件的测试版 - 还可以下载GitHub Workflow[自动利用Pyinstaller构建的版本(点击跳转)](https://github.com/wszqkzqk/pypvz/releases/tag/Current.Version.Built.with.Pyinstaller): - 在程序闪退时有报错窗口弹出 - 程序性能较差,不推荐 diff --git a/source/component/map.py b/source/component/map.py index 8fbb0ca..ba5884e 100755 --- a/source/component/map.py +++ b/source/component/map.py @@ -12,22 +12,27 @@ class Map(): if self.background_type in c.POOL_EQUIPPED_BACKGROUNDS: self.width = c.GRID_POOL_X_LEN self.height = c.GRID_POOL_Y_LEN + self.gridHeightSize = c.GRID_POOL_Y_SIZE self.map = [[(deepcopy(c.MAP_STATE_EMPTY), deepcopy(c.MAP_STATE_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: self.width = c.GRID_ROOF_X_LEN self.height = c.GRID_ROOF_Y_LEN + self.gridHeightSize = c.GRID_ROOF_Y_SIZE self.map = [[deepcopy(c.MAP_STATE_TILE) for x in range(self.width)] for y in range(self.height)] elif self.background_type == c.BACKGROUND_SINGLE: self.width = c.GRID_X_LEN self.height = c.GRID_Y_LEN + self.gridHeightSize = c.GRID_Y_SIZE self.map = [[(deepcopy(c.MAP_STATE_UNAVAILABLE), deepcopy(c.MAP_STATE_EMPTY))[y == 2] for x in range(self.width)] for y in range(self.height)] elif self.background_type == c.BACKGROUND_TRIPLE: self.width = c.GRID_X_LEN self.height = c.GRID_Y_LEN + self.gridHeightSize = c.GRID_Y_SIZE self.map = [[(deepcopy(c.MAP_STATE_UNAVAILABLE), deepcopy(c.MAP_STATE_EMPTY))[y in {1, 2, 3}] for x in range(self.width)] for y in range(self.height)] else: self.width = c.GRID_X_LEN self.height = c.GRID_Y_LEN + self.gridHeightSize = c.GRID_Y_SIZE self.map = [[deepcopy(c.MAP_STATE_EMPTY) for x in range(self.width)] for y in range(self.height)] def isValid(self, map_x, map_y): diff --git a/source/component/menubar.py b/source/component/menubar.py index fd0ced8..b82b049 100755 --- a/source/component/menubar.py +++ b/source/component/menubar.py @@ -113,6 +113,10 @@ plantInfo = (# 元组 (植物名称, 卡片名称, 阳光, 冷却时间) c.CARD_COFFEEBEAN, 75, 7500), + (c.GARLIC, + c.CARD_GARLIC, + 50, + 7500), # 应当保证这两个在一般模式下不可选的特殊植物恒在最后 (c.WALLNUTBOWLING, c.CARD_WALLNUT, diff --git a/source/component/zombie.py b/source/component/zombie.py index 3e44fe4..7e91988 100755 --- a/source/component/zombie.py +++ b/source/component/zombie.py @@ -20,6 +20,10 @@ class Zombie(pg.sprite.Sprite): self.rect = self.image.get_rect() self.rect.x = x self.rect.bottom = y + # 大蒜换行移动像素值,< 0时向上,= 0时不变,> 0时向上 + self.targetYChange = 0 + self.originalY = y + self.toChangeGroup = False self.helmetHealth = helmetHealth self.helmetType2Health = helmetType2Health @@ -191,13 +195,45 @@ class Zombie(pg.sprite.Sprite): if self.helmetType2Health <= 0 and self.helmetType2: self.changeFrames(self.walk_frames) self.helmetType2 = False - if (self.current_time - self.walk_timer) > (c.ZOMBIE_WALK_INTERVAL * self.getTimeRatio()): + + if ((self.current_time - self.walk_timer) > (c.ZOMBIE_WALK_INTERVAL * self.getTimeRatio()) + and self.handleGarlicYChange()): self.walk_timer = self.current_time if self.is_hypno: self.rect.x += 1 else: self.rect.x -= 1 + def handleGarlicYChange(self): + if self.targetYChange < 0: + self.setWalk() + if self.rect.bottom > self.originalY + self.targetYChange: # 注意这里加的是负数 + self.rect.bottom -= 3 + # 过半时换行 + if ((self.toChangeGroup) and + (self.rect.bottom >= self.originalY + 0.5*self.targetYChange)): + self.level.zombie_groups[self.mapY].remove(self) + self.level.zombie_groups[self.targetMapY].add(self) + else: + self.rect.bottom = self.originalY + self.targetYChange + self.targetYChange = 0 + return None + elif self.targetYChange > 0: + self.setWalk() + if self.rect.bottom < self.originalY + self.targetYChange: # 注意这里加的是负数 + self.rect.bottom += 3 + # 过半时换行 + if ((self.toChangeGroup) and + (self.rect.bottom <= self.originalY + 0.5*self.targetYChange)): + self.level.zombie_groups[self.mapY].remove(self) + self.level.zombie_groups[self.targetMapY].add(self) + else: + self.rect.bottom = self.originalY + self.targetYChange + self.targetYChange = 0 + return None + else: + return True + def attacking(self): if self.checkToDie(self.losthead_attack_frames): return @@ -676,7 +712,8 @@ class NewspaperZombie(Zombie): self.helmetType2 = False # 触发报纸撕裂音效 pg.mixer.Sound(os.path.join(os.path.dirname(os.path.dirname(os.path.dirname(__file__))) ,"resources", "sound", "newspaperRip.ogg")).play() - if (self.current_time - self.walk_timer) > (c.ZOMBIE_WALK_INTERVAL * self.getTimeRatio()): + if ((self.current_time - self.walk_timer) > (c.ZOMBIE_WALK_INTERVAL * self.getTimeRatio()) + and self.handleGarlicYChange()): self.walk_timer = self.current_time if self.frames == self.lostnewspaper_frames: pass @@ -1158,7 +1195,8 @@ class SnorkelZombie(Zombie): if self.swimming: self.changeFrames(self.walk_frames) self.swimming = False - if (self.current_time - self.walk_timer) > (c.ZOMBIE_WALK_INTERVAL * self.getTimeRatio()): + if ((self.current_time - self.walk_timer) > (c.ZOMBIE_WALK_INTERVAL * self.getTimeRatio()) + and self.handleGarlicYChange()): self.walk_timer = self.current_time # 正在上浮或者下潜不用移动 if (self.frames == self.float_frames) or (self.frames == self.sink_frames): diff --git a/source/constants.py b/source/constants.py index 6515db8..fd541c8 100755 --- a/source/constants.py +++ b/source/constants.py @@ -1,5 +1,5 @@ # 冒险模式起始关卡 -START_LEVEL_NUM = 1 +START_LEVEL_NUM = 0 # 小游戏模式起始关卡 START_LITTLE_GAME_NUM = 1 @@ -23,7 +23,7 @@ SCREEN_SIZE = (SCREEN_WIDTH, SCREEN_HEIGHT) # 最大数量 CARD_MAX_NUM = 10 # 这里以后可以增加解锁功能,从最初的6格逐渐解锁到10格 # 最小数量 -CARD_LIST_NUM = CARD_MAX_NUM +CARD_LIST_NUM = 0#CARD_MAX_NUM # 方格数据 # 一般 diff --git a/source/state/level.py b/source/state/level.py index 83d3c6b..e4e5c91 100644 --- a/source/state/level.py +++ b/source/state/level.py @@ -1040,7 +1040,6 @@ class Level(tool.State): # 被攻击对象是植物时才可能刷新 if zombie.prey_is_plant: # 新植物种在被攻击植物同一格时才可能刷新 - mapX, mapY = self.map.getMapIndex(zombie.prey.rect.centerx, zombie.prey.rect.centery) if (mapX, mapY) == self.newPlantAndPositon[1]: # 如果被攻击植物是睡莲和花盆,同一格种了植物必然刷新 # 如果被攻击植物不是睡莲和花盆,同一格种了南瓜头才刷新 @@ -1128,6 +1127,22 @@ class Level(tool.State): elif targetPlant.name == c.REDWALLNUTBOWLING: if targetPlant.state == c.IDLE: targetPlant.setAttack() + elif targetPlant.name == c.GARLIC: + zombie.setAttack(targetPlant) + # 向吃过大蒜的僵尸传入level + zombie.level = self + zombie.toChangeGroup = True + zombie.mapY = i + if i == 0: + _move = 1 + elif i == self.map_y_len - 1: + _move = -1 + else: + _move = randint(0, 1)*2 - 1 + if self.map.map[i][0][c.MAP_PLOT_TYPE] != self.map.map[i + _move][0][c.MAP_PLOT_TYPE]: + _move = -_move + zombie.targetMapY = i + _move + zombie.targetYChange = _move * self.map.gridHeightSize else: zombie.setAttack(targetPlant)