refac: add good food class and bad food class

This commit is contained in:
Kylin_on_Mac 2023-10-10 22:52:05 +08:00
parent 87335c3b34
commit ba349a6104
3 changed files with 52 additions and 26 deletions

View File

@ -17,11 +17,21 @@ BALL_W = 30
# food
class FoodTypeEnum(StringEnum):
# TODO add good_lv1~good_lv3
GREEN = auto()
RED = auto()
FOOD_COLOR_MAP = {FoodTypeEnum.GREEN: "#009688",
FoodTypeEnum.RED: "#FF1744"}
GOOD_1 = auto()
GOOD_2 = auto()
GOOD_3 = auto()
BAD_1 = auto()
BAD_2 = auto()
BAD_3 = auto()
FOOD_COLOR_MAP = {
FoodTypeEnum.GOOD_1: "#009688",
FoodTypeEnum.GOOD_2: "#009688",
FoodTypeEnum.GOOD_3: "#009688",
FoodTypeEnum.BAD_1: "#FF1744",
FoodTypeEnum.BAD_2: "#FF1744",
FoodTypeEnum.BAD_3: "#FF1744"
}
# path of assets
ASSET_PATH = path.join(path.dirname(__file__), "..", "asset")

View File

@ -5,12 +5,12 @@ from mlgame.view.view_model import create_rect_view_data
class Food(pygame.sprite.Sprite):
def __init__(self, group, type: FoodTypeEnum):
def __init__(self, group):
pygame.sprite.Sprite.__init__(self, group)
self.image = pygame.Surface([8, 8])
self.type = type
self.type = None
self.score = 0
self.color = FOOD_COLOR_MAP[type]
self.color = None
self.rect = self.image.get_rect()
self.angle = 0
@ -28,3 +28,25 @@ class Food(pygame.sprite.Sprite):
self.rect.height,
self.color
)
class GoodFoodLv1(Food):
def __init__(self, group):
super().__init__(group)
self.image = pygame.Surface([8, 8])
self.type = FoodTypeEnum.GOOD_1
self.color = FOOD_COLOR_MAP[self.type]
self.score = 1
self.rect = self.image.get_rect()
self.angle = 0
class BadFoodLv1(Food):
def __init__(self, group):
super().__init__(group)
self.image = pygame.Surface([8, 8])
self.type = FoodTypeEnum.BAD_1
self.color = FOOD_COLOR_MAP[self.type]
self.score = -1
self.rect = self.image.get_rect()
self.angle = 0

View File

@ -9,9 +9,8 @@ from mlgame.utils.enum import get_ai_name
from mlgame.view.decorator import check_game_progress, check_game_result
from mlgame.view.view_model import *
from .env import *
from .env import FoodTypeEnum
from .foods import GoodFoodLv1, BadFoodLv1
from .game_object import Ball
from .foods import Food
from .sound_controller import SoundController
@ -84,8 +83,8 @@ class EasyGame(PaiaGame):
self.ball = Ball()
self.foods.empty()
self.score = 0
self._create_foods(self._green_food_count, FoodTypeEnum.GREEN)
self._create_foods(self._red_food_count, FoodTypeEnum.RED)
self._create_foods(GoodFoodLv1, self._green_food_count)
self._create_foods(BadFoodLv1, self._red_food_count)
self.frame_count = 0
self._frame_count_down = self._frame_limit
self.sound_controller.play_music()
@ -120,15 +119,12 @@ class EasyGame(PaiaGame):
if hits:
for food in hits:
self.score += food.score
self._create_foods(food.__class__, 1)
# TODO add food score to function
if food.type == FoodTypeEnum.GREEN:
if isinstance(food, (GoodFoodLv1,)):
self.sound_controller.play_eating_good()
self._create_foods(1, FoodTypeEnum.GREEN)
elif food.type == FoodTypeEnum.RED:
elif isinstance(food, (BadFoodLv1,)):
self.sound_controller.play_eating_bad()
self._create_foods(1, FoodTypeEnum.RED)
def get_data_from_game_to_player(self):
"""
@ -193,7 +189,6 @@ class EasyGame(PaiaGame):
"""
Get the initial scene and object information for drawing on the web
"""
# TODO add music or sound
# bg_path = path.join(ASSET_PATH, "img/background.jpg")
# background = create_asset_init_data(
# "background", WIDTH, HEIGHT, bg_path,
@ -274,11 +269,10 @@ class EasyGame(PaiaGame):
cmd_1p.append("NONE")
return {get_ai_name(0): cmd_1p}
def _create_foods(self, count: int = 5, type: FoodTypeEnum = FoodTypeEnum.GREEN):
def _create_foods(self, FOOD_TYPE, count: int = 5):
for i in range(count):
# add food to group
food = Food(self.foods, type)
food = FOOD_TYPE(self.foods)
food.rect.centerx = random.randint(self.playground.left, self.playground.right)
food.rect.centery = random.randint(self.playground.top, self.playground.bottom)
pass