瀏覽代碼

Draw tile edges

Maxime Chevalier-Boisvert 5 年之前
父節點
當前提交
e17fe41fbf
共有 2 個文件被更改,包括 22 次插入26 次删除
  1. 19 23
      gym_minigrid/minigrid.py
  2. 3 3
      gym_minigrid/rendering.py

+ 19 - 23
gym_minigrid/minigrid.py

@@ -157,8 +157,8 @@ class Goal(WorldObj):
     def can_overlap(self):
         return True
 
-    def render(self, r):
-        fill_coords(img, point_in_rect(0.5, 0.5, 0.5, 0.5), COLORS[self.color])
+    def render(self, img):
+        fill_coords(img, point_in_rect(0, 1, 0, 1), COLORS[self.color])
 
 class Floor(WorldObj):
     """
@@ -235,8 +235,8 @@ class Wall(WorldObj):
     def see_behind(self):
         return False
 
-    def render(self, r):
-        fill_coords(img, point_in_rect(0.5, 0.5, 0.5, 0.5), COLORS[self.color])
+    def render(self, img):
+        fill_coords(img, point_in_rect(0, 1, 0, 1), COLORS[self.color])
 
 class Door(WorldObj):
     def __init__(self, color, is_open=False, is_locked=False):
@@ -518,14 +518,20 @@ class Grid:
         """
 
         # Hash map lookup key for the cache
-        key = obj.encode() + (agent_dir, highlight, tile_size)
+        key = (agent_dir, highlight, tile_size)
+        key = obj.encode() + key if obj else key
 
         if key in cls.tile_cache:
-            return tile_cache[key]
+            return cls.tile_cache[key]
 
         img = np.zeros(shape=(tile_size, tile_size, 3), dtype=np.uint8)
 
-        obj.render_tile(img)
+        # Draw the grid lines (top and left edges)
+        fill_coords(img, point_in_rect(0, 0.031, 0, 1), (100, 100, 100))
+        fill_coords(img, point_in_rect(0, 1, 0, 0.031), (100, 100, 100))
+
+        if obj != None:
+            obj.render(img)
 
         # TODO: overlay agent on top
         if agent_dir is not None:
@@ -536,7 +542,7 @@ class Grid:
             pass
 
         # Cache the rendered tile
-        tile_cache[key] = img
+        cls.tile_cache[key] = img
 
         return img
 
@@ -553,33 +559,23 @@ class Grid:
 
         img = np.zeros(shape=(height_px, width_px, 3), dtype=np.uint8)
 
-        """
-        # Draw grid lines
-        r.setLineColor(100, 100, 100)
-        for rowIdx in range(0, self.height):
-            y = TILE_PIXELS * rowIdx
-            r.drawLine(0, y, widthPx, y)
-        for colIdx in range(0, self.width):
-            x = TILE_PIXELS * colIdx
-            r.drawLine(x, 0, x, heightPx)
-        """
-
         # Render the grid
         for j in range(0, self.height):
             for i in range(0, self.width):
                 cell = self.get(i, j)
-                if cell == None:
-                    continue
 
-                """
                 tile_img = Grid.render_tile(
                     cell,
                     agent_dir=None,
                     highlight=False,
                     tile_size=tile_size
                 )
-                """
 
+                ymin = j * tile_size
+                ymax = (j+1) * tile_size
+                xmin = i * tile_size
+                xmax = (i+1) * tile_size
+                img[ymin:ymax, xmin:xmax, :] = tile_img
 
         return img
 

+ 3 - 3
gym_minigrid/rendering.py

@@ -17,12 +17,12 @@ def fill_coords(img, fn, color):
 
 def point_in_circle(cx, cy, r):
     def fn(x, y):
-        return (x-cx)*(x-cx) + (y-cy)*(y-cy) < r * r
+        return (x-cx)*(x-cx) + (y-cy)*(y-cy) <= r * r
     return fn
 
-def point_in_rect(cx, cy, rx, ry):
+def point_in_rect(xmin, xmax, ymin, ymax):
     def fn(x, y):
-        return abs(x-cx) < rx and abs(y - cy) < ry
+        return x >= xmin and x <= xmax and y >= ymin and y <= ymax
     return fn
 
 def point_in_triangle(a, b, c):