fix: update key in level file and fix error when reset game

This commit is contained in:
Kylin_on_Mac 2023-10-11 16:18:19 +08:00
parent fc3bf97ffe
commit b7076e4b32
14 changed files with 48 additions and 51 deletions

View File

@ -65,7 +65,8 @@ game = EasyGame(
```bash
# 在easy game中打開終端機
python -m mlgame -i ./ml/ml_play_template.py ./ --time_to_play 1200 --green_food_count 15 --red_food_count 10 --score_to_pass 10 --playground_size 100,200
python -m mlgame -i ./ml/ml_play_template.py ./ --level 3
python -m mlgame -i ./ml/ml_play_template.py ./ --level_file /path_to_file/level_file.json
```
## AI範例

View File

@ -18,8 +18,8 @@
"type": "int",
"min": -1,
"max": 100,
"default": 1,
"help": "選定內建關卡,預設為 1 選擇第一關。"
"default": -1,
"help": "選定內建關卡,請注意,使用此設定將會覆蓋掉其他關卡設定,預設為 -1 不選擇任何關卡。"
}, {
"name": "level_file",
"verbose": "匯入關卡檔案",

View File

@ -5,6 +5,6 @@
200
],
"score_to_pass": 10,
"green_food_count": [3,0,0],
"black_food_count": [0,0,0]
"good_food_count": [3,0,0],
"bad_food_count": [0,0,0]
}

View File

@ -5,6 +5,6 @@
200
],
"score_to_pass": 15,
"green_food_count": [5,0,0],
"black_food_count": [0,0,0]
"good_food_count": [5,0,0],
"bad_food_count": [0,0,0]
}

View File

@ -5,6 +5,6 @@
300
],
"score_to_pass": 15,
"green_food_count": [6,0,0],
"black_food_count": [2,0,0]
"good_food_count": [6,0,0],
"bad_food_count": [2,0,0]
}

View File

@ -5,6 +5,6 @@
300
],
"score_to_pass": 15,
"green_food_count": [8,0,0],
"black_food_count": [4,0,0]
"good_food_count": [8,0,0],
"bad_food_count": [4,0,0]
}

View File

@ -5,6 +5,6 @@
300
],
"score_to_pass": 15,
"green_food_count": [8,3,0],
"black_food_count": [4,0,0]
"good_food_count": [8,3,0],
"bad_food_count": [4,0,0]
}

View File

@ -5,6 +5,6 @@
400
],
"score_to_pass": 15,
"green_food_count": [8,4,0],
"black_food_count": [5,2,0]
"good_food_count": [8,4,0],
"bad_food_count": [5,2,0]
}

View File

@ -5,6 +5,6 @@
400
],
"score_to_pass": 20,
"green_food_count": [8,4,2],
"black_food_count": [4,4,0]
"good_food_count": [8,4,2],
"bad_food_count": [4,4,0]
}

View File

@ -5,6 +5,6 @@
500
],
"score_to_pass": 20,
"green_food_count": [6,4,2],
"black_food_count": [6,4,2]
"good_food_count": [6,4,2],
"bad_food_count": [6,4,2]
}

View File

@ -5,6 +5,6 @@
500
],
"score_to_pass": 25,
"green_food_count": [10,5,3],
"black_food_count": [12,7,4]
"good_food_count": [10,5,3],
"bad_food_count": [12,7,4]
}

View File

@ -5,6 +5,6 @@
600
],
"score_to_pass": 25,
"green_food_count": [12,8,4],
"black_food_count": [25,10,5]
"good_food_count": [12,8,4],
"bad_food_count": [25,10,5]
}

View File

@ -5,7 +5,7 @@
200
],
"score_to_pass": 10,
"green_food_count": [3,0,0],
"black_food_count": [0,0,0]
"good_food_count": [3,0,0],
"bad_food_count": [0,0,0]
}

View File

@ -36,7 +36,7 @@ class EasyGame(PaiaGame):
def __init__(
self,
level: int = 1,
level: int = -1,
level_file: str = None,
sound: str = "off",
*args, **kwargs):
@ -49,17 +49,8 @@ class EasyGame(PaiaGame):
self.foods = pygame.sprite.Group()
self.sound_controller = SoundController(sound)
if path.isfile(self._level_file) or self._level_file == "":
# set by injected file
self._init_game_by_file(self._level_file)
pass
self._init_game()
else:
self._init_game_by_level(self._level)
def _init_game_by_level(self, level: int):
level_file_path = os.path.join(LEVEL_PATH, f"{level:03d}.json")
self._init_game_by_file(level_file_path)
def _init_game_by_file(self, level_file_path: str):
try:
@ -82,8 +73,8 @@ class EasyGame(PaiaGame):
self._playground_w,
self._playground_h
)
self._green_food_count = game_params["green_food_count"]
self._red_food_count = game_params["black_food_count"]
self._good_food_count = game_params["good_food_count"]
self._bad_food_count = game_params["bad_food_count"]
self._score_to_pass = int(game_params["score_to_pass"])
self._frame_limit = int(game_params["time_to_play"])
self.playground.center = (WIDTH / 2, HEIGHT / 2)
@ -94,12 +85,12 @@ class EasyGame(PaiaGame):
self.score = 0
# todo validate food count
self._create_foods(GoodFoodLv1, self._green_food_count[0])
self._create_foods(GoodFoodLv2, self._green_food_count[1])
self._create_foods(GoodFoodLv3, self._green_food_count[2])
self._create_foods(BadFoodLv1, self._red_food_count[0])
self._create_foods(BadFoodLv2, self._red_food_count[1])
self._create_foods(BadFoodLv3, self._red_food_count[2])
self._create_foods(GoodFoodLv1, self._good_food_count[0])
self._create_foods(GoodFoodLv2, self._good_food_count[1])
self._create_foods(GoodFoodLv3, self._good_food_count[2])
self._create_foods(BadFoodLv1, self._bad_food_count[0])
self._create_foods(BadFoodLv2, self._bad_food_count[1])
self._create_foods(BadFoodLv3, self._bad_food_count[2])
self.frame_count = 0
self._frame_count_down = self._frame_limit
@ -182,19 +173,24 @@ class EasyGame(PaiaGame):
def reset(self):
if self.is_passed:
self.sound_controller.play_cheer()
if self._level_file:
pass
elif self.is_passed and self._level != -1:
# win and use level will enter next level
self._level += 1
self._init_game_by_level(self._level)
self.sound_controller.play_cheer()
self._init_game()
pass
def _init_game(self):
if path.isfile(self._level_file) or self._level_file == "":
# set by injected file
self._init_game_by_file(self._level_file)
pass
else:
level_file_path = os.path.join(LEVEL_PATH, f"{self._level:03d}.json")
self._init_game_by_file(level_file_path)
@property
def is_passed(self):
return self.score > self._score_to_pass