standalone.py 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. #!/usr/bin/env python3
  2. from __future__ import division, print_function
  3. import sys
  4. import numpy
  5. import gym
  6. import time
  7. from optparse import OptionParser
  8. import gym_minigrid
  9. def main():
  10. parser = OptionParser()
  11. parser.add_option(
  12. "-e",
  13. "--env-name",
  14. dest="env_name",
  15. help="gym environment to load",
  16. default='MiniGrid-MultiRoom-N6-v0'
  17. )
  18. (options, args) = parser.parse_args()
  19. # Load the gym environment
  20. env = gym.make(options.env_name)
  21. env.reset()
  22. # Create a window to render into
  23. renderer = env.render('human')
  24. def keyDownCb(keyName):
  25. action = 0
  26. if keyName == 'LEFT':
  27. action = env.actions.left
  28. elif keyName == 'RIGHT':
  29. action = env.actions.right
  30. elif keyName == 'UP':
  31. action = env.actions.forward
  32. elif keyName == 'SPACE':
  33. action = env.actions.toggle
  34. elif keyName == 'RETURN':
  35. env.reset()
  36. elif keyName == 'ESCAPE':
  37. sys.exit(0)
  38. else:
  39. print("unknown key %s" % keyName)
  40. return
  41. obs, reward, done, info = env.step(action)
  42. print('step=%s, reward=%s' % (env.stepCount, reward))
  43. if done:
  44. print('done!')
  45. env.reset()
  46. renderer.window.setKeyDownCb(keyDownCb)
  47. while True:
  48. env.render('human')
  49. time.sleep(0.01)
  50. # If the window was closed
  51. if renderer.window == None:
  52. break
  53. if __name__ == "__main__":
  54. main()