refac: reduce duplicated code
This commit is contained in:
parent
53749ff845
commit
3d7553ad44
43
src/foods.py
43
src/foods.py
|
@ -12,16 +12,18 @@ FOOD3_VEL = 4
|
||||||
|
|
||||||
|
|
||||||
class Food(pygame.sprite.Sprite):
|
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)
|
pygame.sprite.Sprite.__init__(self, group)
|
||||||
self.image = pygame.Surface([8, 8])
|
if image_size is None:
|
||||||
self.type = None
|
image_size = [FOOD_LV1_SIZE, FOOD_LV1_SIZE]
|
||||||
self.score = 0
|
self.image = pygame.Surface(image_size)
|
||||||
|
self.type = type
|
||||||
|
self.score = score
|
||||||
self.rect = self.image.get_rect()
|
self.rect = self.image.get_rect()
|
||||||
self.angle = 0
|
self.angle = 0
|
||||||
self.rect_float_x = 0
|
self.rect_float_x = 0
|
||||||
self.rect_float_y = 0
|
self.rect_float_y = 0
|
||||||
self.image_id = ""
|
self.image_id = image_id
|
||||||
|
|
||||||
def update(self, *args, **kwargs) -> None:
|
def update(self, *args, **kwargs) -> None:
|
||||||
pass
|
pass
|
||||||
|
@ -45,13 +47,8 @@ class Food(pygame.sprite.Sprite):
|
||||||
|
|
||||||
class Food1(Food):
|
class Food1(Food):
|
||||||
def __init__(self, group):
|
def __init__(self, group):
|
||||||
super().__init__(group)
|
super().__init__(group,FoodTypeEnum.FOOD_1,"food01",[FOOD_LV1_SIZE, FOOD_LV1_SIZE],
|
||||||
self.image = pygame.Surface([FOOD_LV1_SIZE, FOOD_LV1_SIZE])
|
1)
|
||||||
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"
|
|
||||||
self._vel = FOOD1_VEL
|
self._vel = FOOD1_VEL
|
||||||
|
|
||||||
def update(self, playground: Rect, squid: pygame.sprite.Sprite):
|
def update(self, playground: Rect, squid: pygame.sprite.Sprite):
|
||||||
|
@ -67,19 +64,12 @@ class Food1(Food):
|
||||||
self._vel = - FOOD1_VEL
|
self._vel = - FOOD1_VEL
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class Food2(Food):
|
class Food2(Food):
|
||||||
def __init__(self, group):
|
def __init__(self, group):
|
||||||
super().__init__(group)
|
super().__init__(group, FoodTypeEnum.FOOD_2, "food02", [FOOD_LV2_SIZE, FOOD_LV2_SIZE], 2)
|
||||||
|
|
||||||
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"
|
|
||||||
self._vel = FOOD2_VEL
|
self._vel = FOOD2_VEL
|
||||||
|
|
||||||
|
|
||||||
def update(self, playground: Rect, squid: pygame.sprite.Sprite):
|
def update(self, playground: Rect, squid: pygame.sprite.Sprite):
|
||||||
|
|
||||||
self.rect_float_x += self._vel
|
self.rect_float_x += self._vel
|
||||||
|
@ -93,20 +83,11 @@ class Food2(Food):
|
||||||
self._vel = - FOOD2_VEL
|
self._vel = - FOOD2_VEL
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class Food3(Food):
|
class Food3(Food):
|
||||||
def __init__(self, group):
|
def __init__(self, group):
|
||||||
super().__init__(group)
|
super().__init__(group, FoodTypeEnum.FOOD_3, "food03", [FOOD_LV3_SIZE, FOOD_LV3_SIZE], 4)
|
||||||
|
|
||||||
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"
|
|
||||||
self._vel = FOOD3_VEL
|
self._vel = FOOD3_VEL
|
||||||
|
|
||||||
|
|
||||||
def update(self, playground: Rect, squid: pygame.sprite.Sprite):
|
def update(self, playground: Rect, squid: pygame.sprite.Sprite):
|
||||||
|
|
||||||
self.rect_float_x += self._vel
|
self.rect_float_x += self._vel
|
||||||
|
|
Loading…
Reference in New Issue