class TestGameConfig(unittest.TestCase): def setUp(self): self.game_config = GameConfig(world='pl100') def test_get_buildings_config(self): keys = [ 'speed', 'smith', 'storage', 'market', 'main', 'wood', 'wall', 'statue', 'stable', 'place', 'stone', 'iron', 'garage', 'hide', 'barracks', 'farm', 'snob' ] config = self.game_config.get_config() for key in keys: self.assertIn(key, config) @mock.patch( 'requests.get', return_value=type('response', (), {'iter_content': lambda x: [b'0123456', b'']})) @mock.patch('game_config.open', mock.mock_open()) def test_download_file(self, *args): self.game_config.download_file(self.game_config.buildings_file_name, self.game_config.buildings_file_link) def test_get_file_failure(self): self.game_config.download_file = lambda x, y: None with mock.patch('game_config.open', mock.MagicMock(), create=True) as mocked_open: mocked_open.side_effect = FileNotFoundError() self.assertRaises(FileNotFoundError, self.game_config.get_file, self.game_config.buildings_file_name, self.game_config.buildings_file_link) self.assertTrue(mocked_open.called)
def get_sample_game_player(game_config, name: str = "sample", stock: int = 0): """GamePlayerのサンプルを得る Args: game_config (GameConfig): ゲームの設定オブジェクト name (str, optional): GamePlayerの名前. Defaults to "sample". stock (int, optional): GamePlayerのストック数.これはGameScreen内でgame_settingのストック数で上書きされる. Defaults to 0. Returns: GamePlayer: ゲームプレイヤーオブジェクト """ import random from player import Player # player = Player(_id=random.randint(0, 10000), name=name, matches_num=1, win_num=1) game_config = GameConfig("./jsons/config.json") class GamePlayer: def __init__(self): self.player = None self.character = None self.stock = 0 def __repr__(self): return self.player.name game_player = GamePlayer() game_player.character = game_config.characters["0"] game_player.player = list(game_config.players.values())[0] game_player.stock = stock return game_player
def main(): from game_config import GameConfig pygame.init() pygame.display.set_mode((700, 700)) gc = GameConfig("./jsons/config.json") css = CharacterSelectScreen(gc, {}, {}) css.main()
def init(self): """全てのローディング処理 """ time.sleep(3) pygame.font.init() self.game_config = GameConfig("./jsons/config.json") self.run = False self.next_screen = Screen.START
def get_config(data): configData = data['GameConfig'] mapWidth = int(configData['MapWidth']) mapHeight = int(configData['MapHeight']) bombBlastRadius = int(configData['BombBlastRadius']) missileBlastRadius = int(configData['MissileBlastRadius']) roundsBetweenMissiles = int(configData['RoundsBetweenMissiles']) roundsBeforeIncreasingBlastRadius = int( configData['RoundsBeforeIncreasingBlastRadius']) isFastMissileModeEnabled = bool(configData['IsFastMissileModeEnabled']) config = GameConfig(mapWidth, mapHeight, bombBlastRadius, missileBlastRadius, roundsBetweenMissiles, roundsBeforeIncreasingBlastRadius, isFastMissileModeEnabled) config.print_debug() return config
def __init__(self, size, value, config: GameConfig): """ Create a tile :param size: tile size (squared width and height) in px :param value: tile value :param fg: tile foreground hex color string :param bg: tile background hex color string :param font: tile label font Tuple (face, size) """ self.config = config self.size = size self.value = value self.fg, self.bg = config.get_tile_colors(value) self.font = config.get_label_font() self.__futures = [] self.__current_move_future = None self.__current_position = None self.__merge_event = None self.__destination = None self.__tile = None self.__label = None
def run(): model_file = './current_policy.model' best_policy = PolicyValueNet(6, 6, model_file) config = GameConfig() board = Board(config) game = Game(board) mcts_player1 = MCTSPlayer(best_policy.policy_value_fn, c_puct=5, n_playout=1000) mcts_player2 = MCTS_Pure(c_puct=5, n_playout=1000) mcts_player3 = MCTS_Pure(c_puct=5, n_playout=1000) human = Human(config) human2 = Human(config) human3 = Human(config) game.start_play(mcts_player3, human, mcts_player2)
def __init__(self, game_player1, game_player2, game_setting): """ゲーム画面 Args: game_player1 (GamePlayer): プレイヤー情報(Player, Character, stock(player's rest stock)) game_player2 (GamePlayer): プレイヤー情報(Player, Character, stock(player's rest stock)) game_setting (GameSetting): ゲーム情報(stage, stock(先取)) """ super().__init__() game_config = GameConfig("./jsons/config.json") if game_player1 is None: game_player1 = get_sample_game_player(game_config, name="sample1") else: game_player1 = game_player1 if game_player1.character is None or game_player1.player is None: game_player1 = get_sample_game_player(game_config, name="sample1") if game_player2 is None: game_player2 = get_sample_game_player(game_config, name="sample2") else: game_player2 = game_player2 if game_player2.character is None or game_player2.player is None: game_player2 = get_sample_game_player(game_config, name="sample2") if game_setting is None: self.game_setting = get_sample_game_setting(game_config, stock=5) else: self.game_setting = game_setting self.actor1 = self.Actor(game_player1) self.actor2 = self.Actor(game_player2) # self.font = pygame.font.Font(None, 60) self.font = pygame.font.Font("./fonts/Mplus2-Medium.ttf", 60) self.yattane = pygame.mixer.Sound("./sounds/yattane.mp3") self.uu = pygame.mixer.Sound("./sounds/uu.mp3") self.sokomade = pygame.mixer.Sound("./sounds/sokomade.mp3") self.init()
def __init__(self, init_model=None): self.board_width = 6 self.board_height = 6 self.config = GameConfig() self.board = Board(self.config) self.game = Game(self.board) # training params #学习率0.002 self.learn_rate = 2e-3 #自动调整学习率 kl比较两个概率分布的接近程度。在某个变化范围内,KL散度取到最小值的时候,对应的参数是我们想要的最优参数 self.lr_multiplier = 1.0 # adaptively adjust the learning rate based on KL self.temp = 1.0 # the temperature param self.n_playout = 1500 # num of simulations for each move self.c_puct = 5 #UCTK self.buffer_size = 10000 self.batch_size = 200 # mini-batch size for training self.data_buffer = deque(maxlen=self.buffer_size) self.play_batch_size = 1 self.epochs = 5 # num of train_steps for each update self.kl_targ = 0.02 self.check_freq = 50 # self.check_freq = 25 # self.game_batch_num = 1500 self.game_batch_num = 5000 # num of simulations used for the pure mcts, which is used as # the opponent to evaluate the trained policy self.pure_mcts_playout_num = 5000 if init_model: # start training from an initial policy-value net self.policy_value_net = PolicyValueNet(self.board_width, self.board_height, model_file=init_model) else: # start training from a new policy-value net self.policy_value_net = PolicyValueNet(self.board_width, self.board_height) self.mcts_player = MCTSPlayer(self.policy_value_net.policy_value_fn, c_puct=self.c_puct, n_playout=self.n_playout, is_selfplay=1)
def setUp(self): self.game_config = GameConfig(world='pl100')
self.game_config = game_config self._set_return_btn() def _go_to_title(self): self.next_screen = Screen.START self.run = False def _set_return_btn(self): #SimpleButton(width=rect.width, height=rect.height, text="Return", outline=self.game_config.components["outline"], func = self._go_to_title) font = pygame.font.Font(None, 60) textsurface = font.render("Return", True, (0, 0, 0)) return_btn = RichSprite(*self.display.get_rect().bottomleft, align="left", vertical_align="bottom", image=textsurface) return_btn.rect.move_ip(10, -10) self.hoverable(return_btn, self.game_config.components["outline"], border_width=5) return_btn.change_press_fnc(self._go_to_title) self.middle_sprites.add(return_btn) if __name__ == '__main__': from game_config import GameConfig pygame.init() pygame.display.set_mode((700, 700)) gc = GameConfig("./jsons/config.json") os = OptionScreen(gc) os.main()
def testFileValues(self): cfg = GameConfig(self.temp_file) self.assertEqual(cfg.get_cell_size(), 150) self.assertEqual(cfg.get_cell_padding(), 5) self.assertEqual(cfg.get_label_font(), ("SimSun", 22)) self.assertEqual(cfg.get_board_colors(), ("#111111", "#888888")) self.assertEqual(cfg.get_up_keys(), self.default_values['controls']['up']) self.assertEqual(cfg.get_down_keys(), [83, 40]) self.assertEqual(cfg.get_left_keys(), [65]) self.assertEqual(cfg.get_right_keys(), [68]) self.assertEqual(cfg.get_tile_colors(2), ("#222222", "#000000")) self.assertEqual(cfg.get_tile_colors(8), ("#888888", "#444444")) self.assertEqual(cfg.get_tile_colors(4096), self.default_cell_color)
def testDefaultValues(self): cfg = GameConfig('__dummy__') self.assertEqual(cfg.get_cell_size(), self.default_values['dimensions']['cellSize']) self.assertEqual(cfg.get_cell_padding(), self.default_values['dimensions']['cellPadding']) self.assertEqual(cfg.get_label_font(), (self.default_values['font']['face'], self.default_values['font']['size'])) self.assertEqual(cfg.get_board_colors(), self.default_board_color) self.assertEqual(cfg.get_up_keys(), self.default_values['controls']['up']) self.assertEqual(cfg.get_down_keys(), self.default_values['controls']['down']) self.assertEqual(cfg.get_left_keys(), self.default_values['controls']['left']) self.assertEqual(cfg.get_right_keys(), self.default_values['controls']['right']) self.assertEqual(cfg.get_tile_colors(2), self.default_cell_color) self.assertEqual(cfg.get_tile_colors(8), self.default_cell_color) self.assertEqual(cfg.get_tile_colors(4096), self.default_cell_color)
from controllers.game_controller import GameController from game_config import GameConfig localedir = os.path.join(os.path.abspath(os.path.dirname(__file__)), 'locale') translate = gettext.translation('2048', localedir, languages=['en-US', 'zh_CN'], fallback=True) translate.install() def io_loop_thread(loop): loop.run_forever() def stop_loop(): loop = asyncio.get_event_loop() loop.stop() if __name__ == '__main__': logging.basicConfig(level=logging.DEBUG) io_loop = asyncio.get_event_loop() threading.Thread(target=io_loop_thread, args=(io_loop, )).start() config = GameConfig('config.json') GameController.initialize(config) GameController.new_game() GameController.run() io_loop.call_soon_threadsafe(stop_loop)
self.next_screen = Screen.CHARACTER_SELECT def _go_to_option_screen(self): """オプション画面に画面遷移 """ print("go to option screen") self.run = False self.next_screen = Screen.OPTION def main(self): while self.run: self.get_events() self.update() self.draw() pygame.display.update() self.clock.tick(self.fps) if __name__ == "__main__": import time pygame.init() pygame.display.set_mode((800, 800)) pygame.font.init() game_config = GameConfig("./jsons/config.json") components = game_config.components title_screen = TitleScreen(game_config) title_screen.main()
def __init__(self, world): try: self.config = self.cache[world] except KeyError: self.cache[world] = GameConfig(world).get_config() self.config = self.cache[world]