Created
October 14, 2019 14:41
-
-
Save heiner/0c412edb87d605593a81391d471c1b3f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import os | |
| import select | |
| import sys | |
| import termios | |
| import gym | |
| def play(): | |
| env = gym.make("AdventureNoFrameskip-v4" if len(sys.argv) < 2 else sys.argv[1]) | |
| num_actions = env.action_space.n | |
| unused_observation = env.reset() | |
| while True: | |
| env.render() | |
| rlist, _, _ = select.select((0,), (), ()) | |
| if rlist: | |
| c = os.read(0, 1) | |
| if c == b"q": | |
| break | |
| elif c == b" ": | |
| select.select((0,), (), ()) | |
| elif c[0] in (ord("\n"), ord("\r")): # Return | |
| env.reset() | |
| continue | |
| else: | |
| action = int(c[0] - ord("0")) | |
| if action <= 0 or action >= num_actions: | |
| continue | |
| _, reward, done, info = env.step(action) | |
| print("reward:", reward, "done:", done, "info:", info) | |
| def main(): | |
| old = termios.tcgetattr(0) | |
| new = termios.tcgetattr(0) | |
| # Set to unbuffered, no echo. | |
| new[3] &= ~(termios.ICANON | termios.ECHO | termios.ECHONL) # lflags | |
| termios.tcsetattr(0, termios.TCSANOW, new) | |
| try: | |
| play() | |
| except KeyboardInterrupt: | |
| pass | |
| finally: | |
| termios.tcsetattr(0, termios.TCSANOW, old) | |
| if __name__ == "__main__": | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment