Merge branch 'feature/update_to_fit_mlgame_9_5' into develop

# Conflicts:
#	README.md
#	config.py
This commit is contained in:
Kylin_on_Mac 2022-08-19 13:51:27 +08:00
commit fdf106a0d6
10 changed files with 62 additions and 61 deletions

View File

@ -1,10 +1,9 @@
# **Easy Game** # **Easy Game**
[comment]: <> (![python]&#40;https://img.shields.io/pypi/pyversions/pygame&#41;) [comment]: <> (![python]&#40;https://img.shields.io/pypi/pyversions/pygame&#41;)
![pygame](https://img.shields.io/badge/release-1.2.1gi-red.svg) ![easy_game](https://img.shields.io/github/v/tag/PAIA-Playful-AI-Arena/easy_game)
[![Python 3.9](https://img.shields.io/badge/python-3.9-blue.svg)](https://www.python.org/downloads/release/python-390/) [![Python 3.9](https://img.shields.io/badge/python-3.9-blue.svg)](https://www.python.org/downloads/release/python-390/)
[![MLGame](https://img.shields.io/badge/MLGame-9.4.0-<COLOR>.svg)](https://github.com/PAIA-Playful-AI-Arena/MLGame) [![MLGame](https://img.shields.io/badge/MLGame->9.5.3-<COLOR>.svg)](https://github.com/PAIA-Playful-AI-Arena/MLGame)
[![pygame](https://img.shields.io/badge/pygame-2.0.1-<COLOR>.svg)](https://github.com/pygame/pygame/releases/tag/2.0.1) [![pygame](https://img.shields.io/badge/pygame-2.0.1-<COLOR>.svg)](https://github.com/pygame/pygame/releases/tag/2.0.1)
@ -65,12 +64,10 @@ game = EasyGame(time_to_play=1000, total_point_count=10, score=5, color="FF9800"
## 使用AI玩遊戲 ## 使用AI玩遊戲
```bash ```bash
# python MLGame.py [options] easy_game [time_to_play] [total_point_count] [score] [color] # 在easy game中打開終端機
python MLGame.py -i ml_play_template.py easy_game --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 --total_point_count 15 --score 10 --color FF9800
``` ```
遊戲參數依序是[`time_to_play`] [`total_point_count`] [`score`] [`color`]
## AI範例 ## AI範例
```python ```python

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1018 KiB

After

Width:  |  Height:  |  Size: 317 KiB

View File

@ -1,4 +1,6 @@
{ {"INIT_INFO": [
["ai_name", "name of ai", "AI編號"]
],
"GAME_STATUS": [ "GAME_STATUS": [
["GAME_ALIVE", "alive", "存活"], ["GAME_ALIVE", "alive", "存活"],
["GAME_PASS", "pass", "通關"], ["GAME_PASS", "pass", "通關"],

View File

@ -2,20 +2,9 @@ import sys
from os import path from os import path
sys.path.append(path.dirname(__file__)) sys.path.append(path.dirname(__file__))
from mlgame.utils.parse_config 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_data = read_json_file(config_file)
GAME_VERSION = config_data["version"]
GAME_PARAMS = parse_config(config_data)
# will be equal to config. GAME_SETUP["ml_clients"][0]["name"]
GAME_SETUP = { GAME_SETUP = {
"game": EasyGame, "game": EasyGame,
"ml_clients": EasyGame.ai_clients(),
# "dynamic_ml_clients":True # "dynamic_ml_clients":True
} }

View File

@ -1,12 +1,15 @@
{ {
"game_name": "easy_game", "game_name": "easy_game",
"version": "1.2.1", "version": "2.0.1-beta",
"url": "https://github.com/PAIA-Playful-AI-Arena/easy_game", "url": "https://github.com/PAIA-Playful-AI-Arena/easy_game",
"description": "這是一個吃東西小遊戲,除了讓你熟習所有基本操作,也是 PAIA 的遊戲教學範例", "description": "這是一個吃東西小遊戲,除了讓你熟習所有基本操作,也是 PAIA 的遊戲教學範例",
"logo": [ "logo": [
"./asset/logo.svg", "./asset/logo.svg",
"https://raw.githubusercontent.com/PAIA-Playful-AI-Arena/Paia-Desktop/master/media/easygame.svg" "https://raw.githubusercontent.com/PAIA-Playful-AI-Arena/Paia-Desktop/master/media/easygame.svg"
], ],
"user_num": {
"min": 1,"max": 1
},
"game_params": [ "game_params": [
{ {
"name": "time_to_play", "name": "time_to_play",

View File

@ -3,7 +3,7 @@ import sys
sys.path.append(r"../..") sys.path.append(r"../..")
from mlgame.view.view import PygameView from mlgame.view.view import PygameView
from mlgame.gamedev.generic import quit_or_esc from mlgame.game.generic import quit_or_esc
from src.game import EasyGame from src.game import EasyGame
FPS = 30 FPS = 30

View File

@ -3,7 +3,7 @@ import pygame
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, keyboard:list=[], *args, **kwargs): def update(self, scene_info: dict, keyboard:list=[], *args, **kwargs):

View File

@ -1,6 +1,6 @@
import random import random
class MLPlay: class MLPlay:
def __init__(self): def __init__(self,*args, **kwargs):
print("Initial ml script") print("Initial ml script")
def update(self, scene_info: dict, *args, **kwargs): def update(self, scene_info: dict, *args, **kwargs):

View File

@ -3,8 +3,9 @@ from os import path
import pygame import pygame
from mlgame.gamedev.game_interface import PaiaGame, GameResultState, GameStatus from mlgame.game.paia_game import PaiaGame, GameResultState, GameStatus
from mlgame.view.test_decorator import check_game_progress, check_game_result 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, \ 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
@ -17,8 +18,8 @@ class EasyGame(PaiaGame):
This is a Interface of a game This is a Interface of a game
""" """
def __init__(self, time_to_play, total_point_count, score, color): def __init__(self, time_to_play, total_point_count, score, color, *args, **kwargs):
super().__init__() super().__init__(user_num=1)
self.game_result_state = GameResultState.FAIL 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="#4FC3F7", bias_x=0, bias_y=0)
print(color) print(color)
@ -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[get_ai_name(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()
@ -54,7 +59,7 @@ class EasyGame(PaiaGame):
if not self.is_running: if not self.is_running:
return "QUIT" return "QUIT"
def game_to_player_data(self): def get_data_from_game_to_player(self):
""" """
send something to game AI send something to game AI
we could send different data to different ai we could send different data to different ai
@ -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[get_ai_name(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
@ -101,7 +105,9 @@ class EasyGame(PaiaGame):
""" """
# TODO add music or sound # TODO add music or sound
bg_path = path.join(ASSET_PATH, "img/background.jpg") bg_path = path.join(ASSET_PATH, "img/background.jpg")
background = create_asset_init_data("background", 800, 600, bg_path, "url") background = create_asset_init_data(
"background", 800, 600, 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__, scene_init_data = {"scene": self.scene.__dict__,
"assets": [ "assets": [
background background
@ -139,7 +145,7 @@ class EasyGame(PaiaGame):
"state": self.game_result_state, "state": self.game_result_state,
"attachment": [ "attachment": [
{ {
"player": self.ai_clients()[0]["name"], "player": get_ai_name(0),
"rank": 1, "rank": 1,
"score": self.score "score": self.score
} }
@ -163,21 +169,10 @@ 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 {get_ai_name(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):
# add food to group # add food to group
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"}
]

View File

@ -2,11 +2,20 @@ import random
import pygame.sprite import pygame.sprite
from mlgame.view.view_model import create_rect_view_data
BALL_VEL = 10.5
BALL_H = 50
BALL_W = 50
class Ball(pygame.sprite.Sprite): class Ball(pygame.sprite.Sprite):
def __init__(self, color="#FFEB3B"): def __init__(self, color="#FFEB3B"):
pygame.sprite.Sprite.__init__(self) pygame.sprite.Sprite.__init__(self)
self.image = pygame.Surface([50, 50]) self.origin_image = pygame.Surface([BALL_W, BALL_H])
self.image = self.origin_image
self.color = color self.color = color
self.rect = self.image.get_rect() self.rect = self.image.get_rect()
self.rect.center = (400, 300) self.rect.center = (400, 300)
@ -14,25 +23,31 @@ class Ball(pygame.sprite.Sprite):
def update(self, motion): def update(self, motion):
# for motion in motions: # for motion in motions:
if motion == "UP": if motion == "UP":
self.rect.centery -= 10.5 self.rect.centery -= BALL_VEL
elif motion == "DOWN": elif motion == "DOWN":
self.rect.centery += 10.5 self.rect.centery += BALL_VEL
elif motion == "LEFT": elif motion == "LEFT":
self.rect.centerx -= 10.5 self.rect.centerx -= BALL_VEL
# self.angle += 5
elif motion == "RIGHT": elif motion == "RIGHT":
self.rect.centerx += 10.5 self.rect.centerx += BALL_VEL
# self.angle -= 5
# self.image = pygame.transform.rotate(self.origin_image, self.angle)
# print(self.angle)
# center = self.rect.center
# self.rect = self.image.get_rect()
# self.rect.center = center
@property @property
def game_object_data(self): def game_object_data(self):
return {"type": "rect", return create_rect_view_data(
"name": "ball", "ball",
"x": self.rect.x, self.rect.x,
"y": self.rect.y, self.rect.y,
"angle": 0, self.rect.width,
"width": self.rect.width, self.rect.height,
"height": self.rect.height, self.color
"color": self.color )
}
class Food(pygame.sprite.Sprite): class Food(pygame.sprite.Sprite):