/
Game.py
101 lines (83 loc) · 3.23 KB
/
Game.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
from os import path
import pygame.key
from mlgame.game.paia_game import PaiaGame, GameStatus
from mlgame.utils.enum import get_ai_name
from mlgame.view.view_model import Scene, create_rect_view_data
from src.env import DARKGREY, LIGHTGREY
from .BattleMode import BattleMode
GAME_DIR = path.dirname(__file__)
MAP_DIR = path.join(GAME_DIR, "..", "asset", 'maps')
SOUND_DIR = path.join(GAME_DIR, "..", "asset", "sound")
HEIGHT = 600
WIDTH = 800
class Game(PaiaGame):
def __init__(self, user_num, is_manual: str):
super().__init__(user_num)
self.is_paused = False
self.is_debug = False
self.is_sound = False
self.is_manual = False
if is_manual:
self.is_manual = True
self.scene = Scene(WIDTH, HEIGHT+100, color=DARKGREY, bias_y=100)
self.game_mode = self.set_game_mode()
self.attachements = []
def get_data_from_game_to_player(self) -> dict:
to_players_data = self.game_mode.get_ai_data_to_player()
return to_players_data
def update(self, commands: dict):
self.handle_event()
self.game_mode.debugging(self.is_debug)
if not self.is_paused:
self.frame_count += 1
self.game_mode.update(commands)
if not self.is_running():
return "RESET"
def reset(self):
self.frame_count = 0
self.game_mode.reset()
self.rank()
def get_scene_init_data(self) -> dict:
"""
Get the scene and object information for drawing on the web
"""
game_info = {'scene': self.scene.__dict__,
'assets': self.game_mode.get_init_image_data()}
return game_info
def get_scene_progress_data(self) -> dict:
"""
Get the position of src objects for drawing on the web
"""
scene_progress = {'background': [create_rect_view_data(name="BG", x=0, y=0, width=WIDTH, height=HEIGHT, color=LIGHTGREY)],
'object_list': self.game_mode.get_obj_progress_data(),
'toggle_with_bias': [],
'toggle': self.game_mode.get_toggle_data(),
'foreground': [],
'user_info': [],
'game_sys_info': {}}
return scene_progress
def get_game_result(self):
"""
Get the src result for the web
"""
self.rank()
return {"frame_used": self.frame_count,
"state": self.game_result_state,
"attachment": self.attachements
}
def is_running(self):
return self.game_mode.status == GameStatus.GAME_ALIVE
def rank(self):
self.game_result_state = self.game_mode.state
self.attachements = self.game_mode.get_player_result()
return self.attachements
def handle_event(self):
key_board_list = pygame.key.get_pressed()
if key_board_list[pygame.K_b]:
self.is_debug = not self.is_debug
if key_board_list[pygame.K_SPACE]:
self.is_paused = not self.is_paused
def set_game_mode(self):
play_rect_area = pygame.Rect(0, 0, WIDTH, HEIGHT)
game_mode = BattleMode(play_rect_area, self.is_manual)
return game_mode