forked from gxywy/gridworld
-
Notifications
You must be signed in to change notification settings - Fork 0
/
manual_control.py
95 lines (73 loc) · 2.52 KB
/
manual_control.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
import sys
import numpy as np
from grid_soccer.gridwolrd import SoccerGridWorld
from window import Window
class ManualController:
def __init__(self, env):
self.controlled_player = 0
self.env = env
def reset(self,):
state = env.reset()
img_dict = env.render()
window.show_grid(img_dict)
def step(self, action):
state, reward, done, info = env.step(action)
img_dict = env.render()
# print('action=%d, reward=%.2f' % (action, reward))
if done['blue'] or done['red']:
print('done!')
self.reset()
# else:
window.show_grid(img_dict)
def key_handler(self, event):
print('pressed', event.key)
default_action = [env.actions.no_move, ] * 3
if event.key == 'escape':
window.close()
return
if event.key == 'backspace':
self.reset()
return
if event.key == '1':
self.controlled_player = 1
return
if event.key == '2':
self.controlled_player = 2
return
if event.key == '3':
self.controlled_player = 3
return
if event.key == 'up':
act = default_action.copy()
act[self.controlled_player-1] = env.actions.up
self.step({'blue': act, 'red': default_action})
return
if event.key == 'down':
act = default_action.copy()
act[self.controlled_player-1] = env.actions.down
self.step({'blue': act, 'red': default_action})
return
if event.key == 'left':
act = default_action.copy()
act[self.controlled_player-1] = env.actions.left
self.step({'blue': act, 'red': default_action})
return
if event.key == 'right':
act = default_action.copy()
act[self.controlled_player-1] = env.actions.right
self.step({'blue': act, 'red': default_action})
return
if event.key == ' ':
act = default_action.copy()
act[self.controlled_player-1] = env.actions.ball
self.step({'blue': act, 'red': default_action})
return
if __name__ == '__main__':
env = SoccerGridWorld()
(env.actions.down, ) * 3
window = Window(img_size=(env.height, env.width, 3))
man_ctrl = ManualController(env=env)
window.reg_key_handler(man_ctrl.key_handler)
man_ctrl.reset()
# Blocking event loop
window.show(block=True)