Jelajahi Sumber

Update matplotlib code in responde to #169

Maxime Chevalier-Boisvert 3 tahun lalu
induk
melakukan
2161af9d5d
2 mengubah file dengan 32 tambahan dan 3 penghapusan
  1. 7 3
      gym_minigrid/window.py
  2. 25 0
      test_interactive_mode.py

+ 7 - 3
gym_minigrid/window.py

@@ -44,15 +44,19 @@ class Window:
         Show an image or update the image being shown
         """
 
-        # Show the first image of the environment
+        # If no image has been shown yet,
+        # show the first image of the environment
         if self.imshow_obj is None:
             self.imshow_obj = self.ax.imshow(img, interpolation='bilinear')
 
+        # Update the image data
         self.imshow_obj.set_data(img)
-        self.fig.canvas.draw()
+
+        # Request the window be redrawn
+        self.fig.canvas.draw_idle()
+        self.fig.canvas.flush_events()
 
         # Let matplotlib process UI events
-        # This is needed for interactive mode to work properly
         plt.pause(0.001)
 
     def set_caption(self, text):

+ 25 - 0
test_interactive_mode.py

@@ -0,0 +1,25 @@
+#!/usr/bin/env python3
+
+import time
+import random
+import gym
+import gym_minigrid
+
+# Load the gym environment
+env = gym.make('MiniGrid-Empty-8x8-v0')
+env.reset()
+
+for i in range(0, 100):
+    print("step {}".format(i))
+
+    # Pick a random action
+    action = random.randint(0, env.action_space.n - 1)
+
+    obs, reward, done, info = env.step(action)
+
+    env.render()
+
+    time.sleep(0.05)
+
+# Test the close method
+env.close()