添加类型注解
This commit is contained in:
parent
388b3175e0
commit
2d368f8bf5
@ -3,7 +3,7 @@ from .. import constants as c
|
|||||||
|
|
||||||
# 记录植物种植情况的地图管理工具
|
# 记录植物种植情况的地图管理工具
|
||||||
class Map():
|
class Map():
|
||||||
def __init__(self, background_type):
|
def __init__(self, background_type:int):
|
||||||
self.background_type = background_type
|
self.background_type = background_type
|
||||||
# 注意:从0开始编号
|
# 注意:从0开始编号
|
||||||
if self.background_type in c.POOL_EQUIPPED_BACKGROUNDS:
|
if self.background_type in c.POOL_EQUIPPED_BACKGROUNDS:
|
||||||
@ -55,7 +55,7 @@ class Map():
|
|||||||
for y in range(self.height)
|
for y in range(self.height)
|
||||||
]
|
]
|
||||||
|
|
||||||
def isValid(self, map_x, map_y):
|
def isValid(self, map_x:int, map_y:int) -> bool:
|
||||||
if ((0 <= map_x < self.width)
|
if ((0 <= map_x < self.width)
|
||||||
and (0 <= map_y < self.height)):
|
and (0 <= map_y < self.height)):
|
||||||
return True
|
return True
|
||||||
@ -64,13 +64,13 @@ class Map():
|
|||||||
# 地图单元格状态
|
# 地图单元格状态
|
||||||
# 注意是可变对象,不能直接引用
|
# 注意是可变对象,不能直接引用
|
||||||
# 由于同一格显然不可能种两个相同的植物,所以用集合
|
# 由于同一格显然不可能种两个相同的植物,所以用集合
|
||||||
def initMapGrid(self, plot_type):
|
def initMapGrid(self, plot_type:str) -> set:
|
||||||
return {c.MAP_PLANT:set(), c.MAP_SLEEP:False, c.MAP_PLOT_TYPE:plot_type}
|
return {c.MAP_PLANT:set(), c.MAP_SLEEP:False, c.MAP_PLOT_TYPE:plot_type}
|
||||||
|
|
||||||
# 判断位置是否可用
|
# 判断位置是否可用
|
||||||
# 暂时没有写紫卡植物的判断方法
|
# 暂时没有写紫卡植物的判断方法
|
||||||
# 由于紫卡植物需要移除以前的植物,所以可用另外定义一个函数
|
# 由于紫卡植物需要移除以前的植物,所以可用另外定义一个函数
|
||||||
def isAvailable(self, map_x, map_y, plant_name):
|
def isAvailable(self, map_x:int, map_y:int, plant_name:str) -> bool:
|
||||||
# 咖啡豆和墓碑吞噬者的判别最为特殊
|
# 咖啡豆和墓碑吞噬者的判别最为特殊
|
||||||
if plant_name == c.COFFEEBEAN:
|
if plant_name == c.COFFEEBEAN:
|
||||||
if (self.map[map_y][map_x][c.MAP_SLEEP]
|
if (self.map[map_y][map_x][c.MAP_SLEEP]
|
||||||
@ -147,7 +147,7 @@ class Map():
|
|||||||
else: # 不可种植区域
|
else: # 不可种植区域
|
||||||
return False
|
return False
|
||||||
|
|
||||||
def getMapIndex(self, x, y):
|
def getMapIndex(self, x:int, y:int) -> tuple[int, int]:
|
||||||
if self.background_type in c.POOL_EQUIPPED_BACKGROUNDS:
|
if self.background_type in c.POOL_EQUIPPED_BACKGROUNDS:
|
||||||
x -= c.MAP_POOL_OFFSET_X
|
x -= c.MAP_POOL_OFFSET_X
|
||||||
y -= c.MAP_POOL_OFFSET_Y
|
y -= c.MAP_POOL_OFFSET_Y
|
||||||
@ -166,7 +166,7 @@ class Map():
|
|||||||
y -= c.MAP_OFFSET_Y
|
y -= c.MAP_OFFSET_Y
|
||||||
return (x // c.GRID_X_SIZE, y // c.GRID_Y_SIZE)
|
return (x // c.GRID_X_SIZE, y // c.GRID_Y_SIZE)
|
||||||
|
|
||||||
def getMapGridPos(self, map_x, map_y):
|
def getMapGridPos(self, map_x:int, map_y:int) -> tuple[int, int]:
|
||||||
if self.background_type in c.POOL_EQUIPPED_BACKGROUNDS:
|
if self.background_type in c.POOL_EQUIPPED_BACKGROUNDS:
|
||||||
return (map_x * c.GRID_POOL_X_SIZE + c.GRID_POOL_X_SIZE//2 + c.MAP_POOL_OFFSET_X,
|
return (map_x * c.GRID_POOL_X_SIZE + c.GRID_POOL_X_SIZE//2 + c.MAP_POOL_OFFSET_X,
|
||||||
map_y * c.GRID_POOL_Y_SIZE + c.GRID_POOL_Y_SIZE//5 * 3 + c.MAP_POOL_OFFSET_Y)
|
map_y * c.GRID_POOL_Y_SIZE + c.GRID_POOL_Y_SIZE//5 * 3 + c.MAP_POOL_OFFSET_Y)
|
||||||
@ -177,22 +177,22 @@ class Map():
|
|||||||
return (map_x * c.GRID_X_SIZE + c.GRID_X_SIZE//2 + c.MAP_OFFSET_X,
|
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)
|
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):
|
def setMapGridType(self, map_x:int, map_y:int, plot_type:str):
|
||||||
self.map[map_y][map_x][c.MAP_PLOT_TYPE] = plot_type
|
self.map[map_y][map_x][c.MAP_PLOT_TYPE] = plot_type
|
||||||
|
|
||||||
def addMapPlant(self, map_x, map_y, plant_name, sleep=False):
|
def addMapPlant(self, map_x:int, map_y:int, plant_name:int, sleep:bool=False):
|
||||||
self.map[map_y][map_x][c.MAP_PLANT].add(plant_name)
|
self.map[map_y][map_x][c.MAP_PLANT].add(plant_name)
|
||||||
self.map[map_y][map_x][c.MAP_SLEEP] = sleep
|
self.map[map_y][map_x][c.MAP_SLEEP] = sleep
|
||||||
|
|
||||||
def removeMapPlant(self, map_x, map_y, plant_name):
|
def removeMapPlant(self, map_x:int, map_y:int, plant_name:str):
|
||||||
self.map[map_y][map_x][c.MAP_PLANT].discard(plant_name)
|
self.map[map_y][map_x][c.MAP_PLANT].discard(plant_name)
|
||||||
|
|
||||||
def getRandomMapIndex(self):
|
def getRandomMapIndex(self) -> tuple[int, int]:
|
||||||
map_x = random.randint(0, self.width-1)
|
map_x = random.randint(0, self.width-1)
|
||||||
map_y = random.randint(0, self.height-1)
|
map_y = random.randint(0, self.height-1)
|
||||||
return (map_x, map_y)
|
return (map_x, map_y)
|
||||||
|
|
||||||
def checkPlantToSeed(self, x, y, plant_name):
|
def checkPlantToSeed(self, x:int, y:int, plant_name:str) -> tuple[int, int]:
|
||||||
pos = None
|
pos = None
|
||||||
map_x, map_y = self.getMapIndex(x, y)
|
map_x, map_y = self.getMapIndex(x, y)
|
||||||
if self.isValid(map_x, map_y) and self.isAvailable(map_x, map_y, plant_name):
|
if self.isValid(map_x, map_y) and self.isAvailable(map_x, map_y, plant_name):
|
||||||
|
|||||||
@ -5,7 +5,7 @@ from .. import constants as c
|
|||||||
|
|
||||||
|
|
||||||
class Car(pg.sprite.Sprite):
|
class Car(pg.sprite.Sprite):
|
||||||
def __init__(self, x, y, map_y):
|
def __init__(self, x:int, y:int, map_y:int):
|
||||||
pg.sprite.Sprite.__init__(self)
|
pg.sprite.Sprite.__init__(self)
|
||||||
|
|
||||||
rect = tool.GFX[c.CAR].get_rect()
|
rect = tool.GFX[c.CAR].get_rect()
|
||||||
@ -19,7 +19,7 @@ class Car(pg.sprite.Sprite):
|
|||||||
self.state = c.IDLE
|
self.state = c.IDLE
|
||||||
self.dead = False
|
self.dead = False
|
||||||
|
|
||||||
def update(self, game_info):
|
def update(self, game_info:dict):
|
||||||
self.current_time = game_info[c.CURRENT_TIME]
|
self.current_time = game_info[c.CURRENT_TIME]
|
||||||
if self.state == c.WALK:
|
if self.state == c.WALK:
|
||||||
self.rect.x += 5
|
self.rect.x += 5
|
||||||
@ -37,9 +37,9 @@ class Car(pg.sprite.Sprite):
|
|||||||
|
|
||||||
# 豌豆及孢子类普通子弹
|
# 豌豆及孢子类普通子弹
|
||||||
class Bullet(pg.sprite.Sprite):
|
class Bullet(pg.sprite.Sprite):
|
||||||
def __init__( self, x, start_y, dest_y, name, damage,
|
def __init__( self, x:int, start_y:int, dest_y:int, name:str, damage:int,
|
||||||
effect=None, passed_torchwood_x=None,
|
effect:str=None, passed_torchwood_x:int=None,
|
||||||
damage_type=c.ZOMBIE_DEAFULT_DAMAGE):
|
damage_type:str=c.ZOMBIE_DEAFULT_DAMAGE):
|
||||||
pg.sprite.Sprite.__init__(self)
|
pg.sprite.Sprite.__init__(self)
|
||||||
|
|
||||||
self.name = name
|
self.name = name
|
||||||
|
|||||||
@ -8,7 +8,7 @@ class Menu(tool.State):
|
|||||||
def __init__(self):
|
def __init__(self):
|
||||||
tool.State.__init__(self)
|
tool.State.__init__(self)
|
||||||
|
|
||||||
def startup(self, current_time, persist):
|
def startup(self, current_time:int, persist):
|
||||||
self.next = c.LEVEL
|
self.next = c.LEVEL
|
||||||
self.persist = persist
|
self.persist = persist
|
||||||
self.game_info = persist
|
self.game_info = persist
|
||||||
@ -88,7 +88,7 @@ class Menu(tool.State):
|
|||||||
self.adventure_clicked = False
|
self.adventure_clicked = False
|
||||||
self.option_button_clicked = False
|
self.option_button_clicked = False
|
||||||
|
|
||||||
def checkHilight(self, x, y):
|
def checkHilight(self, x:int, y:int):
|
||||||
# 高亮冒险模式按钮
|
# 高亮冒险模式按钮
|
||||||
if self.inArea(self.adventure_rect, x, y):
|
if self.inArea(self.adventure_rect, x, y):
|
||||||
self.adventure_highlight_time = self.current_time
|
self.adventure_highlight_time = self.current_time
|
||||||
@ -112,7 +112,7 @@ class Menu(tool.State):
|
|||||||
self.littleGame_image = self.chooseHilightImage(self.littleGame_highlight_time, self.littleGame_frames)
|
self.littleGame_image = self.chooseHilightImage(self.littleGame_highlight_time, self.littleGame_frames)
|
||||||
self.help_image = self.chooseHilightImage(self.help_hilight_time, self.help_frames)
|
self.help_image = self.chooseHilightImage(self.help_hilight_time, self.help_frames)
|
||||||
|
|
||||||
def chooseHilightImage(self, hilightTime, frames):
|
def chooseHilightImage(self, hilightTime:int, frames):
|
||||||
if (self.current_time - hilightTime) < 80:
|
if (self.current_time - hilightTime) < 80:
|
||||||
index= 1
|
index= 1
|
||||||
else:
|
else:
|
||||||
@ -206,7 +206,7 @@ class Menu(tool.State):
|
|||||||
self.sunflower_trophy_rect.y = 280
|
self.sunflower_trophy_rect.y = 280
|
||||||
self.sunflower_trophy_show_info_time = 0
|
self.sunflower_trophy_show_info_time = 0
|
||||||
|
|
||||||
def checkSunflowerTrophyInfo(self, surface, x, y):
|
def checkSunflowerTrophyInfo(self, surface:pg.Surface, x:int, y:int):
|
||||||
if self.inArea(self.sunflower_trophy_rect, x, y):
|
if self.inArea(self.sunflower_trophy_rect, x, y):
|
||||||
self.sunflower_trophy_show_info_time = self.current_time
|
self.sunflower_trophy_show_info_time = self.current_time
|
||||||
if (self.current_time - self.sunflower_trophy_show_info_time) < 80:
|
if (self.current_time - self.sunflower_trophy_show_info_time) < 80:
|
||||||
@ -228,7 +228,7 @@ class Menu(tool.State):
|
|||||||
# 播放点击音效
|
# 播放点击音效
|
||||||
c.SOUND_BUTTON_CLICK.play()
|
c.SOUND_BUTTON_CLICK.play()
|
||||||
|
|
||||||
def showCurrentVolumeImage(self, surface):
|
def showCurrentVolumeImage(self, surface:pg.Surface):
|
||||||
# 由于音量可变,因此这一内容不能在一开始就结束加载,而应当不断刷新不断显示
|
# 由于音量可变,因此这一内容不能在一开始就结束加载,而应当不断刷新不断显示
|
||||||
font = pg.font.Font(c.FONT_PATH, 30)
|
font = pg.font.Font(c.FONT_PATH, 30)
|
||||||
volume_tips = font.render(f"音量:{round(self.game_info[c.SOUND_VOLUME]*100):3}%", True, c.LIGHTGRAY)
|
volume_tips = font.render(f"音量:{round(self.game_info[c.SOUND_VOLUME]*100):3}%", True, c.LIGHTGRAY)
|
||||||
@ -237,7 +237,7 @@ class Menu(tool.State):
|
|||||||
volume_tips_rect.y = 247
|
volume_tips_rect.y = 247
|
||||||
surface.blit(volume_tips, volume_tips_rect)
|
surface.blit(volume_tips, volume_tips_rect)
|
||||||
|
|
||||||
def update(self, surface, current_time, mouse_pos, mouse_click):
|
def update(self, surface:pg.Surface, current_time:int, mouse_pos:list, mouse_click):
|
||||||
self.current_time = self.game_info[c.CURRENT_TIME] = current_time
|
self.current_time = self.game_info[c.CURRENT_TIME] = current_time
|
||||||
|
|
||||||
surface.blit(self.bg_image, self.bg_rect)
|
surface.blit(self.bg_image, self.bg_rect)
|
||||||
|
|||||||
@ -18,7 +18,7 @@ class State():
|
|||||||
|
|
||||||
# 当从其他状态进入这个状态时,需要进行的初始化操作
|
# 当从其他状态进入这个状态时,需要进行的初始化操作
|
||||||
@abstractmethod
|
@abstractmethod
|
||||||
def startup(self, current_time, persist):
|
def startup(self, current_time:int, persist:dict):
|
||||||
# 前面加了@abstractmethod表示抽象基类中必须要重新定义的method(method是对象和函数的结合)
|
# 前面加了@abstractmethod表示抽象基类中必须要重新定义的method(method是对象和函数的结合)
|
||||||
pass
|
pass
|
||||||
# 当从这个状态退出时,需要进行的清除操作
|
# 当从这个状态退出时,需要进行的清除操作
|
||||||
@ -27,12 +27,12 @@ class State():
|
|||||||
return self.persist
|
return self.persist
|
||||||
# 在这个状态运行时进行的更新操作
|
# 在这个状态运行时进行的更新操作
|
||||||
@abstractmethod
|
@abstractmethod
|
||||||
def update(self, surface, keys, current_time):
|
def update(self, surface:pg.Surface, keys, current_time:int):
|
||||||
# 前面加了@abstractmethod表示抽象基类中必须要重新定义的method
|
# 前面加了@abstractmethod表示抽象基类中必须要重新定义的method
|
||||||
pass
|
pass
|
||||||
|
|
||||||
# 工具:范围判断函数,用于判断点击
|
# 工具:范围判断函数,用于判断点击
|
||||||
def inArea(self, rect, x, y):
|
def inArea(self, rect:pg.Rect, x:int, y:int):
|
||||||
if (rect.x <= x <= rect.right and
|
if (rect.x <= x <= rect.right and
|
||||||
rect.y <= y <= rect.bottom):
|
rect.y <= y <= rect.bottom):
|
||||||
return True
|
return True
|
||||||
@ -101,7 +101,7 @@ class Control():
|
|||||||
f.write(savedata)
|
f.write(savedata)
|
||||||
self.game_info = c.INIT_USERDATA.copy() # 内部全是不可变对象,浅拷贝即可
|
self.game_info = c.INIT_USERDATA.copy() # 内部全是不可变对象,浅拷贝即可
|
||||||
|
|
||||||
def setup_states(self, state_dict, start_state):
|
def setup_states(self, state_dict:dict, start_state):
|
||||||
self.state_dict = state_dict
|
self.state_dict = state_dict
|
||||||
self.state_name = start_state
|
self.state_name = start_state
|
||||||
self.state = self.state_dict[self.state_name]
|
self.state = self.state_dict[self.state_name]
|
||||||
@ -155,7 +155,8 @@ class Control():
|
|||||||
pg.display.update()
|
pg.display.update()
|
||||||
self.clock.tick(self.fps)
|
self.clock.tick(self.fps)
|
||||||
|
|
||||||
def get_image(sheet, x, y, width, height, colorkey=c.BLACK, scale=1):
|
def get_image( sheet:pg.Surface, x:int, y:int, width:int, height:int,
|
||||||
|
colorkey:tuple[int]=c.BLACK, scale:int=1) -> pg.Surface:
|
||||||
# 不保留alpha通道的图片导入
|
# 不保留alpha通道的图片导入
|
||||||
image = pg.Surface([width, height])
|
image = pg.Surface([width, height])
|
||||||
rect = image.get_rect()
|
rect = image.get_rect()
|
||||||
@ -168,19 +169,21 @@ def get_image(sheet, x, y, width, height, colorkey=c.BLACK, scale=1):
|
|||||||
int(rect.height*scale)))
|
int(rect.height*scale)))
|
||||||
return image
|
return image
|
||||||
|
|
||||||
def get_image_alpha(sheet, x, y, width, height, colorkey=c.BLACK, scale=1):
|
def get_image_alpha(sheet:pg.Surface, x:int, y:int, width:int, height:int,
|
||||||
# 保留alpha通道的图片导入
|
colorkey:tuple[int]=c.BLACK, scale:int=1) -> pg.Surface:
|
||||||
image = pg.Surface([width, height], SRCALPHA)
|
# 保留alpha通道的图片导入
|
||||||
rect = image.get_rect()
|
image = pg.Surface([width, height], SRCALPHA)
|
||||||
|
rect = image.get_rect()
|
||||||
|
|
||||||
image.blit(sheet, (0, 0), (x, y, width, height))
|
image.blit(sheet, (0, 0), (x, y, width, height))
|
||||||
image.set_colorkey(colorkey)
|
image.set_colorkey(colorkey)
|
||||||
image = pg.transform.scale(image,
|
image = pg.transform.scale(image,
|
||||||
(int(rect.width*scale),
|
(int(rect.width*scale),
|
||||||
int(rect.height*scale)))
|
int(rect.height*scale)))
|
||||||
return image
|
return image
|
||||||
|
|
||||||
def load_image_frames(directory, image_name, colorkey, accept):
|
def load_image_frames( directory:str, image_name:str,
|
||||||
|
colorkey:tuple[int], accept:tuple[str]) -> list[pg.Surface]:
|
||||||
frame_list = []
|
frame_list = []
|
||||||
tmp = {}
|
tmp = {}
|
||||||
# image_name is "Peashooter", pic name is "Peashooter_1", get the index 1
|
# image_name is "Peashooter", pic name is "Peashooter_1", get the index 1
|
||||||
@ -204,7 +207,8 @@ def load_image_frames(directory, image_name, colorkey, accept):
|
|||||||
return frame_list
|
return frame_list
|
||||||
|
|
||||||
# colorkeys 是设置图像中的某个颜色值为透明,这里用来消除白边
|
# colorkeys 是设置图像中的某个颜色值为透明,这里用来消除白边
|
||||||
def load_all_gfx(directory, colorkey=c.WHITE, accept=(".png", ".jpg", ".bmp", ".gif", ".webp")):
|
def load_all_gfx( directory:str, colorkey:tuple[int]=c.WHITE,
|
||||||
|
accept:tuple[str]=(".png", ".jpg", ".bmp", ".gif", ".webp")) -> dict[str:pg.Surface]:
|
||||||
graphics = {}
|
graphics = {}
|
||||||
for name1 in os.listdir(directory):
|
for name1 in os.listdir(directory):
|
||||||
# subfolders under the folder resources\graphics
|
# subfolders under the folder resources\graphics
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user