def render(self):
# Close the display if the renderer options is set to disable the display
if not self.display_quitted and not self.renderer_options.show_display:
# Replace the screen surface with in-memory surface
self.screen = self.screen.copy()
# Close the display
pygame.display.quit()
# Prevent from further closing
self.display_quitted = True
# Clear the overlays
self.dirty_groups.clear(self.screen, self.background)
# Update the overlays by the environment state
self._update_overlay_pos()
self._update_overlay_visibility()
# Draw the overlays
dirty = self.dirty_groups.draw(self.screen)
# Update only the dirty surface
if self.renderer_options.show_display:
pygame.display.update(dirty)
# Limit the max frames per second
if self.renderer_options.show_display:
self.clock.tick(self.renderer_options.max_fps)
# Handle the events
if self.renderer_options.show_display:
for event in pygame.event.get():
# Detect the quit event
if event.type == pygame.locals.QUIT:
# Indicate the rendering should stop
return False
# Detect the keydown event
if self.renderer_options.enable_key_events:
if event.type == pygame.locals.KEYDOWN:
# Get the agent index of the first player
team_agent_index = 0
agent_index = self.env.get_agent_index(
'PLAYER', team_agent_index)
# Prepare the cached action
cached_action = None
if event.key == pygame.locals.K_RIGHT:
cached_action = 'MOVE_RIGHT'
elif event.key == pygame.locals.K_UP:
cached_action = 'MOVE_UP'
elif event.key == pygame.locals.K_LEFT:
cached_action = 'MOVE_LEFT'
elif event.key == pygame.locals.K_DOWN:
cached_action = 'MOVE_DOWN'
elif event.key == pygame.locals.K_s:
cached_action = 'STAND'
# Take the cached action and update the state
if cached_action:
self.env.take_cached_action(agent_index, cached_action)
self.env.update_state()
# Indicate the rendering should continue
return True
评论列表
文章目录