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 # food
class FoodTypeEnum(StringEnum): class FoodTypeEnum(StringEnum):
# TODO add good_lv1~good_lv3 GOOD_1 = auto()
GREEN = auto() GOOD_2 = auto()
RED = auto() GOOD_3 = auto()
FOOD_COLOR_MAP = {FoodTypeEnum.GREEN: "#009688", BAD_1 = auto()
FoodTypeEnum.RED: "#FF1744"} 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 # path of assets
ASSET_PATH = path.join(path.dirname(__file__), "..", "asset") 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): class Food(pygame.sprite.Sprite):
def __init__(self, group, type: FoodTypeEnum): def __init__(self, group):
pygame.sprite.Sprite.__init__(self, group) pygame.sprite.Sprite.__init__(self, group)
self.image = pygame.Surface([8, 8]) self.image = pygame.Surface([8, 8])
self.type = type self.type = None
self.score = 0 self.score = 0
self.color = FOOD_COLOR_MAP[type] self.color = None
self.rect = self.image.get_rect() self.rect = self.image.get_rect()
self.angle = 0 self.angle = 0
@ -28,3 +28,25 @@ class Food(pygame.sprite.Sprite):
self.rect.height, self.rect.height,
self.color 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.decorator import check_game_progress, check_game_result
from mlgame.view.view_model import * from mlgame.view.view_model import *
from .env import * from .env import *
from .env import FoodTypeEnum from .foods import GoodFoodLv1, BadFoodLv1
from .game_object import Ball from .game_object import Ball
from .foods import Food
from .sound_controller import SoundController from .sound_controller import SoundController
@ -47,7 +46,7 @@ class EasyGame(PaiaGame):
self.sound_controller = SoundController(sound) self.sound_controller = SoundController(sound)
self._init_game() self._init_game()
def set_game_params_by_level(self, level:int): def set_game_params_by_level(self, level: int):
level_file_path = os.path.join(LEVEL_PATH, f"{level:03d}.json") level_file_path = os.path.join(LEVEL_PATH, f"{level:03d}.json")
self.set_game_params_by_file(level_file_path) self.set_game_params_by_file(level_file_path)
@ -63,7 +62,7 @@ class EasyGame(PaiaGame):
with open(os.path.join(LEVEL_PATH, "001.json")) as f: with open(os.path.join(LEVEL_PATH, "001.json")) as f:
game_params = json.load(f) game_params = json.load(f)
self._level = 1 self._level = 1
self._level_file=None self._level_file = None
self._set_game_params(game_params) self._set_game_params(game_params)
def _set_game_params(self, game_params): def _set_game_params(self, game_params):
@ -84,8 +83,8 @@ class EasyGame(PaiaGame):
self.ball = Ball() self.ball = Ball()
self.foods.empty() self.foods.empty()
self.score = 0 self.score = 0
self._create_foods(self._green_food_count, FoodTypeEnum.GREEN) self._create_foods(GoodFoodLv1, self._green_food_count)
self._create_foods(self._red_food_count, FoodTypeEnum.RED) self._create_foods(BadFoodLv1, self._red_food_count)
self.frame_count = 0 self.frame_count = 0
self._frame_count_down = self._frame_limit self._frame_count_down = self._frame_limit
self.sound_controller.play_music() self.sound_controller.play_music()
@ -119,16 +118,13 @@ class EasyGame(PaiaGame):
hits = pygame.sprite.spritecollide(self.ball, self.foods, True) hits = pygame.sprite.spritecollide(self.ball, self.foods, True)
if hits: if hits:
for food in hits: for food in hits:
self.score+= food.score self.score += food.score
self._create_foods(food.__class__, 1)
# TODO add food score to function # TODO add food score to function
if food.type == FoodTypeEnum.GREEN: if isinstance(food, (GoodFoodLv1,)):
self.sound_controller.play_eating_good() self.sound_controller.play_eating_good()
self._create_foods(1, FoodTypeEnum.GREEN) elif isinstance(food, (BadFoodLv1,)):
elif food.type == FoodTypeEnum.RED:
self.sound_controller.play_eating_bad() self.sound_controller.play_eating_bad()
self._create_foods(1, FoodTypeEnum.RED)
def get_data_from_game_to_player(self): def get_data_from_game_to_player(self):
""" """
@ -140,7 +136,7 @@ class EasyGame(PaiaGame):
for food in self.foods: for food in self.foods:
# TODO add good food and bad food # TODO add good food and bad food
foods_data.append({"x": food.rect.x, "y": food.rect.y,"type":food.type,"score":food.score}) foods_data.append({"x": food.rect.x, "y": food.rect.y, "type": food.type, "score": food.score})
data_to_1p = { data_to_1p = {
"frame": self.frame_count, "frame": self.frame_count,
"ball_x": self.ball.rect.centerx, "ball_x": self.ball.rect.centerx,
@ -193,7 +189,6 @@ class EasyGame(PaiaGame):
""" """
Get the initial scene and object information for drawing on the web 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") # bg_path = path.join(ASSET_PATH, "img/background.jpg")
# background = create_asset_init_data( # background = create_asset_init_data(
# "background", WIDTH, HEIGHT, bg_path, # "background", WIDTH, HEIGHT, bg_path,
@ -274,11 +269,10 @@ class EasyGame(PaiaGame):
cmd_1p.append("NONE") cmd_1p.append("NONE")
return {get_ai_name(0): cmd_1p} 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): for i in range(count):
# add food to group # 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.centerx = random.randint(self.playground.left, self.playground.right)
food.rect.centery = random.randint(self.playground.top, self.playground.bottom) food.rect.centery = random.randint(self.playground.top, self.playground.bottom)
pass pass