doc: update README.md
This commit is contained in:
parent
46e655930d
commit
fec545bfe0
29
README.md
29
README.md
|
@ -22,20 +22,11 @@
|
||||||
```python
|
```python
|
||||||
# main.py
|
# main.py
|
||||||
game = EasyGame(
|
game = EasyGame(
|
||||||
time_to_play, score_to_pass,
|
level: int = 1,
|
||||||
green_food_count, red_food_count,
|
|
||||||
playground_size: list,
|
|
||||||
level: int = -1,
|
|
||||||
level_file: str = None,
|
level_file: str = None,
|
||||||
sound: str = "off")
|
sound: str = "off")
|
||||||
```
|
```
|
||||||
|
- `level`: 選定內建關卡,預設為 1 選擇第一關。
|
||||||
- `time_to_play`:遊戲執行的終止時間,單位是 frame,也就是遊戲內部更新畫面的次數,每更新一次 frame +1
|
|
||||||
- `green_food_count`:遊戲中綠色食物的數量。
|
|
||||||
- `red_food_count`:遊戲中紅色食物的數量。
|
|
||||||
- `score_to_pass`:遊戲通關的點數,要超過這個分數才算過關。
|
|
||||||
- `playground_size`:可移動區域的大小。 使用逗號將數字隔開 `width,height` `100,200`
|
|
||||||
- `level`: 選定內建關卡,請注意,使用此設定將會覆蓋掉上述其他關卡參數設定,預設為 -1 不選擇任何關卡。
|
|
||||||
- `level_file`: 使用外部檔案作為關卡,請注意,使用此設定將會覆蓋掉關卡編號,並且不會自動進入下一關。
|
- `level_file`: 使用外部檔案作為關卡,請注意,使用此設定將會覆蓋掉關卡編號,並且不會自動進入下一關。
|
||||||
- `sound`: 音效。
|
- `sound`: 音效。
|
||||||
|
|
||||||
|
@ -83,10 +74,10 @@ python -m mlgame -i ./ml/ml_play_template.py ./ --time_to_play 1200 --green_food
|
||||||
import random
|
import random
|
||||||
|
|
||||||
class MLPlay:
|
class MLPlay:
|
||||||
def __init__(self):
|
def __init__(self,ai_name,*args, **kwargs):
|
||||||
print("Initial ml script")
|
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)
|
# print("AI received data from game :", scene_info)
|
||||||
|
|
||||||
|
@ -114,12 +105,16 @@ class MLPlay:
|
||||||
"foods": [
|
"foods": [
|
||||||
{
|
{
|
||||||
"x": 656,
|
"x": 656,
|
||||||
"y": 210
|
"y": 210,
|
||||||
|
"type": "GOOD_1",
|
||||||
|
"score": 1
|
||||||
},
|
},
|
||||||
...,
|
...,
|
||||||
{
|
{
|
||||||
"x": 371,
|
"x": 371,
|
||||||
"y": 217
|
"y": 217,
|
||||||
|
"type": "BAD_1",
|
||||||
|
"score": -1
|
||||||
}
|
}
|
||||||
|
|
||||||
],
|
],
|
||||||
|
@ -131,8 +126,10 @@ class MLPlay:
|
||||||
- `frame`:遊戲畫面更新的編號
|
- `frame`:遊戲畫面更新的編號
|
||||||
- `ball_x`:主角方塊的X座標,表示方塊的左邊座標值。
|
- `ball_x`:主角方塊的X座標,表示方塊的左邊座標值。
|
||||||
- `ball_y`:主角方塊的Y座標,表示方塊的上方座標值。
|
- `ball_y`:主角方塊的Y座標,表示方塊的上方座標值。
|
||||||
- `foods`:食物的清單,清單內每一個物件都是一個食物的左上方座標值
|
- `foods`:食物的清單,清單內每一個物件都是一個食物的左上方座標值,也會提供此食物是什麼類型和分數多少。
|
||||||
|
- `type` 食物類型: `GOOD_1`, `GOOD_2`, `GOOD_3`, `BAD_1`, `BAD_2`, `BAD_3`
|
||||||
- `score`:目前得到的分數
|
- `score`:目前得到的分數
|
||||||
|
- `score_to_pass`:通關分數
|
||||||
- `status`: 目前遊戲的狀態
|
- `status`: 目前遊戲的狀態
|
||||||
- `GAME_ALIVE`:遊戲進行中
|
- `GAME_ALIVE`:遊戲進行中
|
||||||
- `GAME_PASS`:遊戲通關
|
- `GAME_PASS`:遊戲通關
|
||||||
|
|
|
@ -18,8 +18,8 @@
|
||||||
"type": "int",
|
"type": "int",
|
||||||
"min": -1,
|
"min": -1,
|
||||||
"max": 100,
|
"max": 100,
|
||||||
"default": -1,
|
"default": 1,
|
||||||
"help": "選定內建關卡,請注意,使用此設定將會覆蓋掉其他關卡設定,預設為 -1 不選擇任何關卡。"
|
"help": "選定內建關卡,預設為 1 選擇第一關。"
|
||||||
}, {
|
}, {
|
||||||
"name": "level_file",
|
"name": "level_file",
|
||||||
"verbose": "匯入關卡檔案",
|
"verbose": "匯入關卡檔案",
|
||||||
|
|
40
src/game.py
40
src/game.py
|
@ -14,6 +14,21 @@ from .game_object import Ball
|
||||||
from .sound_controller import SoundController
|
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):
|
class EasyGame(PaiaGame):
|
||||||
"""
|
"""
|
||||||
This is a Interface of a game
|
This is a Interface of a game
|
||||||
|
@ -21,7 +36,7 @@ class EasyGame(PaiaGame):
|
||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
level: int = -1,
|
level: int = 1,
|
||||||
level_file: str = None,
|
level_file: str = None,
|
||||||
sound: str = "off",
|
sound: str = "off",
|
||||||
*args, **kwargs):
|
*args, **kwargs):
|
||||||
|
@ -38,12 +53,9 @@ class EasyGame(PaiaGame):
|
||||||
# set by injected file
|
# set by injected file
|
||||||
self._init_game_by_file(self._level_file)
|
self._init_game_by_file(self._level_file)
|
||||||
pass
|
pass
|
||||||
elif self._level != -1:
|
|
||||||
# set by level number
|
|
||||||
self._init_game_by_level(self._level)
|
|
||||||
pass
|
|
||||||
else:
|
else:
|
||||||
self._init_game_by_level(1)
|
self._init_game_by_level(self._level)
|
||||||
|
|
||||||
def _init_game_by_level(self, level: int):
|
def _init_game_by_level(self, level: int):
|
||||||
level_file_path = os.path.join(LEVEL_PATH, f"{level:03d}.json")
|
level_file_path = os.path.join(LEVEL_PATH, f"{level:03d}.json")
|
||||||
|
@ -104,7 +116,7 @@ class EasyGame(PaiaGame):
|
||||||
action = "NONE"
|
action = "NONE"
|
||||||
|
|
||||||
self.ball.update(action)
|
self.ball.update(action)
|
||||||
self.revise_ball(self.ball, self.playground)
|
revise_ball(self.ball, self.playground)
|
||||||
# update sprite
|
# update sprite
|
||||||
self.foods.update()
|
self.foods.update()
|
||||||
|
|
||||||
|
@ -282,17 +294,3 @@ class EasyGame(PaiaGame):
|
||||||
food.rect.centerx = random.randint(self.playground.left, self.playground.right)
|
food.rect.centerx = random.randint(self.playground.left, self.playground.right)
|
||||||
food.rect.centery = random.randint(self.playground.top, self.playground.bottom)
|
food.rect.centery = random.randint(self.playground.top, self.playground.bottom)
|
||||||
pass
|
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
|
|
||||||
|
|
Loading…
Reference in New Issue