From 7be8491973ef0ff65c6de4315995d14f331d0718 Mon Sep 17 00:00:00 2001 From: Kylin_on_Mac Date: Thu, 21 Sep 2023 11:08:35 +0800 Subject: [PATCH 01/13] fix: reset game when timeout --- src/game.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/game.py b/src/game.py index f44dbcf..d4add56 100644 --- a/src/game.py +++ b/src/game.py @@ -22,6 +22,8 @@ class EasyGame(PaiaGame): super().__init__(user_num=1) self.game_result_state = GameResultState.FAIL self.scene = Scene(width=800, height=600, color="#4FC3F7", bias_x=0, bias_y=0) + self.total_point_count = total_point_count + self.color = color print(color) self.ball = Ball("#" + color) self.foods = pygame.sprite.Group() @@ -57,7 +59,7 @@ class EasyGame(PaiaGame): # self.draw() if not self.is_running: - return "QUIT" + return "RESET" def get_data_from_game_to_player(self): """ @@ -93,6 +95,14 @@ class EasyGame(PaiaGame): return status def reset(self): + self.score = 0 + self._begin_time = time.time() + self._timer = 0 + self.frame_count = 0 + self.ball = Ball("#" + self.color) + + self.foods = pygame.sprite.Group() + self._create_foods(self.total_point_count) pass @property From 3a9c44502eed8509b1029b922b0bdc601e1416d0 Mon Sep 17 00:00:00 2001 From: Kylin_on_Mac Date: Fri, 22 Sep 2023 11:19:35 +0800 Subject: [PATCH 02/13] feat: change timer from sec to frame from count up to count down --- levels/001.json | 27 +++++++++++++++++++++++++++ levels/template.json | 27 +++++++++++++++++++++++++++ src/game.py | 18 +++++++++--------- 3 files changed, 63 insertions(+), 9 deletions(-) create mode 100644 levels/001.json create mode 100644 levels/template.json diff --git a/levels/001.json b/levels/001.json new file mode 100644 index 0000000..537ddf2 --- /dev/null +++ b/levels/001.json @@ -0,0 +1,27 @@ +{ + "frame_limit": 1200, + "screen_size": [ + 100, + 100 + ], + "score_to_pass": 20, + "mebo_init_lv": 1, + "green_food_counts": [ + 8, + 5, + 2, + 1 + ], + "red_food_counts": [ + 8, + 5, + 2, + 1 + ], + "black_food_counts": [ + 8, + 5, + 2, + 1 + ] +} \ No newline at end of file diff --git a/levels/template.json b/levels/template.json new file mode 100644 index 0000000..537ddf2 --- /dev/null +++ b/levels/template.json @@ -0,0 +1,27 @@ +{ + "frame_limit": 1200, + "screen_size": [ + 100, + 100 + ], + "score_to_pass": 20, + "mebo_init_lv": 1, + "green_food_counts": [ + 8, + 5, + 2, + 1 + ], + "red_food_counts": [ + 8, + 5, + 2, + 1 + ], + "black_food_counts": [ + 8, + 5, + 2, + 1 + ] +} \ No newline at end of file diff --git a/src/game.py b/src/game.py index d4add56..2049764 100644 --- a/src/game.py +++ b/src/game.py @@ -11,6 +11,7 @@ from mlgame.view.view_model import create_text_view_data, create_asset_init_data from .game_object import Ball, Food ASSET_PATH = path.join(path.dirname(__file__), "../asset") +LEVEL_PATH = path.join(path.dirname(__file__), "../levels") class EasyGame(PaiaGame): @@ -21,19 +22,18 @@ class EasyGame(PaiaGame): def __init__(self, time_to_play, total_point_count, score, color, *args, **kwargs): super().__init__(user_num=1) self.game_result_state = GameResultState.FAIL - self.scene = Scene(width=800, height=600, color="#4FC3F7", bias_x=0, bias_y=0) + self.scene = Scene(width=800, height=600, color="#111111", bias_x=0, bias_y=0) self.total_point_count = total_point_count self.color = color - print(color) self.ball = Ball("#" + color) self.foods = pygame.sprite.Group() self.score = 0 self.score_to_win = score self._create_foods(total_point_count) self._begin_time = time.time() - self._timer = 0 + self._frame_count_down = time_to_play + self.frame_limit = time_to_play self.frame_count = 0 - self.time_limit = time_to_play def update(self, commands): # handle command @@ -53,9 +53,10 @@ class EasyGame(PaiaGame): if hits: self.score += len(hits) self._create_foods(len(hits)) - self._timer = round(time.time() - self._begin_time, 3) + # self._timer = round(time.time() - self._begin_time, 3) self.frame_count += 1 + self._frame_count_down = self.frame_limit - self.frame_count # self.draw() if not self.is_running: @@ -97,17 +98,16 @@ class EasyGame(PaiaGame): def reset(self): self.score = 0 self._begin_time = time.time() - self._timer = 0 + self._frame_count_down = 0 self.frame_count = 0 self.ball = Ball("#" + self.color) - self.foods = pygame.sprite.Group() self._create_foods(self.total_point_count) pass @property def is_running(self): - return self.frame_count < self.time_limit + return self.frame_count < self.frame_limit def get_scene_init_data(self): """ @@ -138,7 +138,7 @@ class EasyGame(PaiaGame): game_obj_list.extend(foods_data) backgrounds = [create_image_view_data("background", 0, 0, 800, 600)] foregrounds = [create_text_view_data(f"Score = {str(self.score)}", 650, 50, "#FF0000", "24px Arial BOLD")] - toggle_objs = [create_text_view_data(f"Timer = {str(self._timer)} s", 650, 100, "#FFAA00", "24px Arial")] + toggle_objs = [create_text_view_data(f"{self._frame_count_down:04d} frame", 650, 100, "#FFAA00", "24px Arial BOLD")] scene_progress = create_scene_progress_data(frame=self.frame_count, background=backgrounds, object_list=game_obj_list, foreground=foregrounds, toggle=toggle_objs) From 035b84a4802867ee53fa984c6a2edf3decf0cb42 Mon Sep 17 00:00:00 2001 From: Kylin_on_Mac Date: Fri, 22 Sep 2023 11:42:05 +0800 Subject: [PATCH 03/13] feat: change input parameter and food color --- game_config.json | 11 +++++++++-- src/game.py | 6 +++--- src/game_object.py | 4 +++- 3 files changed, 15 insertions(+), 6 deletions(-) diff --git a/game_config.json b/game_config.json index a1adc48..e8c0c82 100644 --- a/game_config.json +++ b/game_config.json @@ -21,8 +21,15 @@ "help": "set the limit of frame count , actually time will be revised according to your FPS ." }, { - "name": "total_point_count", - "verbose": "總食物數量", + "name": "green_food_count", + "verbose": "綠色食物數量", + "type": "int", + "choices":[ 5 ,10 ,15,20,25,30,35,40,45,50], + "help": "set the total number of points", + "default": 10 + }, { + "name": "black_food_count", + "verbose": "黑色食物數量", "type": "int", "choices":[ 5 ,10 ,15,20,25,30,35,40,45,50], "help": "set the total number of points", diff --git a/src/game.py b/src/game.py index 2049764..c1b8620 100644 --- a/src/game.py +++ b/src/game.py @@ -19,17 +19,17 @@ class EasyGame(PaiaGame): This is a Interface of a game """ - def __init__(self, time_to_play, total_point_count, score, color, *args, **kwargs): + def __init__(self, time_to_play, green_food_count,black_food_count, score, color, *args, **kwargs): super().__init__(user_num=1) self.game_result_state = GameResultState.FAIL self.scene = Scene(width=800, height=600, color="#111111", bias_x=0, bias_y=0) - self.total_point_count = total_point_count + self.total_point_count = green_food_count self.color = color self.ball = Ball("#" + color) self.foods = pygame.sprite.Group() self.score = 0 self.score_to_win = score - self._create_foods(total_point_count) + self._create_foods(self.total_point_count) self._begin_time = time.time() self._frame_count_down = time_to_play self.frame_limit = time_to_play diff --git a/src/game_object.py b/src/game_object.py index 9eafbfb..fe9d06a 100644 --- a/src/game_object.py +++ b/src/game_object.py @@ -4,6 +4,8 @@ import pygame.sprite from mlgame.view.view_model import create_rect_view_data +FOOD_COLOR = "#009688" + BALL_VEL = 10.5 BALL_H = 50 @@ -54,7 +56,7 @@ class Food(pygame.sprite.Sprite): def __init__(self, group): pygame.sprite.Sprite.__init__(self, group) self.image = pygame.Surface([8, 8]) - self.color = "#E91E63" + self.color = FOOD_COLOR self.rect = self.image.get_rect() self.rect.centerx = random.randint(0, 800) self.rect.centery = random.randint(0, 600) From 97308464c00558720aef8ee9d1f64d5fa2e1f939 Mon Sep 17 00:00:00 2001 From: Kylin_on_Mac Date: Fri, 22 Sep 2023 11:59:36 +0800 Subject: [PATCH 04/13] refac: update food color and food data format --- src/game_object.py | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/src/game_object.py b/src/game_object.py index fe9d06a..f37899e 100644 --- a/src/game_object.py +++ b/src/game_object.py @@ -63,18 +63,15 @@ class Food(pygame.sprite.Sprite): self.angle = 0 def update(self) -> None: - self.angle += 10 - if self.angle > 360: - self.angle -= 360 + pass @property def game_object_data(self): - return {"type": "rect", - "name": "ball", - "x": self.rect.x, - "y": self.rect.y, - "angle": 0, - "width": self.rect.width, - "height": self.rect.height, - "color": self.color - } + return create_rect_view_data( + "food", + self.rect.x, + self.rect.y, + self.rect.width, + self.rect.height, + self.color + ) \ No newline at end of file From 079c0c9e43665a8127bec774fcef3fc0c4aae6c1 Mon Sep 17 00:00:00 2001 From: Kylin_on_Mac Date: Fri, 22 Sep 2023 12:30:58 +0800 Subject: [PATCH 05/13] feat: add black food --- src/enums.py | 6 ++++++ src/game.py | 36 +++++++++++++++++++----------------- src/game_object.py | 12 ++++++++---- 3 files changed, 33 insertions(+), 21 deletions(-) create mode 100644 src/enums.py diff --git a/src/enums.py b/src/enums.py new file mode 100644 index 0000000..6c630cd --- /dev/null +++ b/src/enums.py @@ -0,0 +1,6 @@ + +from enum import auto +from mlgame.utils.enum import StringEnum +class FoodTypeEnum(StringEnum): + GREEN = auto() + BLACK = auto() diff --git a/src/game.py b/src/game.py index c1b8620..6ccf24c 100644 --- a/src/game.py +++ b/src/game.py @@ -8,6 +8,7 @@ from mlgame.utils.enum import get_ai_name from mlgame.view.decorator import check_game_progress, check_game_result from mlgame.view.view_model import create_text_view_data, create_asset_init_data, create_image_view_data, Scene, \ create_scene_progress_data +from .enums import FoodTypeEnum from .game_object import Ball, Food ASSET_PATH = path.join(path.dirname(__file__), "../asset") @@ -19,21 +20,26 @@ class EasyGame(PaiaGame): This is a Interface of a game """ - def __init__(self, time_to_play, green_food_count,black_food_count, score, color, *args, **kwargs): + def __init__(self, time_to_play, green_food_count, black_food_count, score, color, *args, **kwargs): super().__init__(user_num=1) self.game_result_state = GameResultState.FAIL self.scene = Scene(width=800, height=600, color="#111111", bias_x=0, bias_y=0) - self.total_point_count = green_food_count + self.green_food_count = green_food_count + self.black_food_count = black_food_count self.color = color - self.ball = Ball("#" + color) self.foods = pygame.sprite.Group() - self.score = 0 self.score_to_win = score - self._create_foods(self.total_point_count) - self._begin_time = time.time() - self._frame_count_down = time_to_play self.frame_limit = time_to_play + self.init_game() + + def init_game(self): + self.ball = Ball("#" + self.color) + 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.frame_count = 0 + self._frame_count_down = self.frame_limit def update(self, commands): # handle command @@ -96,13 +102,8 @@ class EasyGame(PaiaGame): return status def reset(self): - self.score = 0 - self._begin_time = time.time() - self._frame_count_down = 0 - self.frame_count = 0 - self.ball = Ball("#" + self.color) - self.foods = pygame.sprite.Group() - self._create_foods(self.total_point_count) + self.init_game() + pass @property @@ -138,7 +139,8 @@ class EasyGame(PaiaGame): game_obj_list.extend(foods_data) backgrounds = [create_image_view_data("background", 0, 0, 800, 600)] foregrounds = [create_text_view_data(f"Score = {str(self.score)}", 650, 50, "#FF0000", "24px Arial BOLD")] - toggle_objs = [create_text_view_data(f"{self._frame_count_down:04d} frame", 650, 100, "#FFAA00", "24px Arial BOLD")] + toggle_objs = [ + create_text_view_data(f"{self._frame_count_down:04d} frame", 650, 100, "#FFAA00", "24px Arial BOLD")] scene_progress = create_scene_progress_data(frame=self.frame_count, background=backgrounds, object_list=game_obj_list, foreground=foregrounds, toggle=toggle_objs) @@ -181,8 +183,8 @@ class EasyGame(PaiaGame): cmd_1p.append("NONE") return {get_ai_name(0): cmd_1p} - def _create_foods(self, count: int = 5): + def _create_foods(self, count: int = 5, type: FoodTypeEnum = FoodTypeEnum.GREEN): for i in range(count): # add food to group - food = Food(self.foods) + food = Food(self.foods, type) pass diff --git a/src/game_object.py b/src/game_object.py index f37899e..7f51b0b 100644 --- a/src/game_object.py +++ b/src/game_object.py @@ -2,9 +2,11 @@ import random import pygame.sprite +from games.easy_game.src.enums import FoodTypeEnum from mlgame.view.view_model import create_rect_view_data -FOOD_COLOR = "#009688" +FOOD_COLOR_MAP = {FoodTypeEnum.GREEN: "#009688", + FoodTypeEnum.BLACK: "#263238"} BALL_VEL = 10.5 @@ -53,10 +55,12 @@ class Ball(pygame.sprite.Sprite): class Food(pygame.sprite.Sprite): - def __init__(self, group): + def __init__(self, group, type: FoodTypeEnum): pygame.sprite.Sprite.__init__(self, group) self.image = pygame.Surface([8, 8]) - self.color = FOOD_COLOR + self.type = FoodTypeEnum.GREEN + self.color = FOOD_COLOR_MAP[type] + self.rect = self.image.get_rect() self.rect.centerx = random.randint(0, 800) self.rect.centery = random.randint(0, 600) @@ -74,4 +78,4 @@ class Food(pygame.sprite.Sprite): self.rect.width, self.rect.height, self.color - ) \ No newline at end of file + ) From 1d6077548ff2f757a361b5dc6f8de6fd6e5b5ff1 Mon Sep 17 00:00:00 2001 From: Kylin_on_Mac Date: Fri, 22 Sep 2023 12:44:51 +0800 Subject: [PATCH 06/13] feat: minus points after eating black point --- src/game.py | 18 ++++++++++++------ src/game_object.py | 2 +- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/src/game.py b/src/game.py index 6ccf24c..e385502 100644 --- a/src/game.py +++ b/src/game.py @@ -1,4 +1,3 @@ -import time from os import path import pygame @@ -36,8 +35,8 @@ class EasyGame(PaiaGame): self.ball = Ball("#" + self.color) 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 @@ -57,8 +56,15 @@ class EasyGame(PaiaGame): # handle collision hits = pygame.sprite.spritecollide(self.ball, self.foods, True, pygame.sprite.collide_rect_ratio(0.8)) if hits: - self.score += len(hits) - self._create_foods(len(hits)) + for food in hits: + if food.type == FoodTypeEnum.GREEN: + self.score += 1 + self._create_foods(1, FoodTypeEnum.GREEN) + + elif food.type == FoodTypeEnum.BLACK: + self._create_foods(1, FoodTypeEnum.BLACK) + self.score -= 1 + food.kill() # self._timer = round(time.time() - self._begin_time, 3) self.frame_count += 1 @@ -138,7 +144,7 @@ class EasyGame(PaiaGame): game_obj_list = [self.ball.game_object_data] game_obj_list.extend(foods_data) backgrounds = [create_image_view_data("background", 0, 0, 800, 600)] - foregrounds = [create_text_view_data(f"Score = {str(self.score)}", 650, 50, "#FF0000", "24px Arial BOLD")] + foregrounds = [create_text_view_data(f"Score = {self.score:04d}", 650, 50, "#FF0000", "24px Arial BOLD")] toggle_objs = [ create_text_view_data(f"{self._frame_count_down:04d} frame", 650, 100, "#FFAA00", "24px Arial BOLD")] scene_progress = create_scene_progress_data(frame=self.frame_count, background=backgrounds, diff --git a/src/game_object.py b/src/game_object.py index 7f51b0b..b5490a0 100644 --- a/src/game_object.py +++ b/src/game_object.py @@ -58,7 +58,7 @@ class Food(pygame.sprite.Sprite): def __init__(self, group, type: FoodTypeEnum): pygame.sprite.Sprite.__init__(self, group) self.image = pygame.Surface([8, 8]) - self.type = FoodTypeEnum.GREEN + self.type = type self.color = FOOD_COLOR_MAP[type] self.rect = self.image.get_rect() From 628a7678e18174b6c824bd1e6315b291d414edab Mon Sep 17 00:00:00 2001 From: Kylin_on_Mac Date: Fri, 22 Sep 2023 14:48:50 +0800 Subject: [PATCH 07/13] feat: add playground to limit movement of ball --- src/game.py | 44 +++++++++++++++++++++++++++++++++++++------- src/game_object.py | 2 -- 2 files changed, 37 insertions(+), 9 deletions(-) diff --git a/src/game.py b/src/game.py index e385502..e1ff96c 100644 --- a/src/game.py +++ b/src/game.py @@ -1,3 +1,4 @@ +import copy from os import path import pygame @@ -5,8 +6,7 @@ import pygame from mlgame.game.paia_game import PaiaGame, GameResultState, GameStatus from mlgame.utils.enum import get_ai_name from mlgame.view.decorator import check_game_progress, check_game_result -from mlgame.view.view_model import create_text_view_data, create_asset_init_data, create_image_view_data, Scene, \ - create_scene_progress_data +from mlgame.view.view_model import * from .enums import FoodTypeEnum from .game_object import Ball, Food @@ -22,13 +22,22 @@ class EasyGame(PaiaGame): def __init__(self, time_to_play, green_food_count, black_food_count, score, color, *args, **kwargs): super().__init__(user_num=1) self.game_result_state = GameResultState.FAIL + self.playground_w = 400 + self.playground_h = 300 self.scene = Scene(width=800, height=600, color="#111111", bias_x=0, bias_y=0) + self.playground = pygame.Rect( + 350 - (self.playground_w / 2), + 300 - (self.playground_h / 2), + self.playground_w, + self.playground_h + ) self.green_food_count = green_food_count self.black_food_count = black_food_count self.color = color - self.foods = pygame.sprite.Group() self.score_to_win = score self.frame_limit = time_to_play + + self.foods = pygame.sprite.Group() self.init_game() def init_game(self): @@ -49,12 +58,12 @@ class EasyGame(PaiaGame): action = "NONE" # print(ai_1p_cmd) self.ball.update(action) - + self.revise_ball(self.ball, self.playground) # update sprite self.foods.update() # handle collision - hits = pygame.sprite.spritecollide(self.ball, self.foods, True, pygame.sprite.collide_rect_ratio(0.8)) + hits = pygame.sprite.spritecollide(self.ball, self.foods, True) if hits: for food in hits: if food.type == FoodTypeEnum.GREEN: @@ -64,7 +73,6 @@ class EasyGame(PaiaGame): elif food.type == FoodTypeEnum.BLACK: self._create_foods(1, FoodTypeEnum.BLACK) self.score -= 1 - food.kill() # self._timer = round(time.time() - self._begin_time, 3) self.frame_count += 1 @@ -143,7 +151,12 @@ class EasyGame(PaiaGame): foods_data.append(food.game_object_data) game_obj_list = [self.ball.game_object_data] game_obj_list.extend(foods_data) - backgrounds = [create_image_view_data("background", 0, 0, 800, 600)] + backgrounds = [ + create_image_view_data("background", 0, 0, 800, 600), + create_rect_view_data( + "playground", self.playground.x, self.playground.y, + self.playground.w, self.playground.h, "#B3E5FC") + ] foregrounds = [create_text_view_data(f"Score = {self.score:04d}", 650, 50, "#FF0000", "24px Arial BOLD")] toggle_objs = [ create_text_view_data(f"{self._frame_count_down:04d} frame", 650, 100, "#FFAA00", "24px Arial BOLD")] @@ -193,4 +206,21 @@ class EasyGame(PaiaGame): for i in range(count): # add food to group food = Food(self.foods, type) + + food.rect.centerx = random.randint(self.playground.left, self.playground.right) + food.rect.centery = random.randint(self.playground.top, self.playground.bottom) + pass + + def revise_ball(self, ball: Ball, playground: pygame.Rect): + ball_rect = copy.deepcopy(ball.rect) + if ball_rect.left < playground.left: + ball_rect.left = playground.left + elif ball_rect.right > playground.right: + ball_rect.right = playground.right + + if ball_rect.top < playground.top: + ball_rect.top = playground.top + elif ball_rect.bottom > playground.bottom: + ball_rect.bottom = playground.bottom + ball.rect = ball_rect pass diff --git a/src/game_object.py b/src/game_object.py index b5490a0..7d9b778 100644 --- a/src/game_object.py +++ b/src/game_object.py @@ -62,8 +62,6 @@ class Food(pygame.sprite.Sprite): self.color = FOOD_COLOR_MAP[type] self.rect = self.image.get_rect() - self.rect.centerx = random.randint(0, 800) - self.rect.centery = random.randint(0, 600) self.angle = 0 def update(self) -> None: From 8129c284bd453f05f47ae164a4edb5454bd7cb04 Mon Sep 17 00:00:00 2001 From: Kylin_on_Mac Date: Fri, 22 Sep 2023 15:19:14 +0800 Subject: [PATCH 08/13] refac: playground pos and ball color --- game_config.json | 21 --------------------- src/game.py | 30 +++++++++++++++++++----------- src/game_object.py | 9 ++++----- 3 files changed, 23 insertions(+), 37 deletions(-) diff --git a/game_config.json b/game_config.json index e8c0c82..36a95a5 100644 --- a/game_config.json +++ b/game_config.json @@ -43,27 +43,6 @@ "max": 30, "default": 10, "help": "set the score to win this game " - }, - { - "name": "color", - "verbose": "矩形顏色", - "type": "str", - "choices": [ - { - "verbose": "CYAN", - "value": "00BCD4" - }, - { - "verbose": "YELLOW", - "value": "FFEB3B" - }, - { - "verbose": "ORANGE", - "value": "FF9800" - } - ], - "help": "set the color of rectangle", - "default": "00BCD4" } ] } \ No newline at end of file diff --git a/src/game.py b/src/game.py index e1ff96c..9d5846a 100644 --- a/src/game.py +++ b/src/game.py @@ -10,6 +10,11 @@ from mlgame.view.view_model import * from .enums import FoodTypeEnum from .game_object import Ball, Food +BG_COLOR = "#111111" +PG_COLOR = "#B3E5FC" + +WIDTH = 800 +HEIGHT = 600 ASSET_PATH = path.join(path.dirname(__file__), "../asset") LEVEL_PATH = path.join(path.dirname(__file__), "../levels") @@ -19,21 +24,24 @@ class EasyGame(PaiaGame): This is a Interface of a game """ - def __init__(self, time_to_play, green_food_count, black_food_count, score, color, *args, **kwargs): + def __init__( + self, time_to_play, score, green_food_count, black_food_count, + # playground_size + *args, **kwargs): super().__init__(user_num=1) - self.game_result_state = GameResultState.FAIL self.playground_w = 400 self.playground_h = 300 - self.scene = Scene(width=800, height=600, color="#111111", bias_x=0, bias_y=0) + + self.game_result_state = GameResultState.FAIL + self.scene = Scene(width=WIDTH, height=HEIGHT, color=BG_COLOR, bias_x=0, bias_y=0) self.playground = pygame.Rect( - 350 - (self.playground_w / 2), - 300 - (self.playground_h / 2), + 0, 0, self.playground_w, self.playground_h ) + self.playground.center = (WIDTH / 2, HEIGHT / 2) self.green_food_count = green_food_count self.black_food_count = black_food_count - self.color = color self.score_to_win = score self.frame_limit = time_to_play @@ -41,7 +49,7 @@ class EasyGame(PaiaGame): self.init_game() def init_game(self): - self.ball = Ball("#" + self.color) + self.ball = Ball() self.foods.empty() self.score = 0 self._create_foods(self.green_food_count, FoodTypeEnum.GREEN) @@ -56,7 +64,7 @@ class EasyGame(PaiaGame): action = ai_1p_cmd[0] else: action = "NONE" - # print(ai_1p_cmd) + self.ball.update(action) self.revise_ball(self.ball, self.playground) # update sprite @@ -131,7 +139,7 @@ class EasyGame(PaiaGame): # TODO add music or sound bg_path = path.join(ASSET_PATH, "img/background.jpg") background = create_asset_init_data( - "background", 800, 600, 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") scene_init_data = {"scene": self.scene.__dict__, "assets": [ @@ -152,10 +160,10 @@ class EasyGame(PaiaGame): game_obj_list = [self.ball.game_object_data] game_obj_list.extend(foods_data) backgrounds = [ - create_image_view_data("background", 0, 0, 800, 600), + create_image_view_data("background", 0, 0, WIDTH, HEIGHT), create_rect_view_data( "playground", self.playground.x, self.playground.y, - self.playground.w, self.playground.h, "#B3E5FC") + self.playground.w, self.playground.h, PG_COLOR) ] foregrounds = [create_text_view_data(f"Score = {self.score:04d}", 650, 50, "#FF0000", "24px Arial BOLD")] toggle_objs = [ diff --git a/src/game_object.py b/src/game_object.py index 7d9b778..f987cf4 100644 --- a/src/game_object.py +++ b/src/game_object.py @@ -1,5 +1,3 @@ -import random - import pygame.sprite from games.easy_game.src.enums import FoodTypeEnum @@ -7,7 +5,7 @@ from mlgame.view.view_model import create_rect_view_data FOOD_COLOR_MAP = {FoodTypeEnum.GREEN: "#009688", FoodTypeEnum.BLACK: "#263238"} - +BALL_COLOR = "#FFEB3B" BALL_VEL = 10.5 BALL_H = 50 @@ -16,14 +14,15 @@ BALL_W = 50 class Ball(pygame.sprite.Sprite): - def __init__(self, color="#FFEB3B"): + def __init__(self): pygame.sprite.Sprite.__init__(self) self.origin_image = pygame.Surface([BALL_W, BALL_H]) self.image = self.origin_image - self.color = color + self.color = BALL_COLOR self.rect = self.image.get_rect() self.rect.center = (400, 300) + def update(self, motion): # for motion in motions: if motion == "UP": From 9f5b5136e1ee9ef3f27388bcf29e1bffbc654f5d Mon Sep 17 00:00:00 2001 From: Kylin_on_Mac Date: Fri, 22 Sep 2023 15:45:51 +0800 Subject: [PATCH 09/13] feat: set playground size in init --- game_config.json | 10 +++++++++- src/game.py | 13 +++++++------ 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/game_config.json b/game_config.json index 36a95a5..c142412 100644 --- a/game_config.json +++ b/game_config.json @@ -36,13 +36,21 @@ "default": 10 }, { - "name": "score", + "name": "score_to_win", "verbose": "通關分數", "type": "int", "min": 1, "max": 30, "default": 10, "help": "set the score to win this game " + }, + { + "name": "playground_size", + "verbose": "場地尺寸", + "type": "list", + + "default": "500,400", + "help": "set the size of playground " } ] } \ No newline at end of file diff --git a/src/game.py b/src/game.py index 9d5846a..2ac9dbe 100644 --- a/src/game.py +++ b/src/game.py @@ -25,12 +25,12 @@ class EasyGame(PaiaGame): """ def __init__( - self, time_to_play, score, green_food_count, black_food_count, - # playground_size + self, time_to_play, score_to_win, green_food_count, black_food_count, + playground_size:list, *args, **kwargs): super().__init__(user_num=1) - self.playground_w = 400 - self.playground_h = 300 + self.playground_w = int(playground_size[0]) + self.playground_h = int(playground_size[1]) self.game_result_state = GameResultState.FAIL self.scene = Scene(width=WIDTH, height=HEIGHT, color=BG_COLOR, bias_x=0, bias_y=0) @@ -42,7 +42,7 @@ class EasyGame(PaiaGame): self.playground.center = (WIDTH / 2, HEIGHT / 2) self.green_food_count = green_food_count self.black_food_count = black_food_count - self.score_to_win = score + self.score_to_win = score_to_win self.frame_limit = time_to_play self.foods = pygame.sprite.Group() @@ -186,7 +186,8 @@ class EasyGame(PaiaGame): { "player": get_ai_name(0), "rank": 1, - "score": self.score + "score": self.score, + "passed":self.score>self.score_to_win } ] From 3ec08f85108ff17a06291b5bb233525368d48ed2 Mon Sep 17 00:00:00 2001 From: Kylin_on_Mac Date: Fri, 22 Sep 2023 16:19:10 +0800 Subject: [PATCH 10/13] feat: add level in game param --- game_config.json | 56 ++++++++++++++++++++++++++++++++++---------- levels/001.json | 27 ++++----------------- levels/template.json | 29 +++++------------------ src/game.py | 50 +++++++++++++++++++++++++++------------ 4 files changed, 90 insertions(+), 72 deletions(-) diff --git a/game_config.json b/game_config.json index c142412..0153002 100644 --- a/game_config.json +++ b/game_config.json @@ -8,7 +8,8 @@ "https://raw.githubusercontent.com/PAIA-Playful-AI-Arena/Paia-Desktop/master/media/easygame.svg" ], "user_num": { - "min": 1,"max": 1 + "min": 1, + "max": 1 }, "game_params": [ { @@ -24,19 +25,42 @@ "name": "green_food_count", "verbose": "綠色食物數量", "type": "int", - "choices":[ 5 ,10 ,15,20,25,30,35,40,45,50], - "help": "set the total number of points", - "default": 10 - }, { - "name": "black_food_count", - "verbose": "黑色食物數量", - "type": "int", - "choices":[ 5 ,10 ,15,20,25,30,35,40,45,50], + "choices": [ + 5, + 10, + 15, + 20, + 25, + 30, + 35, + 40, + 45, + 50 + ], "help": "set the total number of points", "default": 10 }, { - "name": "score_to_win", + "name": "black_food_count", + "verbose": "黑色食物數量", + "type": "int", + "choices": [ + 5, + 10, + 15, + 20, + 25, + 30, + 35, + 40, + 45, + 50 + ], + "help": "set the total number of points", + "default": 10 + }, + { + "name": "score_to_pass", "verbose": "通關分數", "type": "int", "min": 1, @@ -48,9 +72,17 @@ "name": "playground_size", "verbose": "場地尺寸", "type": "list", - - "default": "500,400", + "default": "400,400", "help": "set the size of playground " + }, + { + "name": "level", + "verbose": "內建關卡編號", + "type": "int", + "min": -1, + "max": 5, + "default": "-1", + "help": "選定內建關卡,請注意,使用此設定將會覆蓋掉其他設定,預設為 -1 不選擇任何關卡。" } ] } \ No newline at end of file diff --git a/levels/001.json b/levels/001.json index 537ddf2..92f13d3 100644 --- a/levels/001.json +++ b/levels/001.json @@ -1,27 +1,10 @@ { - "frame_limit": 1200, - "screen_size": [ + "time_to_play": 1200, + "playground_size": [ 100, - 100 + 200 ], "score_to_pass": 20, - "mebo_init_lv": 1, - "green_food_counts": [ - 8, - 5, - 2, - 1 - ], - "red_food_counts": [ - 8, - 5, - 2, - 1 - ], - "black_food_counts": [ - 8, - 5, - 2, - 1 - ] + "green_food_count": 3, + "black_food_count": 0 } \ No newline at end of file diff --git a/levels/template.json b/levels/template.json index 537ddf2..8e70077 100644 --- a/levels/template.json +++ b/levels/template.json @@ -1,27 +1,10 @@ { - "frame_limit": 1200, - "screen_size": [ - 100, - 100 + "time_to_play": 1200, + "playground_size": [ + 500, + 200 ], "score_to_pass": 20, - "mebo_init_lv": 1, - "green_food_counts": [ - 8, - 5, - 2, - 1 - ], - "red_food_counts": [ - 8, - 5, - 2, - 1 - ], - "black_food_counts": [ - 8, - 5, - 2, - 1 - ] + "green_food_count": 3, + "black_food_count": 1 } \ No newline at end of file diff --git a/src/game.py b/src/game.py index 2ac9dbe..5ecb927 100644 --- a/src/game.py +++ b/src/game.py @@ -1,4 +1,6 @@ import copy +import json +import os.path from os import path import pygame @@ -25,26 +27,44 @@ class EasyGame(PaiaGame): """ def __init__( - self, time_to_play, score_to_win, green_food_count, black_food_count, - playground_size:list, + self, time_to_play, score_to_pass, green_food_count, black_food_count, + playground_size: list, + level: int = -1, + # level_file, *args, **kwargs): super().__init__(user_num=1) - self.playground_w = int(playground_size[0]) - self.playground_h = int(playground_size[1]) self.game_result_state = GameResultState.FAIL self.scene = Scene(width=WIDTH, height=HEIGHT, color=BG_COLOR, bias_x=0, bias_y=0) - self.playground = pygame.Rect( - 0, 0, - self.playground_w, - self.playground_h - ) - self.playground.center = (WIDTH / 2, HEIGHT / 2) - self.green_food_count = green_food_count - self.black_food_count = black_food_count - self.score_to_win = score_to_win - self.frame_limit = time_to_play + 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"]) + pass + else: + 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.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.playground.center = (WIDTH / 2, HEIGHT / 2) self.foods = pygame.sprite.Group() self.init_game() @@ -187,7 +207,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_win } ] From d396ecc9f167910635ec2690069792fb835e3bae Mon Sep 17 00:00:00 2001 From: Kylin_on_Mac Date: Fri, 22 Sep 2023 17:11:54 +0800 Subject: [PATCH 11/13] feat: add 10 levels and add enter next level when pass game --- levels/001.json | 4 +-- levels/002.json | 10 ++++++ levels/003.json | 10 ++++++ levels/004.json | 10 ++++++ levels/005.json | 10 ++++++ levels/006.json | 10 ++++++ levels/007.json | 10 ++++++ levels/008.json | 10 ++++++ levels/009.json | 10 ++++++ levels/010.json | 10 ++++++ src/game.py | 81 +++++++++++++++++++++++++++++----------------- src/game_object.py | 4 +-- 12 files changed, 146 insertions(+), 33 deletions(-) create mode 100644 levels/002.json create mode 100644 levels/003.json create mode 100644 levels/004.json create mode 100644 levels/005.json create mode 100644 levels/006.json create mode 100644 levels/007.json create mode 100644 levels/008.json create mode 100644 levels/009.json create mode 100644 levels/010.json 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): From bd36c3494236e534099ceb5d2ce414d6648ded67 Mon Sep 17 00:00:00 2001 From: Kylin_on_Mac Date: Fri, 22 Sep 2023 17:29:34 +0800 Subject: [PATCH 12/13] refac: change black dot to red dot , update levels file and update information on screen --- game_config.json | 4 ++-- levels/001.json | 2 +- levels/002.json | 2 +- levels/003.json | 2 +- levels/004.json | 2 +- levels/005.json | 2 +- src/enums.py | 2 +- src/game.py | 34 ++++++++++++++++++++-------------- src/game_object.py | 2 +- 9 files changed, 29 insertions(+), 23 deletions(-) diff --git a/game_config.json b/game_config.json index 0153002..19bdc1a 100644 --- a/game_config.json +++ b/game_config.json @@ -41,8 +41,8 @@ "default": 10 }, { - "name": "black_food_count", - "verbose": "黑色食物數量", + "name": "red_food_count", + "verbose": "紅色食物數量", "type": "int", "choices": [ 5, diff --git a/levels/001.json b/levels/001.json index 8327db7..a948b2c 100644 --- a/levels/001.json +++ b/levels/001.json @@ -1,5 +1,5 @@ { - "time_to_play": 600, + "time_to_play": 300, "playground_size": [ 100, 200 diff --git a/levels/002.json b/levels/002.json index ce81a47..bb34c84 100644 --- a/levels/002.json +++ b/levels/002.json @@ -1,5 +1,5 @@ { - "time_to_play": 600, + "time_to_play": 300, "playground_size": [ 200, 200 diff --git a/levels/003.json b/levels/003.json index d8967b0..af6dd30 100644 --- a/levels/003.json +++ b/levels/003.json @@ -1,5 +1,5 @@ { - "time_to_play": 1000, + "time_to_play": 500, "playground_size": [ 300, 300 diff --git a/levels/004.json b/levels/004.json index cf26d15..34d2278 100644 --- a/levels/004.json +++ b/levels/004.json @@ -1,5 +1,5 @@ { - "time_to_play": 1000, + "time_to_play": 600, "playground_size": [ 300, 300 diff --git a/levels/005.json b/levels/005.json index b4b44a2..cee2f71 100644 --- a/levels/005.json +++ b/levels/005.json @@ -1,5 +1,5 @@ { - "time_to_play": 1000, + "time_to_play": 800, "playground_size": [ 300, 300 diff --git a/src/enums.py b/src/enums.py index 6c630cd..67d67c9 100644 --- a/src/enums.py +++ b/src/enums.py @@ -3,4 +3,4 @@ from enum import auto from mlgame.utils.enum import StringEnum class FoodTypeEnum(StringEnum): GREEN = auto() - BLACK = auto() + RED = auto() diff --git a/src/game.py b/src/game.py index b59186b..f69c57d 100644 --- a/src/game.py +++ b/src/game.py @@ -27,7 +27,7 @@ class EasyGame(PaiaGame): """ def __init__( - self, time_to_play, score_to_pass, green_food_count, black_food_count, + self, time_to_play, score_to_pass, green_food_count, red_food_count, playground_size: list, level: int = -1, # level_file, @@ -50,7 +50,7 @@ class EasyGame(PaiaGame): self._playground_h ) self._green_food_count = green_food_count - self._black_food_count = black_food_count + self._red_food_count = red_food_count self._score_to_pass = score_to_pass self._frame_limit = time_to_play self.playground.center = (WIDTH / 2, HEIGHT / 2) @@ -80,7 +80,7 @@ class EasyGame(PaiaGame): self._playground_h ) self._green_food_count = int(game_params["green_food_count"]) - self._black_food_count = int(game_params["black_food_count"]) + self._red_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) @@ -91,7 +91,7 @@ class EasyGame(PaiaGame): 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._red_food_count, FoodTypeEnum.RED) self.frame_count = 0 self._frame_count_down = self._frame_limit @@ -116,8 +116,8 @@ class EasyGame(PaiaGame): self.score += 1 self._create_foods(1, FoodTypeEnum.GREEN) - elif food.type == FoodTypeEnum.BLACK: - self._create_foods(1, FoodTypeEnum.BLACK) + elif food.type == FoodTypeEnum.RED: + self._create_foods(1, FoodTypeEnum.RED) self.score -= 1 # self._timer = round(time.time() - self._begin_time, 3) @@ -180,13 +180,13 @@ class EasyGame(PaiaGame): 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") - background = create_asset_init_data( - "background", WIDTH, HEIGHT, bg_path, - github_raw_url="https://raw.githubusercontent.com/PAIA-Playful-AI-Arena/easy_game/main/asset/img/background.jpg") + # bg_path = path.join(ASSET_PATH, "img/background.jpg") + # background = create_asset_init_data( + # "background", WIDTH, HEIGHT, bg_path, + # github_raw_url="https://raw.githubusercontent.com/PAIA-Playful-AI-Arena/easy_game/main/asset/img/background.jpg") scene_init_data = {"scene": self.scene.__dict__, "assets": [ - background + # background ], # "audios": {} } @@ -203,14 +203,20 @@ class EasyGame(PaiaGame): game_obj_list = [self.ball.game_object_data] game_obj_list.extend(foods_data) backgrounds = [ - create_image_view_data("background", 0, 0, WIDTH, HEIGHT), + # create_image_view_data("background", 0, 0, WIDTH, HEIGHT), create_rect_view_data( "playground", self.playground.x, self.playground.y, self.playground.w, self.playground.h, PG_COLOR) ] - foregrounds = [create_text_view_data(f"Score = {self.score:04d}", 650, 50, "#FF0000", "24px Arial BOLD")] + foregrounds = [ + + ] toggle_objs = [ - create_text_view_data(f"{self._frame_count_down:04d} frame", 650, 100, "#FFAA00", "24px Arial BOLD")] + create_text_view_data(f"Score:{self.score:04d}", 600, 50, "#A5D6A7", "24px Arial BOLD"), + create_text_view_data(f" Next:{self._score_to_pass:04d}", 600, 100, "#FF4081", "24px Arial BOLD"), + create_text_view_data(f" Time:{self._frame_count_down:04d}", 600, 150, "#FF5722", "24px Arial BOLD"), + + ] scene_progress = create_scene_progress_data(frame=self.frame_count, background=backgrounds, object_list=game_obj_list, foreground=foregrounds, toggle=toggle_objs) diff --git a/src/game_object.py b/src/game_object.py index acd649e..bda8172 100644 --- a/src/game_object.py +++ b/src/game_object.py @@ -4,7 +4,7 @@ from games.easy_game.src.enums import FoodTypeEnum from mlgame.view.view_model import create_rect_view_data FOOD_COLOR_MAP = {FoodTypeEnum.GREEN: "#009688", - FoodTypeEnum.BLACK: "#263238"} + FoodTypeEnum.RED: "#FF1744"} BALL_COLOR = "#FFEB3B" BALL_VEL = 10.5 From 36864f494db5e9b7694bf8ddbc665717b346c30b Mon Sep 17 00:00:00 2001 From: Kylin_on_Mac Date: Fri, 22 Sep 2023 17:55:43 +0800 Subject: [PATCH 13/13] doc: update README.md --- README.md | 17 +++++++++++------ blockly.json | 4 ++-- game_config.json | 2 +- requirements.txt | 2 +- 4 files changed, 15 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 6b315a5..f35889b 100644 --- a/README.md +++ b/README.md @@ -21,13 +21,17 @@ ```python # main.py -game = EasyGame(time_to_play=1000, total_point_count=10, score=5, color="FF9800") +game = EasyGame( + time_to_play, score_to_pass, green_food_count, red_food_count, + playground_size: list,level: int = -1) ``` - `time_to_play`:遊戲執行的終止時間,單位是 frame,也就是遊戲內部更新畫面的次數,每更新一次 frame +1 -- `total_point_count`:遊戲中食物出現的最大數量。 -- `score`:遊戲通關的點數,要超過這個分數才算過關。 -- `color`:主角方塊的顏色,使用16進位顏色表示法 +- `green_food_count`:遊戲中綠色食物的數量。 +- `red_food_count`:遊戲中紅色食物的數量。 +- `score_to_pass`:遊戲通關的點數,要超過這個分數才算過關。 +- `playground_size`:可移動區域的大小。 使用逗號將數字隔開 `width,height` `100,200` +- `level`: 選定內建關卡,請注意,使用此設定將會覆蓋掉其他設定,預設為 -1 不選擇任何關卡。 ## 玩法 @@ -53,7 +57,7 @@ game = EasyGame(time_to_play=1000, total_point_count=10, score=5, color="FF9800" 2. 座標系統 - 螢幕大小 800 x 600 - - 主角方塊 50 x 50 + - 主角方塊 30 x 30 - 食物方塊 8 x 8 --- @@ -64,7 +68,7 @@ game = EasyGame(time_to_play=1000, total_point_count=10, score=5, color="FF9800" ```bash # 在easy game中,打開終端機 -python -m mlgame -i ./ml/ml_play_template.py ./ --time_to_play 1200 --total_point_count 15 --score 10 --color FF9800 +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 ``` ## AI範例 @@ -163,5 +167,6 @@ class MLPlay: - `player`:玩家編號 - `score`:吃到的食物總數 - `rank`:排名 + - `passed`:是否通關 --- \ No newline at end of file diff --git a/blockly.json b/blockly.json index 209b6bf..922740c 100644 --- a/blockly.json +++ b/blockly.json @@ -20,8 +20,8 @@ [800, "right boundary", "右邊界"], [0, "top boundary", "上邊界"], [600, "bottom boundary", "下邊界"], - [50, "ball width", "球身的寬度"], - [50, "ball height", "球身的高度"], + [30, "ball width", "球身的寬度"], + [30, "ball height", "球身的高度"], [8, "food width", "食物的寬度"], [8, "food height", "食物的高度"] ], diff --git a/game_config.json b/game_config.json index 19bdc1a..bd1b77b 100644 --- a/game_config.json +++ b/game_config.json @@ -1,6 +1,6 @@ { "game_name": "easy_game", - "version": "2.1.0", + "version": "2.2.0-beta", "url": "https://github.com/PAIA-Playful-AI-Arena/easy_game", "description": "這是一個吃東西小遊戲,除了讓你熟習所有基本操作,也是 PAIA 的遊戲教學範例", "logo": [ diff --git a/requirements.txt b/requirements.txt index 6b60966..936367f 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1 +1 @@ -mlgame>=9.5.1.7b0 \ No newline at end of file +mlgame>=10.3.1 \ No newline at end of file