From 5e44207c51e98b57435b1499e1adb6eb08246785 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, 3 Jun 2022 23:19:02 +0800 Subject: [PATCH 01/25] =?UTF-8?q?=E5=88=A0=E9=99=A4=E7=8E=B0=E5=9C=A8?= =?UTF-8?q?=E6=B2=A1=E4=BB=80=E4=B9=88=E7=94=A8=E7=9A=84=E5=86=85=E5=AE=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 1 - 1 file changed, 1 deletion(-) diff --git a/README.md b/README.md index 93fc2d9..7a165da 100644 --- a/README.md +++ b/README.md @@ -44,7 +44,6 @@ python main.py - 程序包含名称、版本等信息 - 得到的验证最多(相对) - 并非每次提交都会更新,更新可能不及时 - - `0.7.25.0`之后的部分版本被标注成了`pre-release`,实际上仍然为普通版本,按需下载即可 - 也可以直接下载GitHub Workflow[自动利用Nuitka构建的版本(点击跳转)](https://github.com/wszqkzqk/pypvz/releases/tag/Latest)(推荐): - 使用MSVC编译 - 每次提交均会更新,保证更新及时 From 1d1f6558ceb7e76b474df16c82648d87c416f68f 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, 3 Jun 2022 23:37:02 +0800 Subject: [PATCH 02/25] =?UTF-8?q?readme=E4=B8=AD=E5=8A=A0=E5=85=A5?= =?UTF-8?q?=E5=A4=A7=E8=92=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 7a165da..0be1e5e 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ **本项目为个人python语言学习的练习项目,仅供个人学习和研究使用,不得用于其他用途。如果这个游戏侵犯了版权,请联系我删除** -* 已有的植物:向日葵,豌豆射手,坚果墙,寒冰射手,樱桃炸弹,双发射手,三线射手,大嘴花,小喷菇,土豆雷,地刺,胆小菇,倭瓜,火爆辣椒,阳光菇,寒冰菇,魅惑菇,火炬树桩,睡莲,杨桃,咖啡豆,海蘑菇,高坚果,缠绕水草,毁灭菇,墓碑吞噬者,大喷菇 +* 已有的植物:向日葵,豌豆射手,坚果墙,寒冰射手,樱桃炸弹,双发射手,三线射手,大嘴花,小喷菇,土豆雷,地刺,胆小菇,倭瓜,火爆辣椒,阳光菇,寒冰菇,魅惑菇,火炬树桩,睡莲,杨桃,咖啡豆,海蘑菇,高坚果,缠绕水草,毁灭菇,墓碑吞噬者,大喷菇,大蒜 * 已有的僵尸:普通僵尸,旗帜僵尸,路障僵尸,铁桶僵尸,读报僵尸,橄榄球僵尸,鸭子救生圈僵尸,铁门僵尸,撑杆跳僵尸,冰车僵尸,潜水僵尸 * 使用 JSON 文件记录关卡信息数据 * 支持选择植物卡片 From 1df7d937f4ad889c385ca97198346b44c8756801 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=98=9F=E5=A4=96=E4=B9=8B=E7=A5=9E?= Date: Sat, 4 Jun 2022 13:51:58 +0800 Subject: [PATCH 03/25] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E5=A4=87=E6=B3=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index 61ff533..ebfb375 100644 --- a/.gitignore +++ b/.gitignore @@ -1,8 +1,9 @@ -# ignore debug +# 忽略构建内容 out/ build/ +# 忽略调试内容 .vscode/ __pycache__/ */__pycache__/ -# ignore test +# 忽略测试文件 test.py From dd7c81f45f652a2df4780ccce126910e09641ac6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=98=9F=E5=A4=96=E4=B9=8B=E7=A5=9E?= Date: Sat, 4 Jun 2022 14:07:33 +0800 Subject: [PATCH 04/25] =?UTF-8?q?=E7=BB=9F=E4=B8=80=E5=BD=A2=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- source/component/plant.py | 2 +- source/component/zombie.py | 4 ++-- source/constants.py | 15 +++++++++------ source/state/level.py | 6 +++--- 4 files changed, 15 insertions(+), 12 deletions(-) diff --git a/source/component/plant.py b/source/component/plant.py index 7d91866..c4e009e 100755 --- a/source/component/plant.py +++ b/source/component/plant.py @@ -1770,7 +1770,7 @@ class FumeShroom(Plant): class IceFrozenPlot(Plant): def __init__(self, x, y): - Plant.__init__(self, x, y, c.ICE_FROZEN_PLOT, c.INF, None) + Plant.__init__(self, x, y, c.ICEFROZENPLOT, c.INF, None) self.timer = 0 def idling(self): diff --git a/source/component/zombie.py b/source/component/zombie.py index 63fe4e1..1e6c517 100755 --- a/source/component/zombie.py +++ b/source/component/zombie.py @@ -1116,10 +1116,10 @@ class Zomboni(Zombie): # 造冰 mapX, mapY = self.map.getMapIndex(self.rect.right - 40, self.rect.bottom) if 0 <= mapX < c.GRID_X_LEN: - if c.ICE_FROZEN_PLOT not in self.map.map[mapY][mapX]: + if c.ICEFROZENPLOT not in self.map.map[mapY][mapX]: x, y = self.map.getMapGridPos(mapX, mapY) self.plant_group.add(self.IceFrozenPlot(x, y)) - self.map.map[mapY][mapX][c.MAP_PLANT].add(c.ICE_FROZEN_PLOT) + self.map.map[mapY][mapX][c.MAP_PLANT].add(c.ICEFROZENPLOT) self.speed = max(0.6, 1.5 - (c.GRID_X_LEN + 1 - mapX)*0.225) diff --git a/source/constants.py b/source/constants.py index 3dee972..96c26f8 100755 --- a/source/constants.py +++ b/source/constants.py @@ -193,7 +193,7 @@ PANEL_Y_INTERNAL = 69 PANEL_X_INTERNAL = 53 BAR_CARD_X_INTERNAL = 51 -# 所选植物信息索引 +# 植物卡片信息索引 PLANT_NAME_INDEX = 0 CARD_INDEX = 1 SUN_INDEX = 2 @@ -237,7 +237,7 @@ SEASHROOM = 'SeaShroom' TALLNUT = 'TallNut' TANGLEKLEP = 'TangleKlep' DOOMSHROOM = 'DoomShroom' -ICE_FROZEN_PLOT = 'IceFrozenPlot' +ICEFROZENPLOT = 'IceFrozenPlot' HOLE = 'Hole' GRAVE = 'Grave' GRAVEBUSTER = 'GraveBuster' @@ -246,9 +246,9 @@ GARLIC = 'Garlic' # 植物集体属性集合 -# 在生效时不用与僵尸进行碰撞检测的对象 +# 在生效时不用与僵尸进行碰撞检测的对象(即生效时不可发生被僵尸啃食的事件) SKIP_ZOMBIE_COLLISION_CHECK_WHEN_WORKING = { - # 注意爆炸坚果的触发也是啃食类碰撞,因此这里不能省略 + # 注意爆炸坚果的触发也是啃食类碰撞,因此只能算作爆炸后不检测 SQUASH, ICESHROOM, REDWALLNUTBOWLING, CHERRYBOMB, JALAPENO, DOOMSHROOM, @@ -257,12 +257,15 @@ SKIP_ZOMBIE_COLLISION_CHECK_WHEN_WORKING = { # 非植物对象 NON_PLANT_OBJECTS = { - HOLE, ICE_FROZEN_PLOT, + HOLE, ICEFROZENPLOT, GRAVE, } # 所有可能不用与僵尸进行碰撞检测的对象 CAN_SKIP_ZOMBIE_COLLISION_CHECK = ( # 这里运用了集合运算 + # 注意这个外围的小括号是用来换行的 + # 各个部分末尾千万不能加逗号!!! + # 生效时不检测的植物 SKIP_ZOMBIE_COLLISION_CHECK_WHEN_WORKING | # 非植物对象 @@ -274,7 +277,7 @@ CAN_SKIP_ZOMBIE_COLLISION_CHECK = ( # 这里运用了集合运算 # 死亡时不触发音效的对象 PLANT_DIE_SOUND_EXCEPTIONS = { WALLNUTBOWLING, TANGLEKLEP, - ICE_FROZEN_PLOT, HOLE, + ICEFROZENPLOT, HOLE, GRAVE, JALAPENO, REDWALLNUTBOWLING, CHERRYBOMB, } diff --git a/source/state/level.py b/source/state/level.py index e4411e5..7629700 100644 --- a/source/state/level.py +++ b/source/state/level.py @@ -189,7 +189,7 @@ class Level(tool.State): unoccupied = [] occupied = [] # 毁灭菇坑与冰道应当特殊化 - exceptionObjects = {c.HOLE, c.ICE_FROZEN_PLOT} + exceptionObjects = {c.HOLE, c.ICEFROZENPLOT} # 遍历能生成墓碑的区域 for mapY in range(0, 4): for mapX in range(4, 8): @@ -1145,7 +1145,7 @@ class Level(tool.State): else: _move = random.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 + _move = -(_move) zombie.targetMapY = i + _move zombie.targetYChange = _move * self.map.gridHeightSize else: @@ -1351,7 +1351,7 @@ class Level(tool.State): targetPlant.explode_x_range, effect=c.BULLET_EFFECT_UNICE) # 消除冰道 for item in self.plant_groups[i]: - if item.name == c.ICE_FROZEN_PLOT: + if item.name == c.ICEFROZENPLOT: item.health = 0 elif targetPlant.name == c.ICESHROOM: self.freezeZombies(targetPlant) From e2d211036d054ed0529e4f8c37fdbd0056eee049 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=98=9F=E5=A4=96=E4=B9=8B=E7=A5=9E?= Date: Sat, 4 Jun 2022 15:48:02 +0800 Subject: [PATCH 05/25] =?UTF-8?q?=E5=87=8F=E6=85=A2=E6=8D=A2=E8=A1=8C?= =?UTF-8?q?=E5=8A=A8=E7=94=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- source/component/zombie.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/source/component/zombie.py b/source/component/zombie.py index 1e6c517..676b584 100755 --- a/source/component/zombie.py +++ b/source/component/zombie.py @@ -208,7 +208,7 @@ class Zombie(pg.sprite.Sprite): if self.targetYChange < 0: self.setWalk() if self.rect.bottom > self.originalY + self.targetYChange: # 注意这里加的是负数 - self.rect.bottom -= 3 + self.rect.bottom -= 2 # 过半时换行 if ((self.toChangeGroup) and (self.rect.bottom >= self.originalY + 0.5*self.targetYChange)): @@ -221,7 +221,7 @@ class Zombie(pg.sprite.Sprite): elif self.targetYChange > 0: self.setWalk() if self.rect.bottom < self.originalY + self.targetYChange: # 注意这里加的是负数 - self.rect.bottom += 3 + self.rect.bottom += 2 # 过半时换行 if ((self.toChangeGroup) and (self.rect.bottom <= self.originalY + 0.5*self.targetYChange)): From 1e86ad16eae3832dd5343190c07da242911c820f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=98=9F=E5=A4=96=E4=B9=8B=E7=A5=9E?= Date: Sat, 4 Jun 2022 16:27:48 +0800 Subject: [PATCH 06/25] =?UTF-8?q?=E4=BD=BF=E6=9D=A8=E6=A1=83=E5=90=91?= =?UTF-8?q?=E5=90=8E=E5=8F=91=E5=B0=84=E7=9A=84=E5=AD=90=E5=BC=B9=E5=8F=AF?= =?UTF-8?q?=E4=BB=A5=E5=BF=BD=E7=95=A5=E4=BA=8C=E7=B1=BB=E9=98=B2=E5=85=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- source/component/plant.py | 12 ++++++++---- source/state/level.py | 2 +- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/source/component/plant.py b/source/component/plant.py index c4e009e..68f5eb7 100755 --- a/source/component/plant.py +++ b/source/component/plant.py @@ -38,7 +38,7 @@ class Car(pg.sprite.Sprite): # 豌豆及孢子类普通子弹 class Bullet(pg.sprite.Sprite): - def __init__(self, x, start_y, dest_y, name, damage, effect=None, passedTorchWood=None): + def __init__(self, x, start_y, dest_y, name, damage, effect=None, passedTorchWood=None, damageType=c.ZOMBIE_DEAFULT_DAMAGE): pg.sprite.Sprite.__init__(self) self.name = name @@ -55,6 +55,7 @@ class Bullet(pg.sprite.Sprite): self.y_vel = 15 if (dest_y > start_y) else -15 self.x_vel = 10 self.damage = damage + self.damageType = damageType self.effect = effect self.state = c.FLY self.current_time = 0 @@ -182,8 +183,8 @@ class Fume(pg.sprite.Sprite): # 杨桃的子弹 class StarBullet(Bullet): - def __init__(self, x, start_y, damage, direction, level): # direction指星星飞行方向 - Bullet.__init__(self, x, start_y, start_y, c.BULLET_STAR, damage) + def __init__(self, x, start_y, damage, direction, level, damageType = c.ZOMBIE_DEAFULT_DAMAGE): # direction指星星飞行方向 + Bullet.__init__(self, x, start_y, start_y, c.BULLET_STAR, damage, damageType = damageType) self.level = level _, self.map_y = self.level.map.getMapIndex(self.rect.x, self.rect.centery) @@ -1338,7 +1339,9 @@ class StarFruit(Plant): if self.shoot_timer == 0: self.shoot_timer = self.current_time - 700 elif (self.current_time - self.shoot_timer) >= 1400: - self.bullet_group.add(StarBullet(self.rect.left - 10, self.rect.y + 15, c.BULLET_DAMAGE_NORMAL, c.STAR_BACKWARD, self.level)) + # 向后打的杨桃子弹无视铁门与报纸防具 + self.bullet_group.add(StarBullet(self.rect.left - 10, self.rect.y + 15, c.BULLET_DAMAGE_NORMAL, c.STAR_BACKWARD, self.level, damageType = c.ZOMBIE_COMMON_DAMAGE)) + # 其他方向的杨桃子弹伤害效果与豌豆等同 self.bullet_group.add(StarBullet(self.rect.centerx - 20, self.rect.bottom - self.rect.h - 15, c.BULLET_DAMAGE_NORMAL, c.STAR_UPWARD, self.level)) self.bullet_group.add(StarBullet(self.rect.centerx - 20, self.rect.bottom - 5, c.BULLET_DAMAGE_NORMAL, c.STAR_DOWNWARD, self.level)) self.bullet_group.add(StarBullet(self.rect.right - 5, self.rect.bottom - 20, c.BULLET_DAMAGE_NORMAL, c.STAR_FORWARD_DOWN, self.level)) @@ -1589,6 +1592,7 @@ class DoomShroom(Plant): else: self.image.set_alpha(255) + # 用于描述毁灭菇的坑 class Hole(Plant): def __init__(self, x, y, plotType): diff --git a/source/state/level.py b/source/state/level.py index 7629700..3f253b1 100644 --- a/source/state/level.py +++ b/source/state/level.py @@ -1015,7 +1015,7 @@ class Level(tool.State): continue if collided_func(zombie, bullet): if zombie.state != c.DIE: - zombie.setDamage(bullet.damage, effect=bullet.effect, damageType=c.ZOMBIE_DEAFULT_DAMAGE) + zombie.setDamage(bullet.damage, effect=bullet.effect, damageType=bullet.damageType) bullet.setExplode() # 火球有溅射伤害 if bullet.name == c.BULLET_FIREBALL: From 1ff2d1a6f6e50cc63caa97948162254120f18294 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=98=9F=E5=A4=96=E4=B9=8B=E7=A5=9E?= Date: Sat, 4 Jun 2022 19:26:04 +0800 Subject: [PATCH 07/25] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=A4=A7=E8=92=9C?= =?UTF-8?q?=E7=9A=84=E5=A4=A7=E9=87=8Fbug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- source/component/zombie.py | 29 +++++++++++++++-------------- source/state/level.py | 6 +++++- 2 files changed, 20 insertions(+), 15 deletions(-) diff --git a/source/component/zombie.py b/source/component/zombie.py index 676b584..9eec302 100755 --- a/source/component/zombie.py +++ b/source/component/zombie.py @@ -196,8 +196,8 @@ class Zombie(pg.sprite.Sprite): self.changeFrames(self.walk_frames) self.helmetType2 = False - if ((self.current_time - self.walk_timer) > (c.ZOMBIE_WALK_INTERVAL * self.getTimeRatio()) - and self.handleGarlicYChange()): + if (self.current_time - self.walk_timer) > (c.ZOMBIE_WALK_INTERVAL * self.getTimeRatio()): + self.handleGarlicYChange() self.walk_timer = self.current_time if self.is_hypno: self.rect.x += 1 @@ -206,33 +206,31 @@ class Zombie(pg.sprite.Sprite): def handleGarlicYChange(self): if self.targetYChange < 0: - self.setWalk() if self.rect.bottom > self.originalY + self.targetYChange: # 注意这里加的是负数 - self.rect.bottom -= 2 + 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) + self.toChangeGroup = False else: self.rect.bottom = self.originalY + self.targetYChange + self.originalY = self.rect.bottom self.targetYChange = 0 - return None elif self.targetYChange > 0: - self.setWalk() if self.rect.bottom < self.originalY + self.targetYChange: # 注意这里加的是负数 - self.rect.bottom += 2 + 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) + self.toChangeGroup = False else: self.rect.bottom = self.originalY + self.targetYChange + self.originalY = self.rect.bottom self.targetYChange = 0 - return None - else: - return True def attacking(self): if self.checkToDie(self.losthead_attack_frames): @@ -715,8 +713,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()) - and self.handleGarlicYChange()): + if ((self.current_time - self.walk_timer) > (c.ZOMBIE_WALK_INTERVAL * self.getTimeRatio())): + self.handleGarlicYChange() self.walk_timer = self.current_time if self.frames == self.lostnewspaper_frames: pass @@ -1178,6 +1176,9 @@ class SnorkelZombie(Zombie): self.frames = self.walk_frames def walking(self): + if self.checkToDie(self.losthead_walk_frames): + return + # 在水池范围内 # 在右侧岸左 if self.rect.centerx <= c.MAP_POOL_FRONT_X - 25: @@ -1199,8 +1200,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()) - and self.handleGarlicYChange()): + if (self.current_time - self.walk_timer) > (c.ZOMBIE_WALK_INTERVAL * self.getTimeRatio()): + self.handleGarlicYChange() self.walk_timer = self.current_time # 正在上浮或者下潜不用移动 if (self.frames == self.float_frames) or (self.frames == self.sink_frames): diff --git a/source/state/level.py b/source/state/level.py index 3f253b1..62f7ef8 100644 --- a/source/state/level.py +++ b/source/state/level.py @@ -1132,6 +1132,9 @@ class Level(tool.State): elif targetPlant.name == c.REDWALLNUTBOWLING: if targetPlant.state == c.IDLE: targetPlant.setAttack() + elif zombie.targetYChange: + # 大蒜作用正在生效的僵尸不进行传递 + continue elif targetPlant.name == c.GARLIC: zombie.setAttack(targetPlant) # 向吃过大蒜的僵尸传入level @@ -1394,7 +1397,8 @@ class Level(tool.State): def checkLose(self): for i in range(self.map_y_len): for zombie in self.zombie_groups[i]: - if zombie.rect.right < -20 and (not zombie.lostHead) and zombie.state != c.DIE: + if zombie.rect.right < -20 and (not zombie.lostHead) and (zombie.state != c.DIE): + print(zombie.rect.right, zombie.lostHead, zombie.state,zombie.name) return True return False From 0db90ba8c87a42dc578a11ba984c2baa989c4b10 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=98=9F=E5=A4=96=E4=B9=8B=E7=A5=9E?= Date: Sat, 4 Jun 2022 21:31:09 +0800 Subject: [PATCH 08/25] =?UTF-8?q?=E6=9B=B4=E6=94=B9=E9=A3=9E=E5=87=BA?= =?UTF-8?q?=E5=B1=8F=E5=B9=95=E5=AF=B9=E8=B1=A1=E7=9A=84=E6=9D=80=E6=AD=BB?= =?UTF-8?q?=E6=9D=A1=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- source/component/plant.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/source/component/plant.py b/source/component/plant.py index 68f5eb7..cffca58 100755 --- a/source/component/plant.py +++ b/source/component/plant.py @@ -207,7 +207,7 @@ class StarBullet(Bullet): else: self.rect.x -= 10 self.handleMapYPosition() - if ((self.rect.x > c.SCREEN_WIDTH + 60) or (self.rect.x < -40) + if ((self.rect.x > c.SCREEN_WIDTH + 60) or (self.rect.x < -60) or (self.rect.y > c.SCREEN_HEIGHT) or (self.rect.y < 0)): self.kill() elif self.state == c.EXPLODE: @@ -1161,7 +1161,7 @@ class WallNutBowling(Plant): self.handleMapYPosition() if self.shouldChangeDirection(): self.changeDirection(-1) - if self.init_rect.x > c.SCREEN_WIDTH: + if self.init_rect.x > c.SCREEN_WIDTH + 60: self.health = 0 self.move_timer += self.move_interval @@ -1253,7 +1253,7 @@ class RedWallNutBowling(Plant): elif (self.current_time - self.move_timer) >= self.move_interval: self.rotate_degree = (self.rotate_degree - 30) % 360 self.init_rect.x += self.vel_x - if self.init_rect.x > c.SCREEN_WIDTH: + if self.init_rect.x > c.SCREEN_WIDTH + 60: self.health = 0 self.move_timer += self.move_interval From 0345119d3466aba3f3d08d9d040b69eb58709606 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=98=9F=E5=A4=96=E4=B9=8B=E7=A5=9E?= Date: Sat, 4 Jun 2022 21:59:51 +0800 Subject: [PATCH 09/25] =?UTF-8?q?=E9=87=8D=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- source/component/menubar.py | 8 ++++---- source/component/plant.py | 21 ++++++++++----------- source/state/level.py | 22 +++++++++++----------- 3 files changed, 25 insertions(+), 26 deletions(-) diff --git a/source/component/menubar.py b/source/component/menubar.py index 0fb6188..94a29f2 100755 --- a/source/component/menubar.py +++ b/source/component/menubar.py @@ -340,8 +340,8 @@ class MoveCard(): def checkMouseClick(self, mouse_pos): x, y = mouse_pos - if(x >= self.rect.x and x <= self.rect.right and - y >= self.rect.y and y <= self.rect.bottom): + if (x >= self.rect.x and x <= self.rect.right and + y >= self.rect.y and y <= self.rect.bottom): return True return False @@ -425,8 +425,8 @@ class MoveBar(): def checkMenuBarClick(self, mouse_pos): x, y = mouse_pos - if(x >= self.rect.x and x <= self.rect.right and - y >= self.rect.y and y <= self.rect.bottom): + if (x >= self.rect.x and x <= self.rect.right and + y >= self.rect.y and y <= self.rect.bottom): return True return False diff --git a/source/component/plant.py b/source/component/plant.py index cffca58..60730af 100755 --- a/source/component/plant.py +++ b/source/component/plant.py @@ -211,7 +211,7 @@ class StarBullet(Bullet): or (self.rect.y > c.SCREEN_HEIGHT) or (self.rect.y < 0)): self.kill() elif self.state == c.EXPLODE: - if (self.current_time - self.explode_timer) > 250: + if (self.current_time - self.explode_timer) >= 250: self.kill() # 这里用的是坚果保龄球的代码改一下,实现子弹换行 @@ -220,7 +220,6 @@ class StarBullet(Bullet): _, map_y1 = self.level.map.getMapIndex(self.rect.x, self.rect.centery + 40) else: _, map_y1 = self.level.map.getMapIndex(self.rect.x, self.rect.centery + 20) - # _, map_y2 = self.level.map.getMapIndex(self.rect.x, self.rect.bottom +20) if (self.map_y != map_y1) and (0 <= map_y1 <= self.level.map_y_len-1): # 换行 self.level.bullet_groups[self.map_y].remove(self) self.level.bullet_groups[map_y1].add(self) @@ -344,8 +343,8 @@ class Plant(pg.sprite.Sprite): self.health -= damage self.hit_timer = self.current_time if ((self.name == c.HYPNOSHROOM) and - (self.state != c.SLEEP) and - (zombie.name not in {c.ZOMBONI, "投石车僵尸(未实现)", "加刚特尔(未实现)"})): + (self.state != c.SLEEP) and + (zombie.name not in {c.ZOMBONI, "投石车僵尸(未实现)", "加刚特尔(未实现)"})): self.zombie_to_hypno = zombie def getPosition(self): @@ -383,7 +382,7 @@ class Sun(Plant): if self.state == c.DIE: return False if (x >= self.rect.x and x <= self.rect.right and - y >= self.rect.y and y <= self.rect.bottom): + y >= self.rect.y and y <= self.rect.bottom): self.state = c.DIE self.kill() return True @@ -626,8 +625,8 @@ class Chomper(Plant): if (zombie.name == c.SNORKELZOMBIE) and (zombie.frames == zombie.swim_frames): return False elif (self.state == c.IDLE and zombie.state != c.DIGEST and - self.rect.x <= zombie.rect.centerx and (not zombie.lostHead) and - (self.rect.x + c.GRID_X_SIZE*2.7 >= zombie.rect.centerx)): + self.rect.x <= zombie.rect.centerx and (not zombie.lostHead) and + (self.rect.x + c.GRID_X_SIZE*2.7 >= zombie.rect.centerx)): return True return False @@ -702,7 +701,7 @@ class PuffShroom(Plant): if (zombie.name == c.SNORKELZOMBIE) and (zombie.frames == zombie.swim_frames): return False if (self.rect.x <= zombie.rect.right and - (self.rect.x + c.GRID_X_SIZE * 4 >= zombie.rect.x) and (zombie.rect.left <= c.SCREEN_WIDTH + 10)): + (self.rect.x + c.GRID_X_SIZE * 4 >= zombie.rect.x) and (zombie.rect.left <= c.SCREEN_WIDTH + 10)): return True return False @@ -796,7 +795,7 @@ class Squash(Plant): def canAttack(self, zombie): # 普通状态 if (self.state == c.IDLE and self.rect.x <= zombie.rect.right and - (self.rect.right + c.GRID_X_SIZE >= zombie.rect.x)): + (self.rect.right + c.GRID_X_SIZE >= zombie.rect.x)): return True # 攻击状态 elif (self.state == c.ATTACK): @@ -1431,7 +1430,7 @@ class SeaShroom(Plant): if (zombie.name == c.SNORKELZOMBIE) and (zombie.frames == zombie.swim_frames): return False if (self.rect.x <= zombie.rect.right and - (self.rect.x + c.GRID_X_SIZE * 4 >= zombie.rect.x) and (zombie.rect.left <= c.SCREEN_WIDTH + 10)): + (self.rect.x + c.GRID_X_SIZE * 4 >= zombie.rect.x) and (zombie.rect.left <= c.SCREEN_WIDTH + 10)): return True return False @@ -1723,7 +1722,7 @@ class FumeShroom(Plant): if (zombie.name == c.SNORKELZOMBIE) and (zombie.frames == zombie.swim_frames): return False if (self.rect.x <= zombie.rect.right and - (self.rect.x + c.GRID_X_SIZE * 5 >= zombie.rect.x) and (zombie.rect.left <= c.SCREEN_WIDTH + 10)): + (self.rect.x + c.GRID_X_SIZE * 5 >= zombie.rect.x) and (zombie.rect.left <= c.SCREEN_WIDTH + 10)): return True return False diff --git a/source/state/level.py b/source/state/level.py index 62f7ef8..41dd82f 100644 --- a/source/state/level.py +++ b/source/state/level.py @@ -581,32 +581,32 @@ class Level(tool.State): # 检查小菜单有没有被点击 def checkLittleMenuClick(self, mouse_pos): x, y = mouse_pos - if(x >= self.little_menu_rect.x and x <= self.little_menu_rect.right and - y >= self.little_menu_rect.y and y <= self.little_menu_rect.bottom): + if (x >= self.little_menu_rect.x and x <= self.little_menu_rect.right and + y >= self.little_menu_rect.y and y <= self.little_menu_rect.bottom): return True return False # 检查小菜单的返回有没有被点击 def checkReturnClick(self, mouse_pos): x, y = mouse_pos - if(x >= self.return_button_rect.x and x <= self.return_button_rect.right and - y >= self.return_button_rect.y and y <= self.return_button_rect.bottom): + if (x >= self.return_button_rect.x and x <= self.return_button_rect.right and + y >= self.return_button_rect.y and y <= self.return_button_rect.bottom): return True return False # 检查小菜单的重新开始有没有被点击 def checkRestartClick(self, mouse_pos): x, y = mouse_pos - if(x >= self.restart_button_rect.x and x <= self.restart_button_rect.right and - y >= self.restart_button_rect.y and y <= self.restart_button_rect.bottom): + if (x >= self.restart_button_rect.x and x <= self.restart_button_rect.right and + y >= self.restart_button_rect.y and y <= self.restart_button_rect.bottom): return True return False # 检查小菜单的主菜单有没有被点击 def checkMainMenuClick(self, mouse_pos): x, y = mouse_pos - if(x >= self.mainMenu_button_rect.x and x <= self.mainMenu_button_rect.right and - y >= self.mainMenu_button_rect.y and y <= self.mainMenu_button_rect.bottom): + if (x >= self.mainMenu_button_rect.x and x <= self.mainMenu_button_rect.right and + y >= self.mainMenu_button_rect.y and y <= self.mainMenu_button_rect.bottom): return True return False @@ -910,7 +910,7 @@ class Level(tool.State): new_plant = plant.TangleKlep(x, y) elif self.plant_name == c.DOOMSHROOM: if ((self.map_data[c.BACKGROUND_TYPE] in c.ON_ROOF_BACKGROUNDS) or - (self.map_data[c.BACKGROUND_TYPE] in c.POOL_EQUIPPED_BACKGROUNDS)): + (self.map_data[c.BACKGROUND_TYPE] in c.POOL_EQUIPPED_BACKGROUNDS)): new_plant = plant.DoomShroom(x, y, self.map.map[map_y][map_x][c.MAP_PLANT], explode_y_range=3) else: new_plant = plant.DoomShroom(x, y, self.map.map[map_y][map_x][c.MAP_PLANT], explode_y_range=2) @@ -1188,7 +1188,7 @@ class Level(tool.State): continue for zombie in self.zombie_groups[i]: if ((abs(zombie.rect.centerx - x) <= x_range) or - ((zombie.rect.right - (x-x_range) > 20) or (zombie.rect.right - (x-x_range))/zombie.rect.width > 0.2, ((x+x_range) - zombie.rect.left > 20) or ((x+x_range) - zombie.rect.left)/zombie.rect.width > 0.2)[zombie.rect.x > x]): # 这代码不太好懂,后面是一个判断僵尸在左还是在右,前面是一个元组,[0]是在左边的情况,[1]是在右边的情况 + ((zombie.rect.right - (x-x_range) > 20) or (zombie.rect.right - (x-x_range))/zombie.rect.width > 0.2, ((x+x_range) - zombie.rect.left > 20) or ((x+x_range) - zombie.rect.left)/zombie.rect.width > 0.2)[zombie.rect.x > x]): # 这代码不太好懂,后面是一个判断僵尸在左还是在右,前面是一个元组,[0]是在左边的情况,[1]是在右边的情况 if effect == c.BULLET_EFFECT_UNICE: zombie.ice_slow_ratio = 1 zombie.setDamage(1800, damageType=c.ZOMBIE_ASH_DAMAGE) @@ -1280,7 +1280,7 @@ class Level(tool.State): for zombie in self.zombie_groups[i]: # 双判断:发生碰撞或在攻击范围内 if ((pg.sprite.collide_mask(zombie, targetPlant)) or - (abs(zombie.rect.centerx - targetPlant.rect.centerx) <= targetPlant.explode_x_range)): + (abs(zombie.rect.centerx - targetPlant.rect.centerx) <= targetPlant.explode_x_range)): zombie.setDamage(1800, damageType=c.ZOMBIE_RANGE_DAMAGE) targetPlant.boomed = True elif targetPlant.name == c.SQUASH: From ae58d15e18113190cb01312c3012d450375d20dd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=98=9F=E5=A4=96=E4=B9=8B=E7=A5=9E?= Date: Sun, 5 Jun 2022 15:53:44 +0800 Subject: [PATCH 10/25] =?UTF-8?q?=E9=87=8D=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- source/component/menubar.py | 4 ++-- source/component/plant.py | 12 ++++++------ source/state/level.py | 2 +- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/source/component/menubar.py b/source/component/menubar.py index 94a29f2..8c1af82 100755 --- a/source/component/menubar.py +++ b/source/component/menubar.py @@ -168,8 +168,8 @@ class MenuBar(): def checkMenuBarClick(self, mouse_pos): x, y = mouse_pos - if(x >= self.rect.x and x <= self.rect.right and - y >= self.rect.y and y <= self.rect.bottom): + if (x >= self.rect.x and x <= self.rect.right and + y >= self.rect.y and y <= self.rect.bottom): return True return False diff --git a/source/component/plant.py b/source/component/plant.py index 60730af..33c0381 100755 --- a/source/component/plant.py +++ b/source/component/plant.py @@ -187,7 +187,7 @@ class StarBullet(Bullet): Bullet.__init__(self, x, start_y, start_y, c.BULLET_STAR, damage, damageType = damageType) self.level = level - _, self.map_y = self.level.map.getMapIndex(self.rect.x, self.rect.centery) + self.map_y = self.level.map.getMapIndex(self.rect.x, self.rect.centery)[1] self.direction = direction def update(self, game_info): @@ -217,9 +217,9 @@ class StarBullet(Bullet): # 这里用的是坚果保龄球的代码改一下,实现子弹换行 def handleMapYPosition(self): if self.direction == c.STAR_UPWARD: - _, map_y1 = self.level.map.getMapIndex(self.rect.x, self.rect.centery + 40) + map_y1 = self.level.map.getMapIndex(self.rect.x, self.rect.centery + 40)[1] else: - _, map_y1 = self.level.map.getMapIndex(self.rect.x, self.rect.centery + 20) + map_y1 = self.level.map.getMapIndex(self.rect.x, self.rect.centery + 20)[1] if (self.map_y != map_y1) and (0 <= map_y1 <= self.level.map_y_len-1): # 换行 self.level.bullet_groups[self.map_y].remove(self) self.level.bullet_groups[map_y1].add(self) @@ -1170,8 +1170,8 @@ class WallNutBowling(Plant): return True def handleMapYPosition(self): - _, map_y1 = self.level.map.getMapIndex(self.init_rect.x, self.init_rect.centery) - _, map_y2 = self.level.map.getMapIndex(self.init_rect.x, self.init_rect.bottom) + map_y1 = self.level.map.getMapIndex(self.init_rect.x, self.init_rect.centery)[1] + map_y2 = self.level.map.getMapIndex(self.init_rect.x, self.init_rect.bottom)[1] if self.map_y != map_y1 and map_y1 == map_y2: # wallnut bowls to another row, should modify which plant group it belongs to self.level.plant_groups[self.map_y].remove(self) @@ -1319,7 +1319,7 @@ class StarFruit(Plant): if (zombie.name == c.SNORKELZOMBIE) and (zombie.frames == zombie.swim_frames): return False if zombie.state != c.DIE: - _, zombieMapY = self.level.map.getMapIndex(zombie.rect.centerx, zombie.rect.bottom) + zombieMapY = self.level.map.getMapIndex(zombie.rect.centerx, zombie.rect.bottom)[1] if (self.rect.x >= zombie.rect.x) and (self.map_y == zombieMapY): # 对于同行且在杨桃后的僵尸 return True # 斜向上,理想直线方程为:f(zombie.rect.x) = -0.75*(zombie.rect.x - (self.rect.right - 5)) + self.rect.y - 10 diff --git a/source/state/level.py b/source/state/level.py index 41dd82f..9c73ab2 100644 --- a/source/state/level.py +++ b/source/state/level.py @@ -320,7 +320,7 @@ class Level(tool.State): def setupCars(self): self.cars = [] for i in range(self.map_y_len): - _, y = self.map.getMapGridPos(0, i) + y = self.map.getMapGridPos(0, i)[1] self.cars.append(plant.Car(-40, y+20, i)) # 更新函数每帧被调用,将鼠标事件传入给状态处理函数 From 80fdac722027cbebb38ab941147dd78aa540be25 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=98=9F=E5=A4=96=E4=B9=8B=E7=A5=9E?= Date: Tue, 7 Jun 2022 13:26:16 +0800 Subject: [PATCH 11/25] =?UTF-8?q?=E4=BC=98=E5=8C=96=E6=AF=81=E7=81=AD?= =?UTF-8?q?=E8=8F=87=E7=A7=8D=E6=A4=8D=E5=88=A4=E6=96=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- source/state/level.py | 7 +++---- source/state/mainmenu.py | 4 ++-- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/source/state/level.py b/source/state/level.py index 9c73ab2..89005c6 100644 --- a/source/state/level.py +++ b/source/state/level.py @@ -909,11 +909,10 @@ class Level(tool.State): elif self.plant_name == c.TANGLEKLEP: new_plant = plant.TangleKlep(x, y) elif self.plant_name == c.DOOMSHROOM: - if ((self.map_data[c.BACKGROUND_TYPE] in c.ON_ROOF_BACKGROUNDS) or - (self.map_data[c.BACKGROUND_TYPE] in c.POOL_EQUIPPED_BACKGROUNDS)): - new_plant = plant.DoomShroom(x, y, self.map.map[map_y][map_x][c.MAP_PLANT], explode_y_range=3) - else: + if self.map.gridHeightSize == c.GRID_Y_SIZE: new_plant = plant.DoomShroom(x, y, self.map.map[map_y][map_x][c.MAP_PLANT], explode_y_range=2) + else: + new_plant = plant.DoomShroom(x, y, self.map.map[map_y][map_x][c.MAP_PLANT], explode_y_range=3) elif self.plant_name == c.GRAVEBUSTER: new_plant = plant.GraveBuster(x, y, self.plant_groups[map_y], self.map, map_x) elif self.plant_name == c.FUMESHROOM: diff --git a/source/state/mainmenu.py b/source/state/mainmenu.py index 55649e4..143a920 100644 --- a/source/state/mainmenu.py +++ b/source/state/mainmenu.py @@ -97,8 +97,10 @@ class Menu(tool.State): # 高亮冒险模式按钮 if self.inAreaAdventure(x, y): self.adventure_highlight_time = self.current_time + # 高亮退出按钮 elif self.inAreaExit(x, y): self.exit_highlight_time = self.current_time + # 高亮小游戏按钮 elif self.inAreaLittleGame(x, y): self.littleGame_highlight_time = self.current_time @@ -126,8 +128,6 @@ class Menu(tool.State): x, y = mouse_pos if self.inAreaLittleGame(x, y): self.done = True - # 确实小游戏还是用的level - # 因为目前暂时没有生存模式和解谜模式,所以暂时设置为这样 self.persist[c.GAME_MODE] = c.MODE_LITTLEGAME # 播放点击音效 pg.mixer.Sound(os.path.join(os.path.dirname(os.path.dirname(os.path.dirname(__file__))) ,"resources", "sound", "buttonclick.ogg")).play() From 1a54844ffe334fbd5f34ced0963c3377b3a15c12 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=98=9F=E5=A4=96=E4=B9=8B=E7=A5=9E?= Date: Tue, 7 Jun 2022 14:06:01 +0800 Subject: [PATCH 12/25] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- source/state/level.py | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/source/state/level.py b/source/state/level.py index 89005c6..d4d222d 100644 --- a/source/state/level.py +++ b/source/state/level.py @@ -93,15 +93,11 @@ class Level(tool.State): self.sun_group = pg.sprite.Group() self.head_group = pg.sprite.Group() - self.plant_groups = [] - self.zombie_groups = [] - self.hypno_zombie_groups = [] #zombies who are hypno after eating hypnoshroom - self.bullet_groups = [] - for i in range(self.map_y_len): - self.plant_groups.append(pg.sprite.Group()) - self.zombie_groups.append(pg.sprite.Group()) - self.hypno_zombie_groups.append(pg.sprite.Group()) - self.bullet_groups.append(pg.sprite.Group()) + # 改用列表生成器直接生成内容,不再在这里使用for循环 + self.plant_groups = [pg.sprite.Group() for i in range(self.map_y_len)] + self.zombie_groups = [pg.sprite.Group() for i in range(self.map_y_len)] + self.hypno_zombie_groups = [pg.sprite.Group() for i in range(self.map_y_len)] #zombies who are hypno after eating hypnoshroom + self.bullet_groups = [pg.sprite.Group() for i in range(self.map_y_len)] # 按照规则生成每一波僵尸 From 4b19f7f8518866f6b10e13371382884815ca238f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=98=9F=E5=A4=96=E4=B9=8B=E7=A5=9E?= Date: Tue, 7 Jun 2022 14:38:57 +0800 Subject: [PATCH 13/25] =?UTF-8?q?=E4=BC=98=E5=8C=96=E5=9C=B0=E5=9B=BE?= =?UTF-8?q?=E7=94=9F=E6=88=90=E6=80=A7=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- source/component/map.py | 12 +++++------- source/constants.py | 14 ++++++++++---- 2 files changed, 15 insertions(+), 11 deletions(-) diff --git a/source/component/map.py b/source/component/map.py index 89e994b..f95527e 100755 --- a/source/component/map.py +++ b/source/component/map.py @@ -2,38 +2,36 @@ import random import pygame as pg from .. import tool from .. import constants as c -from copy import deepcopy class Map(): def __init__(self, background_type): self.background_type = background_type # 注意:从0开始编号 - # 集合内容需要deepcopy 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)] + self.map = [[(c.MAP_STATE_EMPTY(), 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)] + self.map = [[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)] + self.map = [[(c.MAP_STATE_UNAVAILABLE(), 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)] + self.map = [[(c.MAP_STATE_UNAVAILABLE(), 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)] + 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 diff --git a/source/constants.py b/source/constants.py index 96c26f8..7a34bf7 100755 --- a/source/constants.py +++ b/source/constants.py @@ -158,10 +158,16 @@ MAP_WATER = 'water' MAP_TILE = 'tile' # 指屋顶上的瓦片 MAP_UNAVAILABLE = 'unavailable' # 指完全不能种植物的地方,包括无草皮的荒地和坚果保龄球等红线右侧 # 地图单元格状态 -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_TILE = {MAP_PLANT:set(), MAP_SLEEP:False, MAP_PLOT_TYPE:MAP_TILE} -MAP_STATE_UNAVAILABLE = {MAP_PLANT:set(), MAP_SLEEP:False, MAP_PLOT_TYPE:MAP_UNAVAILABLE} +# 注意是可变对象,不能直接引用 +# 不喜欢用深拷贝,直接改用函数表示,需要时直接调用该函数生成即可 +def MAP_STATE_EMPTY(): # 由于同一格显然不可能种两个相同的植物,所以用集合 + return {MAP_PLANT:set(), MAP_SLEEP:False, MAP_PLOT_TYPE:MAP_GRASS} +def MAP_STATE_WATER(): + return {MAP_PLANT:set(), MAP_SLEEP:False, MAP_PLOT_TYPE:MAP_WATER} +def MAP_STATE_TILE(): + return {MAP_PLANT:set(), MAP_SLEEP:False, MAP_PLOT_TYPE:MAP_TILE} +def MAP_STATE_UNAVAILABLE(): + return {MAP_PLANT:set(), MAP_SLEEP:False, MAP_PLOT_TYPE:MAP_UNAVAILABLE} # 地图相关像素数据 BACKGROUND_OFFSET_X = 220 From 87d5d4a89096e8e56303a84c7058c30cef5271b3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=98=9F=E5=A4=96=E4=B9=8B=E7=A5=9E?= Date: Tue, 7 Jun 2022 14:47:52 +0800 Subject: [PATCH 14/25] =?UTF-8?q?=E5=AE=8C=E5=96=84=E8=A1=A5=E5=85=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- source/constants.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/constants.py b/source/constants.py index 7a34bf7..35963da 100755 --- a/source/constants.py +++ b/source/constants.py @@ -136,7 +136,7 @@ BACKGROUND_DAY_LIKE_BACKGROUNDS = { # 夜晚地图的墓碑数量等级 GRADE_GRAVES = 'grade_graves' -# 不同墓碑等级对应的信息 +# 不同墓碑等级对应的信息,列表位置对应的是墓碑等级 GRAVES_GRADE_INFO = (0, 4, 7, 11) # 僵尸生成方式 From d9a49ff1a12d03f7df7851a7af0782bc16849627 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=98=9F=E5=A4=96=E4=B9=8B=E7=A5=9E?= Date: Tue, 7 Jun 2022 14:59:49 +0800 Subject: [PATCH 15/25] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=9D=9A=E6=9E=9C?= =?UTF-8?q?=E4=BF=9D=E9=BE=84=E7=90=83=E9=97=AA=E9=80=80bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- source/component/map.py | 2 +- source/state/level.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/source/component/map.py b/source/component/map.py index f95527e..83b4cf7 100755 --- a/source/component/map.py +++ b/source/component/map.py @@ -137,7 +137,7 @@ class Map(): map_y * c.GRID_Y_SIZE + c.GRID_Y_SIZE//5 * 3 + c.MAP_OFFSET_Y) def setMapGridType(self, map_x, map_y, plot_type): - self.map[map_y][map_x] = plot_type + self.map[map_y][map_x][c.MAP_PLOT_TYPE] = plot_type def addMapPlant(self, map_x, map_y, plantName, sleep=False): self.map[map_y][map_x][c.MAP_PLANT].add(plantName) diff --git a/source/state/level.py b/source/state/level.py index d4d222d..9358848 100644 --- a/source/state/level.py +++ b/source/state/level.py @@ -340,7 +340,7 @@ class Level(tool.State): def initBowlingMap(self): for x in range(3, self.map.width): for y in range(self.map.height): - self.map.setMapGridType(x, y, c.MAP_STATE_UNAVAILABLE) # 将坚果保龄球红线右侧设置为不可种植任何植物 + self.map.setMapGridType(x, y, c.MAP_UNAVAILABLE) # 将坚果保龄球红线右侧设置为不可种植任何植物 def initState(self): if c.CHOOSEBAR_TYPE in self.map_data: From dd623e1c7193269e90dc2aecba62da50d71f1072 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=98=9F=E5=A4=96=E4=B9=8B=E7=A5=9E?= Date: Tue, 7 Jun 2022 21:53:00 +0800 Subject: [PATCH 16/25] =?UTF-8?q?=E7=BC=96=E8=AF=91=E5=BC=80=E5=90=AFLTO?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 ++ source/constants.py | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 0be1e5e..e3522de 100644 --- a/README.md +++ b/README.md @@ -110,6 +110,7 @@ nuitka --mingw64 --standalone ` --include-data-file=C:\Users\17265\AppData\Local\Programs\Python\Python310\Lib\site-packages\pygame\libopusfile-0.dll=libopusfile-0.dll ` --include-data-file=C:\Users\17265\AppData\Local\Programs\Python\Python310\Lib\site-packages\pygame\libvorbisfile-3.dll=libvorbisfile-3.dll ` --include-data-file=C:\Users\17265\AppData\Local\Programs\Python\Python310\Lib\site-packages\pygame\libvorbis-0.dll=libvorbis-0.dll ` + --lto=yes ` --windows-disable-console ` --windows-product-name=pypvz ` --windows-company-name=null ` @@ -121,6 +122,7 @@ nuitka --mingw64 --standalone ` * 其中`C:\Users\17265\AppData\Local\Programs\Python\Python310\Lib\site-packages\pygame\xxx.dll`应当替换为`xxx.dll`实际所在路径 * 由于仅复制了`opus`与`vorbis`的解码器,故要求所有背景音乐都要以opus或vorbis编码 * `--windows-product-version=`表示版本号信息,所跟内容格式必须为`x.x.x.x` +* 建议开启`--lto=yes`选项优化链接,如果编译失败可以关闭此选项 可执行文件生成路径为`./out/main.exe` diff --git a/source/constants.py b/source/constants.py index 35963da..2e04c36 100755 --- a/source/constants.py +++ b/source/constants.py @@ -270,7 +270,7 @@ NON_PLANT_OBJECTS = { # 所有可能不用与僵尸进行碰撞检测的对象 CAN_SKIP_ZOMBIE_COLLISION_CHECK = ( # 这里运用了集合运算 # 注意这个外围的小括号是用来换行的 - # 各个部分末尾千万不能加逗号!!! + # 各个部分末!尾!千!万!不!能!加!逗!号!!! # 生效时不检测的植物 SKIP_ZOMBIE_COLLISION_CHECK_WHEN_WORKING | From 5bc2a15db811ba85530a092f213f73d3472e1792 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=98=9F=E5=A4=96=E4=B9=8B=E7=A5=9E?= Date: Wed, 8 Jun 2022 10:22:12 +0800 Subject: [PATCH 17/25] =?UTF-8?q?=E6=9B=B4=E6=94=B9=E5=9C=B0=E5=9B=BE?= =?UTF-8?q?=E5=88=9D=E5=A7=8B=E5=8C=96=E5=87=BD=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- source/component/map.py | 10 +++++----- source/constants.py | 11 +++-------- 2 files changed, 8 insertions(+), 13 deletions(-) diff --git a/source/component/map.py b/source/component/map.py index 83b4cf7..56d02de 100755 --- a/source/component/map.py +++ b/source/component/map.py @@ -11,27 +11,27 @@ class Map(): self.width = c.GRID_POOL_X_LEN self.height = c.GRID_POOL_Y_LEN self.gridHeightSize = c.GRID_POOL_Y_SIZE - self.map = [[(c.MAP_STATE_EMPTY(), c.MAP_STATE_WATER())[y in {2, 3}] for x in range(self.width)] for y in range(self.height)] + self.map = [[(c.INIT_MAP_GRID(c.MAP_GRASS), c.INIT_MAP_GRID(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: self.width = c.GRID_ROOF_X_LEN self.height = c.GRID_ROOF_Y_LEN self.gridHeightSize = c.GRID_ROOF_Y_SIZE - self.map = [[c.MAP_STATE_TILE() for x in range(self.width)] for y in range(self.height)] + self.map = [[c.INIT_MAP_GRID(c.MAP_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 = [[(c.MAP_STATE_UNAVAILABLE(), c.MAP_STATE_EMPTY())[y == 2] for x in range(self.width)] for y in range(self.height)] + self.map = [[(c.INIT_MAP_GRID(c.MAP_UNAVAILABLE), c.INIT_MAP_GRID(c.MAP_GRASS))[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 = [[(c.MAP_STATE_UNAVAILABLE(), c.MAP_STATE_EMPTY())[y in {1, 2, 3}] for x in range(self.width)] for y in range(self.height)] + self.map = [[(c.INIT_MAP_GRID(c.MAP_UNAVAILABLE), c.INIT_MAP_GRID(c.MAP_GRASS))[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 = [[c.MAP_STATE_EMPTY() for x in range(self.width)] for y in range(self.height)] + self.map = [[c.INIT_MAP_GRID(c.MAP_GRASS) 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 diff --git a/source/constants.py b/source/constants.py index 2e04c36..84ac369 100755 --- a/source/constants.py +++ b/source/constants.py @@ -160,14 +160,9 @@ MAP_UNAVAILABLE = 'unavailable' # 指完全不能种植物的地方,包括无 # 地图单元格状态 # 注意是可变对象,不能直接引用 # 不喜欢用深拷贝,直接改用函数表示,需要时直接调用该函数生成即可 -def MAP_STATE_EMPTY(): # 由于同一格显然不可能种两个相同的植物,所以用集合 - return {MAP_PLANT:set(), MAP_SLEEP:False, MAP_PLOT_TYPE:MAP_GRASS} -def MAP_STATE_WATER(): - return {MAP_PLANT:set(), MAP_SLEEP:False, MAP_PLOT_TYPE:MAP_WATER} -def MAP_STATE_TILE(): - return {MAP_PLANT:set(), MAP_SLEEP:False, MAP_PLOT_TYPE:MAP_TILE} -def MAP_STATE_UNAVAILABLE(): - return {MAP_PLANT:set(), MAP_SLEEP:False, MAP_PLOT_TYPE:MAP_UNAVAILABLE} +# 由于同一格显然不可能种两个相同的植物,所以用集合 +def INIT_MAP_GRID(PLOT_TYPE): + return {MAP_PLANT:set(), MAP_SLEEP:False, MAP_PLOT_TYPE:POLT_TYPE} # 地图相关像素数据 BACKGROUND_OFFSET_X = 220 From da7c3ce908857c497c52980fd54dde664ecc5e4d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=98=9F=E5=A4=96=E4=B9=8B=E7=A5=9E?= Date: Wed, 8 Jun 2022 10:55:22 +0800 Subject: [PATCH 18/25] =?UTF-8?q?=E6=9B=B4=E6=94=B9infbi?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- source/constants.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/constants.py b/source/constants.py index 84ac369..b00903a 100755 --- a/source/constants.py +++ b/source/constants.py @@ -639,4 +639,4 @@ CHOOSE = 'choose' PLAY = 'play' # 无穷大常量 -INF = float('inf') +INF = 1e1024 # 等价于float("inf") From c967c56c54d9f8215d1cec223ce921933b44410c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=98=9F=E5=A4=96=E4=B9=8B=E7=A5=9E?= Date: Wed, 8 Jun 2022 11:04:25 +0800 Subject: [PATCH 19/25] =?UTF-8?q?=E6=81=A2=E5=A4=8Dinf=E5=8E=9F=E8=AE=BE?= =?UTF-8?q?=E5=AE=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- source/constants.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/constants.py b/source/constants.py index b00903a..bf5d3dc 100755 --- a/source/constants.py +++ b/source/constants.py @@ -639,4 +639,4 @@ CHOOSE = 'choose' PLAY = 'play' # 无穷大常量 -INF = 1e1024 # 等价于float("inf") +INF = float("inf") From ecd511495de3be7d8a026e8c49d076154bc59479 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=98=9F=E5=A4=96=E4=B9=8B=E7=A5=9E?= Date: Thu, 9 Jun 2022 11:06:39 +0800 Subject: [PATCH 20/25] =?UTF-8?q?=E6=95=B4=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.py | 6 +++--- source/constants.py | 2 +- source/state/screen.py | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/main.py b/main.py index 39052fe..75571f0 100755 --- a/main.py +++ b/main.py @@ -7,10 +7,10 @@ from source.state import mainmenu, screen, level if __name__=='__main__': # 控制状态机运行 game = tool.Control() - state_dict = { c.MAIN_MENU: mainmenu.Menu(), + state_dict = { c.MAIN_MENU: mainmenu.Menu(), c.GAME_VICTORY: screen.GameVictoryScreen(), - c.GAME_LOSE: screen.GameLoseScreen(), - c.LEVEL: level.Level() + c.GAME_LOSE: screen.GameLoseScreen(), + c.LEVEL: level.Level() } game.setup_states(state_dict, c.MAIN_MENU) game.run() diff --git a/source/constants.py b/source/constants.py index bf5d3dc..8ab6161 100755 --- a/source/constants.py +++ b/source/constants.py @@ -639,4 +639,4 @@ CHOOSE = 'choose' PLAY = 'play' # 无穷大常量 -INF = float("inf") +INF = float("inf") # python传递字符串性能较低,故在这里对inf声明一次,以后仅需调用即可 diff --git a/source/state/screen.py b/source/state/screen.py index c865f4d..a08793e 100644 --- a/source/state/screen.py +++ b/source/state/screen.py @@ -30,7 +30,7 @@ class Screen(tool.State): self.rect.y = 0 def update(self, surface, current_time, mouse_pos, mouse_click): - if(current_time - self.start_time) < self.end_time: + if (current_time - self.start_time) < self.end_time: surface.fill(c.WHITE) surface.blit(self.image, self.rect) else: From cbc1cbd97f531101fc7a7aab55f0edc5caafda93 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=98=9F=E5=A4=96=E4=B9=8B=E7=A5=9E?= Date: Sat, 11 Jun 2022 19:09:52 +0800 Subject: [PATCH 21/25] =?UTF-8?q?=E4=BC=98=E5=8C=96=E8=BE=93=E5=87=BA?= =?UTF-8?q?=E6=98=BE=E7=A4=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- source/tool.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/tool.py b/source/tool.py index d35852e..fdc2e46 100755 --- a/source/tool.py +++ b/source/tool.py @@ -90,7 +90,7 @@ class Control(): self.mouse_pos = pg.mouse.get_pos() self.mouse_click[0], _, self.mouse_click[1] = pg.mouse.get_pressed() # self.mouse_click[0]表示左键,self.mouse_click[1]表示右键 - print('点击位置:', self.mouse_pos, '左右键点击情况:', self.mouse_click) + print(f'''点击位置: ({self.mouse_pos[0]:3}, {self.mouse_pos[1]:3}) 左右键点击情况: {self.mouse_click}''') def run(self): From 8d06608e148a18cb850d4b4577c15a4ce7775f1b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=98=9F=E5=A4=96=E4=B9=8B=E7=A5=9E?= Date: Sat, 11 Jun 2022 19:12:32 +0800 Subject: [PATCH 22/25] =?UTF-8?q?=E4=BC=98=E5=8C=96=E7=82=B9=E5=87=BB?= =?UTF-8?q?=E4=BD=8D=E7=BD=AE=E8=BE=93=E5=87=BA=E6=98=BE=E7=A4=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- source/tool.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/source/tool.py b/source/tool.py index fdc2e46..5d1249e 100755 --- a/source/tool.py +++ b/source/tool.py @@ -90,7 +90,8 @@ class Control(): self.mouse_pos = pg.mouse.get_pos() self.mouse_click[0], _, self.mouse_click[1] = pg.mouse.get_pressed() # self.mouse_click[0]表示左键,self.mouse_click[1]表示右键 - print(f'''点击位置: ({self.mouse_pos[0]:3}, {self.mouse_pos[1]:3}) 左右键点击情况: {self.mouse_click}''') + print( f"点击位置: ({self.mouse_pos[0]:3}, {self.mouse_pos[1]:3})", + f"左右键点击情况: {self.mouse_click}") def run(self): From 54e40df6c411e8d67372203ac3f73d20243abe94 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=98=9F=E5=A4=96=E4=B9=8B=E7=A5=9E?= Date: Sun, 12 Jun 2022 13:47:13 +0800 Subject: [PATCH 23/25] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=8F=98=E9=87=8F?= =?UTF-8?q?=E5=90=8D=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- source/constants.py | 2 +- source/tool.py | 12 +++++------- 2 files changed, 6 insertions(+), 8 deletions(-) diff --git a/source/constants.py b/source/constants.py index 8ab6161..71550f7 100755 --- a/source/constants.py +++ b/source/constants.py @@ -162,7 +162,7 @@ MAP_UNAVAILABLE = 'unavailable' # 指完全不能种植物的地方,包括无 # 不喜欢用深拷贝,直接改用函数表示,需要时直接调用该函数生成即可 # 由于同一格显然不可能种两个相同的植物,所以用集合 def INIT_MAP_GRID(PLOT_TYPE): - return {MAP_PLANT:set(), MAP_SLEEP:False, MAP_PLOT_TYPE:POLT_TYPE} + return {MAP_PLANT:set(), MAP_SLEEP:False, MAP_PLOT_TYPE:PLOT_TYPE} # 地图相关像素数据 BACKGROUND_OFFSET_X = 220 diff --git a/source/tool.py b/source/tool.py index 5d1249e..4248d7d 100755 --- a/source/tool.py +++ b/source/tool.py @@ -14,7 +14,7 @@ class State(): self.done = False # false 代表未做完 self.next = None # 表示这个状态退出后要转到的下一个状态 self.persist = {} # 在状态间转换时需要传递的数据 - + # 当从其他状态进入这个状态时,需要进行的初始化操作 @abstractmethod def startup(self, current_time, persist): @@ -189,16 +189,14 @@ def load_all_gfx(directory, colorkey=c.WHITE, accept=('.png', '.jpg', '.bmp', '. # 用于消除文件边框影响 def loadZombieImageRect(): file_path = os.path.join(os.path.dirname(os.path.dirname(__file__)), 'resources', 'data', 'entity', 'zombie.json') - f = open(file_path) - data = json.load(f) - f.close() + with open(file_path) as f: + data = json.load(f) return data[c.ZOMBIE_IMAGE_RECT] def loadPlantImageRect(): file_path = os.path.join(os.path.dirname(os.path.dirname(__file__)), 'resources', 'data', 'entity', 'plant.json') - f = open(file_path) - data = json.load(f) - f.close() + with open(file_path) as f: + data = json.load(f) return data[c.PLANT_IMAGE_RECT] pg.init() From c3dc470dae1bb97963fdc2a02604d4ae0c68fa27 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=98=9F=E5=A4=96=E4=B9=8B=E7=A5=9E?= Date: Sun, 12 Jun 2022 15:57:04 +0800 Subject: [PATCH 24/25] =?UTF-8?q?=E5=8D=87=E7=BA=A7=E5=88=B0python=203.10?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 +- source/state/level.py | 185 +++++++++++++++++++++--------------------- 2 files changed, 94 insertions(+), 93 deletions(-) diff --git a/README.md b/README.md index e3522de..8c89457 100644 --- a/README.md +++ b/README.md @@ -21,7 +21,7 @@ ## 环境要求 -* `Python` >= 3.7,最好使用最新版 +* `Python` >= 3.10,最好使用最新版 * `Python-Pygame` >= 1.9,最好使用最新版 ## 开始游戏 diff --git a/source/state/level.py b/source/state/level.py index 9358848..8fda7ad 100644 --- a/source/state/level.py +++ b/source/state/level.py @@ -800,35 +800,35 @@ class Level(tool.State): x, y = self.map.getMapGridPos(0, map_y) # 新增的僵尸也需要在这里声明 - if name == c.NORMAL_ZOMBIE: - self.zombie_groups[map_y].add(zombie.NormalZombie(c.ZOMBIE_START_X + random.randint(-20, 20) + hugeWaveMove, y, self.head_group)) - elif name == c.CONEHEAD_ZOMBIE: - self.zombie_groups[map_y].add(zombie.ConeHeadZombie(c.ZOMBIE_START_X + random.randint(-20, 20) + hugeWaveMove, y, self.head_group)) - elif name == c.BUCKETHEAD_ZOMBIE: - self.zombie_groups[map_y].add(zombie.BucketHeadZombie(c.ZOMBIE_START_X + random.randint(-20, 20) + hugeWaveMove, y, self.head_group)) - elif name == c.FLAG_ZOMBIE: - self.zombie_groups[map_y].add(zombie.FlagZombie(c.ZOMBIE_START_X, y, self.head_group)) - elif name == c.NEWSPAPER_ZOMBIE: - self.zombie_groups[map_y].add(zombie.NewspaperZombie(c.ZOMBIE_START_X + random.randint(-20, 20) + hugeWaveMove, y, self.head_group)) - elif name == c.FOOTBALL_ZOMBIE: - self.zombie_groups[map_y].add(zombie.FootballZombie(c.ZOMBIE_START_X + random.randint(-20, 20) + hugeWaveMove, y, self.head_group)) - elif name == c.DUCKY_TUBE_ZOMBIE: - self.zombie_groups[map_y].add(zombie.DuckyTubeZombie(c.ZOMBIE_START_X + random.randint(-20, 20) + hugeWaveMove, y, self.head_group)) - elif name == c.CONEHEAD_DUCKY_TUBE_ZOMBIE: - self.zombie_groups[map_y].add(zombie.ConeHeadDuckyTubeZombie(c.ZOMBIE_START_X + random.randint(-20, 20) + hugeWaveMove, y, self.head_group)) - elif name == c.BUCKETHEAD_DUCKY_TUBE_ZOMBIE: - self.zombie_groups[map_y].add(zombie.BucketHeadDuckyTubeZombie(c.ZOMBIE_START_X + random.randint(-20, 20) + hugeWaveMove, y, self.head_group)) - elif name == c.SCREEN_DOOR_ZOMBIE: - self.zombie_groups[map_y].add(zombie.ScreenDoorZombie(c.ZOMBIE_START_X + random.randint(-20, 20) + hugeWaveMove, y, self.head_group)) - elif name == c.POLE_VAULTING_ZOMBIE: - # 本来撑杆跳生成位置不同,对齐左端可认为修正了一部分(看作移动了70),只需要相对修改即可 - self.zombie_groups[map_y].add(zombie.PoleVaultingZombie(c.ZOMBIE_START_X + random.randint(0, 10) + hugeWaveMove, y, self.head_group)) - elif name == c.ZOMBONI: - # 冰车僵尸生成位置不同 - self.zombie_groups[map_y].add(zombie.Zomboni(c.ZOMBIE_START_X + random.randint(0, 10) + hugeWaveMove, y, self.plant_groups[map_y], self.map, plant.IceFrozenPlot)) - elif name == c.SNORKELZOMBIE: - # 潜水僵尸生成位置不同 - self.zombie_groups[map_y].add(zombie.SnorkelZombie(c.ZOMBIE_START_X + random.randint(0, 10) + hugeWaveMove, y, self.head_group)) + match name: + case c.NORMAL_ZOMBIE: + self.zombie_groups[map_y].add(zombie.NormalZombie(c.ZOMBIE_START_X + random.randint(-20, 20) + hugeWaveMove, y, self.head_group)) + case c.CONEHEAD_ZOMBIE: + self.zombie_groups[map_y].add(zombie.ConeHeadZombie(c.ZOMBIE_START_X + random.randint(-20, 20) + hugeWaveMove, y, self.head_group)) + case c.BUCKETHEAD_ZOMBIE: + self.zombie_groups[map_y].add(zombie.BucketHeadZombie(c.ZOMBIE_START_X + random.randint(-20, 20) + hugeWaveMove, y, self.head_group)) + case c.FLAG_ZOMBIE: + self.zombie_groups[map_y].add(zombie.FlagZombie(c.ZOMBIE_START_X, y, self.head_group)) + case c.NEWSPAPER_ZOMBIE: + self.zombie_groups[map_y].add(zombie.NewspaperZombie(c.ZOMBIE_START_X + random.randint(-20, 20) + hugeWaveMove, y, self.head_group)) + case c.FOOTBALL_ZOMBIE: + self.zombie_groups[map_y].add(zombie.FootballZombie(c.ZOMBIE_START_X + random.randint(-20, 20) + hugeWaveMove, y, self.head_group)) + case c.DUCKY_TUBE_ZOMBIE: + self.zombie_groups[map_y].add(zombie.DuckyTubeZombie(c.ZOMBIE_START_X + random.randint(-20, 20) + hugeWaveMove, y, self.head_group)) + case c.CONEHEAD_DUCKY_TUBE_ZOMBIE: + self.zombie_groups[map_y].add(zombie.ConeHeadDuckyTubeZombie(c.ZOMBIE_START_X + random.randint(-20, 20) + hugeWaveMove, y, self.head_group)) + case c.BUCKETHEAD_DUCKY_TUBE_ZOMBIE: + self.zombie_groups[map_y].add(zombie.BucketHeadDuckyTubeZombie(c.ZOMBIE_START_X + random.randint(-20, 20) + hugeWaveMove, y, self.head_group)) + case c.SCREEN_DOOR_ZOMBIE: + self.zombie_groups[map_y].add(zombie.ScreenDoorZombie(c.ZOMBIE_START_X + random.randint(-20, 20) + hugeWaveMove, y, self.head_group)) + case c.POLE_VAULTING_ZOMBIE: + # 本来撑杆跳生成位置不同,对齐左端可认为修正了一部分(看作移动了70),只需要相对修改即可 + self.zombie_groups[map_y].add(zombie.PoleVaultingZombie(c.ZOMBIE_START_X + random.randint(0, 10) + hugeWaveMove, y, self.head_group)) + case c.ZOMBONI: + # 冰车僵尸生成位置不同 + self.zombie_groups[map_y].add(zombie.Zomboni(c.ZOMBIE_START_X + random.randint(0, 10) + hugeWaveMove, y, self.plant_groups[map_y], self.map, plant.IceFrozenPlot)) + case c.SNORKELZOMBIE: + self.zombie_groups[map_y].add(zombie.SnorkelZombie(c.ZOMBIE_START_X + random.randint(0, 10) + hugeWaveMove, y, self.head_group)) # 能否种植物的判断: # 先判断位置是否合法 isValid(map_x, map_y) @@ -852,69 +852,70 @@ class Level(tool.State): map_x, map_y = self.map.getMapIndex(x, y) # 新植物也需要在这里声明 - if self.plant_name == c.SUNFLOWER: - new_plant = plant.SunFlower(x, y, self.sun_group) - elif self.plant_name == c.PEASHOOTER: - new_plant = plant.PeaShooter(x, y, self.bullet_groups[map_y]) - elif self.plant_name == c.SNOWPEASHOOTER: - new_plant = plant.SnowPeaShooter(x, y, self.bullet_groups[map_y]) - elif self.plant_name == c.WALLNUT: - new_plant = plant.WallNut(x, y) - elif self.plant_name == c.CHERRYBOMB: - new_plant = plant.CherryBomb(x, y) - elif self.plant_name == c.THREEPEASHOOTER: - new_plant = plant.ThreePeaShooter(x, y, self.bullet_groups, map_y, self.map.background_type) - elif self.plant_name == c.REPEATERPEA: - new_plant = plant.RepeaterPea(x, y, self.bullet_groups[map_y]) - elif self.plant_name == c.CHOMPER: - new_plant = plant.Chomper(x, y) - elif self.plant_name == c.PUFFSHROOM: - new_plant = plant.PuffShroom(x, y, self.bullet_groups[map_y]) - elif self.plant_name == c.POTATOMINE: - new_plant = plant.PotatoMine(x, y) - elif self.plant_name == c.SQUASH: - new_plant = plant.Squash(x, y, self.map.map[map_y][map_x][c.MAP_PLANT]) - elif self.plant_name == c.SPIKEWEED: - new_plant = plant.Spikeweed(x, y) - elif self.plant_name == c.JALAPENO: - new_plant = plant.Jalapeno(x, y) - elif self.plant_name == c.SCAREDYSHROOM: - new_plant = plant.ScaredyShroom(x, y, self.bullet_groups[map_y]) - elif self.plant_name == c.SUNSHROOM: - new_plant = plant.SunShroom(x, y, self.sun_group) - elif self.plant_name == c.ICESHROOM: - new_plant = plant.IceShroom(x, y) - elif self.plant_name == c.HYPNOSHROOM: - new_plant = plant.HypnoShroom(x, y) - elif self.plant_name == c.WALLNUTBOWLING: - new_plant = plant.WallNutBowling(x, y, map_y, self) - elif self.plant_name == c.REDWALLNUTBOWLING: - new_plant = plant.RedWallNutBowling(x, y) - elif self.plant_name == c.LILYPAD: - new_plant = plant.LilyPad(x, y) - elif self.plant_name == c.TORCHWOOD: - new_plant = plant.TorchWood(x, y, self.bullet_groups[map_y]) - elif self.plant_name == c.STARFRUIT: - new_plant = plant.StarFruit(x, y, self.bullet_groups[map_y], self) - elif self.plant_name == c.COFFEEBEAN: - new_plant = plant.CoffeeBean(x, y, self.plant_groups[map_y], self.map.map[map_y][map_x], self.map, map_x) - elif self.plant_name == c.SEASHROOM: - new_plant = plant.SeaShroom(x, y, self.bullet_groups[map_y]) - elif self.plant_name == c.TALLNUT: - new_plant = plant.TallNut(x, y) - elif self.plant_name == c.TANGLEKLEP: - new_plant = plant.TangleKlep(x, y) - elif self.plant_name == c.DOOMSHROOM: - if self.map.gridHeightSize == c.GRID_Y_SIZE: - new_plant = plant.DoomShroom(x, y, self.map.map[map_y][map_x][c.MAP_PLANT], explode_y_range=2) - else: - new_plant = plant.DoomShroom(x, y, self.map.map[map_y][map_x][c.MAP_PLANT], explode_y_range=3) - elif self.plant_name == c.GRAVEBUSTER: - new_plant = plant.GraveBuster(x, y, self.plant_groups[map_y], self.map, map_x) - elif self.plant_name == c.FUMESHROOM: - new_plant = plant.FumeShroom(x, y, self.bullet_groups[map_y], self.zombie_groups[map_y]) - elif self.plant_name == c.GARLIC: - new_plant = plant.Garlic(x, y) + match self.plant_name: + case c.SUNFLOWER: + new_plant = plant.SunFlower(x, y, self.sun_group) + case c.PEASHOOTER: + new_plant = plant.PeaShooter(x, y, self.bullet_groups[map_y]) + case c.SNOWPEASHOOTER: + new_plant = plant.SnowPeaShooter(x, y, self.bullet_groups[map_y]) + case c.WALLNUT: + new_plant = plant.WallNut(x, y) + case c.CHERRYBOMB: + new_plant = plant.CherryBomb(x, y) + case c.THREEPEASHOOTER: + new_plant = plant.ThreePeaShooter(x, y, self.bullet_groups, map_y, self.map.background_type) + case c.REPEATERPEA: + new_plant = plant.RepeaterPea(x, y, self.bullet_groups[map_y]) + case c.CHOMPER: + new_plant = plant.Chomper(x, y) + case c.PUFFSHROOM: + new_plant = plant.PuffShroom(x, y, self.bullet_groups[map_y]) + case c.POTATOMINE: + new_plant = plant.PotatoMine(x, y) + case c.SQUASH: + new_plant = plant.Squash(x, y, self.map.map[map_y][map_x][c.MAP_PLANT]) + case c.SPIKEWEED: + new_plant = plant.Spikeweed(x, y) + case c.JALAPENO: + new_plant = plant.Jalapeno(x, y) + case c.SCAREDYSHROOM: + new_plant = plant.ScaredyShroom(x, y, self.bullet_groups[map_y]) + case c.SUNSHROOM: + new_plant = plant.SunShroom(x, y, self.sun_group) + case c.ICESHROOM: + new_plant = plant.IceShroom(x, y) + case c.HYPNOSHROOM: + new_plant = plant.HypnoShroom(x, y) + case c.WALLNUTBOWLING: + new_plant = plant.WallNutBowling(x, y, map_y, self) + case c.REDWALLNUTBOWLING: + new_plant = plant.RedWallNutBowling(x, y) + case c.LILYPAD: + new_plant = plant.LilyPad(x, y) + case c.TORCHWOOD: + new_plant = plant.TorchWood(x, y, self.bullet_groups[map_y]) + case c.STARFRUIT: + new_plant = plant.StarFruit(x, y, self.bullet_groups[map_y], self) + case c.COFFEEBEAN: + new_plant = plant.CoffeeBean(x, y, self.plant_groups[map_y], self.map.map[map_y][map_x], self.map, map_x) + case c.SEASHROOM: + new_plant = plant.SeaShroom(x, y, self.bullet_groups[map_y]) + case c.TALLNUT: + new_plant = plant.TallNut(x, y) + case c.TANGLEKLEP: + new_plant = plant.TangleKlep(x, y) + case c.DOOMSHROOM: + if self.map.gridHeightSize == c.GRID_Y_SIZE: + new_plant = plant.DoomShroom(x, y, self.map.map[map_y][map_x][c.MAP_PLANT], explode_y_range=3) + else: + new_plant = plant.DoomShroom(x, y, self.map.map[map_y][map_x][c.MAP_PLANT], explode_y_range=2) + case c.GRAVEBUSTER: + new_plant = plant.GraveBuster(x, y, self.plant_groups[map_y], self.map, map_x) + case c.FUMESHROOM: + new_plant = plant.FumeShroom(x, y, self.bullet_groups[map_y], self.zombie_groups[map_y]) + case c.GARLIC: + new_plant = plant.Garlic(x, y) if new_plant.can_sleep and self.background_type in c.DAYTIME_BACKGROUNDS: From 2af6371a1016c3725fc67b4a4ee888dcf8085364 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=98=9F=E5=A4=96=E4=B9=8B=E7=A5=9E?= Date: Sun, 12 Jun 2022 16:11:16 +0800 Subject: [PATCH 25/25] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=83=B5=E5=B0=B8?= =?UTF-8?q?=E5=95=83=E9=A3=9F=E7=9A=84bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- source/component/zombie.py | 2 +- source/state/level.py | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/source/component/zombie.py b/source/component/zombie.py index 9eec302..2b0adab 100755 --- a/source/component/zombie.py +++ b/source/component/zombie.py @@ -433,7 +433,7 @@ class Zombie(pg.sprite.Sprite): self.health -= damage else: print('警告:植物攻击类型错误,现在默认进行类豌豆射手型攻击') - setDamage(damage, effect=effect, damageType=c.ZOMBIE_DEAFULT_DAMAGE) + self.setDamage(damage, effect=effect, damageType=c.ZOMBIE_DEAFULT_DAMAGE) # 记录攻击时间 self.hit_timer = self.current_time diff --git a/source/state/level.py b/source/state/level.py index 8fda7ad..4c37b62 100644 --- a/source/state/level.py +++ b/source/state/level.py @@ -1041,7 +1041,7 @@ class Level(tool.State): # 被攻击对象是植物时才可能刷新 if zombie.prey_is_plant: # 新植物种在被攻击植物同一格时才可能刷新 - if (mapX, mapY) == self.newPlantAndPositon[1]: + if (zombie.preyMapX, zombie.preyMapY) == self.newPlantAndPositon[1]: # 如果被攻击植物是睡莲和花盆,同一格种了植物必然刷新 # 如果被攻击植物不是睡莲和花盆,同一格种了南瓜头才刷新 if ((zombie.prey.name not in {c.LILYPAD, "花盆(未实现)"}) @@ -1098,11 +1098,11 @@ class Level(tool.State): targetPlant = None if targetPlant: + zombie.preyMapX, zombie.preyMapY = self.map.getMapIndex(targetPlant.rect.centerx, targetPlant.rect.centery) # 撑杆跳的特殊情况 if zombie.name in {c.POLE_VAULTING_ZOMBIE} and (not zombie.jumped): if not zombie.jumping: - zombie.jumpMap_x, zombie.jumpMap_y = self.map.getMapIndex(targetPlant.rect.centerx, targetPlant.rect.centery) - zombie.jumpMap_x, zombie.jumpMap_y = min(c.GRID_X_LEN - 1, zombie.jumpMap_x), min(self.map_y_len - 1, zombie.jumpMap_y) + zombie.jumpMap_x, zombie.jumpMap_y = min(c.GRID_X_LEN - 1, zombie.preyMapX), min(self.map_y_len - 1, zombie.preyMapY) jumpX = targetPlant.rect.x - c.GRID_X_SIZE * 0.6 if c.TALLNUT in self.map.map[zombie.jumpMap_y][zombie.jumpMap_x][c.MAP_PLANT]: zombie.setJump(False, jumpX)