diff --git a/levels/001.json b/levels/001.json index 92f13d3..8327db7 100644 --- a/levels/001.json +++ b/levels/001.json @@ -1,10 +1,10 @@ { - "time_to_play": 1200, + "time_to_play": 600, "playground_size": [ 100, 200 ], - "score_to_pass": 20, + "score_to_pass": 10, "green_food_count": 3, "black_food_count": 0 } \ No newline at end of file diff --git a/levels/002.json b/levels/002.json new file mode 100644 index 0000000..ce81a47 --- /dev/null +++ b/levels/002.json @@ -0,0 +1,10 @@ +{ + "time_to_play": 600, + "playground_size": [ + 200, + 200 + ], + "score_to_pass": 15, + "green_food_count": 5, + "black_food_count": 0 +} \ No newline at end of file diff --git a/levels/003.json b/levels/003.json new file mode 100644 index 0000000..d8967b0 --- /dev/null +++ b/levels/003.json @@ -0,0 +1,10 @@ +{ + "time_to_play": 1000, + "playground_size": [ + 300, + 300 + ], + "score_to_pass": 15, + "green_food_count": 10, + "black_food_count": 0 +} \ No newline at end of file diff --git a/levels/004.json b/levels/004.json new file mode 100644 index 0000000..cf26d15 --- /dev/null +++ b/levels/004.json @@ -0,0 +1,10 @@ +{ + "time_to_play": 1000, + "playground_size": [ + 300, + 300 + ], + "score_to_pass": 15, + "green_food_count": 7, + "black_food_count": 3 +} \ No newline at end of file diff --git a/levels/005.json b/levels/005.json new file mode 100644 index 0000000..b4b44a2 --- /dev/null +++ b/levels/005.json @@ -0,0 +1,10 @@ +{ + "time_to_play": 1000, + "playground_size": [ + 300, + 300 + ], + "score_to_pass": 15, + "green_food_count": 7, + "black_food_count": 7 +} \ No newline at end of file diff --git a/levels/006.json b/levels/006.json new file mode 100644 index 0000000..ef06d11 --- /dev/null +++ b/levels/006.json @@ -0,0 +1,10 @@ +{ + "time_to_play": 1000, + "playground_size": [ + 400, + 400 + ], + "score_to_pass": 15, + "green_food_count": 7, + "black_food_count": 10 +} \ No newline at end of file diff --git a/levels/007.json b/levels/007.json new file mode 100644 index 0000000..49c8e26 --- /dev/null +++ b/levels/007.json @@ -0,0 +1,10 @@ +{ + "time_to_play": 1000, + "playground_size": [ + 400, + 400 + ], + "score_to_pass": 20, + "green_food_count": 7, + "black_food_count": 13 +} \ No newline at end of file diff --git a/levels/008.json b/levels/008.json new file mode 100644 index 0000000..61a8a29 --- /dev/null +++ b/levels/008.json @@ -0,0 +1,10 @@ +{ + "time_to_play": 1200, + "playground_size": [ + 500, + 500 + ], + "score_to_pass": 20, + "green_food_count": 10, + "black_food_count": 15 +} \ No newline at end of file diff --git a/levels/009.json b/levels/009.json new file mode 100644 index 0000000..bda3f34 --- /dev/null +++ b/levels/009.json @@ -0,0 +1,10 @@ +{ + "time_to_play": 1200, + "playground_size": [ + 600, + 500 + ], + "score_to_pass": 25, + "green_food_count": 15, + "black_food_count": 30 +} \ No newline at end of file diff --git a/levels/010.json b/levels/010.json new file mode 100644 index 0000000..845f344 --- /dev/null +++ b/levels/010.json @@ -0,0 +1,10 @@ +{ + "time_to_play": 1200, + "playground_size": [ + 700, + 600 + ], + "score_to_pass": 25, + "green_food_count": 20, + "black_food_count": 40 +} \ No newline at end of file diff --git a/src/game.py b/src/game.py index 5ecb927..b59186b 100644 --- a/src/game.py +++ b/src/game.py @@ -36,46 +36,64 @@ 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: - with open(os.path.join(LEVEL_PATH, f"{level:03d}.json")) as f: - game_params = json.load(f) - self.playground_w = int(game_params["playground_size"][0]) - self.playground_h = int(game_params["playground_size"][1]) - self.playground = pygame.Rect( - 0, 0, - self.playground_w, - self.playground_h - ) - self.green_food_count = int(game_params["green_food_count"]) - self.black_food_count = int(game_params["black_food_count"]) - self.score_to_win = int(game_params["score_to_pass"]) - self.frame_limit = int(game_params["time_to_play"]) + + self.set_game_params_by_level(level) pass else: - self.playground_w = int(playground_size[0]) - self.playground_h = int(playground_size[1]) + self._playground_w = int(playground_size[0]) + self._playground_h = int(playground_size[1]) self.playground = pygame.Rect( 0, 0, - self.playground_w, - self.playground_h + self._playground_w, + self._playground_h ) - self.green_food_count = green_food_count - self.black_food_count = black_food_count - self.score_to_win = score_to_pass - self.frame_limit = time_to_play + self._green_food_count = green_food_count + self._black_food_count = black_food_count + self._score_to_pass = score_to_pass + self._frame_limit = time_to_play + self.playground.center = (WIDTH / 2, HEIGHT / 2) - self.playground.center = (WIDTH / 2, HEIGHT / 2) self.foods = pygame.sprite.Group() self.init_game() + 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 + with open(level_file_path) as f: + game_params = json.load(f) + else: + # If the file doesn't exist, use default parameters + + with open(os.path.join(LEVEL_PATH, "001.json")) as f: + game_params = json.load(f) + self._level=1 + + + self._playground_w = int(game_params["playground_size"][0]) + self._playground_h = int(game_params["playground_size"][1]) + self.playground = pygame.Rect( + 0, 0, + self._playground_w, + self._playground_h + ) + self._green_food_count = int(game_params["green_food_count"]) + self._black_food_count = int(game_params["black_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) + + def init_game(self): self.ball = Ball() self.foods.empty() self.score = 0 - self._create_foods(self.green_food_count, FoodTypeEnum.GREEN) - self._create_foods(self.black_food_count, FoodTypeEnum.BLACK) + self._create_foods(self._green_food_count, FoodTypeEnum.GREEN) + self._create_foods(self._black_food_count, FoodTypeEnum.BLACK) self.frame_count = 0 - self._frame_count_down = self.frame_limit + self._frame_count_down = self._frame_limit def update(self, commands): # handle command @@ -104,7 +122,7 @@ class EasyGame(PaiaGame): # self._timer = round(time.time() - self._begin_time, 3) self.frame_count += 1 - self._frame_count_down = self.frame_limit - self.frame_count + self._frame_count_down = self._frame_limit - self.frame_count # self.draw() if not self.is_running: @@ -137,20 +155,25 @@ class EasyGame(PaiaGame): if self.is_running: status = GameStatus.GAME_ALIVE - elif self.score > self.score_to_win: + elif self.score > self._score_to_pass: status = GameStatus.GAME_PASS else: status = GameStatus.GAME_OVER return status def reset(self): + if 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) + self.init_game() pass @property def is_running(self): - return self.frame_count < self.frame_limit + return self.frame_count < self._frame_limit def get_scene_init_data(self): """ @@ -207,7 +230,7 @@ class EasyGame(PaiaGame): "player": get_ai_name(0), "rank": 1, "score": self.score, - "passed": self.score > self.score_to_win + "passed": self.score > self._score_to_pass } ] diff --git a/src/game_object.py b/src/game_object.py index f987cf4..acd649e 100644 --- a/src/game_object.py +++ b/src/game_object.py @@ -8,9 +8,9 @@ FOOD_COLOR_MAP = {FoodTypeEnum.GREEN: "#009688", BALL_COLOR = "#FFEB3B" BALL_VEL = 10.5 -BALL_H = 50 +BALL_H = 30 -BALL_W = 50 +BALL_W = 30 class Ball(pygame.sprite.Sprite):