refac: reduce duplicated code

This commit is contained in:
Kylin_on_Mac 2023-11-10 10:53:56 +08:00
parent 53749ff845
commit 3d7553ad44
1 changed files with 12 additions and 31 deletions

View File

@ -12,16 +12,18 @@ FOOD3_VEL = 4
class Food(pygame.sprite.Sprite):
def __init__(self, group):
def __init__(self, group, type: FoodTypeEnum, image_id: str, image_size=None, score: int = 1):
pygame.sprite.Sprite.__init__(self, group)
self.image = pygame.Surface([8, 8])
self.type = None
self.score = 0
if image_size is None:
image_size = [FOOD_LV1_SIZE, FOOD_LV1_SIZE]
self.image = pygame.Surface(image_size)
self.type = type
self.score = score
self.rect = self.image.get_rect()
self.angle = 0
self.rect_float_x = 0
self.rect_float_y = 0
self.image_id = ""
self.image_id = image_id
def update(self, *args, **kwargs) -> None:
pass
@ -45,13 +47,8 @@ class Food(pygame.sprite.Sprite):
class Food1(Food):
def __init__(self, group):
super().__init__(group)
self.image = pygame.Surface([FOOD_LV1_SIZE, FOOD_LV1_SIZE])
self.type = FoodTypeEnum.FOOD_1
self.color = FOOD_COLOR_MAP[self.type]
self.score = 1
self.rect = self.image.get_rect()
self.image_id="food01"
super().__init__(group,FoodTypeEnum.FOOD_1,"food01",[FOOD_LV1_SIZE, FOOD_LV1_SIZE],
1)
self._vel = FOOD1_VEL
def update(self, playground: Rect, squid: pygame.sprite.Sprite):
@ -67,19 +64,12 @@ class Food1(Food):
self._vel = - FOOD1_VEL
class Food2(Food):
def __init__(self, group):
super().__init__(group)
self.image = pygame.Surface([FOOD_LV2_SIZE, FOOD_LV2_SIZE])
self.type = FoodTypeEnum.FOOD_1
self.score = 2
self.rect = self.image.get_rect()
self.image_id="food02"
super().__init__(group, FoodTypeEnum.FOOD_2, "food02", [FOOD_LV2_SIZE, FOOD_LV2_SIZE], 2)
self._vel = FOOD2_VEL
def update(self, playground: Rect, squid: pygame.sprite.Sprite):
self.rect_float_x += self._vel
@ -93,20 +83,11 @@ class Food2(Food):
self._vel = - FOOD2_VEL
class Food3(Food):
def __init__(self, group):
super().__init__(group)
self.image = pygame.Surface([FOOD_LV3_SIZE, FOOD_LV3_SIZE])
self.type = FoodTypeEnum.FOOD_3
self.score = 4
self.rect = self.image.get_rect()
self.image_id="food03"
super().__init__(group, FoodTypeEnum.FOOD_3, "food03", [FOOD_LV3_SIZE, FOOD_LV3_SIZE], 4)
self._vel = FOOD3_VEL
def update(self, playground: Rect, squid: pygame.sprite.Sprite):
self.rect_float_x += self._vel