MCPcopy Index your code
hub / github.com/rlcode/reinforcement-learning / render

Method render

1-grid-world/env.py:106–143  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

104 self.q_overlay = q_table
105
106 def render(self):
107 if self._screen is None:
108 self._screen = _open(self.title, (WIDTH * UNIT, HEIGHT * UNIT + self.HUD))
109 self._font = pygame.font.SysFont(None, 18)
110 self._hud_font = pygame.font.SysFont(None, 22)
111 _pump_events()
112 s = self._screen
113 hud = self.HUD
114 s.fill(WHITE)
115 # HUD bar.
116 pygame.draw.rect(s, (30, 30, 30), pygame.Rect(0, 0, WIDTH * UNIT, hud))
117 # Steps display rounds down to the nearest 5 so the number doesn't
118 # flicker every frame (real step count is still in self.steps).
119 steps_shown = (self.steps // 5) * 5
120 last = f"{self.last_reward:+d}" if self.last_reward is not None else "—"
121 t = self._hud_font.render(
122 f"Episode: {self.episode} Steps: {steps_shown} Last Score: {last}",
123 True, (240, 240, 240))
124 s.blit(t, (8, (hud - t.get_height()) // 2))
125 # Grid + landmarks.
126 _grid_lines(s, UNIT, y_off=hud)
127 for ox, oy in self.obstacles:
128 _triangle(s, ox, oy, UNIT, OBSTACLE_COLOR, y_off=hud)
129 _circle(s, *self.goal, unit=UNIT, color=GOAL_COLOR, y_off=hud)
130 _square(s, *self.agent, unit=UNIT, color=AGENT_COLOR, y_off=hud)
131 if self.q_overlay is not None:
132 offsets = [(0, -UNIT // 2 + 10), (0, UNIT // 2 - 10),
133 (-UNIT // 2 + 15, 0), (UNIT // 2 - 15, 0)]
134 for x in range(WIDTH):
135 for y in range(HEIGHT):
136 qs = self.q_overlay.get(str([x, y]))
137 if qs is None: continue
138 cx, cy = _center(x, y, UNIT, y_off=hud)
139 for i, q in enumerate(qs):
140 t = self._font.render(f"{q:+.2f}", True, TEXT_COLOR)
141 s.blit(t, t.get_rect(center=(cx + offsets[i][0], cy + offsets[i][1])))
142 pygame.display.flip()
143 time.sleep(FPS_DELAY)
144
145
146# ---------------------------------------------------------------------------

Callers 5

resetMethod · 0.95
4-q_learning.pyFile · 0.45
3-sarsa.pyFile · 0.45
renderMethod · 0.45
_renderMethod · 0.45

Calls 7

_openFunction · 0.85
_pump_eventsFunction · 0.85
_grid_linesFunction · 0.85
_triangleFunction · 0.85
_circleFunction · 0.85
_squareFunction · 0.85
_centerFunction · 0.85

Tested by

no test coverage detected