update
This commit is contained in:
parent
3eb14e009d
commit
458bf2fa46
10
README.md
10
README.md
|
@ -1,14 +1,14 @@
|
||||||
# **Easy Game**
|
# **Easy Game**
|
||||||
<img src="./asset/logo.svg" alt="logo" width="100"/>
|
|
||||||
|
|
||||||
[comment]: <> ()
|
[comment]: <> ()
|
||||||
|

|
||||||
|
|
||||||

|
|
||||||
[](https://www.python.org/downloads/release/python-390/)
|
[](https://www.python.org/downloads/release/python-390/)
|
||||||
[](https://github.com/PAIA-Playful-AI-Arena/MLGame)
|
[](https://github.com/PAIA-Playful-AI-Arena/MLGame)
|
||||||
[](https://github.com/pygame/pygame/releases/tag/2.0.1)
|
[](https://github.com/pygame/pygame/releases/tag/2.0.1)
|
||||||
---
|
|
||||||
這是一個吃東西小遊戲,除了讓你熟習所有基本操作,也是 PAIA 的遊戲教學範例
|
|
||||||
|
這是一個吃東西小遊戲,也是 PAIA 的遊戲教學範例
|
||||||
|
|
||||||

|

|
||||||
|
|
||||||
|
|
|
@ -1,8 +1,9 @@
|
||||||
|
import sys
|
||||||
from os import path
|
from os import path
|
||||||
|
sys.path.append(path.dirname(__file__))
|
||||||
|
|
||||||
from mlgame.utils.parse_config import read_json_file, parse_config
|
from mlgame.argument.tool import read_json_file, parse_config
|
||||||
from .src.game import EasyGame
|
from src.game import EasyGame
|
||||||
|
|
||||||
config_file = path.join(path.dirname(__file__), "game_config.json")
|
config_file = path.join(path.dirname(__file__), "game_config.json")
|
||||||
|
|
||||||
|
@ -15,6 +16,5 @@ GAME_PARAMS = parse_config(config_data)
|
||||||
|
|
||||||
GAME_SETUP = {
|
GAME_SETUP = {
|
||||||
"game": EasyGame,
|
"game": EasyGame,
|
||||||
"ml_clients": EasyGame.ai_clients(),
|
|
||||||
# "dynamic_ml_clients":True
|
# "dynamic_ml_clients":True
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,38 +0,0 @@
|
||||||
import random
|
|
||||||
import pygame
|
|
||||||
|
|
||||||
|
|
||||||
class MLPlay:
|
|
||||||
def __init__(self):
|
|
||||||
print("Initial ml script")
|
|
||||||
|
|
||||||
def update(self, scene_info: dict, keyboard:list=[], *args, **kwargs):
|
|
||||||
"""
|
|
||||||
Generate the command according to the received scene information
|
|
||||||
"""
|
|
||||||
# print("AI received data from game :", json.dumps(scene_info))
|
|
||||||
# print(scene_info)
|
|
||||||
actions = []
|
|
||||||
|
|
||||||
if pygame.K_w in keyboard or pygame.K_UP in keyboard:
|
|
||||||
actions.append("UP")
|
|
||||||
elif pygame.K_s in keyboard or pygame.K_DOWN in keyboard:
|
|
||||||
actions.append("DOWN")
|
|
||||||
|
|
||||||
elif pygame.K_a in keyboard or pygame.K_LEFT in keyboard:
|
|
||||||
actions.append("LEFT")
|
|
||||||
elif pygame.K_d in keyboard or pygame.K_RIGHT in keyboard:
|
|
||||||
actions.append("RIGHT")
|
|
||||||
else:
|
|
||||||
actions.append("NONE")
|
|
||||||
|
|
||||||
return actions
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def reset(self):
|
|
||||||
"""
|
|
||||||
Reset the status
|
|
||||||
"""
|
|
||||||
print("reset ml script")
|
|
||||||
pass
|
|
|
@ -1,21 +0,0 @@
|
||||||
import random
|
|
||||||
class MLPlay:
|
|
||||||
def __init__(self):
|
|
||||||
print("Initial ml script")
|
|
||||||
|
|
||||||
def update(self, scene_info: dict, *args, **kwargs):
|
|
||||||
"""
|
|
||||||
Generate the command according to the received scene information
|
|
||||||
"""
|
|
||||||
# print("AI received data from game :", json.dumps(scene_info))
|
|
||||||
# print(scene_info)
|
|
||||||
actions = ["UP", "DOWN", "LEFT", "RIGHT"]
|
|
||||||
|
|
||||||
return random.sample(actions, 1)
|
|
||||||
|
|
||||||
def reset(self):
|
|
||||||
"""
|
|
||||||
Reset the status
|
|
||||||
"""
|
|
||||||
print("reset ml script")
|
|
||||||
pass
|
|
30
src/game.py
30
src/game.py
|
@ -3,8 +3,9 @@ from os import path
|
||||||
|
|
||||||
import pygame
|
import pygame
|
||||||
|
|
||||||
from mlgame.gamedev.game_interface import PaiaGame, GameResultState, GameStatus
|
from mlgame.argument.model import AI_NAMES
|
||||||
from mlgame.view.test_decorator import check_game_progress, check_game_result
|
from mlgame.gamedev.paia_game import PaiaGame, GameResultState, GameStatus
|
||||||
|
from mlgame.tests.test_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, \
|
from mlgame.view.view_model import create_text_view_data, create_asset_init_data, create_image_view_data, Scene, \
|
||||||
create_scene_progress_data
|
create_scene_progress_data
|
||||||
from .game_object import Ball, Food
|
from .game_object import Ball, Food
|
||||||
|
@ -34,9 +35,13 @@ class EasyGame(PaiaGame):
|
||||||
|
|
||||||
def update(self, commands):
|
def update(self, commands):
|
||||||
# handle command
|
# handle command
|
||||||
ai_1p_cmd = commands[self.ai_clients()[0]["name"]][0]
|
ai_1p_cmd = commands[AI_NAMES[0]]
|
||||||
|
if ai_1p_cmd is not None:
|
||||||
|
action = ai_1p_cmd[0]
|
||||||
|
else:
|
||||||
|
action = "NONE"
|
||||||
# print(ai_1p_cmd)
|
# print(ai_1p_cmd)
|
||||||
self.ball.update(ai_1p_cmd)
|
self.ball.update(action)
|
||||||
|
|
||||||
# update sprite
|
# update sprite
|
||||||
self.foods.update()
|
self.foods.update()
|
||||||
|
@ -72,8 +77,7 @@ class EasyGame(PaiaGame):
|
||||||
"status": self.get_game_status()
|
"status": self.get_game_status()
|
||||||
}
|
}
|
||||||
|
|
||||||
for ai_client in self.ai_clients():
|
to_players_data[AI_NAMES[0]] = data_to_1p
|
||||||
to_players_data[ai_client['name']] = data_to_1p
|
|
||||||
# should be equal to config. GAME_SETUP["ml_clients"][0]["name"]
|
# should be equal to config. GAME_SETUP["ml_clients"][0]["name"]
|
||||||
|
|
||||||
return to_players_data
|
return to_players_data
|
||||||
|
@ -139,7 +143,7 @@ class EasyGame(PaiaGame):
|
||||||
"state": self.game_result_state,
|
"state": self.game_result_state,
|
||||||
"attachment": [
|
"attachment": [
|
||||||
{
|
{
|
||||||
"player": self.ai_clients()[0]["name"],
|
"player": AI_NAMES[0],
|
||||||
"rank": 1,
|
"rank": 1,
|
||||||
"score": self.score
|
"score": self.score
|
||||||
}
|
}
|
||||||
|
@ -163,8 +167,7 @@ class EasyGame(PaiaGame):
|
||||||
cmd_1p.append("RIGHT")
|
cmd_1p.append("RIGHT")
|
||||||
else:
|
else:
|
||||||
cmd_1p.append("NONE")
|
cmd_1p.append("NONE")
|
||||||
ai_1p = self.ai_clients()[0]["name"]
|
return {AI_NAMES[0]: cmd_1p}
|
||||||
return {ai_1p: cmd_1p}
|
|
||||||
|
|
||||||
def _create_foods(self, count: int = 5):
|
def _create_foods(self, count: int = 5):
|
||||||
for i in range(count):
|
for i in range(count):
|
||||||
|
@ -172,12 +175,3 @@ class EasyGame(PaiaGame):
|
||||||
food = Food(self.foods)
|
food = Food(self.foods)
|
||||||
pass
|
pass
|
||||||
|
|
||||||
@staticmethod
|
|
||||||
def ai_clients():
|
|
||||||
"""
|
|
||||||
let MLGame know how to parse your ai,
|
|
||||||
you can also use this names to get different cmd and send different data to each ai client
|
|
||||||
"""
|
|
||||||
return [
|
|
||||||
{"name": "1P"}
|
|
||||||
]
|
|
||||||
|
|
Loading…
Reference in New Issue