From f38e415add63146a9fb48d2869a800f4b070f2de 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, 11 Aug 2022 16:11:11 +0800 Subject: [PATCH 1/5] =?UTF-8?q?=E5=B0=86screen=E8=AE=BE=E7=BD=AE=E4=B8=BA?= =?UTF-8?q?=E6=8A=BD=E8=B1=A1=E5=9F=BA=E7=B1=BB=EF=BC=9B=E9=87=8D=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- source/component/map.py | 8 ++++---- source/component/menubar.py | 26 ++++++++++++++------------ source/state/level.py | 4 +++- source/state/screen.py | 2 ++ 4 files changed, 23 insertions(+), 17 deletions(-) diff --git a/source/component/map.py b/source/component/map.py index 87b6d40..11af345 100755 --- a/source/component/map.py +++ b/source/component/map.py @@ -56,10 +56,10 @@ class Map(): ] def isValid(self, map_x, map_y): - if (map_x < 0 or map_x >= self.width or - map_y < 0 or map_y >= self.height): - return False - return True + if ((0 <= map_x < self.width) + and (0 <= map_y < self.height)): + return True + return False # 地图单元格状态 # 注意是可变对象,不能直接引用 diff --git a/source/component/menubar.py b/source/component/menubar.py index 52bd69b..96cf58d 100755 --- a/source/component/menubar.py +++ b/source/component/menubar.py @@ -22,7 +22,8 @@ def getSunValueImage(sun_value): return image def getCardPool(data): - card_pool = {c.PLANT_CARD_INFO[c.PLANT_CARD_INDEX[card_name]]: data[card_name] for card_name in data} + card_pool = {c.PLANT_CARD_INFO[c.PLANT_CARD_INDEX[card_name]]: data[card_name] + for card_name in data} return card_pool class Card(): @@ -36,7 +37,8 @@ class Card(): self.sun_cost_img = font.render(str(c.PLANT_CARD_INFO[index][c.SUN_INDEX]), True, c.BLACK) self.sun_cost_img_rect = self.sun_cost_img.get_rect() sun_cost_img_x = 32 - self.sun_cost_img_rect.w - self.orig_image.blit(self.sun_cost_img, (sun_cost_img_x, 52, self.sun_cost_img_rect.w, self.sun_cost_img_rect.h)) + self.orig_image.blit(self.sun_cost_img, + (sun_cost_img_x, 52, self.sun_cost_img_rect.w, self.sun_cost_img_rect.h)) self.index = index self.sun_cost = c.PLANT_CARD_INFO[index][c.SUN_INDEX] @@ -86,7 +88,7 @@ class Card(): image = pg.Surface((self.rect.w, self.rect.h)) # 黑底 frozen_image = self.orig_image frozen_image.set_alpha(128) - frozen_height = (self.frozen_time - time)/self.frozen_time * self.rect.h + frozen_height = ((self.frozen_time - time)/self.frozen_time) * self.rect.h image.blit(frozen_image, (0,0), (0, 0, self.rect.w, frozen_height)) self.orig_image.set_alpha(192) @@ -177,8 +179,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 (self.rect.x <= x <= self.rect.right and + self.rect.y <= y <= self.rect.bottom): return True return False @@ -299,8 +301,8 @@ class Panel(): return False x, y = mouse_pos - if (x >= self.button_rect.x and x <= self.button_rect.right and - y >= self.button_rect.y and y <= self.button_rect.bottom): + if (self.button_rect.x <= x <= self.button_rect.right and + self.button_rect.y <= y <= self.button_rect.bottom): return True return False @@ -349,8 +351,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 (self.rect.x <= x <= self.rect.right and + self.rect.y <= y <= self.rect.bottom): return True return False @@ -426,7 +428,7 @@ class MoveBar(): card.update(left_x, self.current_time) left_x = card.rect.right + 1 - if(self.current_time - self.create_timer) > c.MOVEBAR_CARD_FRESH_TIME: + if (self.current_time - self.create_timer) > c.MOVEBAR_CARD_FRESH_TIME: if self.createCard(): self.create_timer = self.current_time @@ -440,8 +442,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 (self.rect.x <= x <= self.rect.right and + self.rect.y <= y <= self.rect.bottom): return True return False diff --git a/source/state/level.py b/source/state/level.py index 1602ead..3b2c848 100644 --- a/source/state/level.py +++ b/source/state/level.py @@ -26,6 +26,7 @@ class Level(tool.State): # 导入地图参数 self.loadMap() self.map = map.Map(self.map_data[c.BACKGROUND_TYPE]) + self.map_x_len = self.map.width self.map_y_len = self.map.height self.setupBackground() self.initState() @@ -1115,7 +1116,8 @@ class Level(tool.State): zombie.health = 0 c.SOUND_BOWLING_IMPACT.play() elif not zombie.jumping: - zombie.jump_map_x, zombie.jump_map_y = min(c.GRID_X_LEN - 1, zombie.prey_map_x), min(self.map_y_len - 1, zombie.prey_map_y) + zombie.jump_map_x = min(self.map_x_len - 1, zombie.prey_map_x) + zombie.jump_map_y = min(self.map_y_len - 1, zombie.prey_map_y) jump_x = target_plant.rect.x - c.GRID_X_SIZE * 0.6 if c.TALLNUT in self.map.map[zombie.jump_map_y][zombie.jump_map_x][c.MAP_PLANT]: zombie.setJump(False, jump_x) diff --git a/source/state/screen.py b/source/state/screen.py index 6837c0c..ea8fd6c 100644 --- a/source/state/screen.py +++ b/source/state/screen.py @@ -1,5 +1,6 @@ import os import pygame as pg +from abc import abstractmethod from .. import tool from .. import constants as c @@ -7,6 +8,7 @@ class Screen(tool.State): def __init__(self): tool.State.__init__(self) + @abstractmethod def startup(self, current_time, persist): pass From f46585264d260e83cfd495dbe3f51bcf6db7456a 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, 14 Aug 2022 10:53:40 +0800 Subject: [PATCH 2/5] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E9=93=B2=E5=AD=90?= =?UTF-8?q?=E4=BD=8D=E7=BD=AE=E5=AF=BC=E8=87=B4=E7=9A=84=E5=88=97=E8=A1=A8?= =?UTF-8?q?=E8=B6=8A=E7=95=8Cbug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- source/state/level.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/source/state/level.py b/source/state/level.py index 3b2c848..f8d711f 100644 --- a/source/state/level.py +++ b/source/state/level.py @@ -632,6 +632,9 @@ class Level(tool.State): def shovelRemovePlant(self, mouse_pos): x, y = mouse_pos map_x, map_y = self.map.getMapIndex(x, y) + if ((map_y < 0) or (map_y >= self.map.height) + or (map_x < 0) or (map_x >= self.map.width)): + return for i in self.plant_groups[map_y]: if (x >= i.rect.x and x <= i.rect.right and y >= i.rect.y and y <= i.rect.bottom): @@ -1469,6 +1472,9 @@ class Level(tool.State): # 铲子接近植物时会高亮提示 map_x, map_y = self.map.getMapIndex(x, y) surface.blit(self.shovel, self.shovel_rect) + if ((map_y < 0) or (map_y >= self.map.height) + or (map_x < 0) or (map_x >= self.map.width)): + return for i in self.plant_groups[map_y]: if (x >= i.rect.x and x <= i.rect.right and y >= i.rect.y and y <= i.rect.bottom): From cf20f9733f2bf2c2ece63c0174ed8737763a88c9 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, 14 Aug 2022 10:58:12 +0800 Subject: [PATCH 3/5] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E6=9B=B4=E5=A4=9A?= =?UTF-8?q?=E7=9A=84=E5=9C=B0=E5=9B=BE=E8=B6=8A=E7=95=8Cbug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- source/state/level.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/source/state/level.py b/source/state/level.py index f8d711f..46f38bf 100644 --- a/source/state/level.py +++ b/source/state/level.py @@ -333,8 +333,8 @@ class Level(tool.State): return self.before_pause_time def initBowlingMap(self): - for x in range(3, self.map.width): - for y in range(self.map.height): + for x in range(3, self.map_x_len): + for y in range(self.map_y_len): self.map.setMapGridType(x, y, c.MAP_UNAVAILABLE) # 将坚果保龄球红线右侧设置为不可种植任何植物 def initState(self): @@ -632,8 +632,8 @@ class Level(tool.State): def shovelRemovePlant(self, mouse_pos): x, y = mouse_pos map_x, map_y = self.map.getMapIndex(x, y) - if ((map_y < 0) or (map_y >= self.map.height) - or (map_x < 0) or (map_x >= self.map.width)): + if ((map_y < 0) or (map_y >= self.map_y_len) + or (map_x < 0) or (map_x >= self.map_x_len)): return for i in self.plant_groups[map_y]: if (x >= i.rect.x and x <= i.rect.right and @@ -1085,7 +1085,7 @@ class Level(tool.State): # 默认为最右侧的一个植物 target_plant = max(attackable_common_plants, key=lambda i: i.rect.x) map_x, map_y = self.map.getMapIndex(target_plant.rect.centerx, target_plant.rect.centery) - if not (map_x >= self.map.width or map_y >= self.map.height): + if (0 <= map_x < self.map_x_len and 0 <= map_y < self.map_y_len): if c.PUMPKINHEAD in self.map.map[map_y][map_x][c.MAP_PLANT]: for actual_target_plant in self.plant_groups[i]: # 检测同一格的其他植物 @@ -1472,8 +1472,8 @@ class Level(tool.State): # 铲子接近植物时会高亮提示 map_x, map_y = self.map.getMapIndex(x, y) surface.blit(self.shovel, self.shovel_rect) - if ((map_y < 0) or (map_y >= self.map.height) - or (map_x < 0) or (map_x >= self.map.width)): + if ((map_y < 0) or (map_y >= self.map_y_len) + or (map_x < 0) or (map_x >= self.map_x_len)): return for i in self.plant_groups[map_y]: if (x >= i.rect.x and x <= i.rect.right and From 57501a8f222dc2ea53c5f2eeca1dd3b35f2b78cc 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, 14 Aug 2022 11:03:55 +0800 Subject: [PATCH 4/5] =?UTF-8?q?=E7=BB=9F=E4=B8=80=E8=B6=8A=E7=95=8C?= =?UTF-8?q?=E5=88=A4=E6=96=AD=E5=87=BD=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- source/state/level.py | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/source/state/level.py b/source/state/level.py index 46f38bf..be0370e 100644 --- a/source/state/level.py +++ b/source/state/level.py @@ -632,8 +632,7 @@ class Level(tool.State): def shovelRemovePlant(self, mouse_pos): x, y = mouse_pos map_x, map_y = self.map.getMapIndex(x, y) - if ((map_y < 0) or (map_y >= self.map_y_len) - or (map_x < 0) or (map_x >= self.map_x_len)): + if not self.map.isValid(map_x, map_y): return for i in self.plant_groups[map_y]: if (x >= i.rect.x and x <= i.rect.right and @@ -1085,7 +1084,7 @@ class Level(tool.State): # 默认为最右侧的一个植物 target_plant = max(attackable_common_plants, key=lambda i: i.rect.x) map_x, map_y = self.map.getMapIndex(target_plant.rect.centerx, target_plant.rect.centery) - if (0 <= map_x < self.map_x_len and 0 <= map_y < self.map_y_len): + if self.map.isValid(map_x, map_y): if c.PUMPKINHEAD in self.map.map[map_y][map_x][c.MAP_PLANT]: for actual_target_plant in self.plant_groups[i]: # 检测同一格的其他植物 @@ -1472,8 +1471,7 @@ class Level(tool.State): # 铲子接近植物时会高亮提示 map_x, map_y = self.map.getMapIndex(x, y) surface.blit(self.shovel, self.shovel_rect) - if ((map_y < 0) or (map_y >= self.map_y_len) - or (map_x < 0) or (map_x >= self.map_x_len)): + if not self.map.isValid(map_x, map_y): return for i in self.plant_groups[map_y]: if (x >= i.rect.x and x <= i.rect.right and From 8ce2ccf734c7049e4cc6bb4786a213a8cfe5e6be 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, 30 Aug 2022 15:06:03 +0800 Subject: [PATCH 5/5] =?UTF-8?q?=E7=BB=9F=E4=B8=80=E5=8F=98=E9=87=8F?= =?UTF-8?q?=E5=91=BD=E5=90=8D=E9=A3=8E=E6=A0=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- source/component/plant.py | 12 ++++++------ source/constants.py | 2 +- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/source/component/plant.py b/source/component/plant.py index 813137c..60d88fe 100755 --- a/source/component/plant.py +++ b/source/component/plant.py @@ -1606,9 +1606,9 @@ class DoomShroom(Plant): # 用于描述毁灭菇的坑 class Hole(Plant): - def __init__(self, x, y, plotType): + def __init__(self, x, y, plot_type): # 指定区域类型这一句必须放在前面,否则加载图片判断将会失败 - self.plotType = plotType + self.plot_type = plot_type Plant.__init__(self, x, y, c.HOLE, c.INF, None) self.timer = 0 self.shallow = False @@ -1638,9 +1638,9 @@ class Hole(Plant): for i, name in enumerate(name_list): self.loadFrames(frame_list[i], name) - if self.plotType == c.MAP_TILE: + if self.plot_type == c.MAP_TILE: self.frames = self.roof_frames - elif self.plotType == c.MAP_WATER: + elif self.plot_type == c.MAP_WATER: self.frames = self.water_frames else: self.frames = self.idle_frames @@ -1649,9 +1649,9 @@ class Hole(Plant): if self.timer == 0: self.timer = self.current_time elif (not self.shallow) and (self.current_time - self.timer >= 90000): - if self.plotType == c.MAP_TILE: + if self.plot_type == c.MAP_TILE: self.frames = self.roof2_frames - elif self.plotType == c.MAP_WATER: + elif self.plot_type == c.MAP_WATER: self.frames = self.water2_frames else: self.frames = self.idle2_frames diff --git a/source/constants.py b/source/constants.py index ceae9c5..6eef56c 100755 --- a/source/constants.py +++ b/source/constants.py @@ -180,7 +180,7 @@ SURVIVAL_ROUNDS = "survival_rounds" # 地图单元格属性 MAP_PLANT = "plantnames" MAP_SLEEP = "sleep" # 有没有休眠的蘑菇,作是否能种植咖啡豆的判断 -MAP_PLOT_TYPE = "plotType" +MAP_PLOT_TYPE = "plot_type" # 地图单元格区域类型 MAP_GRASS = "grass" MAP_WATER = "water"