(self)
| 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 | # --------------------------------------------------------------------------- |
no test coverage detected