-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplay_environment.py
More file actions
93 lines (71 loc) · 2.17 KB
/
play_environment.py
File metadata and controls
93 lines (71 loc) · 2.17 KB
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
import sys
from pynput import keyboard
from pynput.keyboard import Key
import gym
from multiprocessing import Process, Value
import time
from environments.gym import GymEnvironment
from environments.flappy_bird import FlappyBird
from environments.coin_collector import CoinCollector
from environments.snake import SnakeGame
# env = GymEnvironment(gym.make('LunarLander-v2'))
# env = FlappyBird()
# env = CoinCollector()
env = SnakeGame()
# # Default Empty Mappings:
# mappings = {}
# # Flappy Bird Mappings:
# mappings = {
# Key.space: 1
# }
# # Snake Game Mappings:
mappings = {
Key.left: 0,
Key.up: 1,
Key.right: 2,
Key.down: 3
}
default_action = 0
action = default_action
click_action = True
use_switch_back = True
switch_back = False
v = Value('i', 0)
def key_listener(v):
def set_action(act):
v.value = act
def on_key_press(key):
# print('Pressed Key %s' % key)
if key in mappings:
key = "'" + str(mappings[key]) + "'"
if str(key)[1:-1].isdigit() and int(str(key)[1:-1]) < env.num_of_actions():
set_action(int(str(key)[1:-1]))
switch_back = click_action
def on_key_release(key):
# print('Released Key %s' % key)
if not click_action and str(key)[1:-1].isdigit() and int(str(key)[1:-1]) < env.num_of_actions():
set_action(default_action)
with keyboard.Listener(on_release=on_key_release, on_press=on_key_press) as listener:
listener.join()
if __name__ == '__main__':
p = Process(target=key_listener, args=(v, ))
p.start()
done = False
while not done or input('Play again? (Y/n)').lower()[0] == 'y':
done = False
score = 0
observation = env.reset()
action = default_action
while not done:
action = v.value
observation, reward, done, info = env.step(action)
print(len(observation), env.len_of_state())
score += reward
env.render()
if switch_back and use_switch_back:
v.value = default_action
switch_back = False
time.sleep(0.1)
env.close()
p.terminate()
sys.exit()