feat: support level_file arg

This commit is contained in:
Kylin_on_Mac 2023-09-23 10:58:17 +08:00
parent bfc5ab7c9a
commit 55b195ae14
3 changed files with 30 additions and 13 deletions

View File

@ -83,6 +83,12 @@
"max": 5,
"default": "-1",
"help": "選定內建關卡,請注意,使用此設定將會覆蓋掉其他關卡設定,預設為 -1 不選擇任何關卡。"
}, {
"name": "level_file",
"verbose": "匯入關卡檔案",
"type": "path",
"default": "",
"help": "可匯入自定義的關卡資料,請參考內建範例的資料格式來設定。使用此設定會覆蓋掉其他關卡設定,也不會使用關卡編號,也不會自動進入下一關。"
},
{
"name": "sound",

View File

@ -3,8 +3,5 @@
"playground_size": [
500,
200
],
"score_to_pass": 20,
"green_food_count": 3,
"black_food_count": 1
]
}

View File

@ -23,7 +23,7 @@ class EasyGame(PaiaGame):
self, time_to_play, score_to_pass, green_food_count, red_food_count,
playground_size: list,
level: int = -1,
level_file=None,
level_file: str = None,
sound: str = "off",
*args, **kwargs):
super().__init__(user_num=1)
@ -31,11 +31,17 @@ class EasyGame(PaiaGame):
self.game_result_state = GameResultState.FAIL
self.scene = Scene(width=WIDTH, height=HEIGHT, color=BG_COLOR, bias_x=0, bias_y=0)
self._level = level
if level != -1:
self.set_game_params_by_level(level)
self._level_file = level_file
if self._level_file is not None or level == "":
# set by injected file
self.set_game_params_by_file(self._level_file)
pass
elif self._level != -1:
# set by level number
self.set_game_params_by_level(self._level)
pass
else:
# set by game params
self._playground_w = int(playground_size[0])
self._playground_h = int(playground_size[1])
self.playground = pygame.Rect(
@ -55,17 +61,23 @@ class EasyGame(PaiaGame):
def set_game_params_by_level(self, level):
level_file_path = os.path.join(LEVEL_PATH, f"{level:03d}.json")
if os.path.exists(level_file_path):
# If the file exists, load parameters from the file
self.set_game_params_by_file(level_file_path)
def set_game_params_by_file(self, level_file_path: str):
try:
with open(level_file_path) as f:
game_params = json.load(f)
else:
self._set_game_params(game_params)
except:
# If the file doesn't exist, use default parameters
print("此關卡檔案不存在,遊戲將會會自動使用第一關檔案 001.json。")
print("This level file is not existed , game will load 001.json automatically.")
with open(os.path.join(LEVEL_PATH, "001.json")) as f:
game_params = json.load(f)
self._level = 1
self._set_game_params(game_params)
def _set_game_params(self, game_params):
self._playground_w = int(game_params["playground_size"][0])
self._playground_h = int(game_params["playground_size"][1])
self.playground = pygame.Rect(
@ -159,7 +171,9 @@ class EasyGame(PaiaGame):
return status
def reset(self):
if self.score > self._score_to_pass and self._level != -1:
if self._level_file:
pass
elif self.score > self._score_to_pass and self._level != -1:
# win and use level will enter next level
self._level += 1
self.set_game_params_by_level(self._level)