doc: update README.md

This commit is contained in:
Kylin_on_Mac 2023-10-10 23:36:52 +08:00
parent 46e655930d
commit fec545bfe0
3 changed files with 34 additions and 39 deletions

View File

@ -22,20 +22,11 @@
```python
# main.py
game = EasyGame(
time_to_play, score_to_pass,
green_food_count, red_food_count,
playground_size: list,
level: int = -1,
level: int = 1,
level_file: str = None,
sound: str = "off")
```
- `time_to_play`:遊戲執行的終止時間,單位是 frame也就是遊戲內部更新畫面的次數每更新一次 frame +1
- `green_food_count`:遊戲中綠色食物的數量。
- `red_food_count`:遊戲中紅色食物的數量。
- `score_to_pass`:遊戲通關的點數,要超過這個分數才算過關。
- `playground_size`:可移動區域的大小。 使用逗號將數字隔開 `width,height` `100,200`
- `level`: 選定內建關卡,請注意,使用此設定將會覆蓋掉上述其他關卡參數設定,預設為 -1 不選擇任何關卡。
- `level`: 選定內建關卡,預設為 1 選擇第一關。
- `level_file`: 使用外部檔案作為關卡,請注意,使用此設定將會覆蓋掉關卡編號,並且不會自動進入下一關。
- `sound`: 音效。
@ -83,10 +74,10 @@ python -m mlgame -i ./ml/ml_play_template.py ./ --time_to_play 1200 --green_food
import random
class MLPlay:
def __init__(self):
def __init__(self,ai_name,*args, **kwargs):
print("Initial ml script")
def update(self, scene_info: dict):
def update(self, scene_info: dict,,*args, **kwargs):
# print("AI received data from game :", scene_info)
@ -114,12 +105,16 @@ class MLPlay:
"foods": [
{
"x": 656,
"y": 210
"y": 210,
"type": "GOOD_1",
"score": 1
},
...,
{
"x": 371,
"y": 217
"y": 217,
"type": "BAD_1",
"score": -1
}
],
@ -131,8 +126,10 @@ class MLPlay:
- `frame`:遊戲畫面更新的編號
- `ball_x`:主角方塊的X座標,表示方塊的左邊座標值。
- `ball_y`:主角方塊的Y座標,表示方塊的上方座標值。
- `foods`:食物的清單,清單內每一個物件都是一個食物的左上方座標值
- `foods`:食物的清單,清單內每一個物件都是一個食物的左上方座標值,也會提供此食物是什麼類型和分數多少。
- `type` 食物類型: `GOOD_1`, `GOOD_2`, `GOOD_3`, `BAD_1`, `BAD_2`, `BAD_3`
- `score`:目前得到的分數
- `score_to_pass`:通關分數
- `status` 目前遊戲的狀態
- `GAME_ALIVE`:遊戲進行中
- `GAME_PASS`:遊戲通關

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

@ -14,6 +14,21 @@ from .game_object import Ball
from .sound_controller import SoundController
def revise_ball(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
class EasyGame(PaiaGame):
"""
This is a Interface of a game
@ -21,7 +36,7 @@ class EasyGame(PaiaGame):
def __init__(
self,
level: int = -1,
level: int = 1,
level_file: str = None,
sound: str = "off",
*args, **kwargs):
@ -38,12 +53,9 @@ class EasyGame(PaiaGame):
# set by injected file
self._init_game_by_file(self._level_file)
pass
elif self._level != -1:
# set by level number
self._init_game_by_level(self._level)
pass
else:
self._init_game_by_level(1)
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")
@ -104,7 +116,7 @@ class EasyGame(PaiaGame):
action = "NONE"
self.ball.update(action)
self.revise_ball(self.ball, self.playground)
revise_ball(self.ball, self.playground)
# update sprite
self.foods.update()
@ -282,17 +294,3 @@ class EasyGame(PaiaGame):
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