feat: add bg

This commit is contained in:
Kylin_on_Mac 2023-11-10 10:02:53 +08:00
parent b929f12cec
commit 64259dfc24
5 changed files with 49 additions and 42 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 317 KiB

View File

@ -1,13 +1,13 @@
{ {
"time_to_play": 300, "time_to_play": 300,
"playground_size_w":100, "playground_size_w":300,
"playground_size_h":200, "playground_size_h":300,
"score_to_pass": 10, "score_to_pass": 10,
"good_food_count": [0,0,0], "good_food_count": [0,0,0],
"bad_food_count": [0,0,0], "bad_food_count": [0,0,0],
"food_1": 3, "food_1": 3,
"food_2": 0, "food_2": 1,
"food_3": 0, "food_3": 1,
"garbage_1": 3, "garbage_1": 3,
"garbage_2": 0, "garbage_2": 0,
"garbage_3": 0 "garbage_3": 0

View File

@ -3,21 +3,21 @@ from os import path
from mlgame.utils.enum import StringEnum from mlgame.utils.enum import StringEnum
# game # game
WIDTH = 800 WIDTH = 900
HEIGHT = 600 HEIGHT = 600
BG_COLOR = "#111111" BG_COLOR = "#2B2B49"
PG_COLOR = "#B3E5FC" PG_COLOR = "#B3E5FC"
# ball # ball
BALL_COLOR = "#FFEB3B" BALL_COLOR = "#FFEB3B"
BALL_VEL = 10 BALL_VEL = 10
BALL_H = 30 BALL_H = 50
BALL_W = 30 BALL_W = 50
BALL_GROWTH_SCORE_STEP = 15 BALL_GROWTH_SCORE_STEP = 15
BALL_GROWTH_SIZE_STEP=10 BALL_GROWTH_SIZE_STEP=10
BALL_GROWTH_VEL_STEP=3 BALL_GROWTH_VEL_STEP=3
BALL_SIZE_MAX = 100 BALL_SIZE_MAX = 125
BALL_SIZE_MIN = 10 BALL_SIZE_MIN = 20
BALL_VEL_MAX = 25 BALL_VEL_MAX = 25
BALL_VEL_MIN = 10 BALL_VEL_MIN = 10
@ -39,9 +39,9 @@ FOOD_COLOR_MAP = {
FoodTypeEnum.BAD_2: "#FF1744", FoodTypeEnum.BAD_2: "#FF1744",
FoodTypeEnum.BAD_3: "#FF1744" FoodTypeEnum.BAD_3: "#FF1744"
} }
FOOD_LV1_SIZE = 20 FOOD_LV1_SIZE = 30
FOOD_LV2_SIZE = 12 FOOD_LV2_SIZE = 40
FOOD_LV3_SIZE = 16 FOOD_LV3_SIZE = 50
# path of assets # path of assets
ASSET_PATH = path.join(path.dirname(__file__), "..", "asset") ASSET_PATH = path.join(path.dirname(__file__), "..", "asset")

View File

@ -3,7 +3,6 @@ import json
import os.path import os.path
import pygame import pygame
from orjson import orjson
from mlgame.game.paia_game import PaiaGame, GameResultState, GameStatus from mlgame.game.paia_game import PaiaGame, GameResultState, GameStatus
from mlgame.utils.enum import get_ai_name from mlgame.utils.enum import get_ai_name
@ -52,7 +51,6 @@ class EasyGame(PaiaGame):
self._init_game() self._init_game()
@timeit_in_perf @timeit_in_perf
def _init_game_by_file(self, level_file_path: str): def _init_game_by_file(self, level_file_path: str):
try: try:
@ -71,8 +69,8 @@ class EasyGame(PaiaGame):
# set game params # set game params
self.playground = pygame.Rect( self.playground = pygame.Rect(
0, 0, 0, 0,
game_params.playground_w, game_params.playground_size_h,
game_params.playground_h game_params.playground_size_w
) )
self._good_food_count = game_params.good_food_count self._good_food_count = game_params.good_food_count
self._bad_food_count = game_params.bad_food_count self._bad_food_count = game_params.bad_food_count
@ -84,8 +82,9 @@ class EasyGame(PaiaGame):
self.ball = Ball() self.ball = Ball()
self.foods.empty() self.foods.empty()
if not isinstance(self._good_food_count,list) or len(self._good_food_count)<3: if not isinstance(self._good_food_count, list) or len(self._good_food_count) < 3:
raise Exception("你的關卡檔案格式有誤,請在'good_food_count' 欄位後面填入一個長度為3的陣列舉例 [1,2,3]") raise Exception(
"你的關卡檔案格式有誤,請在'good_food_count' 欄位後面填入一個長度為3的陣列舉例 [1,2,3]")
elif not isinstance(self._bad_food_count, list) or len(self._bad_food_count) < 3: elif not isinstance(self._bad_food_count, list) or len(self._bad_food_count) < 3:
raise Exception("你的關卡檔案格式有誤,請在'bad_food_count' 欄位後面填入一個長度為3的陣列舉例 [1,2,3]") raise Exception("你的關卡檔案格式有誤,請在'bad_food_count' 欄位後面填入一個長度為3的陣列舉例 [1,2,3]")
@ -102,8 +101,6 @@ class EasyGame(PaiaGame):
self._frame_count_down = self._frame_limit self._frame_count_down = self._frame_limit
self.sound_controller.play_music() self.sound_controller.play_music()
def update(self, commands): def update(self, commands):
# handle command # handle command
ai_1p_cmd = commands[get_ai_name(0)] ai_1p_cmd = commands[get_ai_name(0)]
@ -115,7 +112,7 @@ class EasyGame(PaiaGame):
self.ball.update(action) self.ball.update(action)
revise_ball(self.ball, self.playground) revise_ball(self.ball, self.playground)
# update sprite # update sprite
self.foods.update(playground=self.playground,squid=self.ball) self.foods.update(playground=self.playground, squid=self.ball)
# handle collision # handle collision
@ -135,11 +132,11 @@ class EasyGame(PaiaGame):
for food in hits: for food in hits:
# self.ball.score += food.score # self.ball.score += food.score
# growth play special sound # growth play special sound
self.ball.eat_food_and_change_level_and_play_sound(food,self.sound_controller) self.ball.eat_food_and_change_level_and_play_sound(food, self.sound_controller)
self._create_foods(food.__class__, 1) self._create_foods(food.__class__, 1)
if isinstance(food, (GoodFoodLv1,GoodFoodLv2,GoodFoodLv3,)): if isinstance(food, (GoodFoodLv1, GoodFoodLv2, GoodFoodLv3,)):
self.sound_controller.play_eating_good() self.sound_controller.play_eating_good()
elif isinstance(food, (BadFoodLv1,BadFoodLv2,BadFoodLv3,)): elif isinstance(food, (BadFoodLv1, BadFoodLv2, BadFoodLv3,)):
self.sound_controller.play_eating_bad() self.sound_controller.play_eating_bad()
def get_data_from_game_to_player(self): def get_data_from_game_to_player(self):
@ -158,12 +155,12 @@ class EasyGame(PaiaGame):
# TODO 確認要提供中心點座標還是左上角座標。 # TODO 確認要提供中心點座標還是左上角座標。
"player_x": self.ball.rect.centerx, "player_x": self.ball.rect.centerx,
"player_y": self.ball.rect.centery, "player_y": self.ball.rect.centery,
"player_size":self.ball.rect.width, "player_size": self.ball.rect.width,
"player_vel":self.ball.vel, "player_vel": self.ball.vel,
"foods": foods_data, "foods": foods_data,
"score": self.ball.score, "score": self.ball.score,
"score_to_pass":self._score_to_pass, "score_to_pass": self._score_to_pass,
"status": self.get_game_status() "status": self.get_game_status()
} }
@ -192,8 +189,6 @@ class EasyGame(PaiaGame):
self.sound_controller.play_fail() self.sound_controller.play_fail()
self._init_game() self._init_game()
pass pass
def _init_game(self): def _init_game(self):
@ -221,14 +216,25 @@ class EasyGame(PaiaGame):
# background = create_asset_init_data( # background = create_asset_init_data(
# "background", WIDTH, HEIGHT, bg_path, # "background", WIDTH, HEIGHT, bg_path,
# github_raw_url="https://raw.githubusercontent.com/PAIA-Playful-AI-Arena/easy_game/main/asset/img/background.jpg") # github_raw_url="https://raw.githubusercontent.com/PAIA-Playful-AI-Arena/easy_game/main/asset/img/background.jpg")
bg_path = path.join(ASSET_IMAGE_DIR, "background.png")
food01_path = path.join(ASSET_IMAGE_DIR, "food_01.png") food01_path = path.join(ASSET_IMAGE_DIR, "food_01.png")
# TODO
food01_url = food01_path food01_url = food01_path
scene_init_data = {"scene": self.scene.__dict__, bg_url = bg_path
"assets": [
create_asset_init_data("food01", 20, 20, food01_path,food01_url) scene_init_data = {
], "scene": self.scene.__dict__,
# "audios": {} "assets": [
} create_asset_init_data("bg", 1000, 1000, bg_path, bg_url),
create_asset_init_data("food01", 20, 20, food01_path, food01_url)
],
"background": [
create_image_view_data(
'bg', self.playground.x, self.playground.y,
self.playground.w, self.playground.h)
]
# "audios": {}
}
return scene_init_data return scene_init_data
@check_game_progress @check_game_progress
@ -243,9 +249,9 @@ class EasyGame(PaiaGame):
game_obj_list.extend(foods_data) game_obj_list.extend(foods_data)
backgrounds = [ backgrounds = [
# create_image_view_data("background", 0, 0, WIDTH, HEIGHT), # create_image_view_data("background", 0, 0, WIDTH, HEIGHT),
create_rect_view_data( # create_rect_view_data(
"playground", self.playground.x, self.playground.y, # "playground", self.playground.x, self.playground.y,
self.playground.w, self.playground.h, PG_COLOR) # self.playground.w, self.playground.h, PG_COLOR)
] ]
foregrounds = [ foregrounds = [

View File

@ -12,15 +12,16 @@ from mlgame.view.view_model import create_rect_view_data
class LevelParams(pydantic.BaseModel): class LevelParams(pydantic.BaseModel):
playground_w: int = 100 # TODO max and min
playground_h: int = 200 playground_size_w: int = 300
playground_size_h: int = 300
score_to_pass: int = 10 score_to_pass: int = 10
time_to_play: int = 300 time_to_play: int = 300
good_food_count: List[int] = [] good_food_count: List[int] = []
bad_food_count: List[int] = [] bad_food_count: List[int] = []
food_1: int = 0 food_1: int = 3
food_2: int = 0 food_2: int = 0
food_3: int = 0 food_3: int = 0
garbage_1: int = 0 garbage_1: int = 0