How to make cartpole game from GYM where user can play with keyboard

Viewed 737

I have learned about cart pole from open ai GYM and I was wondering it is possible to make a game where user can control the pole. I have tried to take user key stroke value as input and put that in action but the environment just freezes. Is there any way to slow the game and take user input to control? I am taking user input as

a = input()
action = int(a)

but it seems the wrong way.

1 Answers

This simple loop works for me:

import gym
env = gym.make("CartPole-v0")
env.reset()
while True:
    action = int(input("Action: "))
    if action in (0, 1):
        env.step(action)
        env.render()

You can build upon it to achieve what you want.

To get non-blocking input, that is without waiting for the user to press enter, check out this question: Python nonblocking console input.

Related