refac: add good food class and bad food class
This commit is contained in:
parent
87335c3b34
commit
ba349a6104
20
src/env.py
20
src/env.py
|
@ -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")
|
||||||
|
|
28
src/foods.py
28
src/foods.py
|
@ -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
|
||||||
|
|
22
src/game.py
22
src/game.py
|
@ -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
|
||||||
|
|
||||||
|
|
||||||
|
@ -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()
|
||||||
|
@ -120,15 +119,12 @@ class EasyGame(PaiaGame):
|
||||||
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):
|
||||||
"""
|
"""
|
||||||
|
@ -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
|
||||||
|
|
Loading…
Reference in New Issue