Farama-Foundation / MAgent2

An engine for high performance multi-agent environments with very large numbers of agents, along with a set of reference environments
https://magent2.farama.org
MIT License
219 stars 37 forks source link

Render freeze when window lose focus. #15

Open Carton9 opened 1 year ago

Carton9 commented 1 year ago

from magent2.environments import battle_v4,adversarial_pursuit_v4 from pettingzoo.utils import random_demo env = adversarial_pursuit_v4.env(render_mode='human',max_cycles=200) random_demo(env, render=True, episodes=2) For the code above, the render window will freeze when it loss focus.

scottmayberry commented 1 year ago

To solve: Scroll to end of render.py in magent2 python library folder. Add the link pygame.event.pump() above the line if self.mode == "human":

observation = pygame.surfarray.pixels3d(self.display)
new_observation = np.copy(observation)
del observation
################
pygame.event.pump()
################
if self.mode == "human":
    pygame.display.flip()
return (
    np.transpose(new_observation, axes=(1, 0, 2))
    if mode == "rgb_array"
    else None
)

This will cause issues with using the close button. If you need to use the close button, here is another option. You will need to add import sys and from pygame.locals import QUIT to the render function in the same render.py. Then add the highlighted code below.

observation = pygame.surfarray.pixels3d(self.display)
new_observation = np.copy(observation)
del observation
###################
for event in pygame.event.get():
    if event.type == QUIT:
        pygame.quit()
        sys.exit()
###################
if self.mode == "human":
    pygame.display.flip()
return (
    np.transpose(new_observation, axes=(1, 0, 2))
    if mode == "rgb_array"
    else None
)