-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
play_game.py
71 lines (59 loc) · 2.18 KB
/
play_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
# coding=utf-8
# Copyright 2019 Google LLC
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Script allowing to play the game by multiple players."""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from absl import app
from absl import flags
from absl import logging
from gfootball.env import config
from gfootball.env import football_env
FLAGS = flags.FLAGS
flags.DEFINE_string('players', 'keyboard:left_players=1',
'Semicolon separated list of players, single keyboard '
'player on the left by default')
flags.DEFINE_string('level', '', 'Level to play')
flags.DEFINE_enum('action_set', 'default', ['default', 'full'], 'Action set')
flags.DEFINE_bool('real_time', True,
'If true, environment will slow down so humans can play.')
flags.DEFINE_bool('render', True, 'Whether to do game rendering.')
def main(_):
players = FLAGS.players.split(';') if FLAGS.players else ''
assert not (any(['agent' in player for player in players])
), ('Player type \'agent\' can not be used with play_game.')
cfg_values = {
'action_set': FLAGS.action_set,
'dump_full_episodes': True,
'players': players,
'real_time': FLAGS.real_time,
}
if FLAGS.level:
cfg_values['level'] = FLAGS.level
cfg = config.Config(cfg_values)
env = football_env.FootballEnv(cfg)
if FLAGS.render:
env.render()
env.reset()
try:
while True:
_, _, done, _ = env.step([])
if done:
env.reset()
except KeyboardInterrupt:
logging.warning('Game stopped, writing dump...')
env.write_dump('shutdown')
exit(1)
if __name__ == '__main__':
app.run(main)