-
Notifications
You must be signed in to change notification settings - Fork 2
/
manual_control.py
59 lines (48 loc) · 1.08 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
import sys
import numpy as np
from gridwolrd import GridWorld
from window import Window
def reset():
state = env.reset()
window.show_grid(state)
def step(action):
state, reward, done = env.step(action)
print('action=%d, reward=%.2f' % (action, reward))
if done:
print('done!')
reset()
else:
window.show_grid(state)
def key_handler(event):
print('pressed', event.key)
if event.key == 'escape':
window.close()
return
if event.key == 'backspace':
reset()
return
if event.key == 'up':
step(env.actions.up)
return
if event.key == 'down':
step(env.actions.down)
return
if event.key == 'left':
step(env.actions.left)
return
if event.key == 'right':
step(env.actions.right)
return
if __name__ == '__main__':
if len(sys.argv) >= 2:
env = GridWorld((int(sys.argv[1]), int(sys.argv[2])))
else:
env = GridWorld()
file = input("grid file (default None):")
if file is not None:
env.load_file(file.replace("\\", "/").replace("\"", ""))
window = Window()
window.reg_key_handler(key_handler)
reset()
# Blocking event loop
window.show(block=True)