2

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.

T2020
  • 61
  • 6

1 Answers1

0

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.

Armadillan
  • 530
  • 3
  • 15
  • Thanks for the help. I have looked into msvcrt using jupyter notebook but it does not work for me. I have pressed buttons but it does not register. Do you know what could be the problem? – T2020 Jan 12 '21 at 15:16
  • I don't think it's possible to use msvcrt.getch() in jupyter. I don't have a lot of experience with jupyter, but I can't find a way to do it. I found [this](https://pypi.org/project/readchar) which might work, but I haven't tried it. If you want to get input without blocking the game, but are ok with pressing enter each time, you can probably use [this](https://stackoverflow.com/a/53344690/10601555). – Armadillan Jan 12 '21 at 21:31